@voiceflow/fetch 1.5.3 → 1.5.5
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/build/cjs/client-configuration.interface.d.ts +1 -0
- package/build/cjs/client-configuration.interface.d.ts.map +1 -0
- package/build/cjs/client-configuration.interface.js +0 -1
- package/build/cjs/fetch.client.d.ts +1 -0
- package/build/cjs/fetch.client.d.ts.map +1 -0
- package/build/cjs/fetch.client.js +11 -21
- package/build/cjs/fetch.client.test.d.ts +1 -0
- package/build/cjs/fetch.client.test.d.ts.map +1 -0
- package/build/cjs/fetch.client.test.js +39 -42
- package/build/cjs/fetch.interface.d.ts +1 -0
- package/build/cjs/fetch.interface.d.ts.map +1 -0
- package/build/cjs/fetch.interface.js +0 -1
- package/build/cjs/http-method.enum.d.ts +1 -0
- package/build/cjs/http-method.enum.d.ts.map +1 -0
- package/build/cjs/http-method.enum.js +0 -1
- package/build/cjs/main.d.ts +1 -0
- package/build/cjs/main.d.ts.map +1 -0
- package/build/cjs/main.js +0 -1
- package/build/cjs/request-options.interface.d.ts +1 -0
- package/build/cjs/request-options.interface.d.ts.map +1 -0
- package/build/cjs/request-options.interface.js +0 -1
- package/build/esm/client-configuration.interface.d.ts +1 -0
- package/build/esm/client-configuration.interface.d.ts.map +1 -0
- package/build/esm/client-configuration.interface.js +0 -1
- package/build/esm/fetch.client.d.ts +1 -0
- package/build/esm/fetch.client.d.ts.map +1 -0
- package/build/esm/fetch.client.js +11 -21
- package/build/esm/fetch.client.test.d.ts +1 -0
- package/build/esm/fetch.client.test.d.ts.map +1 -0
- package/build/esm/fetch.client.test.js +39 -42
- package/build/esm/fetch.interface.d.ts +1 -0
- package/build/esm/fetch.interface.d.ts.map +1 -0
- package/build/esm/fetch.interface.js +0 -1
- package/build/esm/http-method.enum.d.ts +1 -0
- package/build/esm/http-method.enum.d.ts.map +1 -0
- package/build/esm/http-method.enum.js +0 -1
- package/build/esm/main.d.ts +1 -0
- package/build/esm/main.d.ts.map +1 -0
- package/build/esm/main.js +0 -1
- package/build/esm/request-options.interface.d.ts +1 -0
- package/build/esm/request-options.interface.d.ts.map +1 -0
- package/build/esm/request-options.interface.js +0 -1
- package/package.json +13 -6
- package/build/cjs/client-configuration.interface.js.map +0 -1
- package/build/cjs/fetch.client.js.map +0 -1
- package/build/cjs/fetch.client.test.js.map +0 -1
- package/build/cjs/fetch.interface.js.map +0 -1
- package/build/cjs/http-method.enum.js.map +0 -1
- package/build/cjs/main.js.map +0 -1
- package/build/cjs/request-options.interface.js.map +0 -1
- package/build/esm/client-configuration.interface.js.map +0 -1
- package/build/esm/fetch.client.js.map +0 -1
- package/build/esm/fetch.client.test.js.map +0 -1
- package/build/esm/fetch.interface.js.map +0 -1
- package/build/esm/http-method.enum.js.map +0 -1
- package/build/esm/main.js.map +0 -1
- package/build/esm/request-options.interface.js.map +0 -1
- package/build/tsconfig.build.tsbuildinfo +0 -1
- package/build/tsconfig.esm.tsbuildinfo +0 -1
|
@@ -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"}
|
|
@@ -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,GA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
|
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
|
|
16
|
+
this.config = config ?? {};
|
|
29
17
|
}
|
|
30
18
|
else {
|
|
31
|
-
this.config =
|
|
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
|
|
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
|
|
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,
|
|
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,
|
|
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
|
-
|
|
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
|
|
@@ -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
|
-
(
|
|
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
|
-
(
|
|
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
|
-
(
|
|
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 =
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
119
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
147
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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 =
|
|
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
|
-
|
|
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:
|
|
181
|
+
sandbox.get({ url: `${baseURL}/${path}`, headers: { ...globalHeaders, ...headers } }, 200);
|
|
184
182
|
await fetch.get(path, { headers });
|
|
185
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
219
|
-
|
|
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
|
-
|
|
228
|
+
fail('expected to throw ClientException');
|
|
231
229
|
}
|
|
232
230
|
catch (err) {
|
|
233
231
|
if (exception_1.ClientException.instanceOf(err)) {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
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
|
-
|
|
239
|
+
fail('should be an instance of ClientException');
|
|
242
240
|
}
|
|
243
241
|
}
|
|
244
|
-
|
|
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"}
|
|
@@ -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"}
|
package/build/cjs/main.d.ts
CHANGED
|
@@ -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
|
@@ -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"}
|
|
@@ -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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch.client.d.ts","sourceRoot":"","sources":["../../src/f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,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
|
|
13
|
+
this.config = config ?? {};
|
|
26
14
|
}
|
|
27
15
|
else {
|
|
28
|
-
this.config =
|
|
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
|
|
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
|
|
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,
|
|
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,
|
|
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
|
-
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch.client.test.d.ts","sourceRoot":"","sources":["../../src/fetch.client.test.ts"],"names":[],"mappings":""}
|