@zimic/interceptor 1.1.7 → 1.2.0-canary.0
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/dist/{chunk-C4BVIHSP.js → chunk-6VVX2QLH.js} +2 -2
- package/dist/{chunk-C4BVIHSP.js.map → chunk-6VVX2QLH.js.map} +1 -1
- package/dist/{chunk-YPRAL5BM.mjs → chunk-HHCXRRYN.mjs} +2 -2
- package/dist/{chunk-YPRAL5BM.mjs.map → chunk-HHCXRRYN.mjs.map} +1 -1
- package/dist/cli.js +18 -18
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +2 -2
- package/dist/cli.mjs.map +1 -1
- package/dist/http.d.ts +7 -1
- package/dist/http.js +53 -5
- package/dist/http.js.map +1 -1
- package/dist/http.mjs +53 -5
- package/dist/http.mjs.map +1 -1
- package/dist/server.js +6 -6
- package/dist/server.mjs +1 -1
- package/package.json +1 -1
- package/src/http/index.ts +1 -0
- package/src/http/requestHandler/HttpRequestHandlerClient.ts +41 -7
- package/src/http/requestHandler/LocalHttpRequestHandler.ts +9 -0
- package/src/http/requestHandler/RemoteHttpRequestHandler.ts +9 -0
- package/src/http/requestHandler/types/public.ts +16 -0
- package/src/http/requestHandler/types/requests.ts +5 -0
- package/src/utils/numbers.ts +11 -2
- package/src/utils/time.ts +10 -0
package/dist/server.js
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunk6VVX2QLH_js = require('./chunk-6VVX2QLH.js');
|
|
4
4
|
require('./chunk-DGUM43GV.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 chunk6VVX2QLH_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 chunk6VVX2QLH_js.DEFAULT_PREFLIGHT_STATUS_CODE; }
|
|
15
15
|
});
|
|
16
16
|
Object.defineProperty(exports, "NotRunningInterceptorServerError", {
|
|
17
17
|
enumerable: true,
|
|
18
|
-
get: function () { return
|
|
18
|
+
get: function () { return chunk6VVX2QLH_js.NotRunningInterceptorServerError_default; }
|
|
19
19
|
});
|
|
20
20
|
Object.defineProperty(exports, "RunningInterceptorServerError", {
|
|
21
21
|
enumerable: true,
|
|
22
|
-
get: function () { return
|
|
22
|
+
get: function () { return chunk6VVX2QLH_js.RunningInterceptorServerError_default; }
|
|
23
23
|
});
|
|
24
24
|
Object.defineProperty(exports, "createInterceptorServer", {
|
|
25
25
|
enumerable: true,
|
|
26
|
-
get: function () { return
|
|
26
|
+
get: function () { return chunk6VVX2QLH_js.createInterceptorServer; }
|
|
27
27
|
});
|
|
28
28
|
//# sourceMappingURL=server.js.map
|
|
29
29
|
//# sourceMappingURL=server.js.map
|
package/dist/server.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { DEFAULT_ACCESS_CONTROL_HEADERS, DEFAULT_PREFLIGHT_STATUS_CODE, NotRunningInterceptorServerError_default as NotRunningInterceptorServerError, RunningInterceptorServerError_default as RunningInterceptorServerError, createInterceptorServer } from './chunk-
|
|
1
|
+
export { DEFAULT_ACCESS_CONTROL_HEADERS, DEFAULT_PREFLIGHT_STATUS_CODE, NotRunningInterceptorServerError_default as NotRunningInterceptorServerError, RunningInterceptorServerError_default as RunningInterceptorServerError, createInterceptorServer } from './chunk-HHCXRRYN.mjs';
|
|
2
2
|
import './chunk-BJTO5JO5.mjs';
|
|
3
3
|
//# sourceMappingURL=server.mjs.map
|
|
4
4
|
//# sourceMappingURL=server.mjs.map
|
package/package.json
CHANGED
package/src/http/index.ts
CHANGED
|
@@ -17,6 +17,7 @@ export { default as TimesCheckError } from './requestHandler/errors/TimesCheckEr
|
|
|
17
17
|
export type {
|
|
18
18
|
HttpRequestHandlerResponseDeclaration,
|
|
19
19
|
HttpRequestHandlerResponseDeclarationFactory,
|
|
20
|
+
HttpRequestHandlerResponseDelayFactory,
|
|
20
21
|
HttpInterceptorRequest,
|
|
21
22
|
HttpInterceptorResponse,
|
|
22
23
|
InterceptedHttpInterceptorRequest,
|
|
@@ -12,9 +12,11 @@ import {
|
|
|
12
12
|
import blobEquals from '@zimic/utils/data/blobEquals';
|
|
13
13
|
import jsonContains from '@zimic/utils/data/jsonContains';
|
|
14
14
|
import jsonEquals from '@zimic/utils/data/jsonEquals';
|
|
15
|
+
import waitForDelay from '@zimic/utils/time/waitForDelay';
|
|
15
16
|
import { Default, Range } from '@zimic/utils/types';
|
|
16
17
|
|
|
17
18
|
import { convertArrayBufferToBlob, convertReadableStreamToBlob } from '@/utils/data';
|
|
19
|
+
import { random } from '@/utils/numbers';
|
|
18
20
|
|
|
19
21
|
import HttpInterceptorClient from '../interceptor/HttpInterceptorClient';
|
|
20
22
|
import DisabledRequestSavingError from './errors/DisabledRequestSavingError';
|
|
@@ -27,6 +29,7 @@ import {
|
|
|
27
29
|
HttpInterceptorResponse,
|
|
28
30
|
HttpRequestHandlerResponseDeclaration,
|
|
29
31
|
HttpRequestHandlerResponseDeclarationFactory,
|
|
32
|
+
HttpRequestHandlerResponseDelayFactory,
|
|
30
33
|
InterceptedHttpInterceptorRequest,
|
|
31
34
|
} from './types/requests';
|
|
32
35
|
import {
|
|
@@ -60,7 +63,7 @@ class HttpRequestHandlerClient<
|
|
|
60
63
|
numberOfRequests: DEFAULT_NUMBER_OF_REQUEST_LIMITS,
|
|
61
64
|
};
|
|
62
65
|
|
|
63
|
-
private
|
|
66
|
+
private timesPointer?: TimesDeclarationPointer;
|
|
64
67
|
|
|
65
68
|
private numberOfMatchedRequests = 0;
|
|
66
69
|
private unmatchedRequestGroups: UnmatchedHttpInterceptorRequestGroup[] = [];
|
|
@@ -72,6 +75,8 @@ class HttpRequestHandlerClient<
|
|
|
72
75
|
StatusCode
|
|
73
76
|
>;
|
|
74
77
|
|
|
78
|
+
private createResponseDelay?: HttpRequestHandlerResponseDelayFactory<Path, Default<Schema[Path][Method]>>;
|
|
79
|
+
|
|
75
80
|
constructor(
|
|
76
81
|
private interceptor: HttpInterceptorClient<Schema>,
|
|
77
82
|
public method: Method,
|
|
@@ -84,6 +89,28 @@ class HttpRequestHandlerClient<
|
|
|
84
89
|
return this;
|
|
85
90
|
}
|
|
86
91
|
|
|
92
|
+
delay(
|
|
93
|
+
minMilliseconds: number | HttpRequestHandlerResponseDelayFactory<Path, Default<Schema[Path][Method]>>,
|
|
94
|
+
maxMilliseconds?: number,
|
|
95
|
+
): this {
|
|
96
|
+
if (minMilliseconds === maxMilliseconds) {
|
|
97
|
+
return this.delay(minMilliseconds);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (typeof minMilliseconds === 'number' && typeof maxMilliseconds === 'number') {
|
|
101
|
+
this.createResponseDelay = () => random(minMilliseconds, maxMilliseconds);
|
|
102
|
+
return this;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (typeof minMilliseconds === 'number') {
|
|
106
|
+
this.createResponseDelay = () => minMilliseconds;
|
|
107
|
+
return this;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
this.createResponseDelay = minMilliseconds;
|
|
111
|
+
return this;
|
|
112
|
+
}
|
|
113
|
+
|
|
87
114
|
respond<NewStatusCode extends HttpStatusCode>(
|
|
88
115
|
declaration:
|
|
89
116
|
| HttpRequestHandlerResponseDeclaration<Default<Schema[Path][Method]>, NewStatusCode>
|
|
@@ -122,7 +149,7 @@ class HttpRequestHandlerClient<
|
|
|
122
149
|
max: maxNumberOfRequests ?? minNumberOfRequests,
|
|
123
150
|
};
|
|
124
151
|
|
|
125
|
-
this.
|
|
152
|
+
this.timesPointer = new TimesDeclarationPointer(minNumberOfRequests, maxNumberOfRequests);
|
|
126
153
|
|
|
127
154
|
return this;
|
|
128
155
|
}
|
|
@@ -136,7 +163,7 @@ class HttpRequestHandlerClient<
|
|
|
136
163
|
throw new TimesCheckError({
|
|
137
164
|
requestLimits: this.limits.numberOfRequests,
|
|
138
165
|
numberOfMatchedRequests: this.numberOfMatchedRequests,
|
|
139
|
-
declarationPointer: this.
|
|
166
|
+
declarationPointer: this.timesPointer,
|
|
140
167
|
unmatchedRequestGroups: this.unmatchedRequestGroups,
|
|
141
168
|
hasRestrictions: this.restrictions.length > 0,
|
|
142
169
|
requestSaving: this.interceptor.requestSaving,
|
|
@@ -151,7 +178,7 @@ class HttpRequestHandlerClient<
|
|
|
151
178
|
numberOfRequests: DEFAULT_NUMBER_OF_REQUEST_LIMITS,
|
|
152
179
|
};
|
|
153
180
|
|
|
154
|
-
this.
|
|
181
|
+
this.timesPointer = undefined;
|
|
155
182
|
|
|
156
183
|
this.numberOfMatchedRequests = 0;
|
|
157
184
|
this.unmatchedRequestGroups.length = 0;
|
|
@@ -159,6 +186,7 @@ class HttpRequestHandlerClient<
|
|
|
159
186
|
this.clearInterceptedRequests();
|
|
160
187
|
|
|
161
188
|
this.createResponseDeclaration = undefined;
|
|
189
|
+
this.createResponseDelay = undefined;
|
|
162
190
|
|
|
163
191
|
return this;
|
|
164
192
|
}
|
|
@@ -196,9 +224,7 @@ class HttpRequestHandlerClient<
|
|
|
196
224
|
options: { diff: RestrictionDiffs },
|
|
197
225
|
) {
|
|
198
226
|
const shouldSaveUnmatchedRequests =
|
|
199
|
-
this.interceptor.requestSaving.enabled &&
|
|
200
|
-
this.restrictions.length > 0 &&
|
|
201
|
-
this.timesDeclarationPointer !== undefined;
|
|
227
|
+
this.interceptor.requestSaving.enabled && this.restrictions.length > 0 && this.timesPointer !== undefined;
|
|
202
228
|
|
|
203
229
|
if (shouldSaveUnmatchedRequests) {
|
|
204
230
|
this.unmatchedRequestGroups.push({ request, diff: options.diff });
|
|
@@ -414,6 +440,14 @@ class HttpRequestHandlerClient<
|
|
|
414
440
|
throw new NoResponseDefinitionError();
|
|
415
441
|
}
|
|
416
442
|
|
|
443
|
+
if (this.createResponseDelay) {
|
|
444
|
+
const delay = await this.createResponseDelay(request);
|
|
445
|
+
|
|
446
|
+
if (delay > 0) {
|
|
447
|
+
await waitForDelay(delay);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
417
451
|
const appliedDeclaration = await this.createResponseDeclaration(request);
|
|
418
452
|
return appliedDeclaration;
|
|
419
453
|
}
|
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
HttpInterceptorResponse,
|
|
10
10
|
HttpRequestHandlerResponseDeclaration,
|
|
11
11
|
HttpRequestHandlerResponseDeclarationFactory,
|
|
12
|
+
HttpRequestHandlerResponseDelayFactory,
|
|
12
13
|
InterceptedHttpInterceptorRequest,
|
|
13
14
|
} from './types/requests';
|
|
14
15
|
import { HttpRequestHandlerRestriction } from './types/restrictions';
|
|
@@ -41,6 +42,14 @@ class LocalHttpRequestHandler<
|
|
|
41
42
|
return this;
|
|
42
43
|
}
|
|
43
44
|
|
|
45
|
+
delay(
|
|
46
|
+
minMilliseconds: number | HttpRequestHandlerResponseDelayFactory<Path, Default<Schema[Path][Method]>>,
|
|
47
|
+
maxMilliseconds?: number,
|
|
48
|
+
): this {
|
|
49
|
+
this.client.delay(minMilliseconds, maxMilliseconds);
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
|
|
44
53
|
respond<NewStatusCode extends HttpStatusCode>(
|
|
45
54
|
declaration:
|
|
46
55
|
| HttpRequestHandlerResponseDeclaration<Default<Schema[Path][Method]>, NewStatusCode>
|
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
HttpInterceptorResponse,
|
|
13
13
|
HttpRequestHandlerResponseDeclaration,
|
|
14
14
|
HttpRequestHandlerResponseDeclarationFactory,
|
|
15
|
+
HttpRequestHandlerResponseDelayFactory,
|
|
15
16
|
InterceptedHttpInterceptorRequest,
|
|
16
17
|
} from './types/requests';
|
|
17
18
|
import { HttpRequestHandlerRestriction } from './types/restrictions';
|
|
@@ -75,6 +76,14 @@ class RemoteHttpRequestHandler<
|
|
|
75
76
|
return this.unsynced;
|
|
76
77
|
}
|
|
77
78
|
|
|
79
|
+
delay(
|
|
80
|
+
minMilliseconds: number | HttpRequestHandlerResponseDelayFactory<Path, Default<Schema[Path][Method]>>,
|
|
81
|
+
maxMilliseconds?: number,
|
|
82
|
+
): this {
|
|
83
|
+
this.client.delay(minMilliseconds, maxMilliseconds);
|
|
84
|
+
return this.unsynced;
|
|
85
|
+
}
|
|
86
|
+
|
|
78
87
|
respond<NewStatusCode extends HttpStatusCode>(
|
|
79
88
|
declaration:
|
|
80
89
|
| HttpRequestHandlerResponseDeclaration<Default<Schema[Path][Method]>, NewStatusCode>
|
|
@@ -11,6 +11,7 @@ import HttpRequestHandlerClient from '../HttpRequestHandlerClient';
|
|
|
11
11
|
import {
|
|
12
12
|
HttpRequestHandlerResponseDeclaration,
|
|
13
13
|
HttpRequestHandlerResponseDeclarationFactory,
|
|
14
|
+
HttpRequestHandlerResponseDelayFactory,
|
|
14
15
|
InterceptedHttpInterceptorRequest,
|
|
15
16
|
} from './requests';
|
|
16
17
|
import { HttpRequestHandlerRestriction } from './restrictions';
|
|
@@ -56,6 +57,12 @@ export interface LocalHttpRequestHandler<
|
|
|
56
57
|
/** @see {@link https://zimic.dev/docs/interceptor/api/http-request-handler#handlerwithrestriction `handler.with()` API reference} */
|
|
57
58
|
with: (restriction: HttpRequestHandlerRestriction<Schema, Method, Path>) => this;
|
|
58
59
|
|
|
60
|
+
/** @see {@link https://zimic.dev/docs/interceptor/api/http-request-handler#handlerdelay `handler.delay()` API reference} */
|
|
61
|
+
delay: ((
|
|
62
|
+
milliseconds: number | HttpRequestHandlerResponseDelayFactory<Path, Default<Schema[Path][Method]>>,
|
|
63
|
+
) => this) &
|
|
64
|
+
((minMilliseconds: number, maxMilliseconds: number) => this);
|
|
65
|
+
|
|
59
66
|
/** @see {@link https://zimic.dev/docs/interceptor/api/http-request-handler#handlerrespond `handler.respond()` API reference} */
|
|
60
67
|
respond: <NewStatusCode extends HttpResponseSchemaStatusCode<Default<Default<Schema[Path][Method]>['response']>>>(
|
|
61
68
|
declaration:
|
|
@@ -99,6 +106,15 @@ export interface SyncedRemoteHttpRequestHandler<
|
|
|
99
106
|
restriction: HttpRequestHandlerRestriction<Schema, Method, Path>,
|
|
100
107
|
) => PendingRemoteHttpRequestHandler<Schema, Method, Path, StatusCode>;
|
|
101
108
|
|
|
109
|
+
/** @see {@link https://zimic.dev/docs/interceptor/api/http-request-handler#handlerdelay `handler.delay()` API reference} */
|
|
110
|
+
delay: ((
|
|
111
|
+
milliseconds: number | HttpRequestHandlerResponseDelayFactory<Path, Default<Schema[Path][Method]>>,
|
|
112
|
+
) => PendingRemoteHttpRequestHandler<Schema, Method, Path, StatusCode>) &
|
|
113
|
+
((
|
|
114
|
+
minMilliseconds: number,
|
|
115
|
+
maxMilliseconds: number,
|
|
116
|
+
) => PendingRemoteHttpRequestHandler<Schema, Method, Path, StatusCode>);
|
|
117
|
+
|
|
102
118
|
/** @see {@link https://zimic.dev/docs/interceptor/api/http-request-handler#handlerrespond `handler.respond()` API reference} */
|
|
103
119
|
respond: <NewStatusCode extends HttpResponseSchemaStatusCode<Default<Default<Schema[Path][Method]>['response']>>>(
|
|
104
120
|
declaration:
|
|
@@ -62,6 +62,11 @@ export type HttpRequestHandlerResponseDeclarationFactory<
|
|
|
62
62
|
request: Omit<HttpInterceptorRequest<Path, MethodSchema>, 'response'>,
|
|
63
63
|
) => PossiblePromise<HttpRequestHandlerResponseDeclaration<MethodSchema, StatusCode>>;
|
|
64
64
|
|
|
65
|
+
/** @see {@link https://zimic.dev/docs/interceptor/api/http-request-handler#handlerdelay `handler.delay()` API reference} */
|
|
66
|
+
export type HttpRequestHandlerResponseDelayFactory<Path extends string, MethodSchema extends HttpMethodSchema> = (
|
|
67
|
+
request: Omit<HttpInterceptorRequest<Path, MethodSchema>, 'response'>,
|
|
68
|
+
) => PossiblePromise<number>;
|
|
69
|
+
|
|
65
70
|
/** @see {@link https://zimic.dev/docs/interceptor/api/http-request-handler#handlerrequests `handler.requests` API reference} */
|
|
66
71
|
export interface HttpInterceptorRequest<Path extends string, MethodSchema extends HttpMethodSchema>
|
|
67
72
|
extends Omit<HttpRequest, keyof Body | 'headers' | 'clone'> {
|
package/src/utils/numbers.ts
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
|
+
export function random(lowerLimit: number, upperLimit: number): number {
|
|
2
|
+
const range = Math.max(upperLimit - lowerLimit, 0);
|
|
3
|
+
|
|
4
|
+
if (range === 0) {
|
|
5
|
+
return lowerLimit;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
return Math.random() * range + lowerLimit;
|
|
9
|
+
}
|
|
10
|
+
|
|
1
11
|
export function randomInt(lowerLimit: number, upperLimit: number): number {
|
|
2
|
-
|
|
3
|
-
return Math.floor(Math.random() * range + lowerLimit);
|
|
12
|
+
return Math.floor(random(lowerLimit, upperLimit));
|
|
4
13
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export async function usingElapsedTime<ReturnType>(callback: () => Promise<ReturnType>) {
|
|
2
|
+
const startTime = performance.now();
|
|
3
|
+
|
|
4
|
+
const result = await callback();
|
|
5
|
+
|
|
6
|
+
const endTime = performance.now();
|
|
7
|
+
const elapsedTime = endTime - startTime;
|
|
8
|
+
|
|
9
|
+
return { result, startTime, endTime, elapsedTime };
|
|
10
|
+
}
|