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