@zimic/interceptor 0.20.2 → 0.21.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/server.js CHANGED
@@ -1,29 +1,29 @@
1
1
  'use strict';
2
2
 
3
- var chunkIUNCQOCT_js = require('./chunk-IUNCQOCT.js');
3
+ var chunkVN36ZR3C_js = require('./chunk-VN36ZR3C.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 chunkIUNCQOCT_js.DEFAULT_ACCESS_CONTROL_HEADERS; }
10
+ get: function () { return chunkVN36ZR3C_js.DEFAULT_ACCESS_CONTROL_HEADERS; }
11
11
  });
12
12
  Object.defineProperty(exports, "DEFAULT_PREFLIGHT_STATUS_CODE", {
13
13
  enumerable: true,
14
- get: function () { return chunkIUNCQOCT_js.DEFAULT_PREFLIGHT_STATUS_CODE; }
14
+ get: function () { return chunkVN36ZR3C_js.DEFAULT_PREFLIGHT_STATUS_CODE; }
15
15
  });
16
16
  Object.defineProperty(exports, "NotRunningInterceptorServerError", {
17
17
  enumerable: true,
18
- get: function () { return chunkIUNCQOCT_js.NotRunningInterceptorServerError_default; }
18
+ get: function () { return chunkVN36ZR3C_js.NotRunningInterceptorServerError_default; }
19
19
  });
20
20
  Object.defineProperty(exports, "RunningInterceptorServerError", {
21
21
  enumerable: true,
22
- get: function () { return chunkIUNCQOCT_js.RunningInterceptorServerError_default; }
22
+ get: function () { return chunkVN36ZR3C_js.RunningInterceptorServerError_default; }
23
23
  });
24
24
  Object.defineProperty(exports, "createInterceptorServer", {
25
25
  enumerable: true,
26
- get: function () { return chunkIUNCQOCT_js.createInterceptorServer; }
26
+ get: function () { return chunkVN36ZR3C_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-TLZZ3DX6.mjs';
1
+ export { DEFAULT_ACCESS_CONTROL_HEADERS, DEFAULT_PREFLIGHT_STATUS_CODE, NotRunningInterceptorServerError_default as NotRunningInterceptorServerError, RunningInterceptorServerError_default as RunningInterceptorServerError, createInterceptorServer } from './chunk-FQH2EMUC.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.20.2",
17
+ "version": "0.21.0-canary.0",
18
18
  "repository": {
19
19
  "type": "git",
20
20
  "url": "https://github.com/zimicjs/zimic.git",
@@ -103,14 +103,14 @@
103
103
  "tsup": "^8.4.0",
104
104
  "typescript": "^5.8.3",
105
105
  "vitest": "^3.2.4",
106
- "@zimic/eslint-config-node": "0.0.0",
107
106
  "@zimic/lint-staged-config": "0.0.0",
107
+ "@zimic/eslint-config-node": "0.0.0",
108
108
  "@zimic/tsconfig": "0.0.0",
109
109
  "@zimic/utils": "0.0.0"
110
110
  },
111
111
  "peerDependencies": {
112
112
  "typescript": ">=5.0.0",
113
- "@zimic/http": "^0.6.0 || 0.6.1-canary.2"
113
+ "@zimic/http": "^0.6.0 || 0.6.1"
114
114
  },
115
115
  "peerDependenciesMeta": {
116
116
  "typescript": {
@@ -212,7 +212,8 @@ abstract class HttpInterceptorWorker {
212
212
  declaration.body instanceof FormData ||
213
213
  declaration.body instanceof URLSearchParams ||
214
214
  declaration.body instanceof Blob ||
215
- declaration.body instanceof ArrayBuffer
215
+ declaration.body instanceof ArrayBuffer ||
216
+ declaration.body instanceof ReadableStream
216
217
  ) {
217
218
  return new Response(declaration.body ?? null, { headers, status }) as HttpResponse;
218
219
  }
@@ -399,7 +400,7 @@ abstract class HttpInterceptorWorker {
399
400
  try {
400
401
  return await this.parseRawBodyAsJSON<Body>(resource);
401
402
  } catch {
402
- return await this.parseRawBodyAsText<Body>(resourceClone);
403
+ return await this.parseRawBodyAsBlob<Body>(resourceClone);
403
404
  }
404
405
  } catch (error) {
405
406
  console.error(error);
@@ -14,6 +14,8 @@ import jsonContains from '@zimic/utils/data/jsonContains';
14
14
  import jsonEquals from '@zimic/utils/data/jsonEquals';
15
15
  import { Default, Range } from '@zimic/utils/types';
16
16
 
17
+ import { convertArrayBufferToBlob, convertReadableStreamToBlob } from '@/utils/data';
18
+
17
19
  import HttpInterceptorClient from '../interceptor/HttpInterceptorClient';
18
20
  import DisabledRequestSavingError from './errors/DisabledRequestSavingError';
19
21
  import NoResponseDefinitionError from './errors/NoResponseDefinitionError';
@@ -331,7 +333,11 @@ class HttpRequestHandlerClient<
331
333
  };
332
334
  }
333
335
 
334
- if (restrictionBody instanceof Blob) {
336
+ if (
337
+ restrictionBody instanceof Blob ||
338
+ restrictionBody instanceof ArrayBuffer ||
339
+ restrictionBody instanceof ReadableStream
340
+ ) {
335
341
  if (!(body instanceof Blob)) {
336
342
  return {
337
343
  value: false,
@@ -339,7 +345,17 @@ class HttpRequestHandlerClient<
339
345
  };
340
346
  }
341
347
 
342
- const matchesRestriction = await blobEquals(body, restrictionBody);
348
+ let restrictionBodyAsBlob: Blob;
349
+
350
+ if (restrictionBody instanceof ArrayBuffer) {
351
+ restrictionBodyAsBlob = convertArrayBufferToBlob(restrictionBody, { type: body.type });
352
+ } else if (restrictionBody instanceof ReadableStream) {
353
+ restrictionBodyAsBlob = await convertReadableStreamToBlob(restrictionBody, { type: body.type });
354
+ } else {
355
+ restrictionBodyAsBlob = restrictionBody;
356
+ }
357
+
358
+ const matchesRestriction = await blobEquals(body, restrictionBodyAsBlob);
343
359
 
344
360
  return matchesRestriction
345
361
  ? { value: true }
@@ -14,7 +14,7 @@ import {
14
14
  HttpStatusCode,
15
15
  InferPathParams,
16
16
  } from '@zimic/http';
17
- import { Default, PartialByKey, PossiblePromise } from '@zimic/utils/types';
17
+ import { Default, PartialByKey, PossiblePromise, ReplaceBy } from '@zimic/utils/types';
18
18
 
19
19
  type HttpRequestHandlerResponseBody<
20
20
  ResponseSchema extends HttpResponseSchema,
@@ -65,14 +65,35 @@ export type HttpRequestHandlerResponseDeclarationFactory<
65
65
  /** @see {@link https://zimic.dev/docs/interceptor/api/http-request-handler#handlerrequests `handler.requests` API reference} */
66
66
  export interface HttpInterceptorRequest<Path extends string, MethodSchema extends HttpMethodSchema>
67
67
  extends Omit<HttpRequest, keyof Body | 'headers' | 'clone'> {
68
- /** The headers of the request. */
68
+ /**
69
+ * The headers of the request.
70
+ *
71
+ * @see {@link https://zimic.dev/docs/interceptor/guides/http/headers#using-request-headers Using request headers}
72
+ */
69
73
  headers: HttpHeaders<Default<HttpRequestHeadersSchema<MethodSchema>>>;
70
- /** The path parameters of the request. They are parsed from the path string when using dynamic paths. */
74
+
75
+ /**
76
+ * The path parameters of the request. They are parsed from the path string when using dynamic paths.
77
+ *
78
+ * @see {@link https://zimic.dev/docs/interceptor/guides/http/path-params#using-request-path-params Using request path parameters}
79
+ */
71
80
  pathParams: InferPathParams<Path>;
72
- /** The search parameters of the request. */
81
+
82
+ /**
83
+ * The search parameters of the request.
84
+ *
85
+ * @see {@link https://zimic.dev/docs/interceptor/guides/http/search-params#using-request-search-params Using request search parameters}
86
+ */
73
87
  searchParams: HttpSearchParams<Default<HttpRequestSearchParamsSchema<MethodSchema>>>;
74
- /** The body of the request. It is already parsed by default. */
75
- body: HttpRequestBodySchema<MethodSchema>;
88
+
89
+ /**
90
+ * The body of the request. It is already parsed by default as detailed in
91
+ * {@link https://zimic.dev/docs/interceptor/guides/http/bodies#default-body-parsing Default body parsing}.
92
+ *
93
+ * @see {@link https://zimic.dev/docs/interceptor/guides/http/bodies#using-request-bodies Using request bodies}
94
+ */
95
+ body: ReplaceBy<HttpRequestBodySchema<MethodSchema>, ArrayBuffer | ReadableStream, Blob>;
96
+
76
97
  /** The raw request object. */
77
98
  raw: HttpRequest<HttpRequestBodySchema<MethodSchema>, Default<HttpRequestHeadersSchema<MethodSchema>>>;
78
99
  }
@@ -80,12 +101,24 @@ export interface HttpInterceptorRequest<Path extends string, MethodSchema extend
80
101
  /** @see {@link https://zimic.dev/docs/interceptor/api/http-request-handler#handlerrequests `handler.requests` API reference} */
81
102
  export interface HttpInterceptorResponse<MethodSchema extends HttpMethodSchema, StatusCode extends HttpStatusCode>
82
103
  extends Omit<HttpResponse, keyof Body | 'headers' | 'clone'> {
83
- /** The headers of the response. */
104
+ /**
105
+ * The headers of the response.
106
+ *
107
+ * @see {@link https://zimic.dev/docs/interceptor/guides/http/headers#using-response-headers Using response headers}
108
+ */
84
109
  headers: HttpHeaders<Default<HttpResponseHeadersSchema<MethodSchema, StatusCode>>>;
110
+
85
111
  /** The status code of the response. */
86
112
  status: StatusCode;
87
- /** The body of the response. It is already parsed by default. */
88
- body: HttpResponseBodySchema<MethodSchema, StatusCode>;
113
+
114
+ /**
115
+ * The body of the response. It is already parsed by default as detailed in
116
+ * {@link https://zimic.dev/docs/interceptor/guides/http/bodies#default-body-parsing Default body parsing}.
117
+ *
118
+ * @see {@link https://zimic.dev/docs/interceptor/guides/http/bodies#using-response-bodies Using response bodies}
119
+ */
120
+ body: ReplaceBy<HttpResponseBodySchema<MethodSchema, StatusCode>, ArrayBuffer | ReadableStream, Blob>;
121
+
89
122
  /** The raw response object. */
90
123
  raw: HttpResponse<
91
124
  HttpResponseBodySchema<MethodSchema, StatusCode>,
@@ -49,7 +49,11 @@ type PartialBodyOrSchema<Body extends HttpBody> =
49
49
  ? HttpSearchParams<Partial<Schema>> | HttpSearchParams<Schema>
50
50
  : Body extends Blob
51
51
  ? Body
52
- : DeepPartial<Body>;
52
+ : Body extends ArrayBuffer
53
+ ? Body
54
+ : Body extends ReadableStream
55
+ ? Body
56
+ : DeepPartial<Body>;
53
57
 
54
58
  /** @see {@link https://zimic.dev/docs/interceptor/api/http-request-handler#handlerwithrestriction `handler.with()` API reference} */
55
59
  export type HttpRequestHandlerBodyStaticRestriction<
package/src/utils/data.ts CHANGED
@@ -1,5 +1,28 @@
1
1
  import { isClientSide } from './environment';
2
2
 
3
+ export async function convertReadableStreamToBlob(stream: ReadableStream, options?: BlobPropertyBag): Promise<Blob> {
4
+ const chunks: Uint8Array[] = [];
5
+ const reader = stream.getReader() as ReadableStreamDefaultReader<Uint8Array>;
6
+
7
+ while (true) {
8
+ const readResult = await reader.read();
9
+
10
+ if (readResult.value) {
11
+ chunks.push(readResult.value);
12
+ }
13
+
14
+ if (readResult.done) {
15
+ break;
16
+ }
17
+ }
18
+
19
+ return new Blob(chunks, options);
20
+ }
21
+
22
+ export function convertArrayBufferToBlob(buffer: ArrayBuffer, options?: BlobPropertyBag): Blob {
23
+ return new Blob([buffer], options);
24
+ }
25
+
3
26
  export function convertArrayBufferToBase64(buffer: ArrayBuffer) {
4
27
  if (isClientSide()) {
5
28
  const bufferBytes = new Uint8Array(buffer);