mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
feat: support reviver on JSON.parse (#5926)
* test: test to support reviver on JSON.parse * feat: support reviver for JSON.parse on parse response data #5924 --------- Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
Vendored
+1
@@ -361,6 +361,7 @@ export interface AxiosRequestConfig<D = any> {
|
|||||||
lookup?: ((hostname: string, options: object, cb: (err: Error | null, address: LookupAddress | LookupAddress[], family?: AddressFamily) => void) => void) |
|
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>);
|
((hostname: string, options: object) => Promise<[address: LookupAddressEntry | LookupAddressEntry[], family?: AddressFamily] | LookupAddress>);
|
||||||
withXSRFToken?: boolean | ((config: InternalAxiosRequestConfig) => boolean | undefined);
|
withXSRFToken?: boolean | ((config: InternalAxiosRequestConfig) => boolean | undefined);
|
||||||
|
parseReviver?: (this: any, key: string, value: any) => any;
|
||||||
fetchOptions?: Omit<RequestInit, 'body' | 'headers' | 'method' | 'signal'> | Record<string, any>;
|
fetchOptions?: Omit<RequestInit, 'body' | 'headers' | 'method' | 'signal'> | Record<string, any>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -111,7 +111,7 @@ const defaults = {
|
|||||||
const strictJSONParsing = !silentJSONParsing && JSONRequested;
|
const strictJSONParsing = !silentJSONParsing && JSONRequested;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return JSON.parse(data);
|
return JSON.parse(data, this.parseReviver);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (strictJSONParsing) {
|
if (strictJSONParsing) {
|
||||||
if (e.name === 'SyntaxError') {
|
if (e.name === 'SyntaxError') {
|
||||||
|
|||||||
@@ -2332,4 +2332,22 @@ describe('supports http with nodejs', function () {
|
|||||||
}, /ENOTFOUND/);
|
}, /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'});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user