From 91977b84e3cee4a9d90141ffcc353eb90eaf8d6d Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Wed, 28 Sep 2022 23:04:42 -0600 Subject: [PATCH] feat(browser+lint): prep to make script-tag friendly --- bin/localize-types | 14 -------------- jsconfig.json | 2 +- local-types.js | 5 ----- package-lock.json | 10 +++++++++- package.json | 9 +++------ request.js | 8 ++++++++ types.js | 5 ----- browser.js => urequest.js | 34 ++++++++++++++++++++++++++++------ 8 files changed, 49 insertions(+), 38 deletions(-) delete mode 100755 bin/localize-types delete mode 100644 local-types.js delete mode 100644 types.js rename browser.js => urequest.js (90%) diff --git a/bin/localize-types b/bin/localize-types deleted file mode 100755 index f4e718f..0000000 --- a/bin/localize-types +++ /dev/null @@ -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 diff --git a/jsconfig.json b/jsconfig.json index 39150df..8252cdb 100644 --- a/jsconfig.json +++ b/jsconfig.json @@ -71,6 +71,6 @@ "skipLibCheck": true /* Skip type checking of declaration files. */, "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"] } diff --git a/local-types.js b/local-types.js deleted file mode 100644 index e52085c..0000000 --- a/local-types.js +++ /dev/null @@ -1,5 +0,0 @@ -/** - * @typedef {import('./types.js').RequestOptions} RequestOptions - * @typedef {import('./types.js').Response} Response - * @typedef {import('./types.js').Headers} Headers - */ diff --git a/package-lock.json b/package-lock.json index 3621150..ef4ef22 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,13 @@ { "name": "@root/request", "version": "1.9.1", - "lockfileVersion": 1 + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "name": "@root/request", + "version": "1.9.1", + "license": "(MIT OR Apache-2.0)" + } + } } diff --git a/package.json b/package.json index 5491471..3841633 100644 --- a/package.json +++ b/package.json @@ -4,22 +4,19 @@ "description": "A lightweight, zero-dependency drop-in replacement for request", "main": "index.js", "browser": { - "./request.js": "./browser.js" + "./request.js": "./urequest.js" }, "files": [ - "browser.js", "jsconfig.json", "lib", - "local-types.js", "request.js", - "types.js" + "urequest.js" ], "directories": { "example": "examples" }, "scripts": { - "test": "echo \"Error: no test specified\" && exit 1", - "prepublish": "./bin/localize-types" + "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { "type": "git", diff --git a/request.js b/request.js index f7bba7b..cbcdebe 100644 --- a/request.js +++ b/request.js @@ -4,9 +4,17 @@ var http = require('http'); var https = require('https'); var url = require('url'); var os = require('os'); +//@ts-ignore var pkg = require('./package.json'); 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 = { sendImmediately: true, method: '', diff --git a/types.js b/types.js deleted file mode 100644 index 586580a..0000000 --- a/types.js +++ /dev/null @@ -1,5 +0,0 @@ -/** - * @typedef {import('./').RequestOptions} RequestOptions - * @typedef {import('./').Response} Response - * @typedef {import('./').Headers} Headers - */ diff --git a/browser.js b/urequest.js similarity index 90% rename from browser.js rename to urequest.js index d072e82..ec48572 100644 --- a/browser.js +++ b/urequest.js @@ -1,5 +1,12 @@ '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 //var fetch = window.fetch; @@ -31,8 +38,15 @@ let _optionKeys = Object.keys(_fetchDefaults).concat([ //'userAgent' // not allowed, non-standard for request.js ]); +/** + * @returns {Request>} + */ function setDefaults(_defs) { - return async function request(opts) { + /** + * @param {RequestOptions} opts + * @returns {Promise} + **/ + async function request(opts) { if ('string' === typeof opts) { opts = { url: opts }; } @@ -150,7 +164,9 @@ function setDefaults(_defs) { }); } return result; - }; + } + + return request; } /** @@ -195,7 +211,13 @@ function unicodeToBase64(utf8) { } let defaultRequest = setDefaults({ mode: 'cors' }); -exports.request = defaultRequest; -exports.defaults = setDefaults; -module.exports = defaultRequest; -module.exports.defaults = setDefaults; +//@ts-ignore +exports.urequest = defaultRequest; +//@ts-ignore +exports.urequest.defaults = setDefaults; + +// for backwards compat +if ('undefined' !== typeof module) { + module.exports = defaultRequest; + module.exports.defaults = setDefaults; +}