add jsdoc comments
This commit is contained in:
1 parent
6de354ad47
commit
8c1205bc13
24 files changed
+1228
-425
No files matched your search
+60
-21
@@ -1,28 +1,63 @@
|
||||
const g = typeof window !== 'undefined' ? window : self;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @param {string} url
|
||||
* @returns {string}
|
||||
*/
|
||||
function combineUrl(url) {
|
||||
if (/^(https?|wss?|ftp):/.test(url)) {
|
||||
return url;
|
||||
}
|
||||
if (typeof consts === 'undefined') {
|
||||
if (typeof g.consts === 'undefined') {
|
||||
return url;
|
||||
}
|
||||
return (consts.path || '') + url;
|
||||
return (g.consts.path || '') + url;
|
||||
}
|
||||
|
||||
export function get(url, options = {}) {
|
||||
/**
|
||||
* @typedef RequestOptions
|
||||
* @property {string} [method]
|
||||
* @property {string} [accept]
|
||||
* @property {Object.<string, string>} [customHeaders]
|
||||
* @property {RequestMode} [mode]
|
||||
* @property {AbortSignal | null} [signal]
|
||||
* @property {boolean} [diagnostic]
|
||||
* @property {(this: XMLHttpRequestUpload, e: ProgressEvent<XMLHttpRequestEventTarget>) => any} [progress]
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {string} url
|
||||
* @param {RequestOptions} [options]
|
||||
* @returns {Promise<Response>}
|
||||
*/
|
||||
export function get(url, options) {
|
||||
return fetch(combineUrl(url), {
|
||||
method: options.method || 'GET',
|
||||
method: options?.method || 'GET',
|
||||
headers: {
|
||||
...options.customHeaders,
|
||||
'Accept': options.accept ?? 'application/json'
|
||||
...options?.customHeaders,
|
||||
'Accept': options?.accept ?? 'application/json'
|
||||
},
|
||||
mode: options.mode,
|
||||
signal: options.signal,
|
||||
mode: options?.mode,
|
||||
signal: options?.signal,
|
||||
cache: 'default'
|
||||
});
|
||||
}
|
||||
|
||||
export function post(url, data, options = {}) {
|
||||
// let contentType;
|
||||
/**
|
||||
* @typedef DiagnosedResponse
|
||||
* @property {number} time
|
||||
* @property {Response} response
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {string} url
|
||||
* @param {BodyInit | null} [data]
|
||||
* @param {RequestOptions} [options]
|
||||
* @returns {Promise<Response | DiagnosedResponse>}
|
||||
*/
|
||||
export function post(url, data, options) {
|
||||
const headers = options?.customHeaders ?? {};
|
||||
if (data instanceof FormData) {
|
||||
// contentType = 'multipart/form-data';
|
||||
} else {
|
||||
@@ -30,21 +65,19 @@ export function post(url, data, options = {}) {
|
||||
data = JSON.stringify(data);
|
||||
}
|
||||
// contentType = 'application/json';
|
||||
if (options.customHeaders == null) {
|
||||
options.customHeaders = {};
|
||||
}
|
||||
if (options.customHeaders['Content-Type'] == null) {
|
||||
options.customHeaders['Content-Type'] = 'application/json';
|
||||
if (headers['Content-Type'] == null) {
|
||||
headers['Content-Type'] = 'application/json';
|
||||
}
|
||||
}
|
||||
/** @type {RequestInit} */
|
||||
const opts = {
|
||||
method: options.method || 'POST',
|
||||
headers: options.customHeaders,
|
||||
method: options?.method || 'POST',
|
||||
headers,
|
||||
body: data,
|
||||
signal: options.signal,
|
||||
signal: options?.signal,
|
||||
cache: 'no-cache'
|
||||
};
|
||||
if (options.diagnostic) {
|
||||
if (options?.diagnostic) {
|
||||
const started = new Date().getTime();
|
||||
return new Promise((resolve, reject) => {
|
||||
fetch(combineUrl(url), opts).then(response => {
|
||||
@@ -58,7 +91,13 @@ export function post(url, data, options = {}) {
|
||||
return fetch(combineUrl(url), opts);
|
||||
}
|
||||
|
||||
export function upload(url, data, options = {}) {
|
||||
/**
|
||||
* @param {string} url
|
||||
* @param {FormData} data
|
||||
* @param {RequestOptions} options
|
||||
* @returns {Promise<XMLHttpRequest>}
|
||||
*/
|
||||
export function upload(url, data, options) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = new XMLHttpRequest();
|
||||
request.onreadystatechange = function () {
|
||||
@@ -73,7 +112,7 @@ export function upload(url, data, options = {}) {
|
||||
if (typeof options.progress === 'function') {
|
||||
request.upload.addEventListener('progress', function (ev) {
|
||||
if (ev.lengthComputable) {
|
||||
options.progress.call(this, ev);
|
||||
options.progress?.call(this, ev);
|
||||
}
|
||||
}, false);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user