-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathaxios-fetch-adapter.js
459 lines (413 loc) Β· 12.4 KB
/
axios-fetch-adapter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/* eslint-disable no-plusplus */
/* eslint-disable prefer-template */
/* eslint-disable prefer-arrow-callback */
/* eslint-disable no-var */
/* eslint-disable vars-on-top */
/* eslint-disable no-param-reassign */
/* eslint-disable import/no-extraneous-dependencies */
/**
* This is copied from @vespaiach/axios-fetch-adapter, which exposes an ESM
* module without setting the "type" field in package.json.
*/
import axios from "axios";
import {
EventStreamContentType,
getLines,
getBytes,
getMessages,
} from "./event-source-parse.js";
function tryJsonStringify(data) {
try {
return JSON.stringify(data);
} catch (e) {
return data;
}
}
/**
* In order to avoid import issues with axios 1.x, copying here the internal
* utility functions that we used to import directly from axios.
*/
// Copied from axios/lib/core/settle.js
function settle(resolve, reject, response) {
const { validateStatus } = response.config;
if (!response.status || !validateStatus || validateStatus(response.status)) {
resolve(response);
} else {
reject(
createError(
`Request failed with status code ${response.status} and body ${
typeof response.data === "string"
? response.data
: tryJsonStringify(response.data)
}`,
response.config,
null,
response.request,
response
)
);
}
}
// Copied from axios/lib/helpers/isAbsoluteURL.js
function isAbsoluteURL(url) {
// A URL is considered absolute if it begins with "<scheme>://" or "//" (protocol-relative URL).
// RFC 3986 defines scheme name as a sequence of characters beginning with a letter and followed
// by any combination of letters, digits, plus, period, or hyphen.
return /^([a-z][a-z\d+\-.]*:)?\/\//i.test(url);
}
// Copied from axios/lib/helpers/combineURLs.js
function combineURLs(baseURL, relativeURL) {
return relativeURL
? baseURL.replace(/\/+$/, "") + "/" + relativeURL.replace(/^\/+/, "")
: baseURL;
}
// Copied from axios/lib/helpers/buildURL.js
function encode(val) {
return encodeURIComponent(val)
.replace(/%3A/gi, ":")
.replace(/%24/g, "$")
.replace(/%2C/gi, ",")
.replace(/%20/g, "+")
.replace(/%5B/gi, "[")
.replace(/%5D/gi, "]");
}
function buildURL(url, params, paramsSerializer) {
if (!params) {
return url;
}
var serializedParams;
if (paramsSerializer) {
serializedParams = paramsSerializer(params);
} else if (isURLSearchParams(params)) {
serializedParams = params.toString();
} else {
var parts = [];
forEach(params, function serialize(val, key) {
if (val === null || typeof val === "undefined") {
return;
}
if (isArray(val)) {
key = `${key}[]`;
} else {
val = [val];
}
forEach(val, function parseValue(v) {
if (isDate(v)) {
v = v.toISOString();
} else if (isObject(v)) {
v = JSON.stringify(v);
}
parts.push(`${encode(key)}=${encode(v)}`);
});
});
serializedParams = parts.join("&");
}
if (serializedParams) {
var hashmarkIndex = url.indexOf("#");
if (hashmarkIndex !== -1) {
url = url.slice(0, hashmarkIndex);
}
url += (url.indexOf("?") === -1 ? "?" : "&") + serializedParams;
}
return url;
}
// Copied from axios/lib/core/buildFullPath.js
function buildFullPath(baseURL, requestedURL) {
if (baseURL && !isAbsoluteURL(requestedURL)) {
return combineURLs(baseURL, requestedURL);
}
return requestedURL;
}
// Copied from axios/lib/utils.js
function isUndefined(val) {
return typeof val === "undefined";
}
function isObject(val) {
return val !== null && typeof val === "object";
}
function isDate(val) {
return toString.call(val) === "[object Date]";
}
function isURLSearchParams(val) {
return toString.call(val) === "[object URLSearchParams]";
}
function isArray(val) {
return Array.isArray(val);
}
function forEach(obj, fn) {
// Don't bother if no value provided
if (obj === null || typeof obj === "undefined") {
return;
}
// Force an array if not already something iterable
if (typeof obj !== "object") {
obj = [obj];
}
if (isArray(obj)) {
// Iterate over array values
for (var i = 0, l = obj.length; i < l; i++) {
fn.call(null, obj[i], i, obj);
}
} else {
// Iterate over object keys
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
fn.call(null, obj[key], key, obj);
}
}
}
}
function isFormData(val) {
return toString.call(val) === "[object FormData]";
}
// TODO this needs to be fixed to run in newer browser-like environments
// https://github.com/vespaiach/axios-fetch-adapter/issues/20#issue-1396365322
function isStandardBrowserEnv() {
if (
typeof navigator !== "undefined" &&
// eslint-disable-next-line no-undef
(navigator.product === "ReactNative" ||
// eslint-disable-next-line no-undef
navigator.product === "NativeScript" ||
// eslint-disable-next-line no-undef
navigator.product === "NS")
) {
return false;
}
return typeof window !== "undefined" && typeof document !== "undefined";
}
/**
* - Create a request object
* - Get response body
* - Check if timeout
*/
export default async function fetchAdapter(config) {
const request = createRequest(config);
const data = await getResponse(request, config);
return new Promise((resolve, reject) => {
if (data instanceof Error) {
reject(data);
} else {
// eslint-disable-next-line no-unused-expressions
Object.prototype.toString.call(config.settle) === "[object Function]"
? config.settle(resolve, reject, data)
: settle(resolve, reject, data);
}
});
}
/**
* Fetch API stage two is to get response body. This funtion tries to retrieve
* response body based on response's type
*/
async function getResponse(request, config) {
let stageOne;
try {
stageOne = await fetch(request);
} catch (e) {
if (e && e.name === "AbortError") {
return createError("Request aborted", config, "ECONNABORTED", request);
}
if (e && e.name === "TimeoutError") {
return createError("Request timeout", config, "ECONNABORTED", request);
}
return createError("Network Error", config, "ERR_NETWORK", request);
}
const headers = {};
stageOne.headers.forEach((value, key) => {
headers[key] = value;
});
const response = {
ok: stageOne.ok,
status: stageOne.status,
statusText: stageOne.statusText,
headers,
config,
request,
};
if (stageOne.status >= 200 && stageOne.status !== 204) {
if (config.responseType === "stream") {
const contentType = stageOne.headers.get("content-type");
if (!contentType?.startsWith(EventStreamContentType)) {
// If the content-type is not stream, response is most likely an error
if (stageOne.status >= 400) {
// If the error is a JSON, parse it. Otherwise, return as text
if (contentType?.startsWith("application/json")) {
response.data = await stageOne.json();
return response;
} else {
response.data = await stageOne.text();
return response;
}
}
// If the non-stream response is also not an error, throw
throw new Error(
`Expected content-type to be ${EventStreamContentType}, Actual: ${contentType}`
);
}
await getBytes(stageOne.body, getLines(getMessages(config.onmessage)));
} else {
switch (config.responseType) {
case "arraybuffer":
response.data = await stageOne.arrayBuffer();
break;
case "blob":
response.data = await stageOne.blob();
break;
case "json":
response.data = await stageOne.json();
break;
case "formData":
response.data = await stageOne.formData();
break;
default:
response.data = await stageOne.text();
break;
}
}
}
return response;
}
/**
* This function will create a Request object based on configuration's axios
*/
function createRequest(config) {
const headers = new Headers(config.headers);
// HTTP basic authentication
if (config.auth) {
const username = config.auth.username || "";
const password = config.auth.password
? decodeURI(encodeURIComponent(config.auth.password))
: "";
headers.set("Authorization", `Basic ${btoa(`${username}:${password}`)}`);
}
const method = config.method.toUpperCase();
const options = {
headers,
method,
};
if (method !== "GET" && method !== "HEAD") {
options.body = config.data;
// In these cases the browser will automatically set the correct Content-Type,
// but only if that header hasn't been set yet. So that's why we're deleting it.
if (isFormData(options.body) && isStandardBrowserEnv()) {
headers.delete("Content-Type");
}
}
// Some `fetch` implementations will override the Content-Type to text/plain
// when body is a string.
// See https://github.com/langchain-ai/langchainjs/issues/1010
if (typeof options.body === "string") {
options.body = new TextEncoder().encode(options.body);
}
if (config.mode) {
options.mode = config.mode;
}
if (config.cache) {
options.cache = config.cache;
}
if (config.integrity) {
options.integrity = config.integrity;
}
if (config.redirect) {
options.redirect = config.redirect;
}
if (config.referrer) {
options.referrer = config.referrer;
}
if (config.timeout && config.timeout > 0) {
options.signal = AbortSignal.timeout(config.timeout);
}
if (config.signal) {
// this overrides the timeout signal if both are set
options.signal = config.signal;
}
// This config is similar to XHRβs withCredentials flag, but with three available values instead of two.
// So if withCredentials is not set, default value 'same-origin' will be used
if (!isUndefined(config.withCredentials)) {
options.credentials = config.withCredentials ? "include" : "omit";
}
// for streaming
if (config.responseType === "stream") {
options.headers.set("Accept", EventStreamContentType);
}
const fullPath = buildFullPath(config.baseURL, config.url);
const url = buildURL(fullPath, config.params, config.paramsSerializer);
// Expected browser to throw error if there is any wrong configuration value
return new Request(url, options);
}
/**
* Note:
*
* From version >= 0.27.0, createError function is replaced by AxiosError class.
* So I copy the old createError function here for backward compatible.
*
*
*
* Create an Error with the specified message, config, error code, request and response.
*
* @param {string} message The error message.
* @param {Object} config The config.
* @param {string} [code] The error code (for example, 'ECONNABORTED').
* @param {Object} [request] The request.
* @param {Object} [response] The response.
* @returns {Error} The created error.
*/
function createError(message, config, code, request, response) {
if (axios.AxiosError && typeof axios.AxiosError === "function") {
return new axios.AxiosError(
message,
axios.AxiosError[code],
config,
request,
response
);
}
const error = new Error(message);
return enhanceError(error, config, code, request, response);
}
/**
*
* Note:
*
* This function is for backward compatible.
*
*
* Update an Error with the specified config, error code, and response.
*
* @param {Error} error The error to update.
* @param {Object} config The config.
* @param {string} [code] The error code (for example, 'ECONNABORTED').
* @param {Object} [request] The request.
* @param {Object} [response] The response.
* @returns {Error} The error.
*/
function enhanceError(error, config, code, request, response) {
error.config = config;
if (code) {
error.code = code;
}
error.request = request;
error.response = response;
error.isAxiosError = true;
error.toJSON = function toJSON() {
return {
// Standard
message: this.message,
name: this.name,
// Microsoft
description: this.description,
number: this.number,
// Mozilla
fileName: this.fileName,
lineNumber: this.lineNumber,
columnNumber: this.columnNumber,
stack: this.stack,
// Axios
config: this.config,
code: this.code,
status:
this.response && this.response.status ? this.response.status : null,
};
};
return error;
}