@mswjs/interceptors 0.17.0 → 0.17.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.
package/README.md CHANGED
@@ -9,7 +9,7 @@ Low-level HTTP/HTTPS/XHR/fetch request interception library.
9
9
  - `http.get`/`http.request`
10
10
  - `https.get`/`https.request`
11
11
  - `XMLHttpRequest`
12
- - `fetch`
12
+ - `window.fetch`
13
13
  - Any third-party libraries that use the modules above (i.e. `axios`, `request`, `node-fetch`, `supertest`, etc.)
14
14
 
15
15
  ## Motivation
@@ -145,7 +145,10 @@ import { XMLHttpRequestInterceptor } from '@mswjs/interceptors/lib/interceptors/
145
145
 
146
146
  const interceptor = BatchInterceptor({
147
147
  name: 'my-interceptor',
148
- interceptors: [ClientRequestInterceptor, XMLHttpRequestInterceptor],
148
+ interceptors: [
149
+ new ClientRequestInterceptor(),
150
+ new XMLHttpRequestInterceptor(),
151
+ ],
149
152
  })
150
153
 
151
154
  // This "request" listener will be called on both
@@ -11,7 +11,7 @@ export declare class IsomorphicRequest {
11
11
  readonly method: string;
12
12
  readonly headers: Headers;
13
13
  readonly credentials: RequestCredentials;
14
- private readonly body;
14
+ private readonly _body;
15
15
  private _bodyUsed;
16
16
  constructor(url: URL);
17
17
  constructor(url: URL, init: RequestInit);
@@ -52,7 +52,7 @@ var IsomorphicRequest = /** @class */ (function () {
52
52
  this.method = input.method;
53
53
  this.headers = input.headers;
54
54
  this.credentials = input.credentials;
55
- this.body = input.body || defaultBody;
55
+ this._body = input._body || defaultBody;
56
56
  return;
57
57
  }
58
58
  this.id = uuid_1.uuidv4();
@@ -60,7 +60,7 @@ var IsomorphicRequest = /** @class */ (function () {
60
60
  this.method = init.method || 'GET';
61
61
  this.headers = new lib_1.Headers(init.headers);
62
62
  this.credentials = init.credentials || 'same-origin';
63
- this.body = init.body || defaultBody;
63
+ this._body = init.body || defaultBody;
64
64
  }
65
65
  Object.defineProperty(IsomorphicRequest.prototype, "bodyUsed", {
66
66
  get: function () {
@@ -74,7 +74,7 @@ var IsomorphicRequest = /** @class */ (function () {
74
74
  return __generator(this, function (_a) {
75
75
  outvariant_1.invariant(!this.bodyUsed, 'Failed to execute "text" on "IsomorphicRequest": body buffer already read');
76
76
  this._bodyUsed = true;
77
- return [2 /*return*/, bufferUtils_1.decodeBuffer(this.body)];
77
+ return [2 /*return*/, bufferUtils_1.decodeBuffer(this._body)];
78
78
  });
79
79
  });
80
80
  };
@@ -84,7 +84,7 @@ var IsomorphicRequest = /** @class */ (function () {
84
84
  return __generator(this, function (_a) {
85
85
  outvariant_1.invariant(!this.bodyUsed, 'Failed to execute "json" on "IsomorphicRequest": body buffer already read');
86
86
  this._bodyUsed = true;
87
- text = bufferUtils_1.decodeBuffer(this.body);
87
+ text = bufferUtils_1.decodeBuffer(this._body);
88
88
  return [2 /*return*/, JSON.parse(text)];
89
89
  });
90
90
  });
@@ -94,7 +94,7 @@ var IsomorphicRequest = /** @class */ (function () {
94
94
  return __generator(this, function (_a) {
95
95
  outvariant_1.invariant(!this.bodyUsed, 'Failed to execute "arrayBuffer" on "IsomorphicRequest": body buffer already read');
96
96
  this._bodyUsed = true;
97
- return [2 /*return*/, this.body];
97
+ return [2 /*return*/, this._body];
98
98
  });
99
99
  });
100
100
  };
@@ -1 +1 @@
1
- {"version":3,"file":"IsomorphicRequest.js","sourceRoot":"","sources":["../src/IsomorphicRequest.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,4CAA8C;AAC9C,yCAAsC;AACtC,mDAAkD;AAClD,qCAAqC;AASrC;IAaE,2BAAY,KAA8B,EAAE,IAAsB;QAAtB,qBAAA,EAAA,SAAsB;QAChE,IAAM,WAAW,GAAG,IAAI,WAAW,CAAC,CAAC,CAAC,CAAA;QACtC,IAAI,CAAC,SAAS,GAAG,KAAK,CAAA;QAEtB,IAAI,KAAK,YAAY,iBAAiB,EAAE;YACtC,IAAI,CAAC,EAAE,GAA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,KAAK,CAAA;QAClC,IAAI,CAAC,OAAO,GAAG,IAAI,aAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;QACxC,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,WAAW,IAAI,aAAa,CAAA;QACpD,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,IAAI,IAAI,WAAW,CAAA;IACvC,CAAC;IAED,sBAAW,uCAAQ;aAAnB;YACE,OAAO,IAAI,CAAC,SAAS,CAAA;QACvB,CAAC;;;OAAA;IAEY,gCAAI,GAAjB;;;gBACE,sBAAS,CACP,CAAC,IAAI,CAAC,QAAQ,EACd,2EAA2E,CAC5E,CAAA;gBAED,IAAI,CAAC,SAAS,GAAG,IAAI,CAAA;gBACrB,sBAAO,0BAAY,CAAC,IAAI,CAAC,KAAK,CAAC,EAAA;;;KAChC;IAEY,gCAAI,GAAjB;;;;gBACE,sBAAS,CACP,CAAC,IAAI,CAAC,QAAQ,EACd,2EAA2E,CAC5E,CAAA;gBAED,IAAI,CAAC,SAAS,GAAG,IAAI,CAAA;gBACf,IAAI,GAAG,0BAAY,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;gBACrC,sBAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,EAAA;;;KACxB;IAEY,uCAAW,GAAxB;;;gBACE,sBAAS,CACP,CAAC,IAAI,CAAC,QAAQ,EACd,kFAAkF,CACnF,CAAA;gBAED,IAAI,CAAC,SAAS,GAAG,IAAI,CAAA;gBACrB,sBAAO,IAAI,CAAC,KAAK,EAAA;;;KAClB;IAEM,iCAAK,GAAZ;QACE,OAAO,IAAI,iBAAiB,CAAC,IAAI,CAAC,CAAA;IACpC,CAAC;IACH,wBAAC;AAAD,CAAC,AAzED,IAyEC;AAzEY,8CAAiB"}
package/lib/index.d.ts CHANGED
@@ -4,3 +4,4 @@ export * from './BatchInterceptor';
4
4
  export * from './IsomorphicRequest';
5
5
  export * from './InteractiveIsomorphicRequest';
6
6
  export { getCleanUrl } from './utils/getCleanUrl';
7
+ export { encodeBuffer, decodeBuffer } from './utils/bufferUtils';
package/lib/index.js CHANGED
@@ -10,7 +10,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
10
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
- exports.getCleanUrl = void 0;
13
+ exports.decodeBuffer = exports.encodeBuffer = exports.getCleanUrl = void 0;
14
14
  __exportStar(require("./glossary"), exports);
15
15
  __exportStar(require("./Interceptor"), exports);
16
16
  __exportStar(require("./BatchInterceptor"), exports);
@@ -19,4 +19,7 @@ __exportStar(require("./InteractiveIsomorphicRequest"), exports);
19
19
  /* Utils */
20
20
  var getCleanUrl_1 = require("./utils/getCleanUrl");
21
21
  Object.defineProperty(exports, "getCleanUrl", { enumerable: true, get: function () { return getCleanUrl_1.getCleanUrl; } });
22
+ var bufferUtils_1 = require("./utils/bufferUtils");
23
+ Object.defineProperty(exports, "encodeBuffer", { enumerable: true, get: function () { return bufferUtils_1.encodeBuffer; } });
24
+ Object.defineProperty(exports, "decodeBuffer", { enumerable: true, get: function () { return bufferUtils_1.decodeBuffer; } });
22
25
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,6CAA0B;AAC1B,gDAA6B;AAC7B,qDAAkC;AAClC,sDAAmC;AACnC,iEAA8C;AAE9C,WAAW;AACX,mDAAiD;AAAxC,0GAAA,WAAW,OAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,6CAA0B;AAC1B,gDAA6B;AAC7B,qDAAkC;AAClC,sDAAmC;AACnC,iEAA8C;AAE9C,WAAW;AACX,mDAAiD;AAAxC,0GAAA,WAAW,OAAA;AACpB,mDAAgE;AAAvD,2GAAA,YAAY,OAAA;AAAE,2GAAA,YAAY,OAAA"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mswjs/interceptors",
3
3
  "description": "Low-level HTTP/HTTPS/XHR/fetch request interception library.",
4
- "version": "0.17.0",
4
+ "version": "0.17.3",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "author": "Artem Zakharchenko",
@@ -67,6 +67,7 @@
67
67
  },
68
68
  "dependencies": {
69
69
  "@open-draft/until": "^1.0.3",
70
+ "@types/debug": "^4.1.7",
70
71
  "@xmldom/xmldom": "^0.7.5",
71
72
  "debug": "^4.3.3",
72
73
  "headers-polyfill": "^3.0.4",
@@ -82,20 +82,18 @@ it('returns a copy of isomorphic request instance', () => {
82
82
 
83
83
  expect(request.id).toBe(derivedRequest.id)
84
84
  expect(request.url.href).toBe(derivedRequest.url.href)
85
- expect(request['body']).toEqual(derivedRequest['body'])
85
+ expect(request['_body']).toEqual(derivedRequest['_body'])
86
86
  expect(request.headers).toEqual(derivedRequest.headers)
87
87
  expect(request.method).toBe(derivedRequest.method)
88
88
  expect(request.credentials).toBe(derivedRequest.credentials)
89
89
  expect(request.bodyUsed).toBe(false)
90
90
  })
91
91
 
92
- it('clones current isomorphic request instance', async () => {
92
+ it('clones current isomorphic request instance', () => {
93
93
  const request = new IsomorphicRequest(url, {
94
94
  body,
95
95
  headers: { foo: 'bar' },
96
96
  })
97
- await request.text()
98
-
99
97
  const clonedRequest = request.clone()
100
98
 
101
99
  expect(clonedRequest.id).toBe(request.id)
@@ -103,6 +101,6 @@ it('clones current isomorphic request instance', async () => {
103
101
  expect(clonedRequest.url.href).toBe(request.url.href)
104
102
  expect(clonedRequest.headers).toEqual(request.headers)
105
103
  expect(clonedRequest.credentials).toBe(request.credentials)
106
- expect(clonedRequest['body']).toEqual(request['body'])
104
+ expect(clonedRequest['_body']).toEqual(request['_body'])
107
105
  expect(clonedRequest.bodyUsed).toBe(false)
108
106
  })
@@ -17,7 +17,7 @@ export class IsomorphicRequest {
17
17
  public readonly headers: Headers
18
18
  public readonly credentials: RequestCredentials
19
19
 
20
- private readonly body: ArrayBuffer
20
+ private readonly _body: ArrayBuffer
21
21
  private _bodyUsed: boolean
22
22
 
23
23
  constructor(url: URL)
@@ -33,7 +33,7 @@ export class IsomorphicRequest {
33
33
  this.method = input.method
34
34
  this.headers = input.headers
35
35
  this.credentials = input.credentials
36
- this.body = input.body || defaultBody
36
+ this._body = input._body || defaultBody
37
37
  return
38
38
  }
39
39
 
@@ -42,7 +42,7 @@ export class IsomorphicRequest {
42
42
  this.method = init.method || 'GET'
43
43
  this.headers = new Headers(init.headers)
44
44
  this.credentials = init.credentials || 'same-origin'
45
- this.body = init.body || defaultBody
45
+ this._body = init.body || defaultBody
46
46
  }
47
47
 
48
48
  public get bodyUsed(): boolean {
@@ -56,7 +56,7 @@ export class IsomorphicRequest {
56
56
  )
57
57
 
58
58
  this._bodyUsed = true
59
- return decodeBuffer(this.body)
59
+ return decodeBuffer(this._body)
60
60
  }
61
61
 
62
62
  public async json<T = any>(): Promise<T> {
@@ -66,7 +66,7 @@ export class IsomorphicRequest {
66
66
  )
67
67
 
68
68
  this._bodyUsed = true
69
- const text = decodeBuffer(this.body)
69
+ const text = decodeBuffer(this._body)
70
70
  return JSON.parse(text)
71
71
  }
72
72
 
@@ -77,7 +77,7 @@ export class IsomorphicRequest {
77
77
  )
78
78
 
79
79
  this._bodyUsed = true
80
- return this.body
80
+ return this._body
81
81
  }
82
82
 
83
83
  public clone(): IsomorphicRequest {
package/src/index.ts CHANGED
@@ -6,3 +6,4 @@ export * from './InteractiveIsomorphicRequest'
6
6
 
7
7
  /* Utils */
8
8
  export { getCleanUrl } from './utils/getCleanUrl'
9
+ export { encodeBuffer, decodeBuffer } from './utils/bufferUtils'