From bad25164f914dafeb11c4f5f2d716c8b3399f03b Mon Sep 17 00:00:00 2001 From: suhaotian Date: Thu, 29 Feb 2024 18:31:41 +1100 Subject: [PATCH] feat: compatiable axios option: `paramsSerializer` and `withCredentials` --- src/interceptors.ts | 4 +++- src/tests/index.test.ts | 2 +- src/types.ts | 4 ++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/interceptors.ts b/src/interceptors.ts index 121f63a..4563c3d 100644 --- a/src/interceptors.ts +++ b/src/interceptors.ts @@ -8,7 +8,7 @@ const jsonType = 'application/json'; export async function defaultRequestInterceptor( req: XiorInterceptorRequestConfig ): Promise { - const encode = req.encode || encodeParams; + const encode = req.encode || req.paramsSerializer || encodeParams; const encodeURI = req.encodeURI !== false; const method = req.method ? req.method.toUpperCase() : 'GET'; @@ -58,5 +58,7 @@ export async function defaultRequestInterceptor( _url, method, headers, + encode, + paramsSerializer: encode, }; } diff --git a/src/tests/index.test.ts b/src/tests/index.test.ts index 2e560a5..fca4238 100644 --- a/src/tests/index.test.ts +++ b/src/tests/index.test.ts @@ -120,7 +120,7 @@ describe('xior tests', () => { }); describe('Options `encode` and `encodeURI` tests', () => { - const xiorInstance = xior.create({ baseURL }); + const xiorInstance = xior.create({ baseURL, withCredentials: true }); it('default encode should work', async () => { const { data, request } = await xiorInstance.get<{ diff --git a/src/types.ts b/src/types.ts index ef2cdfa..4ed02c1 100644 --- a/src/types.ts +++ b/src/types.ts @@ -5,7 +5,9 @@ export interface XiorRequestConfig extends Omit { params?: Record; /** If no set, default depends on browsers timeout */ timeout?: number; + /** @deprecated use paramsSerializer */ encode?: (params: Record) => string; + paramsSerializer?: (params: Record) => string; /** Use encodeURIComponent, default: true */ encodeURI?: boolean; /** @@ -16,6 +18,8 @@ export interface XiorRequestConfig extends Omit { data?: any; /** encoded url with params */ _url?: string; + /** useless now, compitable axios */ + withCredentials?: boolean; } export type XiorInterceptorRequestConfig = XiorRequestConfig & {