Skip to content

Commit c838f37

Browse files
committed
feat: accepts promises for change config and change response
1 parent 9f8e442 commit c838f37

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

src/runtime/handler.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ export default class Handler {
9090
const pathConfig = this.path;
9191
const paramsConfig = this.params;
9292
const interceptorId = this.scope.axios.interceptors.request.use(
93-
requestConfig => {
93+
async requestConfig => {
9494
if (matchRequest(verbConfig, pathConfig, requestConfig, paramsConfig)) {
9595
if (once) ejectFromRequest(this.scope.axios, interceptorId);
96-
const result = configChanger(requestConfig);
96+
const result = await Promise.resolve(configChanger(requestConfig));
9797
return result;
9898
}
9999
return requestConfig;
@@ -127,10 +127,10 @@ export default class Handler {
127127
const pathConfig = this.path;
128128
const paramsConfig = this.params;
129129
const interceptorId = this.scope.axios.interceptors.response.use(
130-
(response: AxiosResponse<T>) => {
130+
async (response: AxiosResponse<T>) => {
131131
if (matchResponse(verbConfig, pathConfig, response, paramsConfig)) {
132132
if (once) ejectFromResponse(this.scope.axios, interceptorId);
133-
response.data = responseChanger(response.data);
133+
response.data = await Promise.resolve(responseChanger(response.data));
134134
}
135135
return response;
136136
},

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export type RouteConfig = (
66

77
export type RequestConfigChanger = (
88
config: InternalAxiosRequestConfig,
9-
) => InternalAxiosRequestConfig;
9+
) => InternalAxiosRequestConfig | Promise<InternalAxiosRequestConfig>;
1010

1111
// eslint-disable-next-line @typescript-eslint/no-explicit-any
12-
export type ResponseChanger<T = any> = (data: T) => T;
12+
export type ResponseChanger<T = any> = (data: T) => T | Promise<T>;

test/index.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,28 @@ describe('axios-dev-proxy tests', () => {
244244
expect(response.data).toEqual({ data: 2, xpto: 2 });
245245
expect(response.status).toEqual(200);
246246
});
247+
248+
it('should change original response with promise', async () => {
249+
server.get('/').reply(200, { data: 1, xpto: 2 });
250+
251+
proxy.onGet('/').changeResponseDataOnce<{
252+
data: number;
253+
xpto: number;
254+
}>(
255+
data =>
256+
new Promise(resolve => {
257+
resolve({
258+
...data,
259+
data: 2,
260+
});
261+
}),
262+
);
263+
264+
const response = await api.get('/');
265+
266+
expect(response.data).toEqual({ data: 2, xpto: 2 });
267+
expect(response.status).toEqual(200);
268+
});
247269
});
248270

249271
describe('always GET configs', () => {
@@ -448,5 +470,24 @@ describe('axios-dev-proxy tests', () => {
448470
expect(server.pendingMocks()).toHaveLength(1);
449471
expect(server2.pendingMocks()).toHaveLength(1);
450472
});
473+
474+
it('should change the request config once with a promise', async () => {
475+
const server2 = nock('https://api-2.com.br');
476+
server2.get('/config').reply(200, { data: 2 });
477+
server.get('/config').reply(200, { data: 1 });
478+
proxy.onGet('/config').changeRequestOnce(
479+
async config =>
480+
new Promise(resolve => {
481+
config.baseURL = 'https://api-2.com.br';
482+
resolve(config);
483+
}),
484+
);
485+
486+
await api.get('/config');
487+
await api.get('/config');
488+
489+
expect(server.isDone()).toBe(true);
490+
expect(server2.isDone()).toBe(true);
491+
});
451492
});
452493
});

0 commit comments

Comments
 (0)