diff --git a/index.d.ts b/index.d.ts index a0796c46..27f3b7d2 100644 --- a/index.d.ts +++ b/index.d.ts @@ -361,6 +361,7 @@ export interface AxiosRequestConfig { lookup?: ((hostname: string, options: object, cb: (err: Error | null, address: LookupAddress | LookupAddress[], family?: AddressFamily) => void) => void) | ((hostname: string, options: object) => Promise<[address: LookupAddressEntry | LookupAddressEntry[], family?: AddressFamily] | LookupAddress>); withXSRFToken?: boolean | ((config: InternalAxiosRequestConfig) => boolean | undefined); + parseReviver?: (this: any, key: string, value: any) => any; fetchOptions?: Omit | Record; } diff --git a/lib/defaults/index.js b/lib/defaults/index.js index e543fea2..cc14a8b3 100644 --- a/lib/defaults/index.js +++ b/lib/defaults/index.js @@ -111,7 +111,7 @@ const defaults = { const strictJSONParsing = !silentJSONParsing && JSONRequested; try { - return JSON.parse(data); + return JSON.parse(data, this.parseReviver); } catch (e) { if (strictJSONParsing) { if (e.name === 'SyntaxError') { diff --git a/test/unit/adapters/http.js b/test/unit/adapters/http.js index 6d80ad69..04a846db 100644 --- a/test/unit/adapters/http.js +++ b/test/unit/adapters/http.js @@ -2332,4 +2332,22 @@ describe('supports http with nodejs', function () { }, /ENOTFOUND/); }); }); + + describe('JSON', function() { + it('should support reviver on JSON.parse', async function () { + server = await startHTTPServer(async (_, res) => { + res.end(JSON.stringify({ + foo: 'bar' + })); + }); + + const {data} = await axios.get(LOCAL_SERVER_URL, { + parseReviver: (key, value) => { + return key === 'foo' ? 'success' : value; + }, + }); + + assert.deepStrictEqual(data, {foo: 'success'}); + }); + }); });