Skip to content

Commit f668dae

Browse files
committed
refactor: Create handler class
1 parent 848a669 commit f668dae

File tree

2 files changed

+144
-125
lines changed

2 files changed

+144
-125
lines changed

src/runtime/handler.ts

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
import type { AxiosResponse } from 'axios';
2+
import {
3+
ejectFromRequest,
4+
ejectFromResponse,
5+
matchRequest,
6+
matchResponse,
7+
} from './helpers';
8+
import type Proxy from './proxy';
9+
10+
export default class Handler {
11+
scope!: Proxy;
12+
13+
verb!: string;
14+
15+
path!: string;
16+
17+
params?: object;
18+
19+
constructor(scope: Proxy, verb: string, path: string, params?: object) {
20+
this.scope = scope;
21+
this.verb = verb;
22+
this.path = path;
23+
this.params = params;
24+
}
25+
26+
private setProxy(
27+
statusCodeOrFunction: number | RouteConfig,
28+
mock?: unknown,
29+
once = false,
30+
) {
31+
const interceptorId = this.scope.axios.interceptors.request.use(
32+
requestConfig => {
33+
if (matchRequest(this.verb, this.path, requestConfig, this.params)) {
34+
requestConfig.adapter = config => {
35+
if (once) ejectFromRequest(this.scope.axios, interceptorId);
36+
return new Promise((resolve, reject) => {
37+
if (typeof statusCodeOrFunction === 'function') {
38+
Promise.resolve(statusCodeOrFunction(requestConfig)).then(
39+
result => {
40+
const [status, data] = result;
41+
const response = {
42+
data,
43+
status,
44+
headers: {},
45+
statusText: '',
46+
config,
47+
request: requestConfig,
48+
};
49+
if (status < 400) resolve(response);
50+
else reject(response);
51+
},
52+
);
53+
return;
54+
}
55+
const status = statusCodeOrFunction;
56+
const response: AxiosResponse = {
57+
data: mock,
58+
status,
59+
headers: {},
60+
statusText: '',
61+
config,
62+
request: requestConfig,
63+
};
64+
65+
if (status < 400) resolve(response);
66+
else reject(response);
67+
});
68+
};
69+
}
70+
return requestConfig;
71+
},
72+
);
73+
}
74+
75+
private setRequestConfigChanger(
76+
configChanger: RequestConfigChanger,
77+
once = false,
78+
) {
79+
const verbConfig = this.verb;
80+
const pathConfig = this.path;
81+
const paramsConfig = this.params;
82+
const interceptorId = this.scope.axios.interceptors.request.use(
83+
requestConfig => {
84+
if (matchRequest(verbConfig, pathConfig, requestConfig, paramsConfig)) {
85+
if (once) ejectFromRequest(this.scope.axios, interceptorId);
86+
const result = configChanger(requestConfig);
87+
return result;
88+
}
89+
return requestConfig;
90+
},
91+
);
92+
}
93+
94+
private setPrintableResponse(once = false) {
95+
const verbConfig = this.verb;
96+
const pathConfig = this.path;
97+
const paramsConfig = this.params;
98+
const interceptorId = this.scope.axios.interceptors.response.use(
99+
response => {
100+
if (matchResponse(verbConfig, pathConfig, response, paramsConfig)) {
101+
if (once) ejectFromResponse(this.scope.axios, interceptorId);
102+
console.log('Response from:', this.path);
103+
console.log(JSON.stringify(response.data, null, 2));
104+
}
105+
return response;
106+
},
107+
);
108+
}
109+
110+
reply(statusCodeOrConfig: number | RouteConfig, mock?: unknown) {
111+
this.setProxy(statusCodeOrConfig, mock);
112+
return this.scope;
113+
}
114+
115+
replyOnce(statusCodeOrConfig: number | RouteConfig, mock?: unknown) {
116+
this.setProxy(statusCodeOrConfig, mock, true);
117+
return this.scope;
118+
}
119+
120+
changeRequest(changer: RequestConfigChanger) {
121+
this.setRequestConfigChanger(changer);
122+
return this;
123+
}
124+
125+
changeRequestOnce(changer: RequestConfigChanger) {
126+
this.setRequestConfigChanger(changer, true);
127+
return this;
128+
}
129+
130+
printResponse() {
131+
this.setPrintableResponse();
132+
return this;
133+
}
134+
135+
printResponseOnce() {
136+
this.setPrintableResponse(true);
137+
return this;
138+
}
139+
}

src/runtime/proxy.ts

Lines changed: 5 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
import type { AxiosResponse, AxiosInstance } from 'axios';
2-
import {
3-
clearAll,
4-
ejectFromRequest,
5-
ejectFromResponse,
6-
matchRequest,
7-
matchResponse,
8-
} from './helpers';
1+
import type { AxiosInstance } from 'axios';
2+
import Handler from './handler';
3+
import { clearAll } from './helpers';
94

105
export default class Proxy {
116
axios: AxiosInstance;
@@ -16,134 +11,19 @@ export default class Proxy {
1611

1712
params?: object;
1813

19-
once: boolean;
20-
2114
constructor(axios: AxiosInstance) {
2215
this.axios = axios;
23-
this.once = false;
24-
}
25-
26-
private setProxy(statusCodeOrFunction: number | RouteConfig, mock?: unknown) {
27-
const verbConfig = this.verb;
28-
const pathConfig = this.path;
29-
const onceConfig = this.once;
30-
const paramsConfig = this.params;
31-
const interceptorId = this.axios.interceptors.request.use(requestConfig => {
32-
if (matchRequest(verbConfig, pathConfig, requestConfig, paramsConfig)) {
33-
requestConfig.adapter = config => {
34-
if (onceConfig) ejectFromRequest(this.axios, interceptorId);
35-
return new Promise((resolve, reject) => {
36-
if (typeof statusCodeOrFunction === 'function') {
37-
Promise.resolve(statusCodeOrFunction(requestConfig)).then(
38-
result => {
39-
const [status, data] = result;
40-
const response = {
41-
data,
42-
status,
43-
headers: {},
44-
statusText: '',
45-
config,
46-
request: requestConfig,
47-
};
48-
if (status < 400) resolve(response);
49-
else reject(response);
50-
},
51-
);
52-
return;
53-
}
54-
const status = statusCodeOrFunction;
55-
const response: AxiosResponse = {
56-
data: mock,
57-
status,
58-
headers: {},
59-
statusText: '',
60-
config,
61-
request: requestConfig,
62-
};
63-
64-
if (status < 400) resolve(response);
65-
else reject(response);
66-
});
67-
};
68-
}
69-
return requestConfig;
70-
});
71-
}
72-
73-
private setRequestConfigChanger(
74-
configChanger: RequestConfigChanger,
75-
once = false,
76-
) {
77-
const verbConfig = this.verb;
78-
const pathConfig = this.path;
79-
const paramsConfig = this.params;
80-
const interceptorId = this.axios.interceptors.request.use(requestConfig => {
81-
if (matchRequest(verbConfig, pathConfig, requestConfig, paramsConfig)) {
82-
if (once) ejectFromRequest(this.axios, interceptorId);
83-
const result = configChanger(requestConfig);
84-
return result;
85-
}
86-
return requestConfig;
87-
});
88-
}
89-
90-
private setPrintableResponse(once = false) {
91-
const verbConfig = this.verb;
92-
const pathConfig = this.path;
93-
const paramsConfig = this.params;
94-
const interceptorId = this.axios.interceptors.response.use(response => {
95-
if (matchResponse(verbConfig, pathConfig, response, paramsConfig)) {
96-
if (once) ejectFromResponse(this.axios, interceptorId);
97-
console.log('Response from:', this.path);
98-
console.log(JSON.stringify(response.data, null, 2));
99-
}
100-
return response;
101-
});
10216
}
10317

10418
private setup(verb: string, path: string, params?: object) {
105-
this.verb = verb;
106-
this.path = path;
107-
this.params = params;
108-
return this;
19+
const handler = new Handler(this, verb, path, params);
20+
return handler;
10921
}
11022

11123
clear() {
11224
clearAll(this.axios);
11325
}
11426

115-
reply(statusCodeOrConfig: number | RouteConfig, mock?: unknown) {
116-
this.once = false;
117-
this.setProxy(statusCodeOrConfig, mock);
118-
return this;
119-
}
120-
121-
replyOnce(statusCodeOrConfig: number | RouteConfig, mock?: unknown) {
122-
this.once = true;
123-
this.setProxy(statusCodeOrConfig, mock);
124-
return this;
125-
}
126-
127-
changeRequest(changer: RequestConfigChanger) {
128-
this.setRequestConfigChanger(changer);
129-
return this;
130-
}
131-
132-
changeRequestOnce(changer: RequestConfigChanger) {
133-
this.setRequestConfigChanger(changer, true);
134-
return this;
135-
}
136-
137-
printResponse() {
138-
this.setPrintableResponse();
139-
return this;
140-
}
141-
142-
printResponseOnce() {
143-
this.setPrintableResponse(true);
144-
return this;
145-
}
146-
14727
onGet(path: string, params?: object) {
14828
return this.setup('get', path, params);
14929
}

0 commit comments

Comments
 (0)