@zimic/interceptor 0.15.0-canary.1 → 0.15.0-canary.3

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.
Files changed (42) hide show
  1. package/dist/{chunk-3O3YPVEG.mjs → chunk-6TSSHQW5.mjs} +50 -25
  2. package/dist/chunk-6TSSHQW5.mjs.map +1 -0
  3. package/dist/{chunk-CWBDZYUG.js → chunk-R2ROSKU4.js} +51 -25
  4. package/dist/chunk-R2ROSKU4.js.map +1 -0
  5. package/dist/cli.js +7 -7
  6. package/dist/cli.js.map +1 -1
  7. package/dist/cli.mjs +3 -3
  8. package/dist/cli.mjs.map +1 -1
  9. package/dist/http.d.ts +84 -40
  10. package/dist/http.js +144 -83
  11. package/dist/http.js.map +1 -1
  12. package/dist/http.mjs +143 -83
  13. package/dist/http.mjs.map +1 -1
  14. package/dist/server.d.ts +17 -11
  15. package/dist/server.js +10 -6
  16. package/dist/server.mjs +1 -1
  17. package/package.json +12 -12
  18. package/src/cli/server/start.ts +1 -1
  19. package/src/http/index.ts +2 -14
  20. package/src/http/interceptor/HttpInterceptorClient.ts +51 -19
  21. package/src/http/interceptor/LocalHttpInterceptor.ts +27 -11
  22. package/src/http/interceptor/RemoteHttpInterceptor.ts +25 -10
  23. package/src/http/interceptor/errors/{NotStartedHttpInterceptorError.ts → NotRunningHttpInterceptorError.ts} +3 -3
  24. package/src/http/interceptor/errors/RunningHttpInterceptorError.ts +15 -0
  25. package/src/http/interceptor/types/options.ts +4 -6
  26. package/src/http/interceptor/types/public.ts +58 -20
  27. package/src/http/interceptorWorker/LocalHttpInterceptorWorker.ts +7 -6
  28. package/src/http/interceptorWorker/RemoteHttpInterceptorWorker.ts +8 -7
  29. package/src/http/requestHandler/HttpRequestHandlerClient.ts +4 -6
  30. package/src/http/requestHandler/types/public.ts +12 -6
  31. package/src/server/InterceptorServer.ts +28 -11
  32. package/src/server/constants.ts +1 -0
  33. package/src/server/errors/NotRunningInterceptorServerError.ts +10 -0
  34. package/src/server/errors/RunningInterceptorServerError.ts +13 -0
  35. package/src/server/index.ts +2 -1
  36. package/src/server/types/public.ts +6 -10
  37. package/src/webSocket/WebSocketHandler.ts +2 -2
  38. package/src/webSocket/errors/NotRunningWebSocketHandlerError.ts +8 -0
  39. package/dist/chunk-3O3YPVEG.mjs.map +0 -1
  40. package/dist/chunk-CWBDZYUG.js.map +0 -1
  41. package/src/server/errors/NotStartedInterceptorServerError.ts +0 -10
  42. package/src/webSocket/errors/NotStartedWebSocketHandlerError.ts +0 -8
package/dist/server.d.ts CHANGED
@@ -29,14 +29,16 @@ interface InterceptorServerOptions {
29
29
  */
30
30
  interface InterceptorServer {
31
31
  /**
32
- * The hostname of the server.
32
+ * The hostname of the server. It can be reassigned to a new value if the server is not running.
33
33
  *
34
+ * @throws {RunningInterceptorServerError} When trying to reassign a new hostname with the server still running.
34
35
  * @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#zimic-server `zimic-interceptor server` API reference}
35
36
  */
36
37
  hostname: string;
37
38
  /**
38
- * The port of the server.
39
+ * The port of the server. It can be reassigned to a new value if the server is not running.
39
40
  *
41
+ * @throws {RunningInterceptorServerError} When trying to reassign a new port with the server still running.
40
42
  * @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#zimic-server `zimic-interceptor server` API reference}
41
43
  */
42
44
  port: number | undefined;
@@ -47,18 +49,13 @@ interface InterceptorServer {
47
49
  * @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#zimic-server `zimic-interceptor server` API reference}
48
50
  */
49
51
  logUnhandledRequests: boolean;
50
- /**
51
- * The HTTP URL of the server.
52
- *
53
- * @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#zimic-server `zimic-interceptor server` API reference}
54
- */
55
- httpURL: string | undefined;
56
52
  /**
57
53
  * Whether the server is running.
58
54
  *
55
+ * @readonly
59
56
  * @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#zimic-server `zimic-interceptor server` API reference}
60
57
  */
61
- isRunning: boolean;
58
+ get isRunning(): boolean;
62
59
  /**
63
60
  * Starts the server.
64
61
  *
@@ -104,8 +101,17 @@ declare class InterceptorServerNamespace {
104
101
  create: typeof createInterceptorServer;
105
102
  }
106
103
 
104
+ /**
105
+ * An error thrown when the interceptor server is running and some operation requires it to be stopped first.
106
+ *
107
+ * @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐server `@zimic/interceptor/server` - API reference}
108
+ */
109
+ declare class RunningInterceptorServerError extends Error {
110
+ constructor(additionalMessage: string);
111
+ }
112
+
107
113
  /** An error thrown when the interceptor server is not running. */
108
- declare class NotStartedInterceptorServerError extends Error {
114
+ declare class NotRunningInterceptorServerError extends Error {
109
115
  constructor();
110
116
  }
111
117
 
@@ -128,4 +134,4 @@ declare const DEFAULT_PREFLIGHT_STATUS_CODE = 204;
128
134
  */
129
135
  declare const interceptorServer: Readonly<InterceptorServerNamespace>;
130
136
 
131
- export { DEFAULT_ACCESS_CONTROL_HEADERS, DEFAULT_PREFLIGHT_STATUS_CODE, type InterceptorServer, InterceptorServerNamespace, type InterceptorServerOptions, NotStartedInterceptorServerError, interceptorServer };
137
+ export { DEFAULT_ACCESS_CONTROL_HEADERS, DEFAULT_PREFLIGHT_STATUS_CODE, type InterceptorServer, InterceptorServerNamespace, type InterceptorServerOptions, NotRunningInterceptorServerError, RunningInterceptorServerError, interceptorServer };
package/dist/server.js CHANGED
@@ -1,25 +1,29 @@
1
1
  'use strict';
2
2
 
3
- var chunkCWBDZYUG_js = require('./chunk-CWBDZYUG.js');
3
+ var chunkR2ROSKU4_js = require('./chunk-R2ROSKU4.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 chunkCWBDZYUG_js.DEFAULT_ACCESS_CONTROL_HEADERS; }
10
+ get: function () { return chunkR2ROSKU4_js.DEFAULT_ACCESS_CONTROL_HEADERS; }
11
11
  });
12
12
  Object.defineProperty(exports, "DEFAULT_PREFLIGHT_STATUS_CODE", {
13
13
  enumerable: true,
14
- get: function () { return chunkCWBDZYUG_js.DEFAULT_PREFLIGHT_STATUS_CODE; }
14
+ get: function () { return chunkR2ROSKU4_js.DEFAULT_PREFLIGHT_STATUS_CODE; }
15
15
  });
16
- Object.defineProperty(exports, "NotStartedInterceptorServerError", {
16
+ Object.defineProperty(exports, "NotRunningInterceptorServerError", {
17
17
  enumerable: true,
18
- get: function () { return chunkCWBDZYUG_js.NotStartedInterceptorServerError_default; }
18
+ get: function () { return chunkR2ROSKU4_js.NotRunningInterceptorServerError_default; }
19
+ });
20
+ Object.defineProperty(exports, "RunningInterceptorServerError", {
21
+ enumerable: true,
22
+ get: function () { return chunkR2ROSKU4_js.RunningInterceptorServerError_default; }
19
23
  });
20
24
  Object.defineProperty(exports, "interceptorServer", {
21
25
  enumerable: true,
22
- get: function () { return chunkCWBDZYUG_js.interceptorServer; }
26
+ get: function () { return chunkR2ROSKU4_js.interceptorServer; }
23
27
  });
24
28
  //# sourceMappingURL=server.js.map
25
29
  //# 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-3O3YPVEG.mjs';
1
+ export { DEFAULT_ACCESS_CONTROL_HEADERS, DEFAULT_PREFLIGHT_STATUS_CODE, NotRunningInterceptorServerError_default as NotRunningInterceptorServerError, RunningInterceptorServerError_default as RunningInterceptorServerError, interceptorServer } from './chunk-6TSSHQW5.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.1",
17
+ "version": "0.15.0-canary.3",
18
18
  "repository": {
19
19
  "type": "git",
20
20
  "url": "https://github.com/zimicjs/zimic.git",
@@ -74,7 +74,7 @@
74
74
  "./package.json": "./package.json"
75
75
  },
76
76
  "dependencies": {
77
- "@whatwg-node/server": "0.9.70",
77
+ "@whatwg-node/server": "0.10.1",
78
78
  "chalk": "4.1.2",
79
79
  "execa": "9.5.2",
80
80
  "isomorphic-ws": "5.0.0",
@@ -86,21 +86,21 @@
86
86
  "bufferutil": "4.0.9"
87
87
  },
88
88
  "devDependencies": {
89
- "@types/node": "^22.13.8",
90
- "@types/ws": "^8.5.14",
89
+ "@types/node": "^22.13.10",
90
+ "@types/ws": "^8.18.0",
91
91
  "@types/yargs": "^17.0.33",
92
- "@vitest/browser": "^3.0.7",
93
- "@vitest/coverage-istanbul": "^3.0.7",
92
+ "@vitest/browser": "^3.0.8",
93
+ "@vitest/coverage-istanbul": "^3.0.8",
94
94
  "dotenv-cli": "^8.0.0",
95
- "eslint": "^9.21.0",
96
- "playwright": "^1.50.1",
95
+ "eslint": "^9.22.0",
96
+ "playwright": "^1.51.0",
97
97
  "tsup": "^8.4.0",
98
98
  "typescript": "^5.8.2",
99
- "vitest": "^3.0.7",
100
- "@zimic/eslint-config-node": "0.0.0",
101
- "@zimic/lint-staged-config": "0.0.0",
99
+ "vitest": "^3.0.8",
100
+ "@zimic/utils": "0.0.0",
102
101
  "@zimic/tsconfig": "0.0.0",
103
- "@zimic/utils": "0.0.0"
102
+ "@zimic/lint-staged-config": "0.0.0",
103
+ "@zimic/eslint-config-node": "0.0.0"
104
104
  },
105
105
  "peerDependencies": {
106
106
  "@zimic/http": "^0.1.0 || ^0.1.0-canary.0",
@@ -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.hostname}:${server.port}`);
63
63
 
64
64
  if (onReady) {
65
65
  try {
package/src/http/index.ts CHANGED
@@ -1,11 +1,9 @@
1
- import { HttpMethodSchema, HttpStatusCode } from '@zimic/http';
2
-
3
1
  import HttpInterceptorNamespace from './namespace/HttpInterceptorNamespace';
4
- import { InterceptedHttpInterceptorRequest } from './requestHandler/types/requests';
5
2
 
6
3
  export { default as InvalidJSONError } from './interceptorWorker/errors/InvalidJSONError';
7
4
  export { default as InvalidFormDataError } from './interceptorWorker/errors/InvalidFormDataError';
8
- export { default as NotStartedHttpInterceptorError } from './interceptor/errors/NotStartedHttpInterceptorError';
5
+ export { default as RunningHttpInterceptorError } from './interceptor/errors/RunningHttpInterceptorError';
6
+ export { default as NotRunningHttpInterceptorError } from './interceptor/errors/NotRunningHttpInterceptorError';
9
7
  export { default as UnknownHttpInterceptorPlatformError } from './interceptor/errors/UnknownHttpInterceptorPlatformError';
10
8
  export { default as UnknownHttpInterceptorTypeError } from './interceptor/errors/UnknownHttpInterceptorTypeError';
11
9
  export { default as UnregisteredBrowserServiceWorkerError } from './interceptorWorker/errors/UnregisteredBrowserServiceWorkerError';
@@ -20,16 +18,6 @@ export type {
20
18
  InterceptedHttpInterceptorRequest,
21
19
  } from './requestHandler/types/requests';
22
20
 
23
- /**
24
- * @deprecated This type was renamed to {@link InterceptedHttpInterceptorRequest `InterceptedHttpInterceptorRequest`}.
25
- * Please use it instead.
26
- */
27
- export type TrackedHttpInterceptorRequest<
28
- Path extends string,
29
- MethodSchema extends HttpMethodSchema,
30
- StatusCode extends HttpStatusCode = never,
31
- > = InterceptedHttpInterceptorRequest<Path, MethodSchema, StatusCode>;
32
-
33
21
  export type {
34
22
  LocalHttpRequestHandler,
35
23
  RemoteHttpRequestHandler,
@@ -9,7 +9,9 @@ import {
9
9
  } from '@zimic/http';
10
10
  import { Default, PossiblePromise } from '@zimic/utils/types';
11
11
  import createRegExpFromURL from '@zimic/utils/url/createRegExpFromURL';
12
+ import excludeURLParams from '@zimic/utils/url/excludeURLParams';
12
13
  import joinURL from '@zimic/utils/url/joinURL';
14
+ import validateURLProtocol from '@zimic/utils/url/validateURLProtocol';
13
15
 
14
16
  import HttpInterceptorWorker from '../interceptorWorker/HttpInterceptorWorker';
15
17
  import LocalHttpInterceptorWorker from '../interceptorWorker/LocalHttpInterceptorWorker';
@@ -18,7 +20,8 @@ import LocalHttpRequestHandler from '../requestHandler/LocalHttpRequestHandler';
18
20
  import RemoteHttpRequestHandler from '../requestHandler/RemoteHttpRequestHandler';
19
21
  import { HttpRequestHandler, InternalHttpRequestHandler } from '../requestHandler/types/public';
20
22
  import { HttpInterceptorRequest } from '../requestHandler/types/requests';
21
- import NotStartedHttpInterceptorError from './errors/NotStartedHttpInterceptorError';
23
+ import NotRunningHttpInterceptorError from './errors/NotRunningHttpInterceptorError';
24
+ import RunningHttpInterceptorError from './errors/RunningHttpInterceptorError';
22
25
  import HttpInterceptorStore from './HttpInterceptorStore';
23
26
  import { UnhandledRequestStrategy } from './types/options';
24
27
  import { HttpInterceptorRequestContext } from './types/requests';
@@ -32,10 +35,14 @@ class HttpInterceptorClient<
32
35
  private worker: HttpInterceptorWorker;
33
36
  private store: HttpInterceptorStore;
34
37
 
35
- private _baseURL: URL;
38
+ private _baseURL!: URL;
39
+ private _saveRequests = false;
40
+
41
+ onUnhandledRequest?: HandlerConstructor extends typeof LocalHttpRequestHandler
42
+ ? UnhandledRequestStrategy.Local
43
+ : UnhandledRequestStrategy.Remote;
44
+
36
45
  isRunning = false;
37
- onUnhandledRequest?: UnhandledRequestStrategy;
38
- shouldSaveRequests = false;
39
46
 
40
47
  private Handler: HandlerConstructor;
41
48
 
@@ -55,26 +62,51 @@ class HttpInterceptorClient<
55
62
  worker: HttpInterceptorWorker;
56
63
  store: HttpInterceptorStore;
57
64
  baseURL: URL;
58
- Handler: HandlerConstructor;
59
- onUnhandledRequest?: UnhandledRequestStrategy;
60
65
  saveRequests?: boolean;
66
+ onUnhandledRequest?: UnhandledRequestStrategy;
67
+ Handler: HandlerConstructor;
61
68
  }) {
62
69
  this.worker = options.worker;
63
70
  this.store = options.store;
64
- this._baseURL = options.baseURL;
71
+
72
+ this.baseURL = options.baseURL;
73
+ this._saveRequests = options.saveRequests ?? false;
74
+ this.onUnhandledRequest = options.onUnhandledRequest satisfies
75
+ | UnhandledRequestStrategy
76
+ | undefined as this['onUnhandledRequest'];
77
+
65
78
  this.Handler = options.Handler;
66
- this.onUnhandledRequest = options.onUnhandledRequest;
67
- this.shouldSaveRequests = options.saveRequests ?? false;
68
79
  }
69
80
 
70
- get baseURLAsString() {
71
- const baseURL = this._baseURL;
81
+ get baseURL() {
82
+ return this._baseURL;
83
+ }
72
84
 
73
- if (baseURL.href === `${baseURL.origin}/`) {
74
- return baseURL.origin;
85
+ set baseURL(newBaseURL: URL) {
86
+ if (this.isRunning) {
87
+ throw new RunningHttpInterceptorError(
88
+ 'Did you forget to call `await interceptor.stop()` before changing the base URL?',
89
+ );
75
90
  }
76
91
 
77
- return baseURL.href;
92
+ validateURLProtocol(newBaseURL, SUPPORTED_BASE_URL_PROTOCOLS);
93
+ excludeURLParams(newBaseURL);
94
+ this._baseURL = newBaseURL;
95
+ }
96
+
97
+ get baseURLAsString() {
98
+ if (this.baseURL.href === `${this.baseURL.origin}/`) {
99
+ return this.baseURL.origin;
100
+ }
101
+ return this.baseURL.href;
102
+ }
103
+
104
+ get saveRequests() {
105
+ return this._saveRequests;
106
+ }
107
+
108
+ set saveRequests(saveRequests: boolean) {
109
+ this._saveRequests = saveRequests;
78
110
  }
79
111
 
80
112
  get platform() {
@@ -102,7 +134,7 @@ class HttpInterceptorClient<
102
134
  if (this.worker instanceof LocalHttpInterceptorWorker) {
103
135
  this.store.markLocalInterceptorAsRunning(this, isRunning);
104
136
  } else {
105
- this.store.markRemoteInterceptorAsRunning(this, isRunning, this._baseURL);
137
+ this.store.markRemoteInterceptorAsRunning(this, isRunning, this.baseURL);
106
138
  }
107
139
  this.isRunning = isRunning;
108
140
  }
@@ -111,7 +143,7 @@ class HttpInterceptorClient<
111
143
  if (this.worker instanceof LocalHttpInterceptorWorker) {
112
144
  return this.store.numberOfRunningLocalInterceptors;
113
145
  } else {
114
- return this.store.numberOfRunningRemoteInterceptors(this._baseURL);
146
+ return this.store.numberOfRunningRemoteInterceptors(this.baseURL);
115
147
  }
116
148
  }
117
149
 
@@ -148,7 +180,7 @@ class HttpInterceptorClient<
148
180
  Path extends HttpSchemaPath<Schema, Method>,
149
181
  >(method: Method, path: Path): HttpRequestHandler<Schema, Method, Path> {
150
182
  if (!this.isRunning) {
151
- throw new NotStartedHttpInterceptorError();
183
+ throw new NotRunningHttpInterceptorError();
152
184
  }
153
185
 
154
186
  const handler = new this.Handler<Schema, Method, Path>(this as SharedHttpInterceptorClient<Schema>, method, path);
@@ -214,7 +246,7 @@ class HttpInterceptorClient<
214
246
  const responseDeclaration = await matchedHandler.applyResponseDeclaration(parsedRequest);
215
247
  const response = HttpInterceptorWorker.createResponseFromDeclaration(request, responseDeclaration);
216
248
 
217
- if (this.shouldSaveRequests) {
249
+ if (this._saveRequests) {
218
250
  const responseClone = response.clone();
219
251
 
220
252
  const parsedResponse = await HttpInterceptorWorker.parseRawResponse<
@@ -265,7 +297,7 @@ class HttpInterceptorClient<
265
297
 
266
298
  clear(options: { onCommitSuccess?: () => void; onCommitError?: () => void } = {}) {
267
299
  if (!this.isRunning) {
268
- throw new NotStartedHttpInterceptorError();
300
+ throw new NotRunningHttpInterceptorError();
269
301
  }
270
302
 
271
303
  const clearResults: PossiblePromise<AnyHttpRequestHandlerClient | void>[] = [];
@@ -1,31 +1,23 @@
1
1
  import { HttpSchema, HttpSchemaMethod, HttpSchemaPath } from '@zimic/http';
2
- import excludeURLParams from '@zimic/utils/url/excludeURLParams';
3
- import validateURLProtocol from '@zimic/utils/url/validateURLProtocol';
4
2
 
5
3
  import LocalHttpRequestHandler from '../requestHandler/LocalHttpRequestHandler';
6
- import HttpInterceptorClient, { SUPPORTED_BASE_URL_PROTOCOLS } from './HttpInterceptorClient';
4
+ import HttpInterceptorClient from './HttpInterceptorClient';
7
5
  import HttpInterceptorStore from './HttpInterceptorStore';
8
6
  import { SyncHttpInterceptorMethodHandler } from './types/handlers';
9
7
  import { LocalHttpInterceptorOptions } from './types/options';
10
8
  import { LocalHttpInterceptor as PublicLocalHttpInterceptor } from './types/public';
11
9
 
12
10
  class LocalHttpInterceptor<Schema extends HttpSchema> implements PublicLocalHttpInterceptor<Schema> {
13
- readonly type: 'local';
14
-
15
11
  private store = new HttpInterceptorStore();
16
12
 
17
- client: HttpInterceptorClient<Schema>;
13
+ client: HttpInterceptorClient<Schema, typeof LocalHttpRequestHandler>;
18
14
 
19
15
  constructor(options: LocalHttpInterceptorOptions) {
20
- this.type = options.type;
21
-
22
16
  const baseURL = new URL(options.baseURL);
23
- validateURLProtocol(baseURL, SUPPORTED_BASE_URL_PROTOCOLS);
24
- excludeURLParams(baseURL);
25
17
 
26
18
  const worker = this.store.getOrCreateLocalWorker({});
27
19
 
28
- this.client = new HttpInterceptorClient<Schema>({
20
+ this.client = new HttpInterceptorClient<Schema, typeof LocalHttpRequestHandler>({
29
21
  worker,
30
22
  store: this.store,
31
23
  baseURL,
@@ -35,10 +27,34 @@ class LocalHttpInterceptor<Schema extends HttpSchema> implements PublicLocalHttp
35
27
  });
36
28
  }
37
29
 
30
+ get type() {
31
+ return 'local' as const;
32
+ }
33
+
38
34
  get baseURL() {
39
35
  return this.client.baseURLAsString;
40
36
  }
41
37
 
38
+ set baseURL(baseURL: LocalHttpInterceptorOptions['baseURL']) {
39
+ this.client.baseURL = new URL(baseURL);
40
+ }
41
+
42
+ get saveRequests() {
43
+ return this.client.saveRequests;
44
+ }
45
+
46
+ set saveRequests(saveRequests: NonNullable<LocalHttpInterceptorOptions['saveRequests']>) {
47
+ this.client.saveRequests = saveRequests;
48
+ }
49
+
50
+ get onUnhandledRequest() {
51
+ return this.client.onUnhandledRequest;
52
+ }
53
+
54
+ set onUnhandledRequest(onUnhandledRequest: LocalHttpInterceptorOptions['onUnhandledRequest']) {
55
+ this.client.onUnhandledRequest = onUnhandledRequest;
56
+ }
57
+
42
58
  get platform() {
43
59
  return this.client.platform;
44
60
  }
@@ -1,30 +1,21 @@
1
1
  import { HttpSchema, HttpSchemaMethod, HttpSchemaPath } from '@zimic/http';
2
- import excludeURLParams from '@zimic/utils/url/excludeURLParams';
3
- import validateURLProtocol from '@zimic/utils/url/validateURLProtocol';
4
2
 
5
3
  import RemoteHttpRequestHandler from '../requestHandler/RemoteHttpRequestHandler';
6
- import HttpInterceptorClient, { SUPPORTED_BASE_URL_PROTOCOLS } from './HttpInterceptorClient';
4
+ import HttpInterceptorClient from './HttpInterceptorClient';
7
5
  import HttpInterceptorStore from './HttpInterceptorStore';
8
6
  import { AsyncHttpInterceptorMethodHandler } from './types/handlers';
9
7
  import { RemoteHttpInterceptorOptions } from './types/options';
10
8
  import { RemoteHttpInterceptor as PublicRemoteHttpInterceptor } from './types/public';
11
9
 
12
10
  class RemoteHttpInterceptor<Schema extends HttpSchema> implements PublicRemoteHttpInterceptor<Schema> {
13
- readonly type: 'remote';
14
-
15
11
  private store = new HttpInterceptorStore();
16
12
 
17
13
  client: HttpInterceptorClient<Schema, typeof RemoteHttpRequestHandler>;
18
14
 
19
15
  constructor(options: RemoteHttpInterceptorOptions) {
20
- this.type = options.type;
21
-
22
16
  const baseURL = new URL(options.baseURL);
23
- validateURLProtocol(baseURL, SUPPORTED_BASE_URL_PROTOCOLS);
24
- excludeURLParams(baseURL);
25
17
 
26
18
  const serverURL = new URL(baseURL.origin);
27
-
28
19
  const worker = this.store.getOrCreateRemoteWorker({ serverURL });
29
20
 
30
21
  this.client = new HttpInterceptorClient<Schema, typeof RemoteHttpRequestHandler>({
@@ -37,10 +28,34 @@ class RemoteHttpInterceptor<Schema extends HttpSchema> implements PublicRemoteHt
37
28
  });
38
29
  }
39
30
 
31
+ get type() {
32
+ return 'remote' as const;
33
+ }
34
+
40
35
  get baseURL() {
41
36
  return this.client.baseURLAsString;
42
37
  }
43
38
 
39
+ set baseURL(baseURL: string) {
40
+ this.client.baseURL = new URL(baseURL);
41
+ }
42
+
43
+ get saveRequests() {
44
+ return this.client.saveRequests;
45
+ }
46
+
47
+ set saveRequests(saveRequests: NonNullable<RemoteHttpInterceptorOptions['saveRequests']>) {
48
+ this.client.saveRequests = saveRequests;
49
+ }
50
+
51
+ get onUnhandledRequest() {
52
+ return this.client.onUnhandledRequest;
53
+ }
54
+
55
+ set onUnhandledRequest(onUnhandledRequest: RemoteHttpInterceptorOptions['onUnhandledRequest']) {
56
+ this.client.onUnhandledRequest = onUnhandledRequest;
57
+ }
58
+
44
59
  get platform() {
45
60
  return this.client.platform;
46
61
  }
@@ -5,11 +5,11 @@
5
5
  * @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorstop `interceptor.stop()` API reference}
6
6
  * @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorisrunning `interceptor.isRunning` API reference}
7
7
  */
8
- class NotStartedHttpInterceptorError extends Error {
8
+ class NotRunningHttpInterceptorError extends Error {
9
9
  constructor() {
10
10
  super('Interceptor is not running. Did you forget to call `await interceptor.start()`?');
11
- this.name = 'NotStartedHttpInterceptorError';
11
+ this.name = 'NotRunningHttpInterceptorError';
12
12
  }
13
13
  }
14
14
 
15
- export default NotStartedHttpInterceptorError;
15
+ export default NotRunningHttpInterceptorError;
@@ -0,0 +1,15 @@
1
+ /**
2
+ * An error thrown when the interceptor is running and some operation requires it to be stopped first.
3
+ *
4
+ * @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorstart `interceptor.start()` API reference}
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` API reference}
7
+ */
8
+ class RunningHttpInterceptorError extends Error {
9
+ constructor(additionalMessage: string) {
10
+ super(`The interceptor is running.${additionalMessage}`);
11
+ this.name = 'RunningHttpInterceptorError';
12
+ }
13
+ }
14
+
15
+ export default RunningHttpInterceptorError;
@@ -116,19 +116,17 @@ export interface SharedHttpInterceptorOptions {
116
116
  * {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#zimic-server interceptor server}. It may include
117
117
  * additional paths to differentiate between conflicting mocks.
118
118
  */
119
- baseURL: string | URL;
119
+ baseURL: string;
120
120
 
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
124
  * {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlerrequests `handler.requests`}.
125
125
  *
126
- * **Important**: Saving the intercepted requests will lead to a memory leak if not accompanied by clearing of the
127
- * interceptor or disposal of the handlers (i.e. garbage collection). If you plan on accessing those requests, such as
128
- * to assert them in your tests, set this option to `true` and make sure to regularly clear the interceptor. A common
129
- * practice is to call
126
+ * **Important**: If `saveRequests` is true, make sure to regularly clear the interceptor to avoid that the requests
127
+ * accumulate in memory. A common practice is to call
130
128
  * {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorclear `interceptor.clear()`}
131
- * after each test. This prevents leaking memory from the accumulated requests.
129
+ * after each test.
132
130
  *
133
131
  * @default false
134
132
  * @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#saving-requests Saving intercepted requests}