@zimic/interceptor 0.17.0-canary.3 → 0.17.0-canary.4

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 chunkPURXNE6R_js = require('./chunk-PURXNE6R.js');
3
+ var chunkMXHLBRPB_js = require('./chunk-MXHLBRPB.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 chunkPURXNE6R_js.DEFAULT_ACCESS_CONTROL_HEADERS; }
10
+ get: function () { return chunkMXHLBRPB_js.DEFAULT_ACCESS_CONTROL_HEADERS; }
11
11
  });
12
12
  Object.defineProperty(exports, "DEFAULT_PREFLIGHT_STATUS_CODE", {
13
13
  enumerable: true,
14
- get: function () { return chunkPURXNE6R_js.DEFAULT_PREFLIGHT_STATUS_CODE; }
14
+ get: function () { return chunkMXHLBRPB_js.DEFAULT_PREFLIGHT_STATUS_CODE; }
15
15
  });
16
16
  Object.defineProperty(exports, "NotRunningInterceptorServerError", {
17
17
  enumerable: true,
18
- get: function () { return chunkPURXNE6R_js.NotRunningInterceptorServerError_default; }
18
+ get: function () { return chunkMXHLBRPB_js.NotRunningInterceptorServerError_default; }
19
19
  });
20
20
  Object.defineProperty(exports, "RunningInterceptorServerError", {
21
21
  enumerable: true,
22
- get: function () { return chunkPURXNE6R_js.RunningInterceptorServerError_default; }
22
+ get: function () { return chunkMXHLBRPB_js.RunningInterceptorServerError_default; }
23
23
  });
24
24
  Object.defineProperty(exports, "createInterceptorServer", {
25
25
  enumerable: true,
26
- get: function () { return chunkPURXNE6R_js.createInterceptorServer; }
26
+ get: function () { return chunkMXHLBRPB_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-L75WKVZO.mjs';
1
+ export { DEFAULT_ACCESS_CONTROL_HEADERS, DEFAULT_PREFLIGHT_STATUS_CODE, NotRunningInterceptorServerError_default as NotRunningInterceptorServerError, RunningInterceptorServerError_default as RunningInterceptorServerError, createInterceptorServer } from './chunk-OGL76CKO.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.17.0-canary.3",
17
+ "version": "0.17.0-canary.4",
18
18
  "repository": {
19
19
  "type": "git",
20
20
  "url": "https://github.com/zimicjs/zimic.git",
@@ -101,8 +101,8 @@
101
101
  "vitest": "^3.1.1",
102
102
  "@zimic/eslint-config-node": "0.0.0",
103
103
  "@zimic/lint-staged-config": "0.0.0",
104
- "@zimic/tsconfig": "0.0.0",
105
- "@zimic/utils": "0.0.0"
104
+ "@zimic/utils": "0.0.0",
105
+ "@zimic/tsconfig": "0.0.0"
106
106
  },
107
107
  "peerDependencies": {
108
108
  "@zimic/http": "^0.3.0 || ^0.3.0-canary.0",
@@ -18,10 +18,10 @@ class LocalHttpInterceptor<Schema extends HttpSchema> implements PublicLocalHttp
18
18
  this.client = new HttpInterceptorClient<Schema, typeof LocalHttpRequestHandler>({
19
19
  store: this.store,
20
20
  baseURL,
21
- createWorker() {
21
+ createWorker: () => {
22
22
  return this.store.getOrCreateLocalWorker({});
23
23
  },
24
- deleteWorker() {
24
+ deleteWorker: () => {
25
25
  this.store.deleteLocalWorker();
26
26
  },
27
27
  Handler: LocalHttpRequestHandler,
@@ -1,6 +1,7 @@
1
1
  import { HttpSchema, HttpSchemaMethod, HttpSchemaPath } from '@zimic/http';
2
2
 
3
3
  import RemoteHttpRequestHandler from '../requestHandler/RemoteHttpRequestHandler';
4
+ import RunningHttpInterceptorError from './errors/RunningHttpInterceptorError';
4
5
  import HttpInterceptorClient from './HttpInterceptorClient';
5
6
  import HttpInterceptorStore from './HttpInterceptorStore';
6
7
  import { AsyncHttpInterceptorMethodHandler } from './types/handlers';
@@ -9,22 +10,25 @@ import { HttpInterceptorRequestSaving, RemoteHttpInterceptor as PublicRemoteHttp
9
10
 
10
11
  class RemoteHttpInterceptor<Schema extends HttpSchema> implements PublicRemoteHttpInterceptor<Schema> {
11
12
  private store = new HttpInterceptorStore();
12
-
13
13
  client: HttpInterceptorClient<Schema, typeof RemoteHttpRequestHandler>;
14
14
 
15
+ private _auth?: RemoteHttpInterceptorOptions['auth'];
16
+
15
17
  constructor(options: RemoteHttpInterceptorOptions) {
18
+ this._auth = options.auth;
19
+
16
20
  const baseURL = new URL(options.baseURL);
17
21
 
18
22
  this.client = new HttpInterceptorClient<Schema, typeof RemoteHttpRequestHandler>({
19
23
  store: this.store,
20
24
  baseURL,
21
- createWorker() {
25
+ createWorker: () => {
22
26
  return this.store.getOrCreateRemoteWorker({
23
27
  serverURL: new URL(baseURL.origin),
24
- auth: options.auth,
28
+ auth: this._auth,
25
29
  });
26
30
  },
27
- deleteWorker() {
31
+ deleteWorker: () => {
28
32
  this.store.deleteRemoteWorker(baseURL, { auth: options.auth });
29
33
  },
30
34
  Handler: RemoteHttpRequestHandler,
@@ -53,6 +57,33 @@ class RemoteHttpInterceptor<Schema extends HttpSchema> implements PublicRemoteHt
53
57
  this.client.requestSaving = requestSaving;
54
58
  }
55
59
 
60
+ get auth() {
61
+ return this._auth;
62
+ }
63
+
64
+ set auth(auth: RemoteHttpInterceptorOptions['auth'] | undefined) {
65
+ const cannotChangeAuthWhileRunningMessage =
66
+ 'Did you forget to call `await interceptor.stop()` before changing the authentication parameters?';
67
+
68
+ if (this.isRunning) {
69
+ throw new RunningHttpInterceptorError(cannotChangeAuthWhileRunningMessage);
70
+ }
71
+
72
+ if (!auth) {
73
+ this._auth = undefined;
74
+ return;
75
+ }
76
+
77
+ this._auth = new Proxy(auth, {
78
+ set: (target, property, value) => {
79
+ if (this.isRunning) {
80
+ throw new RunningHttpInterceptorError(cannotChangeAuthWhileRunningMessage);
81
+ }
82
+ return Reflect.set(target, property, value);
83
+ },
84
+ });
85
+ }
86
+
56
87
  get onUnhandledRequest() {
57
88
  return this.client.onUnhandledRequest;
58
89
  }
@@ -7,7 +7,7 @@
7
7
  */
8
8
  class RunningHttpInterceptorError extends Error {
9
9
  constructor(additionalMessage: string) {
10
- super(`The interceptor is running.${additionalMessage}`);
10
+ super(`The interceptor is running. ${additionalMessage}`);
11
11
  this.name = 'RunningHttpInterceptorError';
12
12
  }
13
13
  }
@@ -1,7 +1,7 @@
1
1
  import { HttpSchema } from '@zimic/http';
2
2
 
3
3
  import { SyncHttpInterceptorMethodHandler, AsyncHttpInterceptorMethodHandler } from './handlers';
4
- import { HttpInterceptorPlatform, UnhandledRequestStrategy } from './options';
4
+ import { HttpInterceptorPlatform, RemoteHttpInterceptorOptions, UnhandledRequestStrategy } from './options';
5
5
 
6
6
  /**
7
7
  * Configures if the intercepted requests are saved and how they are handled.
@@ -163,7 +163,7 @@ export interface LocalHttpInterceptor<Schema extends HttpSchema> extends HttpInt
163
163
  *
164
164
  * @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#unhandled-requests Unhandled requests}
165
165
  */
166
- onUnhandledRequest: UnhandledRequestStrategy.Local | undefined;
166
+ onUnhandledRequest?: UnhandledRequestStrategy.Local;
167
167
 
168
168
  /**
169
169
  * Creates a GET
@@ -367,6 +367,14 @@ export interface RemoteHttpInterceptor<Schema extends HttpSchema> extends HttpIn
367
367
  /** @readonly */
368
368
  get type(): 'remote';
369
369
 
370
+ /**
371
+ * Options to authenticate the interceptor when connecting to an interceptor server. This is required if the
372
+ * interceptor server was started with the `--tokens-dir` option.
373
+ *
374
+ * @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐interceptor‐server#authentication Interceptor server authentication}
375
+ */
376
+ auth?: RemoteHttpInterceptorOptions['auth'];
377
+
370
378
  /**
371
379
  * The strategy to use for unhandled requests. If a request starts with the base URL of the interceptor, but no
372
380
  * matching handler exists, this strategy will be used. If a function is provided, it will be called with the
@@ -374,7 +382,7 @@ export interface RemoteHttpInterceptor<Schema extends HttpSchema> extends HttpIn
374
382
  *
375
383
  * @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#unhandled-requests Unhandled requests}
376
384
  */
377
- onUnhandledRequest: UnhandledRequestStrategy.Remote | undefined;
385
+ onUnhandledRequest?: UnhandledRequestStrategy.Remote;
378
386
 
379
387
  /**
380
388
  * Creates a GET
@@ -174,11 +174,10 @@ export async function readInterceptorTokenFromFile(
174
174
  return validation.data.token;
175
175
  }
176
176
 
177
- export async function createInterceptorToken(options: {
178
- name?: string;
179
- tokensDirectory: string;
180
- }): Promise<InterceptorToken> {
181
- const { name, tokensDirectory } = options;
177
+ export async function createInterceptorToken(
178
+ options: { name?: string; tokensDirectory?: string } = {},
179
+ ): Promise<InterceptorToken> {
180
+ const { name, tokensDirectory = DEFAULT_INTERCEPTOR_TOKENS_DIRECTORY } = options;
182
181
 
183
182
  const tokensDirectoryExists = await pathExists(tokensDirectory);
184
183
 
@@ -226,14 +225,16 @@ export async function createInterceptorToken(options: {
226
225
  return token;
227
226
  }
228
227
 
229
- export async function listInterceptorTokens(options: { tokensDirectory: string }) {
230
- const tokensDirectoryExists = await pathExists(options.tokensDirectory);
228
+ export async function listInterceptorTokens(options: { tokensDirectory?: string } = {}) {
229
+ const { tokensDirectory = DEFAULT_INTERCEPTOR_TOKENS_DIRECTORY } = options;
230
+
231
+ const tokensDirectoryExists = await pathExists(tokensDirectory);
231
232
 
232
233
  if (!tokensDirectoryExists) {
233
234
  return [];
234
235
  }
235
236
 
236
- const files = await fs.promises.readdir(options.tokensDirectory);
237
+ const files = await fs.promises.readdir(tokensDirectory);
237
238
 
238
239
  const tokenReadPromises = files.map(async (file) => {
239
240
  if (!isValidInterceptorTokenId(file)) {
@@ -241,7 +242,7 @@ export async function listInterceptorTokens(options: { tokensDirectory: string }
241
242
  }
242
243
 
243
244
  const tokenId = file;
244
- const token = await readInterceptorTokenFromFile(tokenId, options);
245
+ const token = await readInterceptorTokenFromFile(tokenId, { tokensDirectory });
245
246
  return token;
246
247
  });
247
248
 
@@ -288,14 +289,16 @@ export async function validateInterceptorToken(tokenValue: string, options: { to
288
289
  }
289
290
  }
290
291
 
291
- export async function removeInterceptorToken(tokenId: string, options: { tokensDirectory: string }) {
292
+ export async function removeInterceptorToken(tokenId: string, options: { tokensDirectory?: string } = {}) {
293
+ const { tokensDirectory = DEFAULT_INTERCEPTOR_TOKENS_DIRECTORY } = options;
294
+
292
295
  /* istanbul ignore if -- @preserve
293
296
  * At this point, we should have a valid tokenId. This is just a sanity check. */
294
297
  if (!isValidInterceptorTokenId(tokenId)) {
295
298
  throw new InvalidInterceptorTokenError(tokenId);
296
299
  }
297
300
 
298
- const tokenFilePath = path.join(options.tokensDirectory, tokenId);
301
+ const tokenFilePath = path.join(tokensDirectory, tokenId);
299
302
 
300
303
  await fs.promises.rm(tokenFilePath, { force: true });
301
304
  }