feat(browser): make script-tag friendly
This commit is contained in:
parent
91977b84e3
commit
fd6ed8722e
70
urequest.js
70
urequest.js
|
@ -1,18 +1,19 @@
|
|||
'use strict';
|
||||
(function (exports) {
|
||||
'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;
|
||||
// `fetch` will be available for node and browsers as a global
|
||||
//var fetch = window.fetch;
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/fetch
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
|
||||
let _fetchDefaults = {
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/fetch
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
|
||||
let _fetchDefaults = {
|
||||
method: 'GET', // *GET, POST, PATCH, PUT, DELETE, etc
|
||||
headers: {},
|
||||
body: undefined, // String, ArrayBuffer, FormData, etc
|
||||
|
@ -25,9 +26,9 @@ let _fetchDefaults = {
|
|||
integrity: '',
|
||||
keepalive: false,
|
||||
signal: null //
|
||||
};
|
||||
};
|
||||
|
||||
let _optionKeys = Object.keys(_fetchDefaults).concat([
|
||||
let _optionKeys = Object.keys(_fetchDefaults).concat([
|
||||
//'encoding', // N/A
|
||||
//'stream', // TODO via getReader
|
||||
//'json' // handled manually
|
||||
|
@ -36,12 +37,12 @@ let _optionKeys = Object.keys(_fetchDefaults).concat([
|
|||
//'formData', // TODO
|
||||
//'FormData', // TODO
|
||||
//'userAgent' // not allowed, non-standard for request.js
|
||||
]);
|
||||
]);
|
||||
|
||||
/**
|
||||
/**
|
||||
* @returns {Request>}
|
||||
*/
|
||||
function setDefaults(_defs) {
|
||||
function setDefaults(_defs) {
|
||||
/**
|
||||
* @param {RequestOptions} opts
|
||||
* @returns {Promise<Response>}
|
||||
|
@ -80,9 +81,13 @@ function setDefaults(_defs) {
|
|||
if ('string' !== typeof opts.auth) {
|
||||
let u = opts.auth.user || opts.auth.username || '';
|
||||
let p = opts.auth.pass || opts.auth.password || '';
|
||||
reqOpts.headers.Authorization = encodeBasicAuth(`${u}:${p}`);
|
||||
reqOpts.headers.Authorization = encodeBasicAuth(
|
||||
`${u}:${p}`
|
||||
);
|
||||
} else if ('string' === typeof opts.auth) {
|
||||
reqOpts.headers.Authorization = encodeBasicAuth(`${opts.auth}`);
|
||||
reqOpts.headers.Authorization = encodeBasicAuth(
|
||||
`${opts.auth}`
|
||||
);
|
||||
}
|
||||
|
||||
// [request-compat]
|
||||
|
@ -167,13 +172,13 @@ function setDefaults(_defs) {
|
|||
}
|
||||
|
||||
return request;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* @param {Iterable.<*>} rheaders
|
||||
* @returns {Object.<String, String>}
|
||||
*/
|
||||
function headersToObj(rheaders) {
|
||||
function headersToObj(rheaders) {
|
||||
/*
|
||||
Array.from(resp.headers.entries()).forEach(function (h) {
|
||||
headers[h[0]] = h[1];
|
||||
|
@ -185,22 +190,22 @@ function headersToObj(rheaders) {
|
|||
resHeaders[k] = rheaders.get(k);
|
||||
});
|
||||
return resHeaders;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* @param {String} utf8
|
||||
* @returns {String}
|
||||
*/
|
||||
function encodeBasicAuth(utf8) {
|
||||
function encodeBasicAuth(utf8) {
|
||||
let b64 = unicodeToBase64(utf8);
|
||||
return `Basic ${b64}`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* @param {String} utf8
|
||||
* @returns {String}
|
||||
*/
|
||||
function unicodeToBase64(utf8) {
|
||||
function unicodeToBase64(utf8) {
|
||||
let str = '';
|
||||
let uint8 = new TextEncoder().encode(utf8);
|
||||
uint8.forEach(function (b) {
|
||||
|
@ -208,16 +213,17 @@ function unicodeToBase64(utf8) {
|
|||
});
|
||||
let b64 = btoa(str);
|
||||
return b64;
|
||||
}
|
||||
}
|
||||
|
||||
let defaultRequest = setDefaults({ mode: 'cors' });
|
||||
//@ts-ignore
|
||||
exports.urequest = defaultRequest;
|
||||
//@ts-ignore
|
||||
exports.urequest.defaults = setDefaults;
|
||||
let defaultRequest = setDefaults({ mode: 'cors' });
|
||||
//@ts-ignore
|
||||
exports.urequest = defaultRequest;
|
||||
//@ts-ignore
|
||||
exports.urequest.defaults = setDefaults;
|
||||
|
||||
// for backwards compat
|
||||
if ('undefined' !== typeof module) {
|
||||
// for backwards compat
|
||||
if ('undefined' !== typeof module) {
|
||||
module.exports = defaultRequest;
|
||||
module.exports.defaults = setDefaults;
|
||||
}
|
||||
}
|
||||
})(('undefined' !== typeof module && module.exports) || window);
|
||||
|
|
Loading…
Reference in New Issue