Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 28 additions & 37 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* @property {string} [xsrfHeaderName] The name of a header to use for passing XSRF cookies
* @property {(status: number) => boolean} [validateStatus] Override status code handling (default: 200-399 is a success)
* @property {Array<(body: any, headers: Headers) => any?>} [transformRequest] An array of transformations to apply to the outgoing request
* @property {Array<(data: any) => any?>} [transformResponse] An array of transformations to apply to response data
* @property {string} [baseURL] a base URL from which to resolve all URLs
* @property {typeof window.fetch} [fetch] Custom window.fetch implementation
* @property {any} [data]
Expand Down Expand Up @@ -102,13 +103,7 @@ export default (function create(/** @type {Options} */ defaults) {
* @param {(...args: T[]) => R} fn
* @returns {(array: T[]) => R}
*/
redaxios.spread = function (fn) {
return function (results) {
return fn.apply(this, results);
};
};
// 3b smaller:
// redaxios.spread = (fn) => /** @type {any} */ (fn.apply.bind(fn, fn));
redaxios.spread = (fn) => /** @type {any} */ (fn.apply.bind(fn, fn));

/**
* @private
Expand All @@ -130,7 +125,7 @@ export default (function create(/** @type {Options} */ defaults) {
for (i in overrides) {
const key = lowerCase ? i.toLowerCase() : i;
const value = /** @type {any} */ (overrides)[i];
out[key] = key in out && typeof value == 'object' ? deepMerge(out[key], value, key === 'headers') : value;
out[key] = key in out && typeof value == 'object' ? deepMerge(out[key], value, key == 'headers') : value;
}
return out;
}
Expand All @@ -146,7 +141,7 @@ export default (function create(/** @type {Options} */ defaults) {
* @returns {Promise<Response<T>>}
*/
function redaxios(url, config, _method, _data) {
if (typeof url !== 'string') {
if (typeof url != 'string') {
config = url;
url = config.url;
}
Expand All @@ -159,13 +154,11 @@ export default (function create(/** @type {Options} */ defaults) {
/** @type {Headers} */
const customHeaders = {};

let data = _data || options.data;
let data = (options.transformRequest || []).reduce((data, f) => {
return f(data, options.headers) || data;
}, _data || options.data);

(options.transformRequest || []).map((f) => {
data = f(data, options.headers) || data;
});

if (data && typeof data === 'object' && typeof data.append !== 'function') {
if (data && typeof data == 'object' && typeof data.append != 'function') {
data = JSON.stringify(data);
customHeaders['content-type'] = 'application/json';
}
Expand Down Expand Up @@ -196,35 +189,33 @@ export default (function create(/** @type {Options} */ defaults) {
body: data,
headers: deepMerge(options.headers, customHeaders, true),
credentials: options.withCredentials ? 'include' : undefined
}).then((res) => {
for (const i in res) {
if (typeof res[i] != 'function') response[i] = res[i];
}

const ok = options.validateStatus ? options.validateStatus(res.status) : res.ok;
if (!ok) return Promise.reject(response);

if (options.responseType == 'stream') {
response.data = res.body;
})
.then((/**@type {globalThis.Response}*/ res, raw) => {
for (const i in res) {
if (typeof res[i] != 'function') response[i] = res[i];
}

const ok = options.validateStatus ? options.validateStatus(res.status) : res.ok;
if (!ok) throw response;

if (!res[options.responseType || 'text']) {
return res.body;
}

raw = res[options.responseType || 'text']().catch(() => undefined);
return raw.then(JSON.parse).catch(() => raw);
})
.then((data) => {
response.data = (options.transformResponse || []).reduce((data, f) => f(data) || data, data);
return response;
}

return res[options.responseType || 'text']()
.then((data) => {
response.data = data;
// its okay if this fails: response.data will be the unparsed value:
response.data = JSON.parse(data);
})
.catch(Object)
.then(() => response);
});
});
}

/**
* @public
* @type {AbortController}
*/
redaxios.CancelToken = /** @type {any} */ (typeof AbortController == 'function' ? AbortController : Object);
redaxios.CancelToken = /** @type {any} */ (typeof AbortController != 'function' ? Object : AbortController);

/**
* @public
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
],
"allowJs": true,
"checkJs": true,
"noImplicitAny": false,
"declaration": true,
"emitDeclarationOnly": true,
"declarationMap": true,
Expand Down