From ffb1a20479d1ebdb6d80540cfcf6800e1b6d104c Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Wed, 22 Jul 2020 23:26:44 -0400 Subject: [PATCH 1/2] Add transformResponse option --- src/index.js | 66 ++++++++++++++++++++++++---------------------------- 1 file changed, 30 insertions(+), 36 deletions(-) diff --git a/src/index.js b/src/index.js index 39f523b..ec262b6 100644 --- a/src/index.js +++ b/src/index.js @@ -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] @@ -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 @@ -146,7 +141,7 @@ export default (function create(/** @type {Options} */ defaults) { * @returns {Promise>} */ function redaxios(url, config, _method, _data) { - if (typeof url !== 'string') { + if (typeof url != 'string') { config = url; url = config.url; } @@ -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'; } @@ -196,35 +189,36 @@ 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((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) throw response; + + try { + return res[options.responseType || 'text'](); + } catch (e) {} + return res.body; + }) + .then((data) => { + try { + data = JSON.parse(data); + } catch (e) {} + response.data = (options.transformResponse || []).reduce((data, f) => { + return 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 From aeb11cf5bbef6434f497f19fdef9122e386b16bc Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Thu, 23 Jul 2020 12:02:10 -0400 Subject: [PATCH 2/2] fix --- src/index.js | 21 +++++++++------------ tsconfig.json | 1 + 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/index.js b/src/index.js index ec262b6..7edabb9 100644 --- a/src/index.js +++ b/src/index.js @@ -125,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; } @@ -190,7 +190,7 @@ export default (function create(/** @type {Options} */ defaults) { headers: deepMerge(options.headers, customHeaders, true), credentials: options.withCredentials ? 'include' : undefined }) - .then((res) => { + .then((/**@type {globalThis.Response}*/ res, raw) => { for (const i in res) { if (typeof res[i] != 'function') response[i] = res[i]; } @@ -198,18 +198,15 @@ export default (function create(/** @type {Options} */ defaults) { const ok = options.validateStatus ? options.validateStatus(res.status) : res.ok; if (!ok) throw response; - try { - return res[options.responseType || 'text'](); - } catch (e) {} - return res.body; + if (!res[options.responseType || 'text']) { + return res.body; + } + + raw = res[options.responseType || 'text']().catch(() => undefined); + return raw.then(JSON.parse).catch(() => raw); }) .then((data) => { - try { - data = JSON.parse(data); - } catch (e) {} - response.data = (options.transformResponse || []).reduce((data, f) => { - return f(data) || data; - }, data); + response.data = (options.transformResponse || []).reduce((data, f) => f(data) || data, data); return response; }); } diff --git a/tsconfig.json b/tsconfig.json index a155f28..3366d6a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,6 +8,7 @@ ], "allowJs": true, "checkJs": true, + "noImplicitAny": false, "declaration": true, "emitDeclarationOnly": true, "declarationMap": true,