Skip to content

Commit

Permalink
feat: compatiable axios option: paramsSerializer and withCredentials
Browse files Browse the repository at this point in the history
  • Loading branch information
suhaotian committed Feb 29, 2024
1 parent 6319f9a commit bad2516
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/interceptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const jsonType = 'application/json';
export async function defaultRequestInterceptor(
req: XiorInterceptorRequestConfig
): Promise<XiorInterceptorRequestConfig> {
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';
Expand Down Expand Up @@ -58,5 +58,7 @@ export async function defaultRequestInterceptor(
_url,
method,
headers,
encode,
paramsSerializer: encode,
};
}
2 changes: 1 addition & 1 deletion src/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<{
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ export interface XiorRequestConfig<T = any> extends Omit<RequestInit, 'body'> {
params?: Record<string, any>;
/** If no set, default depends on browsers timeout */
timeout?: number;
/** @deprecated use paramsSerializer */
encode?: (params: Record<string, any>) => string;
paramsSerializer?: (params: Record<string, any>) => string;
/** Use encodeURIComponent, default: true */
encodeURI?: boolean;
/**
Expand All @@ -16,6 +18,8 @@ export interface XiorRequestConfig<T = any> extends Omit<RequestInit, 'body'> {
data?: any;
/** encoded url with params */
_url?: string;
/** useless now, compitable axios */
withCredentials?: boolean;
}

export type XiorInterceptorRequestConfig<T = any> = XiorRequestConfig & {
Expand Down

0 comments on commit bad2516

Please sign in to comment.