@zimic/interceptor 0.15.0-canary.0 → 0.15.0-canary.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -7
- package/dist/{chunk-YGJD3JT6.mjs → chunk-3O3YPVEG.mjs} +69 -104
- package/dist/chunk-3O3YPVEG.mjs.map +1 -0
- package/dist/{chunk-L6Y4EV6T.js → chunk-CWBDZYUG.js} +69 -104
- package/dist/chunk-CWBDZYUG.js.map +1 -0
- package/dist/cli.js +7 -7
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +3 -3
- package/dist/cli.mjs.map +1 -1
- package/dist/http.d.ts +38 -35
- package/dist/http.js +173 -235
- package/dist/http.js.map +1 -1
- package/dist/http.mjs +173 -235
- package/dist/http.mjs.map +1 -1
- package/dist/server.d.ts +15 -10
- package/dist/server.js +5 -5
- package/dist/server.mjs +1 -1
- package/package.json +2 -2
- package/src/cli/server/start.ts +1 -1
- package/src/http/interceptor/HttpInterceptorClient.ts +23 -34
- package/src/http/interceptor/HttpInterceptorStore.ts +2 -2
- package/src/http/interceptor/LocalHttpInterceptor.ts +22 -25
- package/src/http/interceptor/RemoteHttpInterceptor.ts +22 -25
- package/src/http/interceptor/errors/NotStartedHttpInterceptorError.ts +1 -1
- package/src/http/interceptor/errors/UnknownHttpInterceptorPlatformError.ts +1 -1
- package/src/http/interceptor/types/options.ts +3 -3
- package/src/http/interceptor/types/public.ts +12 -9
- package/src/http/interceptorWorker/HttpInterceptorWorker.ts +10 -28
- package/src/http/interceptorWorker/LocalHttpInterceptorWorker.ts +21 -25
- package/src/http/interceptorWorker/RemoteHttpInterceptorWorker.ts +22 -26
- package/src/http/requestHandler/HttpRequestHandlerClient.ts +11 -22
- package/src/http/requestHandler/LocalHttpRequestHandler.ts +16 -20
- package/src/http/requestHandler/RemoteHttpRequestHandler.ts +18 -28
- package/src/http/requestHandler/types/public.ts +22 -24
- package/src/server/InterceptorServer.ts +45 -68
- package/src/server/types/public.ts +15 -10
- package/src/webSocket/WebSocketClient.ts +1 -1
- package/src/webSocket/WebSocketHandler.ts +11 -19
- package/src/webSocket/WebSocketServer.ts +4 -4
- package/dist/chunk-L6Y4EV6T.js.map +0 -1
- package/dist/chunk-YGJD3JT6.mjs.map +0 -1
package/dist/server.d.ts
CHANGED
|
@@ -29,31 +29,36 @@ interface InterceptorServerOptions {
|
|
|
29
29
|
*/
|
|
30
30
|
interface InterceptorServer {
|
|
31
31
|
/**
|
|
32
|
-
*
|
|
32
|
+
* The hostname of the server.
|
|
33
|
+
*
|
|
33
34
|
* @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#zimic-server `zimic-interceptor server` API reference}
|
|
34
35
|
*/
|
|
35
|
-
hostname:
|
|
36
|
+
hostname: string;
|
|
36
37
|
/**
|
|
37
|
-
*
|
|
38
|
+
* The port of the server.
|
|
39
|
+
*
|
|
38
40
|
* @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#zimic-server `zimic-interceptor server` API reference}
|
|
39
41
|
*/
|
|
40
|
-
port:
|
|
42
|
+
port: number | undefined;
|
|
41
43
|
/**
|
|
44
|
+
* Whether to log warnings about unhandled requests to the console.
|
|
45
|
+
*
|
|
42
46
|
* @default true
|
|
43
|
-
* @returns Whether to log warnings about unhandled requests to the console.
|
|
44
47
|
* @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#zimic-server `zimic-interceptor server` API reference}
|
|
45
48
|
*/
|
|
46
|
-
logUnhandledRequests:
|
|
49
|
+
logUnhandledRequests: boolean;
|
|
47
50
|
/**
|
|
48
|
-
*
|
|
51
|
+
* The HTTP URL of the server.
|
|
52
|
+
*
|
|
49
53
|
* @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#zimic-server `zimic-interceptor server` API reference}
|
|
50
54
|
*/
|
|
51
|
-
httpURL:
|
|
55
|
+
httpURL: string | undefined;
|
|
52
56
|
/**
|
|
53
|
-
*
|
|
57
|
+
* Whether the server is running.
|
|
58
|
+
*
|
|
54
59
|
* @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#zimic-server `zimic-interceptor server` API reference}
|
|
55
60
|
*/
|
|
56
|
-
isRunning:
|
|
61
|
+
isRunning: boolean;
|
|
57
62
|
/**
|
|
58
63
|
* Starts the server.
|
|
59
64
|
*
|
package/dist/server.js
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunkCWBDZYUG_js = require('./chunk-CWBDZYUG.js');
|
|
4
4
|
require('./chunk-WCQVDF3K.js');
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
Object.defineProperty(exports, "DEFAULT_ACCESS_CONTROL_HEADERS", {
|
|
9
9
|
enumerable: true,
|
|
10
|
-
get: function () { return
|
|
10
|
+
get: function () { return chunkCWBDZYUG_js.DEFAULT_ACCESS_CONTROL_HEADERS; }
|
|
11
11
|
});
|
|
12
12
|
Object.defineProperty(exports, "DEFAULT_PREFLIGHT_STATUS_CODE", {
|
|
13
13
|
enumerable: true,
|
|
14
|
-
get: function () { return
|
|
14
|
+
get: function () { return chunkCWBDZYUG_js.DEFAULT_PREFLIGHT_STATUS_CODE; }
|
|
15
15
|
});
|
|
16
16
|
Object.defineProperty(exports, "NotStartedInterceptorServerError", {
|
|
17
17
|
enumerable: true,
|
|
18
|
-
get: function () { return
|
|
18
|
+
get: function () { return chunkCWBDZYUG_js.NotStartedInterceptorServerError_default; }
|
|
19
19
|
});
|
|
20
20
|
Object.defineProperty(exports, "interceptorServer", {
|
|
21
21
|
enumerable: true,
|
|
22
|
-
get: function () { return
|
|
22
|
+
get: function () { return chunkCWBDZYUG_js.interceptorServer; }
|
|
23
23
|
});
|
|
24
24
|
//# sourceMappingURL=server.js.map
|
|
25
25
|
//# sourceMappingURL=server.js.map
|
package/dist/server.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { DEFAULT_ACCESS_CONTROL_HEADERS, DEFAULT_PREFLIGHT_STATUS_CODE, NotStartedInterceptorServerError_default as NotStartedInterceptorServerError, interceptorServer } from './chunk-
|
|
1
|
+
export { DEFAULT_ACCESS_CONTROL_HEADERS, DEFAULT_PREFLIGHT_STATUS_CODE, NotStartedInterceptorServerError_default as NotStartedInterceptorServerError, interceptorServer } from './chunk-3O3YPVEG.mjs';
|
|
2
2
|
import './chunk-CGILA3WO.mjs';
|
|
3
3
|
//# sourceMappingURL=server.mjs.map
|
|
4
4
|
//# sourceMappingURL=server.mjs.map
|
package/package.json
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"api",
|
|
15
15
|
"static"
|
|
16
16
|
],
|
|
17
|
-
"version": "0.15.0-canary.
|
|
17
|
+
"version": "0.15.0-canary.1",
|
|
18
18
|
"repository": {
|
|
19
19
|
"type": "git",
|
|
20
20
|
"url": "https://github.com/zimicjs/zimic.git",
|
|
@@ -98,8 +98,8 @@
|
|
|
98
98
|
"typescript": "^5.8.2",
|
|
99
99
|
"vitest": "^3.0.7",
|
|
100
100
|
"@zimic/eslint-config-node": "0.0.0",
|
|
101
|
-
"@zimic/tsconfig": "0.0.0",
|
|
102
101
|
"@zimic/lint-staged-config": "0.0.0",
|
|
102
|
+
"@zimic/tsconfig": "0.0.0",
|
|
103
103
|
"@zimic/utils": "0.0.0"
|
|
104
104
|
},
|
|
105
105
|
"peerDependencies": {
|
package/src/cli/server/start.ts
CHANGED
|
@@ -59,7 +59,7 @@ async function startInterceptorServer({
|
|
|
59
59
|
|
|
60
60
|
await server.start();
|
|
61
61
|
|
|
62
|
-
logWithPrefix(`${ephemeral ? 'Ephemeral s' : 'S'}erver is running on ${server.httpURL
|
|
62
|
+
logWithPrefix(`${ephemeral ? 'Ephemeral s' : 'S'}erver is running on ${server.httpURL}`);
|
|
63
63
|
|
|
64
64
|
if (onReady) {
|
|
65
65
|
try {
|
|
@@ -31,10 +31,11 @@ class HttpInterceptorClient<
|
|
|
31
31
|
> {
|
|
32
32
|
private worker: HttpInterceptorWorker;
|
|
33
33
|
private store: HttpInterceptorStore;
|
|
34
|
+
|
|
34
35
|
private _baseURL: URL;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
isRunning = false;
|
|
37
|
+
onUnhandledRequest?: UnhandledRequestStrategy;
|
|
38
|
+
shouldSaveRequests = false;
|
|
38
39
|
|
|
39
40
|
private Handler: HandlerConstructor;
|
|
40
41
|
|
|
@@ -62,34 +63,22 @@ class HttpInterceptorClient<
|
|
|
62
63
|
this.store = options.store;
|
|
63
64
|
this._baseURL = options.baseURL;
|
|
64
65
|
this.Handler = options.Handler;
|
|
65
|
-
this.
|
|
66
|
-
this.
|
|
66
|
+
this.onUnhandledRequest = options.onUnhandledRequest;
|
|
67
|
+
this.shouldSaveRequests = options.saveRequests ?? false;
|
|
67
68
|
}
|
|
68
69
|
|
|
69
|
-
|
|
70
|
+
get baseURLAsString() {
|
|
70
71
|
const baseURL = this._baseURL;
|
|
71
72
|
|
|
72
73
|
if (baseURL.href === `${baseURL.origin}/`) {
|
|
73
74
|
return baseURL.origin;
|
|
74
75
|
}
|
|
75
76
|
|
|
76
|
-
return baseURL.
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
platform() {
|
|
80
|
-
return this.worker.platform();
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
onUnhandledRequest() {
|
|
84
|
-
return this._onUnhandledRequest;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
isRunning() {
|
|
88
|
-
return this.worker.isRunning() && this._isRunning;
|
|
77
|
+
return baseURL.href;
|
|
89
78
|
}
|
|
90
79
|
|
|
91
|
-
|
|
92
|
-
return this.
|
|
80
|
+
get platform() {
|
|
81
|
+
return this.worker.platform;
|
|
93
82
|
}
|
|
94
83
|
|
|
95
84
|
async start() {
|
|
@@ -115,12 +104,12 @@ class HttpInterceptorClient<
|
|
|
115
104
|
} else {
|
|
116
105
|
this.store.markRemoteInterceptorAsRunning(this, isRunning, this._baseURL);
|
|
117
106
|
}
|
|
118
|
-
this.
|
|
107
|
+
this.isRunning = isRunning;
|
|
119
108
|
}
|
|
120
109
|
|
|
121
110
|
private numberOfRunningInterceptors() {
|
|
122
111
|
if (this.worker instanceof LocalHttpInterceptorWorker) {
|
|
123
|
-
return this.store.numberOfRunningLocalInterceptors
|
|
112
|
+
return this.store.numberOfRunningLocalInterceptors;
|
|
124
113
|
} else {
|
|
125
114
|
return this.store.numberOfRunningRemoteInterceptors(this._baseURL);
|
|
126
115
|
}
|
|
@@ -158,7 +147,7 @@ class HttpInterceptorClient<
|
|
|
158
147
|
Method extends HttpSchemaMethod<Schema>,
|
|
159
148
|
Path extends HttpSchemaPath<Schema, Method>,
|
|
160
149
|
>(method: Method, path: Path): HttpRequestHandler<Schema, Method, Path> {
|
|
161
|
-
if (!this.isRunning
|
|
150
|
+
if (!this.isRunning) {
|
|
162
151
|
throw new NotStartedHttpInterceptorError();
|
|
163
152
|
}
|
|
164
153
|
|
|
@@ -173,30 +162,30 @@ class HttpInterceptorClient<
|
|
|
173
162
|
Path extends HttpSchemaPath<Schema, Method>,
|
|
174
163
|
StatusCode extends HttpStatusCode = never,
|
|
175
164
|
>(handler: InternalHttpRequestHandler<Schema, Method, Path, StatusCode>) {
|
|
176
|
-
const handlerClients = this.handlerClientsByMethod[handler.method
|
|
165
|
+
const handlerClients = this.handlerClientsByMethod[handler.method].get(handler.path) ?? [];
|
|
177
166
|
|
|
178
|
-
const isAlreadyRegistered = handlerClients.includes(handler.client
|
|
167
|
+
const isAlreadyRegistered = handlerClients.includes(handler.client);
|
|
179
168
|
if (isAlreadyRegistered) {
|
|
180
169
|
return;
|
|
181
170
|
}
|
|
182
171
|
|
|
183
|
-
handlerClients.push(handler.client
|
|
172
|
+
handlerClients.push(handler.client);
|
|
184
173
|
|
|
185
174
|
const isFirstHandlerForMethodPath = handlerClients.length === 1;
|
|
186
175
|
if (!isFirstHandlerForMethodPath) {
|
|
187
176
|
return;
|
|
188
177
|
}
|
|
189
178
|
|
|
190
|
-
this.handlerClientsByMethod[handler.method
|
|
179
|
+
this.handlerClientsByMethod[handler.method].set(handler.path, handlerClients);
|
|
191
180
|
|
|
192
|
-
const url = joinURL(this.
|
|
181
|
+
const url = joinURL(this.baseURLAsString, handler.path);
|
|
193
182
|
const urlRegex = createRegExpFromURL(url);
|
|
194
183
|
|
|
195
|
-
const registrationResult = this.worker.use(this, handler.method
|
|
184
|
+
const registrationResult = this.worker.use(this, handler.method, url, async (context) => {
|
|
196
185
|
const response = await this.handleInterceptedRequest(
|
|
197
186
|
urlRegex,
|
|
198
|
-
handler.method
|
|
199
|
-
handler.path
|
|
187
|
+
handler.method,
|
|
188
|
+
handler.path,
|
|
200
189
|
context as HttpInterceptorRequestContext<Schema, Method, Path>,
|
|
201
190
|
);
|
|
202
191
|
return response;
|
|
@@ -225,7 +214,7 @@ class HttpInterceptorClient<
|
|
|
225
214
|
const responseDeclaration = await matchedHandler.applyResponseDeclaration(parsedRequest);
|
|
226
215
|
const response = HttpInterceptorWorker.createResponseFromDeclaration(request, responseDeclaration);
|
|
227
216
|
|
|
228
|
-
if (this.shouldSaveRequests
|
|
217
|
+
if (this.shouldSaveRequests) {
|
|
229
218
|
const responseClone = response.clone();
|
|
230
219
|
|
|
231
220
|
const parsedResponse = await HttpInterceptorWorker.parseRawResponse<
|
|
@@ -275,7 +264,7 @@ class HttpInterceptorClient<
|
|
|
275
264
|
}
|
|
276
265
|
|
|
277
266
|
clear(options: { onCommitSuccess?: () => void; onCommitError?: () => void } = {}) {
|
|
278
|
-
if (!this.isRunning
|
|
267
|
+
if (!this.isRunning) {
|
|
279
268
|
throw new NotStartedHttpInterceptorError();
|
|
280
269
|
}
|
|
281
270
|
|
|
@@ -16,7 +16,7 @@ class HttpInterceptorStore {
|
|
|
16
16
|
|
|
17
17
|
private class = HttpInterceptorStore;
|
|
18
18
|
|
|
19
|
-
localWorker() {
|
|
19
|
+
get localWorker() {
|
|
20
20
|
return this.class._localWorker;
|
|
21
21
|
}
|
|
22
22
|
|
|
@@ -24,7 +24,7 @@ class HttpInterceptorStore {
|
|
|
24
24
|
return this.class.remoteWorkers.get(baseURL.origin);
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
numberOfRunningLocalInterceptors() {
|
|
27
|
+
get numberOfRunningLocalInterceptors() {
|
|
28
28
|
return this.class.runningLocalInterceptors.size;
|
|
29
29
|
}
|
|
30
30
|
|
|
@@ -13,7 +13,8 @@ class LocalHttpInterceptor<Schema extends HttpSchema> implements PublicLocalHttp
|
|
|
13
13
|
readonly type: 'local';
|
|
14
14
|
|
|
15
15
|
private store = new HttpInterceptorStore();
|
|
16
|
-
|
|
16
|
+
|
|
17
|
+
client: HttpInterceptorClient<Schema>;
|
|
17
18
|
|
|
18
19
|
constructor(options: LocalHttpInterceptorOptions) {
|
|
19
20
|
this.type = options.type;
|
|
@@ -24,7 +25,7 @@ class LocalHttpInterceptor<Schema extends HttpSchema> implements PublicLocalHttp
|
|
|
24
25
|
|
|
25
26
|
const worker = this.store.getOrCreateLocalWorker({});
|
|
26
27
|
|
|
27
|
-
this.
|
|
28
|
+
this.client = new HttpInterceptorClient<Schema>({
|
|
28
29
|
worker,
|
|
29
30
|
store: this.store,
|
|
30
31
|
baseURL,
|
|
@@ -34,73 +35,69 @@ class LocalHttpInterceptor<Schema extends HttpSchema> implements PublicLocalHttp
|
|
|
34
35
|
});
|
|
35
36
|
}
|
|
36
37
|
|
|
37
|
-
|
|
38
|
-
return this.
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
baseURL() {
|
|
42
|
-
return this._client.baseURL();
|
|
38
|
+
get baseURL() {
|
|
39
|
+
return this.client.baseURLAsString;
|
|
43
40
|
}
|
|
44
41
|
|
|
45
|
-
platform() {
|
|
46
|
-
return this.
|
|
42
|
+
get platform() {
|
|
43
|
+
return this.client.platform;
|
|
47
44
|
}
|
|
48
45
|
|
|
49
|
-
isRunning() {
|
|
50
|
-
return this.
|
|
46
|
+
get isRunning() {
|
|
47
|
+
return this.client.isRunning;
|
|
51
48
|
}
|
|
52
49
|
|
|
53
50
|
async start() {
|
|
54
|
-
if (this.isRunning
|
|
51
|
+
if (this.isRunning) {
|
|
55
52
|
return;
|
|
56
53
|
}
|
|
57
54
|
|
|
58
|
-
await this.
|
|
55
|
+
await this.client.start();
|
|
59
56
|
}
|
|
60
57
|
|
|
61
58
|
async stop() {
|
|
62
|
-
if (!this.isRunning
|
|
59
|
+
if (!this.isRunning) {
|
|
63
60
|
return;
|
|
64
61
|
}
|
|
65
62
|
|
|
66
63
|
this.clear();
|
|
67
|
-
await this.
|
|
64
|
+
await this.client.stop();
|
|
68
65
|
}
|
|
69
66
|
|
|
70
67
|
get = ((path: HttpSchemaPath<Schema, HttpSchemaMethod<Schema>>) => {
|
|
71
|
-
return this.
|
|
68
|
+
return this.client.get(path);
|
|
72
69
|
}) as unknown as SyncHttpInterceptorMethodHandler<Schema, 'GET'>;
|
|
73
70
|
|
|
74
71
|
post = ((path: HttpSchemaPath<Schema, HttpSchemaMethod<Schema>>) => {
|
|
75
|
-
return this.
|
|
72
|
+
return this.client.post(path);
|
|
76
73
|
}) as unknown as SyncHttpInterceptorMethodHandler<Schema, 'POST'>;
|
|
77
74
|
|
|
78
75
|
patch = ((path: HttpSchemaPath<Schema, HttpSchemaMethod<Schema>>) => {
|
|
79
|
-
return this.
|
|
76
|
+
return this.client.patch(path);
|
|
80
77
|
}) as unknown as SyncHttpInterceptorMethodHandler<Schema, 'PATCH'>;
|
|
81
78
|
|
|
82
79
|
put = ((path: HttpSchemaPath<Schema, HttpSchemaMethod<Schema>>) => {
|
|
83
|
-
return this.
|
|
80
|
+
return this.client.put(path);
|
|
84
81
|
}) as unknown as SyncHttpInterceptorMethodHandler<Schema, 'PUT'>;
|
|
85
82
|
|
|
86
83
|
delete = ((path: HttpSchemaPath<Schema, HttpSchemaMethod<Schema>>) => {
|
|
87
|
-
return this.
|
|
84
|
+
return this.client.delete(path);
|
|
88
85
|
}) as unknown as SyncHttpInterceptorMethodHandler<Schema, 'DELETE'>;
|
|
89
86
|
|
|
90
87
|
head = ((path: HttpSchemaPath<Schema, HttpSchemaMethod<Schema>>) => {
|
|
91
|
-
return this.
|
|
88
|
+
return this.client.head(path);
|
|
92
89
|
}) as unknown as SyncHttpInterceptorMethodHandler<Schema, 'HEAD'>;
|
|
93
90
|
|
|
94
91
|
options = ((path: HttpSchemaPath<Schema, HttpSchemaMethod<Schema>>) => {
|
|
95
|
-
return this.
|
|
92
|
+
return this.client.options(path);
|
|
96
93
|
}) as unknown as SyncHttpInterceptorMethodHandler<Schema, 'OPTIONS'>;
|
|
97
94
|
|
|
98
95
|
checkTimes() {
|
|
99
|
-
this.
|
|
96
|
+
this.client.checkTimes();
|
|
100
97
|
}
|
|
101
98
|
|
|
102
99
|
clear() {
|
|
103
|
-
this.
|
|
100
|
+
this.client.clear();
|
|
104
101
|
}
|
|
105
102
|
}
|
|
106
103
|
|
|
@@ -13,7 +13,8 @@ class RemoteHttpInterceptor<Schema extends HttpSchema> implements PublicRemoteHt
|
|
|
13
13
|
readonly type: 'remote';
|
|
14
14
|
|
|
15
15
|
private store = new HttpInterceptorStore();
|
|
16
|
-
|
|
16
|
+
|
|
17
|
+
client: HttpInterceptorClient<Schema, typeof RemoteHttpRequestHandler>;
|
|
17
18
|
|
|
18
19
|
constructor(options: RemoteHttpInterceptorOptions) {
|
|
19
20
|
this.type = options.type;
|
|
@@ -26,7 +27,7 @@ class RemoteHttpInterceptor<Schema extends HttpSchema> implements PublicRemoteHt
|
|
|
26
27
|
|
|
27
28
|
const worker = this.store.getOrCreateRemoteWorker({ serverURL });
|
|
28
29
|
|
|
29
|
-
this.
|
|
30
|
+
this.client = new HttpInterceptorClient<Schema, typeof RemoteHttpRequestHandler>({
|
|
30
31
|
worker,
|
|
31
32
|
store: this.store,
|
|
32
33
|
baseURL,
|
|
@@ -36,71 +37,67 @@ class RemoteHttpInterceptor<Schema extends HttpSchema> implements PublicRemoteHt
|
|
|
36
37
|
});
|
|
37
38
|
}
|
|
38
39
|
|
|
39
|
-
|
|
40
|
-
return this.
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
baseURL() {
|
|
44
|
-
return this._client.baseURL();
|
|
40
|
+
get baseURL() {
|
|
41
|
+
return this.client.baseURLAsString;
|
|
45
42
|
}
|
|
46
43
|
|
|
47
|
-
platform() {
|
|
48
|
-
return this.
|
|
44
|
+
get platform() {
|
|
45
|
+
return this.client.platform;
|
|
49
46
|
}
|
|
50
47
|
|
|
51
|
-
isRunning() {
|
|
52
|
-
return this.
|
|
48
|
+
get isRunning() {
|
|
49
|
+
return this.client.isRunning;
|
|
53
50
|
}
|
|
54
51
|
|
|
55
52
|
async start() {
|
|
56
|
-
if (this.isRunning
|
|
53
|
+
if (this.isRunning) {
|
|
57
54
|
return;
|
|
58
55
|
}
|
|
59
56
|
|
|
60
|
-
await this.
|
|
57
|
+
await this.client.start();
|
|
61
58
|
}
|
|
62
59
|
|
|
63
60
|
async stop() {
|
|
64
|
-
if (!this.isRunning
|
|
61
|
+
if (!this.isRunning) {
|
|
65
62
|
return;
|
|
66
63
|
}
|
|
67
64
|
|
|
68
65
|
await this.clear();
|
|
69
|
-
await this.
|
|
66
|
+
await this.client.stop();
|
|
70
67
|
}
|
|
71
68
|
|
|
72
69
|
get = ((path: HttpSchemaPath<Schema, HttpSchemaMethod<Schema>>) => {
|
|
73
|
-
return this.
|
|
70
|
+
return this.client.get(path);
|
|
74
71
|
}) as unknown as AsyncHttpInterceptorMethodHandler<Schema, 'GET'>;
|
|
75
72
|
|
|
76
73
|
post = ((path: HttpSchemaPath<Schema, HttpSchemaMethod<Schema>>) => {
|
|
77
|
-
return this.
|
|
74
|
+
return this.client.post(path);
|
|
78
75
|
}) as unknown as AsyncHttpInterceptorMethodHandler<Schema, 'POST'>;
|
|
79
76
|
|
|
80
77
|
patch = ((path: HttpSchemaPath<Schema, HttpSchemaMethod<Schema>>) => {
|
|
81
|
-
return this.
|
|
78
|
+
return this.client.patch(path);
|
|
82
79
|
}) as unknown as AsyncHttpInterceptorMethodHandler<Schema, 'PATCH'>;
|
|
83
80
|
|
|
84
81
|
put = ((path: HttpSchemaPath<Schema, HttpSchemaMethod<Schema>>) => {
|
|
85
|
-
return this.
|
|
82
|
+
return this.client.put(path);
|
|
86
83
|
}) as unknown as AsyncHttpInterceptorMethodHandler<Schema, 'PUT'>;
|
|
87
84
|
|
|
88
85
|
delete = ((path: HttpSchemaPath<Schema, HttpSchemaMethod<Schema>>) => {
|
|
89
|
-
return this.
|
|
86
|
+
return this.client.delete(path);
|
|
90
87
|
}) as unknown as AsyncHttpInterceptorMethodHandler<Schema, 'DELETE'>;
|
|
91
88
|
|
|
92
89
|
head = ((path: HttpSchemaPath<Schema, HttpSchemaMethod<Schema>>) => {
|
|
93
|
-
return this.
|
|
90
|
+
return this.client.head(path);
|
|
94
91
|
}) as unknown as AsyncHttpInterceptorMethodHandler<Schema, 'HEAD'>;
|
|
95
92
|
|
|
96
93
|
options = ((path: HttpSchemaPath<Schema, HttpSchemaMethod<Schema>>) => {
|
|
97
|
-
return this.
|
|
94
|
+
return this.client.options(path);
|
|
98
95
|
}) as unknown as AsyncHttpInterceptorMethodHandler<Schema, 'OPTIONS'>;
|
|
99
96
|
|
|
100
97
|
checkTimes() {
|
|
101
98
|
return new Promise<void>((resolve, reject) => {
|
|
102
99
|
try {
|
|
103
|
-
this.
|
|
100
|
+
this.client.checkTimes();
|
|
104
101
|
resolve();
|
|
105
102
|
} catch (error) {
|
|
106
103
|
reject(error);
|
|
@@ -110,7 +107,7 @@ class RemoteHttpInterceptor<Schema extends HttpSchema> implements PublicRemoteHt
|
|
|
110
107
|
|
|
111
108
|
async clear() {
|
|
112
109
|
await new Promise<void>((resolve, reject) => {
|
|
113
|
-
this.
|
|
110
|
+
this.client.clear({
|
|
114
111
|
onCommitSuccess: resolve,
|
|
115
112
|
onCommitError: reject,
|
|
116
113
|
});
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorstart `interceptor.start()` API reference}
|
|
5
5
|
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorstop `interceptor.stop()` API reference}
|
|
6
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorisrunning `interceptor.isRunning
|
|
6
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorisrunning `interceptor.isRunning` API reference}
|
|
7
7
|
*/
|
|
8
8
|
class NotStartedHttpInterceptorError extends Error {
|
|
9
9
|
constructor() {
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* An error thrown when an unknown interceptor platform is detected. Currently, the platforms `node` and `browser` are
|
|
3
3
|
* supported.
|
|
4
4
|
*
|
|
5
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorplatform `interceptor.platform
|
|
5
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorplatform `interceptor.platform` API reference}
|
|
6
6
|
*/
|
|
7
7
|
class UnknownHttpInterceptorPlatformError extends Error {
|
|
8
8
|
/* istanbul ignore next -- @preserve
|
|
@@ -12,7 +12,7 @@ export type HttpInterceptorType = 'local' | 'remote';
|
|
|
12
12
|
/**
|
|
13
13
|
* The platform where an HTTP interceptor is running.
|
|
14
14
|
*
|
|
15
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorplatform `interceptor.platform
|
|
15
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorplatform `interceptor.platform` API reference}
|
|
16
16
|
*/
|
|
17
17
|
export type HttpInterceptorPlatform = 'node' | 'browser';
|
|
18
18
|
|
|
@@ -121,9 +121,9 @@ export interface SharedHttpInterceptorOptions {
|
|
|
121
121
|
/**
|
|
122
122
|
* Whether {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#httprequesthandler request handlers}
|
|
123
123
|
* should save their intercepted requests in memory and make them accessible through
|
|
124
|
-
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlerrequests `handler.requests
|
|
124
|
+
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlerrequests `handler.requests`}.
|
|
125
125
|
*
|
|
126
|
-
* **
|
|
126
|
+
* **Important**: Saving the intercepted requests will lead to a memory leak if not accompanied by clearing of the
|
|
127
127
|
* interceptor or disposal of the handlers (i.e. garbage collection). If you plan on accessing those requests, such as
|
|
128
128
|
* to assert them in your tests, set this option to `true` and make sure to regularly clear the interceptor. A common
|
|
129
129
|
* practice is to call
|
|
@@ -13,22 +13,25 @@ import { HttpInterceptorPlatform } from './options';
|
|
|
13
13
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
14
14
|
export interface HttpInterceptor<_Schema extends HttpSchema> {
|
|
15
15
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
16
|
+
* The base URL used by the interceptor.
|
|
17
|
+
*
|
|
18
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorbaseurl `interceptor.baseURL` API reference}
|
|
18
19
|
*/
|
|
19
|
-
baseURL:
|
|
20
|
+
baseURL: string;
|
|
20
21
|
|
|
21
22
|
/**
|
|
22
|
-
*
|
|
23
|
-
*
|
|
23
|
+
* The platform the interceptor is running on.
|
|
24
|
+
*
|
|
25
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorplatform `interceptor.platform` API reference}
|
|
24
26
|
*/
|
|
25
|
-
platform:
|
|
27
|
+
platform: HttpInterceptorPlatform | null;
|
|
26
28
|
|
|
27
29
|
/**
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
+
* Whether the interceptor is currently running and ready to use.
|
|
31
|
+
*
|
|
32
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorisrunning `interceptor.isRunning` API reference}
|
|
30
33
|
*/
|
|
31
|
-
isRunning:
|
|
34
|
+
isRunning: boolean;
|
|
32
35
|
|
|
33
36
|
/**
|
|
34
37
|
* Starts the interceptor, allowing it to intercept HTTP requests.
|
|
@@ -41,8 +41,9 @@ import { HttpResponseFactory } from './types/requests';
|
|
|
41
41
|
abstract class HttpInterceptorWorker {
|
|
42
42
|
abstract readonly type: 'local' | 'remote';
|
|
43
43
|
|
|
44
|
-
|
|
45
|
-
|
|
44
|
+
platform: HttpInterceptorPlatform | null = null;
|
|
45
|
+
isRunning = false;
|
|
46
|
+
|
|
46
47
|
private startingPromise?: Promise<void>;
|
|
47
48
|
private stoppingPromise?: Promise<void>;
|
|
48
49
|
|
|
@@ -50,26 +51,10 @@ abstract class HttpInterceptorWorker {
|
|
|
50
51
|
|
|
51
52
|
private runningInterceptors: AnyHttpInterceptorClient[] = [];
|
|
52
53
|
|
|
53
|
-
platform() {
|
|
54
|
-
return this._platform;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
protected setPlatform(platform: HttpInterceptorPlatform) {
|
|
58
|
-
this._platform = platform;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
isRunning() {
|
|
62
|
-
return this._isRunning;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
protected setIsRunning(isRunning: boolean) {
|
|
66
|
-
this._isRunning = isRunning;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
54
|
abstract start(): Promise<void>;
|
|
70
55
|
|
|
71
56
|
protected async sharedStart(internalStart: () => Promise<void>) {
|
|
72
|
-
if (this.isRunning
|
|
57
|
+
if (this.isRunning) {
|
|
73
58
|
return;
|
|
74
59
|
}
|
|
75
60
|
|
|
@@ -98,7 +83,7 @@ abstract class HttpInterceptorWorker {
|
|
|
98
83
|
abstract stop(): Promise<void>;
|
|
99
84
|
|
|
100
85
|
protected async sharedStop(internalStop: () => PossiblePromise<void>) {
|
|
101
|
-
if (!this.isRunning
|
|
86
|
+
if (!this.isRunning) {
|
|
102
87
|
return;
|
|
103
88
|
}
|
|
104
89
|
if (this.stoppingPromise) {
|
|
@@ -194,8 +179,7 @@ abstract class HttpInterceptorWorker {
|
|
|
194
179
|
|
|
195
180
|
private findInterceptorByRequestBaseURL(request: Request) {
|
|
196
181
|
const interceptor = this.runningInterceptors.findLast((interceptor) => {
|
|
197
|
-
|
|
198
|
-
return request.url.startsWith(baseURL);
|
|
182
|
+
return request.url.startsWith(interceptor.baseURLAsString);
|
|
199
183
|
});
|
|
200
184
|
|
|
201
185
|
return interceptor;
|
|
@@ -217,14 +201,12 @@ abstract class HttpInterceptorWorker {
|
|
|
217
201
|
}
|
|
218
202
|
|
|
219
203
|
private async getInterceptorUnhandledRequestStrategy(request: Request, interceptor: AnyHttpInterceptorClient) {
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
if (typeof interceptorStrategyOrFactory === 'function') {
|
|
204
|
+
if (typeof interceptor.onUnhandledRequest === 'function') {
|
|
223
205
|
const parsedRequest = await HttpInterceptorWorker.parseRawUnhandledRequest(request);
|
|
224
|
-
return
|
|
206
|
+
return interceptor.onUnhandledRequest(parsedRequest);
|
|
225
207
|
}
|
|
226
208
|
|
|
227
|
-
return
|
|
209
|
+
return interceptor.onUnhandledRequest;
|
|
228
210
|
}
|
|
229
211
|
|
|
230
212
|
abstract clearHandlers(): PossiblePromise<void>;
|
|
@@ -233,7 +215,7 @@ abstract class HttpInterceptorWorker {
|
|
|
233
215
|
interceptor: HttpInterceptorClient<Schema>,
|
|
234
216
|
): PossiblePromise<void>;
|
|
235
217
|
|
|
236
|
-
abstract interceptorsWithHandlers(): AnyHttpInterceptorClient[];
|
|
218
|
+
abstract get interceptorsWithHandlers(): AnyHttpInterceptorClient[];
|
|
237
219
|
|
|
238
220
|
static createResponseFromDeclaration(
|
|
239
221
|
request: Request,
|