Skip to content

Commit 848a669

Browse files
committed
refactor: Adjust class name
1 parent be91e83 commit 848a669

File tree

4 files changed

+99
-79
lines changed

4 files changed

+99
-79
lines changed

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { type AxiosInstance } from 'axios';
2-
import Adapter from './runtime/adapter';
2+
import Proxy from './runtime/proxy';
33

44
export function defineProxy(apiInstance: AxiosInstance) {
5-
return new Adapter(apiInstance);
5+
return new Proxy(apiInstance);
66
}

src/runtime/helpers.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import type { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
2+
import { isEqual } from 'ohash';
3+
4+
export function hasSameParams(requestParams: object, proxyParams?: object) {
5+
if (!proxyParams) return true;
6+
return isEqual(requestParams, proxyParams);
7+
}
8+
9+
export function matchRequest(
10+
verb: string,
11+
path: string,
12+
config: AxiosRequestConfig,
13+
params?: object,
14+
) {
15+
return (
16+
config.method === verb &&
17+
config.url === path &&
18+
hasSameParams(config.params, params)
19+
);
20+
}
21+
22+
export function matchResponse(
23+
verb: string,
24+
path: string,
25+
response: AxiosResponse,
26+
params?: object,
27+
) {
28+
return (
29+
response.config.method === verb &&
30+
response.config.url === path &&
31+
hasSameParams(response.config.params, params)
32+
);
33+
}
34+
35+
export function ejectFromRequest(axios: AxiosInstance, id: number) {
36+
axios.interceptors.request.eject(id);
37+
}
38+
39+
export function ejectFromResponse(axios: AxiosInstance, id: number) {
40+
axios.interceptors.response.eject(id);
41+
}
42+
43+
export function clearAll(axios: AxiosInstance) {
44+
axios.interceptors.request.clear();
45+
axios.interceptors.response.clear();
46+
}
Lines changed: 24 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
1-
import type {
2-
AxiosResponse,
3-
AxiosInstance,
4-
AxiosRequestConfig,
5-
InternalAxiosRequestConfig,
6-
} from 'axios';
7-
import { isEqual } from 'ohash';
8-
9-
type RouteConfig = (
10-
config?: AxiosRequestConfig,
11-
) => [number, unknown] | Promise<[number, unknown]>;
12-
13-
type RequestConfigChanger = (
14-
config: InternalAxiosRequestConfig,
15-
) => InternalAxiosRequestConfig;
16-
17-
export default class Adapter {
1+
import type { AxiosResponse, AxiosInstance } from 'axios';
2+
import {
3+
clearAll,
4+
ejectFromRequest,
5+
ejectFromResponse,
6+
matchRequest,
7+
matchResponse,
8+
} from './helpers';
9+
10+
export default class Proxy {
1811
axios: AxiosInstance;
1912

2013
verb!: string;
@@ -30,48 +23,15 @@ export default class Adapter {
3023
this.once = false;
3124
}
3225

33-
private hasSameParams(requestParams: object, proxyParams?: object) {
34-
if (!proxyParams) return true;
35-
return isEqual(requestParams, proxyParams);
36-
}
37-
38-
private matchRequest(
39-
verb: string,
40-
path: string,
41-
config: AxiosRequestConfig,
42-
params?: object,
43-
) {
44-
return (
45-
config.method === verb &&
46-
config.url === path &&
47-
this.hasSameParams(config.params, params)
48-
);
49-
}
50-
51-
private matchResponse(
52-
verb: string,
53-
path: string,
54-
response: AxiosResponse,
55-
params?: object,
56-
) {
57-
return (
58-
response.config.method === verb &&
59-
response.config.url === path &&
60-
this.hasSameParams(response.config.params, params)
61-
);
62-
}
63-
6426
private setProxy(statusCodeOrFunction: number | RouteConfig, mock?: unknown) {
6527
const verbConfig = this.verb;
6628
const pathConfig = this.path;
6729
const onceConfig = this.once;
6830
const paramsConfig = this.params;
6931
const interceptorId = this.axios.interceptors.request.use(requestConfig => {
70-
if (
71-
this.matchRequest(verbConfig, pathConfig, requestConfig, paramsConfig)
72-
) {
32+
if (matchRequest(verbConfig, pathConfig, requestConfig, paramsConfig)) {
7333
requestConfig.adapter = config => {
74-
if (onceConfig) this.ejectFromRequest(interceptorId);
34+
if (onceConfig) ejectFromRequest(this.axios, interceptorId);
7535
return new Promise((resolve, reject) => {
7636
if (typeof statusCodeOrFunction === 'function') {
7737
Promise.resolve(statusCodeOrFunction(requestConfig)).then(
@@ -110,30 +70,30 @@ export default class Adapter {
11070
});
11171
}
11272

113-
private setRequestConfigChanger(configChanger: RequestConfigChanger) {
73+
private setRequestConfigChanger(
74+
configChanger: RequestConfigChanger,
75+
once = false,
76+
) {
11477
const verbConfig = this.verb;
11578
const pathConfig = this.path;
11679
const paramsConfig = this.params;
11780
const interceptorId = this.axios.interceptors.request.use(requestConfig => {
118-
if (
119-
this.matchRequest(verbConfig, pathConfig, requestConfig, paramsConfig)
120-
) {
121-
if (this.once) this.ejectFromRequest(interceptorId);
81+
if (matchRequest(verbConfig, pathConfig, requestConfig, paramsConfig)) {
82+
if (once) ejectFromRequest(this.axios, interceptorId);
12283
const result = configChanger(requestConfig);
12384
return result;
12485
}
12586
return requestConfig;
12687
});
12788
}
12889

129-
private setPrintableResponse() {
90+
private setPrintableResponse(once = false) {
13091
const verbConfig = this.verb;
13192
const pathConfig = this.path;
13293
const paramsConfig = this.params;
133-
const onceConfig = this.once;
13494
const interceptorId = this.axios.interceptors.response.use(response => {
135-
if (this.matchResponse(verbConfig, pathConfig, response, paramsConfig)) {
136-
if (onceConfig) this.ejectFromResponse(interceptorId);
95+
if (matchResponse(verbConfig, pathConfig, response, paramsConfig)) {
96+
if (once) ejectFromResponse(this.axios, interceptorId);
13797
console.log('Response from:', this.path);
13898
console.log(JSON.stringify(response.data, null, 2));
13999
}
@@ -148,17 +108,8 @@ export default class Adapter {
148108
return this;
149109
}
150110

151-
private ejectFromRequest(id: number) {
152-
this.axios.interceptors.request.eject(id);
153-
}
154-
155-
private ejectFromResponse(id: number) {
156-
this.axios.interceptors.response.eject(id);
157-
}
158-
159111
clear() {
160-
this.axios.interceptors.request.clear();
161-
this.axios.interceptors.response.clear();
112+
clearAll(this.axios);
162113
}
163114

164115
reply(statusCodeOrConfig: number | RouteConfig, mock?: unknown) {
@@ -174,26 +125,22 @@ export default class Adapter {
174125
}
175126

176127
changeRequest(changer: RequestConfigChanger) {
177-
this.once = false;
178128
this.setRequestConfigChanger(changer);
179129
return this;
180130
}
181131

182132
changeRequestOnce(changer: RequestConfigChanger) {
183-
this.once = true;
184-
this.setRequestConfigChanger(changer);
133+
this.setRequestConfigChanger(changer, true);
185134
return this;
186135
}
187136

188137
printResponse() {
189-
this.once = false;
190138
this.setPrintableResponse();
191139
return this;
192140
}
193141

194142
printResponseOnce() {
195-
this.once = true;
196-
this.setPrintableResponse();
143+
this.setPrintableResponse(true);
197144
return this;
198145
}
199146

src/types.d.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type {
2+
AxiosInstance,
3+
AxiosRequestConfig,
4+
InternalAxiosRequestConfig,
5+
} from 'axios';
6+
7+
declare global {
8+
type BaseToMix = new (...args: any[]) => {
9+
axios: AxiosInstance;
10+
};
11+
12+
type RouteConfig = (
13+
config?: AxiosRequestConfig,
14+
) => [number, unknown] | Promise<[number, unknown]>;
15+
16+
type RequestConfigChanger = (
17+
config: InternalAxiosRequestConfig,
18+
) => InternalAxiosRequestConfig;
19+
20+
type SetupRequestConfig = {
21+
axios: AxiosInstance;
22+
verb: string;
23+
path: string;
24+
params?: object;
25+
once?: boolean;
26+
};
27+
}

0 commit comments

Comments
 (0)