feat(browser+lint): prep to make script-tag friendly

This commit is contained in:
AJ ONeal 2022-09-28 23:04:42 -06:00
parent b4b55523d2
commit 91977b84e3
No known key found for this signature in database
GPG Key ID: 585419CA6DB0AA23
8 changed files with 49 additions and 38 deletions

View File

@ -1,14 +0,0 @@
#!/bin/sh
my_typedefs="$(
grep typedef ./index.js | cut -d ' ' -f5
)"
rm -f ./types.js
{
echo '/**'
for my_type in $my_typedefs; do
echo " * @typedef {import('./').${my_type}} ${my_type}"
done
echo ' */'
} >> ./types.js

View File

@ -71,6 +71,6 @@
"skipLibCheck": true /* Skip type checking of declaration files. */, "skipLibCheck": true /* Skip type checking of declaration files. */,
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}, },
"include": ["index.js", "browser.js", "./types.js", "./local-types.js", "lib/**/*.js", "bin/**/*.js"], "include": ["index.js", "urequest.js", "./types.js", "./local-types.js", "lib/**/*.js", "bin/**/*.js"],
"exclude": ["node_modules"] "exclude": ["node_modules"]
} }

View File

@ -1,5 +0,0 @@
/**
* @typedef {import('./types.js').RequestOptions} RequestOptions
* @typedef {import('./types.js').Response} Response
* @typedef {import('./types.js').Headers} Headers
*/

10
package-lock.json generated
View File

@ -1,5 +1,13 @@
{ {
"name": "@root/request", "name": "@root/request",
"version": "1.9.1", "version": "1.9.1",
"lockfileVersion": 1 "lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@root/request",
"version": "1.9.1",
"license": "(MIT OR Apache-2.0)"
}
}
} }

View File

@ -4,22 +4,19 @@
"description": "A lightweight, zero-dependency drop-in replacement for request", "description": "A lightweight, zero-dependency drop-in replacement for request",
"main": "index.js", "main": "index.js",
"browser": { "browser": {
"./request.js": "./browser.js" "./request.js": "./urequest.js"
}, },
"files": [ "files": [
"browser.js",
"jsconfig.json", "jsconfig.json",
"lib", "lib",
"local-types.js",
"request.js", "request.js",
"types.js" "urequest.js"
], ],
"directories": { "directories": {
"example": "examples" "example": "examples"
}, },
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1"
"prepublish": "./bin/localize-types"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

View File

@ -4,9 +4,17 @@ var http = require('http');
var https = require('https'); var https = require('https');
var url = require('url'); var url = require('url');
var os = require('os'); var os = require('os');
//@ts-ignore
var pkg = require('./package.json'); var pkg = require('./package.json');
var fs = require('fs'); // only for streams var fs = require('fs'); // only for streams
/**
* @typedef {import('./').Request} Request
* @typedef {import('./').RequestOptions} RequestOptions
* @typedef {import('./').Response} Response
* @typedef {import('./').Headers} Headers
*/
var _defaults = { var _defaults = {
sendImmediately: true, sendImmediately: true,
method: '', method: '',

View File

@ -1,5 +0,0 @@
/**
* @typedef {import('./').RequestOptions} RequestOptions
* @typedef {import('./').Response} Response
* @typedef {import('./').Headers} Headers
*/

View File

@ -1,5 +1,12 @@
'use strict'; 'use strict';
/**
* @typedef {import('./').Request} Request
* @typedef {import('./').RequestOptions} RequestOptions
* @typedef {import('./').Response} Response
* @typedef {import('./').Headers} Headers
*/
// `fetch` will be available for node and browsers as a global // `fetch` will be available for node and browsers as a global
//var fetch = window.fetch; //var fetch = window.fetch;
@ -31,8 +38,15 @@ let _optionKeys = Object.keys(_fetchDefaults).concat([
//'userAgent' // not allowed, non-standard for request.js //'userAgent' // not allowed, non-standard for request.js
]); ]);
/**
* @returns {Request>}
*/
function setDefaults(_defs) { function setDefaults(_defs) {
return async function request(opts) { /**
* @param {RequestOptions} opts
* @returns {Promise<Response>}
**/
async function request(opts) {
if ('string' === typeof opts) { if ('string' === typeof opts) {
opts = { url: opts }; opts = { url: opts };
} }
@ -150,7 +164,9 @@ function setDefaults(_defs) {
}); });
} }
return result; return result;
}; }
return request;
} }
/** /**
@ -195,7 +211,13 @@ function unicodeToBase64(utf8) {
} }
let defaultRequest = setDefaults({ mode: 'cors' }); let defaultRequest = setDefaults({ mode: 'cors' });
exports.request = defaultRequest; //@ts-ignore
exports.defaults = setDefaults; exports.urequest = defaultRequest;
//@ts-ignore
exports.urequest.defaults = setDefaults;
// for backwards compat
if ('undefined' !== typeof module) {
module.exports = defaultRequest; module.exports = defaultRequest;
module.exports.defaults = setDefaults; module.exports.defaults = setDefaults;
}