@voiceflow/fetch 1.5.3 → 1.5.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.
Files changed (59) hide show
  1. package/build/cjs/client-configuration.interface.d.ts +1 -0
  2. package/build/cjs/client-configuration.interface.d.ts.map +1 -0
  3. package/build/cjs/client-configuration.interface.js +0 -1
  4. package/build/cjs/fetch.client.d.ts +1 -0
  5. package/build/cjs/fetch.client.d.ts.map +1 -0
  6. package/build/cjs/fetch.client.js +11 -21
  7. package/build/cjs/fetch.client.test.d.ts +1 -0
  8. package/build/cjs/fetch.client.test.d.ts.map +1 -0
  9. package/build/cjs/fetch.client.test.js +39 -42
  10. package/build/cjs/fetch.interface.d.ts +1 -0
  11. package/build/cjs/fetch.interface.d.ts.map +1 -0
  12. package/build/cjs/fetch.interface.js +0 -1
  13. package/build/cjs/http-method.enum.d.ts +1 -0
  14. package/build/cjs/http-method.enum.d.ts.map +1 -0
  15. package/build/cjs/http-method.enum.js +0 -1
  16. package/build/cjs/main.d.ts +1 -0
  17. package/build/cjs/main.d.ts.map +1 -0
  18. package/build/cjs/main.js +0 -1
  19. package/build/cjs/request-options.interface.d.ts +1 -0
  20. package/build/cjs/request-options.interface.d.ts.map +1 -0
  21. package/build/cjs/request-options.interface.js +0 -1
  22. package/build/esm/client-configuration.interface.d.ts +1 -0
  23. package/build/esm/client-configuration.interface.d.ts.map +1 -0
  24. package/build/esm/client-configuration.interface.js +0 -1
  25. package/build/esm/fetch.client.d.ts +1 -0
  26. package/build/esm/fetch.client.d.ts.map +1 -0
  27. package/build/esm/fetch.client.js +11 -21
  28. package/build/esm/fetch.client.test.d.ts +1 -0
  29. package/build/esm/fetch.client.test.d.ts.map +1 -0
  30. package/build/esm/fetch.client.test.js +39 -42
  31. package/build/esm/fetch.interface.d.ts +1 -0
  32. package/build/esm/fetch.interface.d.ts.map +1 -0
  33. package/build/esm/fetch.interface.js +0 -1
  34. package/build/esm/http-method.enum.d.ts +1 -0
  35. package/build/esm/http-method.enum.d.ts.map +1 -0
  36. package/build/esm/http-method.enum.js +0 -1
  37. package/build/esm/main.d.ts +1 -0
  38. package/build/esm/main.d.ts.map +1 -0
  39. package/build/esm/main.js +0 -1
  40. package/build/esm/request-options.interface.d.ts +1 -0
  41. package/build/esm/request-options.interface.d.ts.map +1 -0
  42. package/build/esm/request-options.interface.js +0 -1
  43. package/package.json +10 -6
  44. package/build/cjs/client-configuration.interface.js.map +0 -1
  45. package/build/cjs/fetch.client.js.map +0 -1
  46. package/build/cjs/fetch.client.test.js.map +0 -1
  47. package/build/cjs/fetch.interface.js.map +0 -1
  48. package/build/cjs/http-method.enum.js.map +0 -1
  49. package/build/cjs/main.js.map +0 -1
  50. package/build/cjs/request-options.interface.js.map +0 -1
  51. package/build/esm/client-configuration.interface.js.map +0 -1
  52. package/build/esm/fetch.client.js.map +0 -1
  53. package/build/esm/fetch.client.test.js.map +0 -1
  54. package/build/esm/fetch.interface.js.map +0 -1
  55. package/build/esm/http-method.enum.js.map +0 -1
  56. package/build/esm/main.js.map +0 -1
  57. package/build/esm/request-options.interface.js.map +0 -1
  58. package/build/tsconfig.build.tsbuildinfo +0 -1
  59. package/build/tsconfig.esm.tsbuildinfo +0 -1
@@ -3,3 +3,4 @@ export interface ClientConfiguration {
3
3
  baseURL?: string;
4
4
  headers?: RequestHeaders;
5
5
  }
6
+ //# sourceMappingURL=client-configuration.interface.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client-configuration.interface.d.ts","sourceRoot":"","sources":["../../src/client-configuration.interface.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAE7D,MAAM,WAAW,mBAAmB;IAClC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,cAAc,CAAC;CAC1B"}
@@ -1,3 +1,2 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- //# sourceMappingURL=client-configuration.interface.js.map
@@ -31,3 +31,4 @@ export declare class FetchClient<Opts extends FetchOptions<any, any> = RequestIn
31
31
  json: <T = unknown>() => Promise<T>;
32
32
  };
33
33
  }
34
+ //# sourceMappingURL=fetch.client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetch.client.d.ts","sourceRoot":"","sources":["../../src/fetch.client.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,mBAAmB,EAAE,MAAM,kCAAkC,CAAC;AACvE,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAE1E,OAAO,EAAkB,cAAc,EAAgB,MAAM,6BAA6B,CAAC;AAE3F,qBAAa,WAAW,CAAC,IAAI,SAAS,YAAY,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,WAAW,EAAE,GAAG,GAAG,GAAG,GAAG,OAAO,EAAE,GAAG,SAAS,aAAa,GAAG,QAAQ;IACnI,OAAO,CAAC,MAAM,CAAC,cAAc;IAI7B,OAAO,CAAC,MAAM,CAAC,YAAY;IAI3B,OAAO,CAAC,MAAM,CAAC,SAAS;IAMxB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAsB;IAE7C,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAuC;gBAGjD,MAAM,CAAC,EAAE,mBAAmB;gBAC5B,KAAK,EAAE,QAAQ,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,CAAC,EAAE,OAAO,CAAC,EAAE,mBAAmB;YAW5D,IAAI;IA0BlB,OAAO,CAAC,YAAY;IAUb,GAAG,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,QAAQ,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,GAAG,OAAO,CAAC,GAAG,CAAC;IAIhE,MAAM,QAbE,MAAM,GAAG,GAAG,YAAY,KAAK,eAAe,IAAI,CAAC,EAAE,QAAQ,CAAC;;MAatB;IAE9C,GAAG,QAfK,MAAM,GAAG,GAAG,YAAY,KAAK,eAAe,IAAI,CAAC,EAAE,QAAQ,CAAC;;MAe5B;IAExC,IAAI,QAjBI,MAAM,GAAG,GAAG,YAAY,KAAK,eAAe,IAAI,CAAC,EAAE,QAAQ,CAAC;;MAiB1B;IAE1C,KAAK,QAnBG,MAAM,GAAG,GAAG,YAAY,KAAK,eAAe,IAAI,CAAC,EAAE,QAAQ,CAAC;;MAmBxB;IAE5C,IAAI,QArBI,MAAM,GAAG,GAAG,YAAY,KAAK,eAAe,IAAI,CAAC,EAAE,QAAQ,CAAC;;MAqB1B;IAE1C,GAAG,QAvBK,MAAM,GAAG,GAAG,YAAY,KAAK,eAAe,IAAI,CAAC,EAAE,QAAQ,CAAC;;MAuB5B;CAChD"}
@@ -1,22 +1,10 @@
1
1
  "use strict";
2
- var __rest = (this && this.__rest) || function (s, e) {
3
- var t = {};
4
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5
- t[p] = s[p];
6
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
7
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9
- t[p[i]] = s[p[i]];
10
- }
11
- return t;
12
- };
13
2
  Object.defineProperty(exports, "__esModule", { value: true });
14
3
  exports.FetchClient = void 0;
15
4
  const exception_1 = require("@voiceflow/exception");
16
5
  const http_method_enum_1 = require("./http-method.enum");
17
6
  class FetchClient {
18
7
  constructor(fetchOrConfig, config) {
19
- var _a;
20
8
  this.delete = this.createMethod(http_method_enum_1.HTTPMethod.DELETE);
21
9
  this.get = this.createMethod(http_method_enum_1.HTTPMethod.GET);
22
10
  this.head = this.createMethod(http_method_enum_1.HTTPMethod.HEAD);
@@ -25,14 +13,14 @@ class FetchClient {
25
13
  this.put = this.createMethod(http_method_enum_1.HTTPMethod.PUT);
26
14
  if (typeof fetchOrConfig === 'function') {
27
15
  this.fetch = fetchOrConfig;
28
- this.config = config !== null && config !== void 0 ? config : {};
16
+ this.config = config ?? {};
29
17
  }
30
18
  else {
31
- this.config = (_a = fetchOrConfig !== null && fetchOrConfig !== void 0 ? fetchOrConfig : config) !== null && _a !== void 0 ? _a : {};
19
+ this.config = fetchOrConfig ?? config ?? {};
32
20
  }
33
21
  }
34
22
  static extractHeaders(headers) {
35
- return new Map(headers instanceof Map ? headers : Object.entries(headers !== null && headers !== void 0 ? headers : {}));
23
+ return new Map(headers instanceof Map ? headers : Object.entries(headers ?? {}));
36
24
  }
37
25
  static extractQuery(query) {
38
26
  return new URLSearchParams(query instanceof Map ? Object.entries(query) : query);
@@ -45,7 +33,7 @@ class FetchClient {
45
33
  /* eslint-enable lines-between-class-members */
46
34
  async send(url, rawOptions) {
47
35
  // eslint-disable-next-line prefer-const
48
- let { json, headers, query, body } = rawOptions, options = __rest(rawOptions, ["json", "headers", "query", "body"]);
36
+ let { json, headers, query, body, ...options } = rawOptions;
49
37
  headers = new Map([...FetchClient.extractHeaders(this.config.headers).entries(), ...FetchClient.extractHeaders(headers).entries()]);
50
38
  query = FetchClient.extractQuery(query);
51
39
  if (json != null) {
@@ -53,7 +41,11 @@ class FetchClient {
53
41
  body = JSON.stringify(json);
54
42
  }
55
43
  const finalURL = typeof url === 'string' ? FetchClient.formatURL(this.config.baseURL, url, query) : url;
56
- const response = await this.raw(finalURL, Object.assign(Object.assign({}, options), { headers: Object.fromEntries(headers.entries()), body }));
44
+ const response = await this.raw(finalURL, {
45
+ ...options,
46
+ headers: Object.fromEntries(headers.entries()),
47
+ body,
48
+ });
57
49
  if (!response.ok) {
58
50
  throw await new exception_1.ClientException(response).build();
59
51
  }
@@ -61,16 +53,14 @@ class FetchClient {
61
53
  }
62
54
  createMethod(method) {
63
55
  return (url, options) => {
64
- const response = this.send(url, Object.assign(Object.assign({}, options), { method: method.toUpperCase() }));
56
+ const response = this.send(url, { ...options, method: method.toUpperCase() });
65
57
  return Object.assign(response, {
66
58
  json: async () => (await response).json(),
67
59
  });
68
60
  };
69
61
  }
70
62
  raw(...args) {
71
- var _a;
72
- return ((_a = this.fetch) !== null && _a !== void 0 ? _a : window.fetch)(...args);
63
+ return (this.fetch ?? window.fetch)(...args);
73
64
  }
74
65
  }
75
66
  exports.FetchClient = FetchClient;
76
- //# sourceMappingURL=fetch.client.js.map
@@ -1 +1,2 @@
1
1
  export {};
2
+ //# sourceMappingURL=fetch.client.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetch.client.test.d.ts","sourceRoot":"","sources":["../../src/fetch.client.test.ts"],"names":[],"mappings":""}
@@ -27,68 +27,66 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
29
  const exception_1 = require("@voiceflow/exception");
30
- const chai_1 = require("chai");
31
30
  const fetch_mock_1 = __importDefault(require("fetch-mock"));
31
+ const jest_fetch_mock_1 = __importDefault(require("jest-fetch-mock"));
32
32
  const node_url_1 = require("node:url");
33
- const sinon = __importStar(require("sinon"));
34
33
  const undici = __importStar(require("undici"));
35
34
  const fetch_client_1 = require("./fetch.client");
36
35
  const TARGET_URL = 'http://example.com/resource/123';
37
36
  const JSON_HEADERS = { 'content-type': 'application/json' };
37
+ jest_fetch_mock_1.default.enableMocks();
38
38
  describe('Fetch Client', () => {
39
39
  let sandbox;
40
+ beforeAll(() => {
41
+ jest_fetch_mock_1.default.mockImplementation(async () => null);
42
+ });
40
43
  beforeEach(() => {
41
44
  sandbox = fetch_mock_1.default.sandbox();
42
45
  });
43
46
  describe('#raw()', () => {
44
47
  const url = 'http://example.com';
45
48
  describe('window.fetch', () => {
46
- let fetchStub;
47
49
  let fetchClient;
48
50
  beforeEach(() => {
49
- fetchStub = sinon.stub(window, 'fetch');
50
51
  fetchClient = new fetch_client_1.FetchClient();
51
52
  });
52
- afterEach(() => {
53
- sinon.restore();
54
- });
55
53
  it('should pass through Request instance and options to window.fetch', async () => {
56
54
  const request = new Request(new URL(url));
57
55
  const options = { cache: 'only-if-cached' };
58
56
  await fetchClient.raw(request, options);
59
- (0, chai_1.expect)(fetchStub).to.be.calledWithExactly(request, options);
57
+ expect(jest_fetch_mock_1.default).toHaveBeenCalledWith(request, options);
60
58
  });
61
59
  it('should pass through URL instance to window.fetch', async () => {
62
60
  const request = new URL(url);
63
61
  await fetchClient.raw(request);
64
- (0, chai_1.expect)(fetchStub).to.be.calledWithExactly(request);
62
+ expect(jest_fetch_mock_1.default).toHaveBeenCalledWith(request);
65
63
  });
66
64
  it('should pass through string to window.fetch', async () => {
67
65
  await fetchClient.raw(url);
68
- (0, chai_1.expect)(fetchStub).to.be.calledWithExactly(url);
66
+ expect(jest_fetch_mock_1.default).toHaveBeenCalledWith(url);
69
67
  });
70
68
  });
71
69
  describe('undici.fetch', () => {
72
70
  let fetchSpy;
73
71
  let fetchClient;
74
72
  beforeEach(() => {
75
- fetchSpy = sinon.spy();
73
+ fetchSpy = jest.fn();
76
74
  fetchClient = new fetch_client_1.FetchClient(fetchSpy);
77
75
  });
78
76
  it('should pass through Request instance and options to undici.fetch', async () => {
79
77
  const request = new undici.Request(new node_url_1.URL(url));
80
78
  const options = { dispatcher: new undici.Dispatcher() };
81
79
  await fetchClient.raw(request, options);
82
- (0, chai_1.expect)(fetchSpy).to.be.calledWithExactly(request, options);
80
+ expect(fetchSpy).toHaveBeenCalledWith(request, options);
83
81
  });
84
82
  it('should pass through URL instance to undici.fetch', async () => {
85
83
  const request = new node_url_1.URL(url);
86
84
  await fetchClient.raw(request);
87
- (0, chai_1.expect)(fetchSpy).to.be.calledWithExactly(request);
85
+ expect(fetchSpy).toHaveBeenCalledWith(request);
88
86
  });
89
87
  it('should pass through string to undici.fetch', async () => {
90
88
  await fetchClient.raw(url);
91
- (0, chai_1.expect)(fetchSpy).to.be.calledWithExactly(url);
89
+ expect(fetchSpy).toHaveBeenCalledWith(url);
92
90
  });
93
91
  });
94
92
  });
@@ -97,7 +95,7 @@ describe('Fetch Client', () => {
97
95
  const fetch = new fetch_client_1.FetchClient(sandbox);
98
96
  sandbox.delete(TARGET_URL, 200);
99
97
  await fetch.delete(TARGET_URL);
100
- (0, chai_1.expect)(sandbox.done()).to.be.true;
98
+ expect(sandbox.done()).toBe(true);
101
99
  });
102
100
  });
103
101
  describe('#get()', () => {
@@ -108,15 +106,15 @@ describe('Fetch Client', () => {
108
106
  await fetch.get(TARGET_URL, {
109
107
  query: { test: 'encode this&' },
110
108
  });
111
- (0, chai_1.expect)(sandbox.done()).to.be.true;
109
+ expect(sandbox.done()).toBe(true);
112
110
  });
113
111
  it('should send GET request', async () => {
114
112
  const data = { foo: 'bar' };
115
113
  const fetch = new fetch_client_1.FetchClient(sandbox);
116
114
  sandbox.get(TARGET_URL, { status: 200, body: data });
117
115
  const result = await fetch.get(TARGET_URL).json();
118
- (0, chai_1.expect)(result).to.eql(data);
119
- (0, chai_1.expect)(sandbox.done()).to.be.true;
116
+ expect(result).toEqual(data);
117
+ expect(sandbox.done()).toBe(true);
120
118
  });
121
119
  });
122
120
  describe('#head()', () => {
@@ -124,7 +122,7 @@ describe('Fetch Client', () => {
124
122
  const fetch = new fetch_client_1.FetchClient(sandbox);
125
123
  sandbox.head(TARGET_URL, 200);
126
124
  await fetch.head(TARGET_URL);
127
- (0, chai_1.expect)(sandbox.done()).to.be.true;
125
+ expect(sandbox.done()).toBe(true);
128
126
  });
129
127
  });
130
128
  describe('#patch()', () => {
@@ -133,7 +131,7 @@ describe('Fetch Client', () => {
133
131
  const fetch = new fetch_client_1.FetchClient(sandbox);
134
132
  sandbox.patch({ url: TARGET_URL, body, headers: JSON_HEADERS }, 200);
135
133
  await fetch.patch(TARGET_URL, { json: body });
136
- (0, chai_1.expect)(sandbox.done()).to.be.true;
134
+ expect(sandbox.done()).toBe(true);
137
135
  });
138
136
  });
139
137
  describe('#post()', () => {
@@ -143,8 +141,8 @@ describe('Fetch Client', () => {
143
141
  const fetch = new fetch_client_1.FetchClient(sandbox);
144
142
  sandbox.post({ url: TARGET_URL, body, headers: JSON_HEADERS }, { status: 200, body: data });
145
143
  const result = await fetch.post(TARGET_URL, { json: body }).json();
146
- (0, chai_1.expect)(result).to.eql(data);
147
- (0, chai_1.expect)(sandbox.done()).to.be.true;
144
+ expect(result).toEqual(data);
145
+ expect(sandbox.done()).toBe(true);
148
146
  });
149
147
  });
150
148
  describe('#put()', () => {
@@ -153,7 +151,7 @@ describe('Fetch Client', () => {
153
151
  const fetch = new fetch_client_1.FetchClient(sandbox);
154
152
  sandbox.put({ url: TARGET_URL, body, headers: JSON_HEADERS }, 200);
155
153
  await fetch.put(TARGET_URL, { json: body });
156
- (0, chai_1.expect)(sandbox.done()).to.be.true;
154
+ expect(sandbox.done()).toBe(true);
157
155
  });
158
156
  });
159
157
  describe('#config.baseURL', () => {
@@ -163,14 +161,14 @@ describe('Fetch Client', () => {
163
161
  const fetch = new fetch_client_1.FetchClient(sandbox, { baseURL });
164
162
  sandbox.get(`${baseURL}/${path}`, 200);
165
163
  await fetch.get(path);
166
- (0, chai_1.expect)(sandbox.done()).to.be.true;
164
+ expect(sandbox.done()).toBe(true);
167
165
  });
168
166
  it('should not prefix request using URL instance', async () => {
169
167
  const url = new node_url_1.URL(TARGET_URL);
170
- const fetchSpy = sinon.spy(async () => new undici.Response());
168
+ const fetchSpy = jest.fn().mockResolvedValue(new undici.Response());
171
169
  const fetchClient = new fetch_client_1.FetchClient(fetchSpy, { baseURL: 'http://foo.com/' });
172
170
  await fetchClient.get(url);
173
- (0, chai_1.expect)(fetchSpy).to.be.calledWithExactly(url, { method: 'GET', headers: {}, body: undefined });
171
+ expect(fetchSpy).toHaveBeenCalledWith(url, { method: 'GET', headers: {}, body: undefined });
174
172
  });
175
173
  });
176
174
  describe('#config.headers', () => {
@@ -180,9 +178,9 @@ describe('Fetch Client', () => {
180
178
  const globalHeaders = { foo: 'one', bar: 'two' };
181
179
  const headers = { bar: 'three', fizz: 'four' };
182
180
  const fetch = new fetch_client_1.FetchClient(sandbox, { baseURL, headers: globalHeaders });
183
- sandbox.get({ url: `${baseURL}/${path}`, headers: Object.assign(Object.assign({}, globalHeaders), headers) }, 200);
181
+ sandbox.get({ url: `${baseURL}/${path}`, headers: { ...globalHeaders, ...headers } }, 200);
184
182
  await fetch.get(path, { headers });
185
- (0, chai_1.expect)(sandbox.done()).to.be.true;
183
+ expect(sandbox.done()).toBe(true);
186
184
  });
187
185
  it('should allow global headers to be updated asynchronously', async () => {
188
186
  const globalHeaders = new Map([['foo', 'bar']]);
@@ -192,7 +190,7 @@ describe('Fetch Client', () => {
192
190
  await fetch.get('one');
193
191
  globalHeaders.set('fizz', 'buzz');
194
192
  await fetch.get('two');
195
- (0, chai_1.expect)(sandbox.done()).to.be.true;
193
+ expect(sandbox.done()).toBe(true);
196
194
  });
197
195
  });
198
196
  describe('request options', () => {
@@ -201,22 +199,22 @@ describe('Fetch Client', () => {
201
199
  const headers = new Map([['foo', 'bar']]);
202
200
  sandbox.get({ url: TARGET_URL, headers: { foo: 'bar' } }, 200);
203
201
  await fetch.get(TARGET_URL, { headers });
204
- (0, chai_1.expect)(sandbox.done()).to.be.true;
202
+ expect(sandbox.done()).toBe(true);
205
203
  });
206
204
  it('should accept headers as an object', async () => {
207
205
  const fetch = new fetch_client_1.FetchClient(sandbox);
208
206
  const headers = { foo: 'bar' };
209
207
  sandbox.get({ url: TARGET_URL, headers }, 200);
210
208
  await fetch.get(TARGET_URL, { headers });
211
- (0, chai_1.expect)(sandbox.done()).to.be.true;
209
+ expect(sandbox.done()).toBe(true);
212
210
  });
213
211
  });
214
212
  describe('error handling', () => {
215
213
  it('should throw ClientException on non-2xx status code', async () => {
216
214
  const fetch = new fetch_client_1.FetchClient(sandbox);
217
215
  sandbox.head(TARGET_URL, 404);
218
- await (0, chai_1.expect)(fetch.head(TARGET_URL)).to.be.rejectedWith(exception_1.ClientException);
219
- (0, chai_1.expect)(sandbox.done()).to.be.true;
216
+ await expect(fetch.head(TARGET_URL)).rejects.toBeInstanceOf(exception_1.ClientException);
217
+ expect(sandbox.done()).toBe(true);
220
218
  });
221
219
  it('should extract error details from response body', async () => {
222
220
  const status = 500;
@@ -227,22 +225,21 @@ describe('Fetch Client', () => {
227
225
  sandbox.get(TARGET_URL, { status, body: { message, cause, details } });
228
226
  try {
229
227
  await fetch.get(TARGET_URL);
230
- chai_1.expect.fail('expected to throw ClientException');
228
+ fail('expected to throw ClientException');
231
229
  }
232
230
  catch (err) {
233
231
  if (exception_1.ClientException.instanceOf(err)) {
234
- (0, chai_1.expect)(err.statusCode).to.eq(status);
235
- (0, chai_1.expect)(err.statusText).to.eq('Internal Server Error');
236
- (0, chai_1.expect)(err.message).to.eq(message);
237
- (0, chai_1.expect)(err.cause).to.eq(cause);
238
- (0, chai_1.expect)(err.details).to.eql(details);
232
+ expect(err.statusCode).toEqual(status);
233
+ expect(err.statusText).toEqual('Internal Server Error');
234
+ expect(err.message).toEqual(message);
235
+ expect(err.cause).toEqual(cause);
236
+ expect(err.details).toEqual(details);
239
237
  }
240
238
  else {
241
- chai_1.expect.fail('should be an instance of ClientException');
239
+ fail('should be an instance of ClientException');
242
240
  }
243
241
  }
244
- (0, chai_1.expect)(sandbox.done()).to.be.true;
242
+ expect(sandbox.done()).toBe(true);
245
243
  });
246
244
  });
247
245
  });
248
- //# sourceMappingURL=fetch.client.test.js.map
@@ -9,3 +9,4 @@ export interface FetchResponse extends BaseResponse {
9
9
  json: () => Promise<any>;
10
10
  }
11
11
  export declare type FetchAPI<Opts extends FetchOptions<any, any>, Req, Res extends FetchResponse> = (input: string | Req, init?: Opts) => Promise<Res>;
12
+ //# sourceMappingURL=fetch.interface.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetch.interface.d.ts","sourceRoot":"","sources":["../../src/fetch.interface.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAEpD,MAAM,WAAW,YAAY,CAAC,OAAO,EAAE,IAAI;IACzC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,OAAO,CAAC;IAChE,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CACtB;AAED,MAAM,WAAW,aAAc,SAAQ,YAAY;IACjD,EAAE,EAAE,OAAO,CAAC;IACZ,IAAI,EAAE,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC;CAC1B;AAED,oBAAY,QAAQ,CAAC,IAAI,SAAS,YAAY,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE,GAAG,EAAE,GAAG,SAAS,aAAa,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,GAAG,EAAE,IAAI,CAAC,EAAE,IAAI,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC"}
@@ -1,3 +1,2 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- //# sourceMappingURL=fetch.interface.js.map
@@ -6,3 +6,4 @@ export declare enum HTTPMethod {
6
6
  POST = "post",
7
7
  PUT = "put"
8
8
  }
9
+ //# sourceMappingURL=http-method.enum.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http-method.enum.d.ts","sourceRoot":"","sources":["../../src/http-method.enum.ts"],"names":[],"mappings":"AAAA,oBAAY,UAAU;IACpB,MAAM,WAAW;IACjB,GAAG,QAAQ;IACX,IAAI,SAAS;IACb,KAAK,UAAU;IACf,IAAI,SAAS;IACb,GAAG,QAAQ;CACZ"}
@@ -10,4 +10,3 @@ var HTTPMethod;
10
10
  HTTPMethod["POST"] = "post";
11
11
  HTTPMethod["PUT"] = "put";
12
12
  })(HTTPMethod = exports.HTTPMethod || (exports.HTTPMethod = {}));
13
- //# sourceMappingURL=http-method.enum.js.map
@@ -3,3 +3,4 @@ export * from './fetch.client';
3
3
  export * from './fetch.interface';
4
4
  export * from './http-method.enum';
5
5
  export * from './request-options.interface';
6
+ //# sourceMappingURL=main.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../../src/main.ts"],"names":[],"mappings":"AAAA,cAAc,kCAAkC,CAAC;AACjD,cAAc,gBAAgB,CAAC;AAC/B,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,6BAA6B,CAAC"}
package/build/cjs/main.js CHANGED
@@ -19,4 +19,3 @@ __exportStar(require("./fetch.client"), exports);
19
19
  __exportStar(require("./fetch.interface"), exports);
20
20
  __exportStar(require("./http-method.enum"), exports);
21
21
  __exportStar(require("./request-options.interface"), exports);
22
- //# sourceMappingURL=main.js.map
@@ -7,3 +7,4 @@ export interface ExtraOptions {
7
7
  query?: RequestQuery | undefined;
8
8
  }
9
9
  export declare type RequestOptions<Opts extends FetchOptions<any, any>> = Omit<Partial<Opts>, keyof ExtraOptions> & ExtraOptions;
10
+ //# sourceMappingURL=request-options.interface.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request-options.interface.d.ts","sourceRoot":"","sources":["../../src/request-options.interface.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjD,oBAAY,cAAc,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAE1E,oBAAY,YAAY,GAAG,eAAe,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAE/G,MAAM,WAAW,YAAY;IAC3B,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,OAAO,CAAC,EAAE,cAAc,CAAC;IACzB,KAAK,CAAC,EAAE,YAAY,GAAG,SAAS,CAAC;CAClC;AAED,oBAAY,cAAc,CAAC,IAAI,SAAS,YAAY,CAAC,GAAG,EAAE,GAAG,CAAC,IAAI,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,MAAM,YAAY,CAAC,GAAG,YAAY,CAAC"}
@@ -1,3 +1,2 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- //# sourceMappingURL=request-options.interface.js.map
@@ -3,3 +3,4 @@ export interface ClientConfiguration {
3
3
  baseURL?: string;
4
4
  headers?: RequestHeaders;
5
5
  }
6
+ //# sourceMappingURL=client-configuration.interface.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client-configuration.interface.d.ts","sourceRoot":"","sources":["../../src/client-configuration.interface.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAE7D,MAAM,WAAW,mBAAmB;IAClC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,cAAc,CAAC;CAC1B"}
@@ -1,2 +1 @@
1
1
  export {};
2
- //# sourceMappingURL=client-configuration.interface.js.map
@@ -31,3 +31,4 @@ export declare class FetchClient<Opts extends FetchOptions<any, any> = RequestIn
31
31
  json: <T = unknown>() => Promise<T>;
32
32
  };
33
33
  }
34
+ //# sourceMappingURL=fetch.client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetch.client.d.ts","sourceRoot":"","sources":["../../src/fetch.client.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,mBAAmB,EAAE,MAAM,kCAAkC,CAAC;AACvE,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAE1E,OAAO,EAAkB,cAAc,EAAgB,MAAM,6BAA6B,CAAC;AAE3F,qBAAa,WAAW,CAAC,IAAI,SAAS,YAAY,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,WAAW,EAAE,GAAG,GAAG,GAAG,GAAG,OAAO,EAAE,GAAG,SAAS,aAAa,GAAG,QAAQ;IACnI,OAAO,CAAC,MAAM,CAAC,cAAc;IAI7B,OAAO,CAAC,MAAM,CAAC,YAAY;IAI3B,OAAO,CAAC,MAAM,CAAC,SAAS;IAMxB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAsB;IAE7C,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAuC;gBAGjD,MAAM,CAAC,EAAE,mBAAmB;gBAC5B,KAAK,EAAE,QAAQ,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,CAAC,EAAE,OAAO,CAAC,EAAE,mBAAmB;YAW5D,IAAI;IA0BlB,OAAO,CAAC,YAAY;IAUb,GAAG,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,QAAQ,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,GAAG,OAAO,CAAC,GAAG,CAAC;IAIhE,MAAM,QAbE,MAAM,GAAG,GAAG,YAAY,KAAK,eAAe,IAAI,CAAC,EAAE,QAAQ,CAAC;;MAatB;IAE9C,GAAG,QAfK,MAAM,GAAG,GAAG,YAAY,KAAK,eAAe,IAAI,CAAC,EAAE,QAAQ,CAAC;;MAe5B;IAExC,IAAI,QAjBI,MAAM,GAAG,GAAG,YAAY,KAAK,eAAe,IAAI,CAAC,EAAE,QAAQ,CAAC;;MAiB1B;IAE1C,KAAK,QAnBG,MAAM,GAAG,GAAG,YAAY,KAAK,eAAe,IAAI,CAAC,EAAE,QAAQ,CAAC;;MAmBxB;IAE5C,IAAI,QArBI,MAAM,GAAG,GAAG,YAAY,KAAK,eAAe,IAAI,CAAC,EAAE,QAAQ,CAAC;;MAqB1B;IAE1C,GAAG,QAvBK,MAAM,GAAG,GAAG,YAAY,KAAK,eAAe,IAAI,CAAC,EAAE,QAAQ,CAAC;;MAuB5B;CAChD"}
@@ -1,19 +1,7 @@
1
- var __rest = (this && this.__rest) || function (s, e) {
2
- var t = {};
3
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
- t[p] = s[p];
5
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
- t[p[i]] = s[p[i]];
9
- }
10
- return t;
11
- };
12
1
  import { ClientException } from '@voiceflow/exception';
13
2
  import { HTTPMethod } from './http-method.enum.js';
14
3
  export class FetchClient {
15
4
  constructor(fetchOrConfig, config) {
16
- var _a;
17
5
  this.delete = this.createMethod(HTTPMethod.DELETE);
18
6
  this.get = this.createMethod(HTTPMethod.GET);
19
7
  this.head = this.createMethod(HTTPMethod.HEAD);
@@ -22,14 +10,14 @@ export class FetchClient {
22
10
  this.put = this.createMethod(HTTPMethod.PUT);
23
11
  if (typeof fetchOrConfig === 'function') {
24
12
  this.fetch = fetchOrConfig;
25
- this.config = config !== null && config !== void 0 ? config : {};
13
+ this.config = config ?? {};
26
14
  }
27
15
  else {
28
- this.config = (_a = fetchOrConfig !== null && fetchOrConfig !== void 0 ? fetchOrConfig : config) !== null && _a !== void 0 ? _a : {};
16
+ this.config = fetchOrConfig ?? config ?? {};
29
17
  }
30
18
  }
31
19
  static extractHeaders(headers) {
32
- return new Map(headers instanceof Map ? headers : Object.entries(headers !== null && headers !== void 0 ? headers : {}));
20
+ return new Map(headers instanceof Map ? headers : Object.entries(headers ?? {}));
33
21
  }
34
22
  static extractQuery(query) {
35
23
  return new URLSearchParams(query instanceof Map ? Object.entries(query) : query);
@@ -42,7 +30,7 @@ export class FetchClient {
42
30
  /* eslint-enable lines-between-class-members */
43
31
  async send(url, rawOptions) {
44
32
  // eslint-disable-next-line prefer-const
45
- let { json, headers, query, body } = rawOptions, options = __rest(rawOptions, ["json", "headers", "query", "body"]);
33
+ let { json, headers, query, body, ...options } = rawOptions;
46
34
  headers = new Map([...FetchClient.extractHeaders(this.config.headers).entries(), ...FetchClient.extractHeaders(headers).entries()]);
47
35
  query = FetchClient.extractQuery(query);
48
36
  if (json != null) {
@@ -50,7 +38,11 @@ export class FetchClient {
50
38
  body = JSON.stringify(json);
51
39
  }
52
40
  const finalURL = typeof url === 'string' ? FetchClient.formatURL(this.config.baseURL, url, query) : url;
53
- const response = await this.raw(finalURL, Object.assign(Object.assign({}, options), { headers: Object.fromEntries(headers.entries()), body }));
41
+ const response = await this.raw(finalURL, {
42
+ ...options,
43
+ headers: Object.fromEntries(headers.entries()),
44
+ body,
45
+ });
54
46
  if (!response.ok) {
55
47
  throw await new ClientException(response).build();
56
48
  }
@@ -58,15 +50,13 @@ export class FetchClient {
58
50
  }
59
51
  createMethod(method) {
60
52
  return (url, options) => {
61
- const response = this.send(url, Object.assign(Object.assign({}, options), { method: method.toUpperCase() }));
53
+ const response = this.send(url, { ...options, method: method.toUpperCase() });
62
54
  return Object.assign(response, {
63
55
  json: async () => (await response).json(),
64
56
  });
65
57
  };
66
58
  }
67
59
  raw(...args) {
68
- var _a;
69
- return ((_a = this.fetch) !== null && _a !== void 0 ? _a : window.fetch)(...args);
60
+ return (this.fetch ?? window.fetch)(...args);
70
61
  }
71
62
  }
72
- //# sourceMappingURL=fetch.client.js.map
@@ -1 +1,2 @@
1
1
  export {};
2
+ //# sourceMappingURL=fetch.client.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetch.client.test.d.ts","sourceRoot":"","sources":["../../src/fetch.client.test.ts"],"names":[],"mappings":""}