@zimic/interceptor 1.1.7 → 1.2.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/dist/server.js CHANGED
@@ -1,29 +1,29 @@
1
1
  'use strict';
2
2
 
3
- var chunkC4BVIHSP_js = require('./chunk-C4BVIHSP.js');
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 chunkC4BVIHSP_js.DEFAULT_ACCESS_CONTROL_HEADERS; }
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 chunkC4BVIHSP_js.DEFAULT_PREFLIGHT_STATUS_CODE; }
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 chunkC4BVIHSP_js.NotRunningInterceptorServerError_default; }
18
+ get: function () { return chunk6VVX2QLH_js.NotRunningInterceptorServerError_default; }
19
19
  });
20
20
  Object.defineProperty(exports, "RunningInterceptorServerError", {
21
21
  enumerable: true,
22
- get: function () { return chunkC4BVIHSP_js.RunningInterceptorServerError_default; }
22
+ get: function () { return chunk6VVX2QLH_js.RunningInterceptorServerError_default; }
23
23
  });
24
24
  Object.defineProperty(exports, "createInterceptorServer", {
25
25
  enumerable: true,
26
- get: function () { return chunkC4BVIHSP_js.createInterceptorServer; }
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-YPRAL5BM.mjs';
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
@@ -14,7 +14,7 @@
14
14
  "api",
15
15
  "static"
16
16
  ],
17
- "version": "1.1.7",
17
+ "version": "1.2.0-canary.1",
18
18
  "homepage": "https://zimic.dev/docs/interceptor",
19
19
  "repository": {
20
20
  "type": "git",
@@ -107,11 +107,12 @@
107
107
  "zod": "^4.1.12"
108
108
  },
109
109
  "devDependencies": {
110
- "@types/node": "^24.8.1",
110
+ "@types/node": "^24.9.1",
111
111
  "@types/ws": "^8.18.1",
112
- "@types/yargs": "^17.0.33",
113
- "@vitest/browser": "^3.2.4",
114
- "@vitest/coverage-istanbul": "^3.2.4",
112
+ "@types/yargs": "^17.0.34",
113
+ "@vitest/browser": "^4.0.3",
114
+ "@vitest/browser-playwright": "^4.0.3",
115
+ "@vitest/coverage-istanbul": "^4.0.3",
115
116
  "@zimic/eslint-config-node": "workspace:*",
116
117
  "@zimic/lint-staged-config": "workspace:*",
117
118
  "@zimic/tsconfig": "workspace:*",
@@ -122,7 +123,7 @@
122
123
  "playwright": "^1.56.1",
123
124
  "tsup": "^8.4.0",
124
125
  "typescript": "^5.9.3",
125
- "vitest": "^3.2.4"
126
+ "vitest": "^4.0.3"
126
127
  },
127
128
  "peerDependencies": {
128
129
  "@zimic/http": "^1.2.0 || workspace:*",
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 timesDeclarationPointer?: TimesDeclarationPointer;
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.timesDeclarationPointer = new TimesDeclarationPointer(minNumberOfRequests, maxNumberOfRequests);
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.timesDeclarationPointer,
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.timesDeclarationPointer = undefined;
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'> {
@@ -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
- const range = upperLimit - lowerLimit;
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
+ }