@taquito/http-utils 17.4.0 → 17.5.0-beta-RC.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.
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.HttpResponseError = exports.HttpRequestFailed = void 0;
3
+ exports.HttpTimeoutError = exports.HttpResponseError = exports.HttpRequestFailed = void 0;
4
4
  const core_1 = require("@taquito/core");
5
5
  /**
6
6
  * @category Error
@@ -29,7 +29,21 @@ class HttpResponseError extends core_1.NetworkError {
29
29
  this.statusText = statusText;
30
30
  this.body = body;
31
31
  this.url = url;
32
- this.name = 'HttpResponse';
32
+ this.name = 'HttpResponseError';
33
33
  }
34
34
  }
35
35
  exports.HttpResponseError = HttpResponseError;
36
+ /**
37
+ * @category Error
38
+ * @description Error
39
+ */
40
+ class HttpTimeoutError extends core_1.NetworkError {
41
+ constructor(timeout, url) {
42
+ super();
43
+ this.timeout = timeout;
44
+ this.url = url;
45
+ this.name = 'HttpTimeoutError';
46
+ this.message = `HTTP request timeout of ${timeout}ms exceeded`;
47
+ }
48
+ }
49
+ exports.HttpTimeoutError = HttpTimeoutError;
@@ -28,23 +28,22 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
28
28
  };
29
29
  var _a;
30
30
  Object.defineProperty(exports, "__esModule", { value: true });
31
- exports.HttpBackend = exports.HttpResponseError = exports.HttpRequestFailed = exports.VERSION = void 0;
32
- const fetch_adapter_1 = require("./fetch-adapter");
33
- const axios_1 = require("axios");
34
- const errors_1 = require("./errors");
31
+ exports.HttpBackend = exports.HttpTimeoutError = exports.HttpResponseError = exports.HttpRequestFailed = exports.VERSION = void 0;
32
+ let fetch = globalThis === null || globalThis === void 0 ? void 0 : globalThis.fetch;
33
+ // Will only use browser fetch if we are in a browser environment,
34
+ // default to the more stable node-fetch otherwise
35
35
  const isNode = typeof process !== 'undefined' && !!((_a = process === null || process === void 0 ? void 0 : process.versions) === null || _a === void 0 ? void 0 : _a.node);
36
- const adapter = isNode ? undefined : fetch_adapter_1.default;
36
+ if (isNode) {
37
+ fetch = require('node-fetch');
38
+ }
39
+ const errors_1 = require("./errors");
37
40
  __exportStar(require("./status_code"), exports);
38
41
  var version_1 = require("./version");
39
42
  Object.defineProperty(exports, "VERSION", { enumerable: true, get: function () { return version_1.VERSION; } });
40
43
  var errors_2 = require("./errors");
41
44
  Object.defineProperty(exports, "HttpRequestFailed", { enumerable: true, get: function () { return errors_2.HttpRequestFailed; } });
42
45
  Object.defineProperty(exports, "HttpResponseError", { enumerable: true, get: function () { return errors_2.HttpResponseError; } });
43
- var ResponseType;
44
- (function (ResponseType) {
45
- ResponseType["TEXT"] = "text";
46
- ResponseType["JSON"] = "json";
47
- })(ResponseType || (ResponseType = {}));
46
+ Object.defineProperty(exports, "HttpTimeoutError", { enumerable: true, get: function () { return errors_2.HttpTimeoutError; } });
48
47
  class HttpBackend {
49
48
  constructor(timeout = 30000) {
50
49
  this.timeout = timeout;
@@ -86,51 +85,55 @@ class HttpBackend {
86
85
  /**
87
86
  *
88
87
  * @param options contains options to be passed for the HTTP request (url, method and timeout)
89
- * @throws {@link HttpRequestFailed} | {@link HttpResponseError}
88
+ * @throws {@link HttpRequestFailed} | {@link HttpResponseError} | {@link HttpTimeoutError}
90
89
  */
91
90
  createRequest({ url, method, timeout = this.timeout, query, headers = {}, json = true }, data) {
92
91
  return __awaiter(this, void 0, void 0, function* () {
92
+ // Serializes query params
93
93
  const urlWithQuery = url + this.serialize(query);
94
- let resType;
95
- let transformResponse = undefined;
94
+ // Adds default header entry if there aren't any Content-Type header
96
95
  if (!headers['Content-Type']) {
97
96
  headers['Content-Type'] = 'application/json';
98
97
  }
99
- if (!json) {
100
- resType = ResponseType.TEXT;
101
- transformResponse = [(v) => v];
102
- }
103
- else {
104
- resType = ResponseType.JSON;
105
- }
98
+ // Creates a new AbortController instance to handle timeouts
99
+ const controller = new AbortController();
100
+ const t = setTimeout(() => controller.abort(), timeout);
106
101
  try {
107
- const response = yield axios_1.default.request({
108
- url: urlWithQuery,
109
- method: method !== null && method !== void 0 ? method : 'GET',
110
- headers: headers,
111
- responseType: resType,
112
- transformResponse,
113
- timeout: timeout,
114
- data: data,
115
- adapter,
102
+ const response = yield fetch(urlWithQuery, {
103
+ method,
104
+ headers,
105
+ body: JSON.stringify(data),
106
+ signal: controller.signal,
116
107
  });
117
- return response.data;
108
+ if (typeof response === 'undefined') {
109
+ throw new Error('Response is undefined');
110
+ }
111
+ // Handle responses with status code >= 400
112
+ if (response.status >= 400) {
113
+ const errorData = yield response.text();
114
+ throw new errors_1.HttpResponseError(`Http error response: (${response.status}) ${errorData}`, response.status, response.statusText, errorData, urlWithQuery);
115
+ }
116
+ if (json) {
117
+ return response.json();
118
+ }
119
+ else {
120
+ return response.text();
121
+ }
118
122
  }
119
- catch (err) {
120
- if ((axios_1.default.isAxiosError(err) && err.response) || (!isNode && err.response)) {
121
- let errorData;
122
- if (typeof err.response.data === 'object') {
123
- errorData = JSON.stringify(err.response.data);
124
- }
125
- else {
126
- errorData = err.response.data;
127
- }
128
- throw new errors_1.HttpResponseError(`Http error response: (${err.response.status}) ${errorData}`, err.response.status, err.response.statusText, errorData, urlWithQuery);
123
+ catch (e) {
124
+ if (e instanceof Error && e.name === 'AbortError') {
125
+ throw new errors_1.HttpTimeoutError(timeout, urlWithQuery);
126
+ }
127
+ else if (e instanceof errors_1.HttpResponseError) {
128
+ throw e;
129
129
  }
130
130
  else {
131
- throw new errors_1.HttpRequestFailed(String(method), urlWithQuery, err);
131
+ throw new errors_1.HttpRequestFailed(String(method), urlWithQuery, e);
132
132
  }
133
133
  }
134
+ finally {
135
+ clearTimeout(t);
136
+ }
134
137
  });
135
138
  }
136
139
  }
@@ -3,6 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.VERSION = void 0;
4
4
  // IMPORTANT: THIS FILE IS AUTO GENERATED! DO NOT MANUALLY EDIT OR CHECKIN!
5
5
  exports.VERSION = {
6
- "commitHash": "a908ab176a8c52c025fd43e7acd452415396f54e",
7
- "version": "17.4.0"
6
+ "commitHash": "f6fd420c519d8d104ce768b53626f6033ecf4cd3",
7
+ "version": "17.5.0-beta-RC.0"
8
8
  };
@@ -1,4 +1,3 @@
1
- import axios from 'axios';
2
1
  import { NetworkError } from '@taquito/core';
3
2
 
4
3
  /******************************************************************************
@@ -33,223 +32,6 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
33
32
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
34
33
  };
35
34
 
36
- /* eslint-disable @typescript-eslint/no-var-requires */
37
- /* eslint-disable @typescript-eslint/no-explicit-any */
38
- const settle = require('axios/lib/core/settle');
39
- const buildURL = require('axios/lib/helpers/buildURL');
40
- const buildFullPath = require('axios/lib/core/buildFullPath');
41
- const { isUndefined, isStandardBrowserEnv, isFormData } = require('axios/lib/utils');
42
- /**
43
- * - Create a request object
44
- * - Get response body
45
- * - Check if timeout
46
- */
47
- function fetchAdapter(config) {
48
- return __awaiter(this, void 0, void 0, function* () {
49
- const request = createRequest(config);
50
- const promiseChain = [getResponse(request, config)];
51
- if (config.timeout && config.timeout > 0) {
52
- promiseChain.push(new Promise((res) => {
53
- setTimeout(() => {
54
- const message = config.timeoutErrorMessage
55
- ? config.timeoutErrorMessage
56
- : 'timeout of ' + config.timeout + 'ms exceeded';
57
- res(createError(message, config, 'ECONNABORTED', request));
58
- }, config.timeout);
59
- }));
60
- }
61
- const data = yield Promise.race(promiseChain);
62
- return new Promise((resolve, reject) => {
63
- if (data instanceof Error) {
64
- reject(data);
65
- }
66
- else {
67
- const c = config;
68
- 'settle' in c && Object.prototype.toString.call(c.settle) === '[object Function]'
69
- ? c.settle(resolve, reject, data)
70
- : settle(resolve, reject, data);
71
- }
72
- });
73
- });
74
- }
75
- /**
76
- * Fetch API stage two is to get response body. This function tries to retrieve
77
- * response body based on response's type
78
- */
79
- function getResponse(request, config) {
80
- return __awaiter(this, void 0, void 0, function* () {
81
- try {
82
- const stageOne = yield fetch(request);
83
- let response = {
84
- ok: stageOne.ok,
85
- status: stageOne.status,
86
- statusText: stageOne.statusText,
87
- headers: new Headers(stageOne.headers),
88
- config: config,
89
- request,
90
- };
91
- if (stageOne.status >= 400) {
92
- return createError('Response Error', config, 'ERR_NETWORK', request, response);
93
- }
94
- response = {
95
- ok: stageOne.ok,
96
- status: stageOne.status,
97
- statusText: stageOne.statusText,
98
- headers: new Headers(stageOne.headers),
99
- config: config,
100
- request,
101
- };
102
- if (stageOne.status >= 200 && stageOne.status !== 204) {
103
- switch (config.responseType) {
104
- case 'arraybuffer':
105
- response.data = yield stageOne.arrayBuffer();
106
- break;
107
- case 'blob':
108
- response.data = yield stageOne.blob();
109
- break;
110
- case 'json':
111
- response.data = yield stageOne.json();
112
- break;
113
- // TODO: the next option does not exist in response type
114
- // case 'formData':
115
- // response.data = await stageOne.formData();
116
- // break;
117
- default:
118
- response.data = yield stageOne.text();
119
- break;
120
- }
121
- }
122
- return response;
123
- }
124
- catch (e) {
125
- return createError('Network Error', config, 'ERR_NETWORK', request);
126
- }
127
- });
128
- }
129
- /**
130
- * This function will create a Request object based on configuration's axios
131
- */
132
- function createRequest(config) {
133
- var _a;
134
- const headers = new Headers(config.headers);
135
- // HTTP basic authentication
136
- if (config.auth) {
137
- const username = config.auth.username || '';
138
- const password = config.auth.password
139
- ? decodeURI(encodeURIComponent(config.auth.password))
140
- : '';
141
- headers.set('Authorization', `Basic ${btoa(username + ':' + password)}`);
142
- }
143
- const method = (_a = config.method) === null || _a === void 0 ? void 0 : _a.toUpperCase();
144
- const options = {
145
- headers: headers,
146
- method,
147
- };
148
- if (method !== 'GET' && method !== 'HEAD') {
149
- options.body = config.data;
150
- // In these cases the browser will automatically set the correct Content-Type,
151
- // but only if that header hasn't been set yet. So that's why we're deleting it.
152
- if (isFormData(options.body) && isStandardBrowserEnv()) {
153
- headers.delete('Content-Type');
154
- }
155
- }
156
- const c = config;
157
- if ('mode' in c) {
158
- options.mode = c.mode;
159
- }
160
- if ('cache' in c) {
161
- options.cache = c.cache;
162
- }
163
- if ('integrity' in c) {
164
- options.integrity = c.integrity;
165
- }
166
- if ('redirect' in c) {
167
- options.redirect = c.redirect;
168
- }
169
- if ('referrer' in c) {
170
- options.referrer = c.referrer;
171
- }
172
- // This config is similar to XHR’s withCredentials flag, but with three available values instead of two.
173
- // So if withCredentials is not set, default value 'same-origin' will be used
174
- if (!isUndefined(c.withCredentials)) {
175
- options.credentials = c.withCredentials ? 'include' : 'omit';
176
- }
177
- const fullPath = buildFullPath(c.baseURL, c.url);
178
- const url = buildURL(fullPath, c.params, c.paramsSerializer);
179
- // Expected browser to throw error if there is any wrong configuration value
180
- return new Request(url, options);
181
- }
182
- /**
183
- * Note:
184
- *
185
- * From version >= 0.27.0, createError function is replaced by AxiosError class.
186
- * So I copy the old createError function here for backward compatible.
187
- *
188
- *
189
- *
190
- * Create an Error with the specified message, config, error code, request and response.
191
- *
192
- * @param {string} message The error message.
193
- * @param {Object} config The config.
194
- * @param {string} [code] The error code (for example, 'ECONNABORTED').
195
- * @param {Object} [request] The request.
196
- * @param {Object} [response] The response.
197
- * @returns {Error} The created error.
198
- */
199
- function createError(message, config, code, request, response) {
200
- // TODO: this code never runs
201
- // if ('AxiosError' in axios && axios.AxiosError && typeof axios.AxiosError === 'function' && isConstructor(axios.AxiosError)) {
202
- // return new axios.AxiosError(message, axios.AxiosError[code], config, request, response);
203
- // }
204
- const error = new Error(message);
205
- return enhanceError(error, config, code, request, response);
206
- }
207
- /**
208
- *
209
- * Note:
210
- *
211
- * This function is for backward compatible.
212
- *
213
- *
214
- * Update an Error with the specified config, error code, and response.
215
- *
216
- * @param {Error} error The error to update.
217
- * @param {Object} config The config.
218
- * @param {string} [code] The error code (for example, 'ECONNABORTED').
219
- * @param {Object} [request] The request.
220
- * @param {Object} [response] The response.
221
- * @returns {Error} The error.
222
- */
223
- function enhanceError(error, config, code, request, response) {
224
- error.config = config;
225
- if (code) {
226
- error.code = code;
227
- }
228
- error.request = request;
229
- error.response = response;
230
- error.isAxiosError = true;
231
- error.toJSON = function toJSON() {
232
- return {
233
- // Standard
234
- message: this.message,
235
- name: this.name,
236
- // Microsoft
237
- description: 'description' in this ? this.description : undefined,
238
- number: 'number' in this ? this.number : undefined,
239
- // Mozilla
240
- fileName: 'fileName' in this ? this.fileName : undefined,
241
- lineNumber: 'lineNumber' in this ? this.lineNumber : undefined,
242
- columnNumber: 'columnNumber' in this ? this.columnNumber : undefined,
243
- stack: this.stack,
244
- // Axios
245
- config: this.config,
246
- code: this.code,
247
- status: this.response && this.response.status ? this.response.status : null,
248
- };
249
- };
250
- return error;
251
- }
252
-
253
35
  /**
254
36
  * @category Error
255
37
  * @description Error that indicates a general failure in making the HTTP request
@@ -276,7 +58,20 @@ class HttpResponseError extends NetworkError {
276
58
  this.statusText = statusText;
277
59
  this.body = body;
278
60
  this.url = url;
279
- this.name = 'HttpResponse';
61
+ this.name = 'HttpResponseError';
62
+ }
63
+ }
64
+ /**
65
+ * @category Error
66
+ * @description Error
67
+ */
68
+ class HttpTimeoutError extends NetworkError {
69
+ constructor(timeout, url) {
70
+ super();
71
+ this.timeout = timeout;
72
+ this.url = url;
73
+ this.name = 'HttpTimeoutError';
74
+ this.message = `HTTP request timeout of ${timeout}ms exceeded`;
280
75
  }
281
76
  }
282
77
 
@@ -602,8 +397,8 @@ var STATUS_CODE;
602
397
 
603
398
  // IMPORTANT: THIS FILE IS AUTO GENERATED! DO NOT MANUALLY EDIT OR CHECKIN!
604
399
  const VERSION = {
605
- "commitHash": "a908ab176a8c52c025fd43e7acd452415396f54e",
606
- "version": "17.4.0"
400
+ "commitHash": "f6fd420c519d8d104ce768b53626f6033ecf4cd3",
401
+ "version": "17.5.0-beta-RC.0"
607
402
  };
608
403
 
609
404
  /**
@@ -611,13 +406,13 @@ const VERSION = {
611
406
  * @module @taquito/http-utils
612
407
  */
613
408
  var _a;
409
+ let fetch = globalThis === null || globalThis === void 0 ? void 0 : globalThis.fetch;
410
+ // Will only use browser fetch if we are in a browser environment,
411
+ // default to the more stable node-fetch otherwise
614
412
  const isNode = typeof process !== 'undefined' && !!((_a = process === null || process === void 0 ? void 0 : process.versions) === null || _a === void 0 ? void 0 : _a.node);
615
- const adapter = isNode ? undefined : fetchAdapter;
616
- var ResponseType;
617
- (function (ResponseType) {
618
- ResponseType["TEXT"] = "text";
619
- ResponseType["JSON"] = "json";
620
- })(ResponseType || (ResponseType = {}));
413
+ if (isNode) {
414
+ fetch = require('node-fetch');
415
+ }
621
416
  class HttpBackend {
622
417
  constructor(timeout = 30000) {
623
418
  this.timeout = timeout;
@@ -659,54 +454,58 @@ class HttpBackend {
659
454
  /**
660
455
  *
661
456
  * @param options contains options to be passed for the HTTP request (url, method and timeout)
662
- * @throws {@link HttpRequestFailed} | {@link HttpResponseError}
457
+ * @throws {@link HttpRequestFailed} | {@link HttpResponseError} | {@link HttpTimeoutError}
663
458
  */
664
459
  createRequest({ url, method, timeout = this.timeout, query, headers = {}, json = true }, data) {
665
460
  return __awaiter(this, void 0, void 0, function* () {
461
+ // Serializes query params
666
462
  const urlWithQuery = url + this.serialize(query);
667
- let resType;
668
- let transformResponse = undefined;
463
+ // Adds default header entry if there aren't any Content-Type header
669
464
  if (!headers['Content-Type']) {
670
465
  headers['Content-Type'] = 'application/json';
671
466
  }
672
- if (!json) {
673
- resType = ResponseType.TEXT;
674
- transformResponse = [(v) => v];
675
- }
676
- else {
677
- resType = ResponseType.JSON;
678
- }
467
+ // Creates a new AbortController instance to handle timeouts
468
+ const controller = new AbortController();
469
+ const t = setTimeout(() => controller.abort(), timeout);
679
470
  try {
680
- const response = yield axios.request({
681
- url: urlWithQuery,
682
- method: method !== null && method !== void 0 ? method : 'GET',
683
- headers: headers,
684
- responseType: resType,
685
- transformResponse,
686
- timeout: timeout,
687
- data: data,
688
- adapter,
471
+ const response = yield fetch(urlWithQuery, {
472
+ method,
473
+ headers,
474
+ body: JSON.stringify(data),
475
+ signal: controller.signal,
689
476
  });
690
- return response.data;
477
+ if (typeof response === 'undefined') {
478
+ throw new Error('Response is undefined');
479
+ }
480
+ // Handle responses with status code >= 400
481
+ if (response.status >= 400) {
482
+ const errorData = yield response.text();
483
+ throw new HttpResponseError(`Http error response: (${response.status}) ${errorData}`, response.status, response.statusText, errorData, urlWithQuery);
484
+ }
485
+ if (json) {
486
+ return response.json();
487
+ }
488
+ else {
489
+ return response.text();
490
+ }
691
491
  }
692
- catch (err) {
693
- if ((axios.isAxiosError(err) && err.response) || (!isNode && err.response)) {
694
- let errorData;
695
- if (typeof err.response.data === 'object') {
696
- errorData = JSON.stringify(err.response.data);
697
- }
698
- else {
699
- errorData = err.response.data;
700
- }
701
- throw new HttpResponseError(`Http error response: (${err.response.status}) ${errorData}`, err.response.status, err.response.statusText, errorData, urlWithQuery);
492
+ catch (e) {
493
+ if (e instanceof Error && e.name === 'AbortError') {
494
+ throw new HttpTimeoutError(timeout, urlWithQuery);
495
+ }
496
+ else if (e instanceof HttpResponseError) {
497
+ throw e;
702
498
  }
703
499
  else {
704
- throw new HttpRequestFailed(String(method), urlWithQuery, err);
500
+ throw new HttpRequestFailed(String(method), urlWithQuery, e);
705
501
  }
706
502
  }
503
+ finally {
504
+ clearTimeout(t);
505
+ }
707
506
  });
708
507
  }
709
508
  }
710
509
 
711
- export { HttpBackend, HttpRequestFailed, HttpResponseError, STATUS_CODE, VERSION };
510
+ export { HttpBackend, HttpRequestFailed, HttpResponseError, HttpTimeoutError, STATUS_CODE, VERSION };
712
511
  //# sourceMappingURL=taquito-http-utils.es6.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"taquito-http-utils.es6.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"taquito-http-utils.es6.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1,8 +1,8 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('axios'), require('@taquito/core')) :
3
- typeof define === 'function' && define.amd ? define(['exports', 'axios', '@taquito/core'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.taquitoHttpUtils = {}, global.axios, global.core));
5
- })(this, (function (exports, axios, core) { 'use strict';
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@taquito/core')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', '@taquito/core'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.taquitoHttpUtils = {}, global.core));
5
+ })(this, (function (exports, core) { 'use strict';
6
6
 
7
7
  /******************************************************************************
8
8
  Copyright (c) Microsoft Corporation.
@@ -36,223 +36,6 @@
36
36
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
37
37
  };
38
38
 
39
- /* eslint-disable @typescript-eslint/no-var-requires */
40
- /* eslint-disable @typescript-eslint/no-explicit-any */
41
- const settle = require('axios/lib/core/settle');
42
- const buildURL = require('axios/lib/helpers/buildURL');
43
- const buildFullPath = require('axios/lib/core/buildFullPath');
44
- const { isUndefined, isStandardBrowserEnv, isFormData } = require('axios/lib/utils');
45
- /**
46
- * - Create a request object
47
- * - Get response body
48
- * - Check if timeout
49
- */
50
- function fetchAdapter(config) {
51
- return __awaiter(this, void 0, void 0, function* () {
52
- const request = createRequest(config);
53
- const promiseChain = [getResponse(request, config)];
54
- if (config.timeout && config.timeout > 0) {
55
- promiseChain.push(new Promise((res) => {
56
- setTimeout(() => {
57
- const message = config.timeoutErrorMessage
58
- ? config.timeoutErrorMessage
59
- : 'timeout of ' + config.timeout + 'ms exceeded';
60
- res(createError(message, config, 'ECONNABORTED', request));
61
- }, config.timeout);
62
- }));
63
- }
64
- const data = yield Promise.race(promiseChain);
65
- return new Promise((resolve, reject) => {
66
- if (data instanceof Error) {
67
- reject(data);
68
- }
69
- else {
70
- const c = config;
71
- 'settle' in c && Object.prototype.toString.call(c.settle) === '[object Function]'
72
- ? c.settle(resolve, reject, data)
73
- : settle(resolve, reject, data);
74
- }
75
- });
76
- });
77
- }
78
- /**
79
- * Fetch API stage two is to get response body. This function tries to retrieve
80
- * response body based on response's type
81
- */
82
- function getResponse(request, config) {
83
- return __awaiter(this, void 0, void 0, function* () {
84
- try {
85
- const stageOne = yield fetch(request);
86
- let response = {
87
- ok: stageOne.ok,
88
- status: stageOne.status,
89
- statusText: stageOne.statusText,
90
- headers: new Headers(stageOne.headers),
91
- config: config,
92
- request,
93
- };
94
- if (stageOne.status >= 400) {
95
- return createError('Response Error', config, 'ERR_NETWORK', request, response);
96
- }
97
- response = {
98
- ok: stageOne.ok,
99
- status: stageOne.status,
100
- statusText: stageOne.statusText,
101
- headers: new Headers(stageOne.headers),
102
- config: config,
103
- request,
104
- };
105
- if (stageOne.status >= 200 && stageOne.status !== 204) {
106
- switch (config.responseType) {
107
- case 'arraybuffer':
108
- response.data = yield stageOne.arrayBuffer();
109
- break;
110
- case 'blob':
111
- response.data = yield stageOne.blob();
112
- break;
113
- case 'json':
114
- response.data = yield stageOne.json();
115
- break;
116
- // TODO: the next option does not exist in response type
117
- // case 'formData':
118
- // response.data = await stageOne.formData();
119
- // break;
120
- default:
121
- response.data = yield stageOne.text();
122
- break;
123
- }
124
- }
125
- return response;
126
- }
127
- catch (e) {
128
- return createError('Network Error', config, 'ERR_NETWORK', request);
129
- }
130
- });
131
- }
132
- /**
133
- * This function will create a Request object based on configuration's axios
134
- */
135
- function createRequest(config) {
136
- var _a;
137
- const headers = new Headers(config.headers);
138
- // HTTP basic authentication
139
- if (config.auth) {
140
- const username = config.auth.username || '';
141
- const password = config.auth.password
142
- ? decodeURI(encodeURIComponent(config.auth.password))
143
- : '';
144
- headers.set('Authorization', `Basic ${btoa(username + ':' + password)}`);
145
- }
146
- const method = (_a = config.method) === null || _a === void 0 ? void 0 : _a.toUpperCase();
147
- const options = {
148
- headers: headers,
149
- method,
150
- };
151
- if (method !== 'GET' && method !== 'HEAD') {
152
- options.body = config.data;
153
- // In these cases the browser will automatically set the correct Content-Type,
154
- // but only if that header hasn't been set yet. So that's why we're deleting it.
155
- if (isFormData(options.body) && isStandardBrowserEnv()) {
156
- headers.delete('Content-Type');
157
- }
158
- }
159
- const c = config;
160
- if ('mode' in c) {
161
- options.mode = c.mode;
162
- }
163
- if ('cache' in c) {
164
- options.cache = c.cache;
165
- }
166
- if ('integrity' in c) {
167
- options.integrity = c.integrity;
168
- }
169
- if ('redirect' in c) {
170
- options.redirect = c.redirect;
171
- }
172
- if ('referrer' in c) {
173
- options.referrer = c.referrer;
174
- }
175
- // This config is similar to XHR’s withCredentials flag, but with three available values instead of two.
176
- // So if withCredentials is not set, default value 'same-origin' will be used
177
- if (!isUndefined(c.withCredentials)) {
178
- options.credentials = c.withCredentials ? 'include' : 'omit';
179
- }
180
- const fullPath = buildFullPath(c.baseURL, c.url);
181
- const url = buildURL(fullPath, c.params, c.paramsSerializer);
182
- // Expected browser to throw error if there is any wrong configuration value
183
- return new Request(url, options);
184
- }
185
- /**
186
- * Note:
187
- *
188
- * From version >= 0.27.0, createError function is replaced by AxiosError class.
189
- * So I copy the old createError function here for backward compatible.
190
- *
191
- *
192
- *
193
- * Create an Error with the specified message, config, error code, request and response.
194
- *
195
- * @param {string} message The error message.
196
- * @param {Object} config The config.
197
- * @param {string} [code] The error code (for example, 'ECONNABORTED').
198
- * @param {Object} [request] The request.
199
- * @param {Object} [response] The response.
200
- * @returns {Error} The created error.
201
- */
202
- function createError(message, config, code, request, response) {
203
- // TODO: this code never runs
204
- // if ('AxiosError' in axios && axios.AxiosError && typeof axios.AxiosError === 'function' && isConstructor(axios.AxiosError)) {
205
- // return new axios.AxiosError(message, axios.AxiosError[code], config, request, response);
206
- // }
207
- const error = new Error(message);
208
- return enhanceError(error, config, code, request, response);
209
- }
210
- /**
211
- *
212
- * Note:
213
- *
214
- * This function is for backward compatible.
215
- *
216
- *
217
- * Update an Error with the specified config, error code, and response.
218
- *
219
- * @param {Error} error The error to update.
220
- * @param {Object} config The config.
221
- * @param {string} [code] The error code (for example, 'ECONNABORTED').
222
- * @param {Object} [request] The request.
223
- * @param {Object} [response] The response.
224
- * @returns {Error} The error.
225
- */
226
- function enhanceError(error, config, code, request, response) {
227
- error.config = config;
228
- if (code) {
229
- error.code = code;
230
- }
231
- error.request = request;
232
- error.response = response;
233
- error.isAxiosError = true;
234
- error.toJSON = function toJSON() {
235
- return {
236
- // Standard
237
- message: this.message,
238
- name: this.name,
239
- // Microsoft
240
- description: 'description' in this ? this.description : undefined,
241
- number: 'number' in this ? this.number : undefined,
242
- // Mozilla
243
- fileName: 'fileName' in this ? this.fileName : undefined,
244
- lineNumber: 'lineNumber' in this ? this.lineNumber : undefined,
245
- columnNumber: 'columnNumber' in this ? this.columnNumber : undefined,
246
- stack: this.stack,
247
- // Axios
248
- config: this.config,
249
- code: this.code,
250
- status: this.response && this.response.status ? this.response.status : null,
251
- };
252
- };
253
- return error;
254
- }
255
-
256
39
  /**
257
40
  * @category Error
258
41
  * @description Error that indicates a general failure in making the HTTP request
@@ -279,7 +62,20 @@
279
62
  this.statusText = statusText;
280
63
  this.body = body;
281
64
  this.url = url;
282
- this.name = 'HttpResponse';
65
+ this.name = 'HttpResponseError';
66
+ }
67
+ }
68
+ /**
69
+ * @category Error
70
+ * @description Error
71
+ */
72
+ class HttpTimeoutError extends core.NetworkError {
73
+ constructor(timeout, url) {
74
+ super();
75
+ this.timeout = timeout;
76
+ this.url = url;
77
+ this.name = 'HttpTimeoutError';
78
+ this.message = `HTTP request timeout of ${timeout}ms exceeded`;
283
79
  }
284
80
  }
285
81
 
@@ -605,8 +401,8 @@
605
401
 
606
402
  // IMPORTANT: THIS FILE IS AUTO GENERATED! DO NOT MANUALLY EDIT OR CHECKIN!
607
403
  const VERSION = {
608
- "commitHash": "a908ab176a8c52c025fd43e7acd452415396f54e",
609
- "version": "17.4.0"
404
+ "commitHash": "f6fd420c519d8d104ce768b53626f6033ecf4cd3",
405
+ "version": "17.5.0-beta-RC.0"
610
406
  };
611
407
 
612
408
  /**
@@ -614,13 +410,13 @@
614
410
  * @module @taquito/http-utils
615
411
  */
616
412
  var _a;
413
+ let fetch = globalThis === null || globalThis === void 0 ? void 0 : globalThis.fetch;
414
+ // Will only use browser fetch if we are in a browser environment,
415
+ // default to the more stable node-fetch otherwise
617
416
  const isNode = typeof process !== 'undefined' && !!((_a = process === null || process === void 0 ? void 0 : process.versions) === null || _a === void 0 ? void 0 : _a.node);
618
- const adapter = isNode ? undefined : fetchAdapter;
619
- var ResponseType;
620
- (function (ResponseType) {
621
- ResponseType["TEXT"] = "text";
622
- ResponseType["JSON"] = "json";
623
- })(ResponseType || (ResponseType = {}));
417
+ if (isNode) {
418
+ fetch = require('node-fetch');
419
+ }
624
420
  class HttpBackend {
625
421
  constructor(timeout = 30000) {
626
422
  this.timeout = timeout;
@@ -662,51 +458,55 @@
662
458
  /**
663
459
  *
664
460
  * @param options contains options to be passed for the HTTP request (url, method and timeout)
665
- * @throws {@link HttpRequestFailed} | {@link HttpResponseError}
461
+ * @throws {@link HttpRequestFailed} | {@link HttpResponseError} | {@link HttpTimeoutError}
666
462
  */
667
463
  createRequest({ url, method, timeout = this.timeout, query, headers = {}, json = true }, data) {
668
464
  return __awaiter(this, void 0, void 0, function* () {
465
+ // Serializes query params
669
466
  const urlWithQuery = url + this.serialize(query);
670
- let resType;
671
- let transformResponse = undefined;
467
+ // Adds default header entry if there aren't any Content-Type header
672
468
  if (!headers['Content-Type']) {
673
469
  headers['Content-Type'] = 'application/json';
674
470
  }
675
- if (!json) {
676
- resType = ResponseType.TEXT;
677
- transformResponse = [(v) => v];
678
- }
679
- else {
680
- resType = ResponseType.JSON;
681
- }
471
+ // Creates a new AbortController instance to handle timeouts
472
+ const controller = new AbortController();
473
+ const t = setTimeout(() => controller.abort(), timeout);
682
474
  try {
683
- const response = yield axios.request({
684
- url: urlWithQuery,
685
- method: method !== null && method !== void 0 ? method : 'GET',
686
- headers: headers,
687
- responseType: resType,
688
- transformResponse,
689
- timeout: timeout,
690
- data: data,
691
- adapter,
475
+ const response = yield fetch(urlWithQuery, {
476
+ method,
477
+ headers,
478
+ body: JSON.stringify(data),
479
+ signal: controller.signal,
692
480
  });
693
- return response.data;
481
+ if (typeof response === 'undefined') {
482
+ throw new Error('Response is undefined');
483
+ }
484
+ // Handle responses with status code >= 400
485
+ if (response.status >= 400) {
486
+ const errorData = yield response.text();
487
+ throw new HttpResponseError(`Http error response: (${response.status}) ${errorData}`, response.status, response.statusText, errorData, urlWithQuery);
488
+ }
489
+ if (json) {
490
+ return response.json();
491
+ }
492
+ else {
493
+ return response.text();
494
+ }
694
495
  }
695
- catch (err) {
696
- if ((axios.isAxiosError(err) && err.response) || (!isNode && err.response)) {
697
- let errorData;
698
- if (typeof err.response.data === 'object') {
699
- errorData = JSON.stringify(err.response.data);
700
- }
701
- else {
702
- errorData = err.response.data;
703
- }
704
- throw new HttpResponseError(`Http error response: (${err.response.status}) ${errorData}`, err.response.status, err.response.statusText, errorData, urlWithQuery);
496
+ catch (e) {
497
+ if (e instanceof Error && e.name === 'AbortError') {
498
+ throw new HttpTimeoutError(timeout, urlWithQuery);
499
+ }
500
+ else if (e instanceof HttpResponseError) {
501
+ throw e;
705
502
  }
706
503
  else {
707
- throw new HttpRequestFailed(String(method), urlWithQuery, err);
504
+ throw new HttpRequestFailed(String(method), urlWithQuery, e);
708
505
  }
709
506
  }
507
+ finally {
508
+ clearTimeout(t);
509
+ }
710
510
  });
711
511
  }
712
512
  }
@@ -714,6 +514,7 @@
714
514
  exports.HttpBackend = HttpBackend;
715
515
  exports.HttpRequestFailed = HttpRequestFailed;
716
516
  exports.HttpResponseError = HttpResponseError;
517
+ exports.HttpTimeoutError = HttpTimeoutError;
717
518
  exports.VERSION = VERSION;
718
519
 
719
520
  }));
@@ -1 +1 @@
1
- {"version":3,"file":"taquito-http-utils.umd.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"taquito-http-utils.umd.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -22,3 +22,12 @@ export declare class HttpResponseError extends NetworkError {
22
22
  readonly url: string;
23
23
  constructor(message: string, status: STATUS_CODE, statusText: string, body: string, url: string);
24
24
  }
25
+ /**
26
+ * @category Error
27
+ * @description Error
28
+ */
29
+ export declare class HttpTimeoutError extends NetworkError {
30
+ readonly timeout: number;
31
+ readonly url: string;
32
+ constructor(timeout: number, url: string);
33
+ }
@@ -4,7 +4,7 @@
4
4
  */
5
5
  export * from './status_code';
6
6
  export { VERSION } from './version';
7
- export { HttpRequestFailed, HttpResponseError } from './errors';
7
+ export { HttpRequestFailed, HttpResponseError, HttpTimeoutError } from './errors';
8
8
  type ObjectType = Record<string, any>;
9
9
  export interface HttpRequestOptions {
10
10
  url: string;
@@ -24,7 +24,7 @@ export declare class HttpBackend {
24
24
  /**
25
25
  *
26
26
  * @param options contains options to be passed for the HTTP request (url, method and timeout)
27
- * @throws {@link HttpRequestFailed} | {@link HttpResponseError}
27
+ * @throws {@link HttpRequestFailed} | {@link HttpResponseError} | {@link HttpTimeoutError}
28
28
  */
29
29
  createRequest<T>({ url, method, timeout, query, headers, json }: HttpRequestOptions, data?: object | string): Promise<T>;
30
30
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taquito/http-utils",
3
- "version": "17.4.0",
3
+ "version": "17.5.0-beta-RC.0",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "tezos"
@@ -58,13 +58,14 @@
58
58
  ]
59
59
  },
60
60
  "dependencies": {
61
- "@taquito/core": "^17.4.0",
62
- "axios": "^0.27.2"
61
+ "@taquito/core": "^17.5.0-beta-RC.0",
62
+ "node-fetch": "^2.7.0"
63
63
  },
64
64
  "devDependencies": {
65
65
  "@types/bluebird": "^3.5.40",
66
66
  "@types/jest": "^29.5.5",
67
67
  "@types/node": "^20",
68
+ "@types/node-fetch": "^2.6.9",
68
69
  "@types/superagent": "^4.1.19",
69
70
  "@typescript-eslint/eslint-plugin": "^6.8.0",
70
71
  "@typescript-eslint/parser": "^6.8.0",
@@ -89,5 +90,5 @@
89
90
  "ts-toolbelt": "^9.6.0",
90
91
  "typescript": "~5.2.2"
91
92
  },
92
- "gitHead": "f7b158c2c93a38d59a113e0cf541960aa25c961d"
93
+ "gitHead": "d2fd61131ece885342262dfd3ef7eeb1381589ee"
93
94
  }
@@ -1,228 +0,0 @@
1
- "use strict";
2
- /* eslint-disable @typescript-eslint/no-var-requires */
3
- /* eslint-disable @typescript-eslint/no-explicit-any */
4
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
5
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
6
- return new (P || (P = Promise))(function (resolve, reject) {
7
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
8
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
9
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
10
- step((generator = generator.apply(thisArg, _arguments || [])).next());
11
- });
12
- };
13
- Object.defineProperty(exports, "__esModule", { value: true });
14
- const settle = require('axios/lib/core/settle');
15
- const buildURL = require('axios/lib/helpers/buildURL');
16
- const buildFullPath = require('axios/lib/core/buildFullPath');
17
- const { isUndefined, isStandardBrowserEnv, isFormData } = require('axios/lib/utils');
18
- /**
19
- * - Create a request object
20
- * - Get response body
21
- * - Check if timeout
22
- */
23
- function fetchAdapter(config) {
24
- return __awaiter(this, void 0, void 0, function* () {
25
- const request = createRequest(config);
26
- const promiseChain = [getResponse(request, config)];
27
- if (config.timeout && config.timeout > 0) {
28
- promiseChain.push(new Promise((res) => {
29
- setTimeout(() => {
30
- const message = config.timeoutErrorMessage
31
- ? config.timeoutErrorMessage
32
- : 'timeout of ' + config.timeout + 'ms exceeded';
33
- res(createError(message, config, 'ECONNABORTED', request));
34
- }, config.timeout);
35
- }));
36
- }
37
- const data = yield Promise.race(promiseChain);
38
- return new Promise((resolve, reject) => {
39
- if (data instanceof Error) {
40
- reject(data);
41
- }
42
- else {
43
- const c = config;
44
- 'settle' in c && Object.prototype.toString.call(c.settle) === '[object Function]'
45
- ? c.settle(resolve, reject, data)
46
- : settle(resolve, reject, data);
47
- }
48
- });
49
- });
50
- }
51
- exports.default = fetchAdapter;
52
- /**
53
- * Fetch API stage two is to get response body. This function tries to retrieve
54
- * response body based on response's type
55
- */
56
- function getResponse(request, config) {
57
- return __awaiter(this, void 0, void 0, function* () {
58
- try {
59
- const stageOne = yield fetch(request);
60
- let response = {
61
- ok: stageOne.ok,
62
- status: stageOne.status,
63
- statusText: stageOne.statusText,
64
- headers: new Headers(stageOne.headers),
65
- config: config,
66
- request,
67
- };
68
- if (stageOne.status >= 400) {
69
- return createError('Response Error', config, 'ERR_NETWORK', request, response);
70
- }
71
- response = {
72
- ok: stageOne.ok,
73
- status: stageOne.status,
74
- statusText: stageOne.statusText,
75
- headers: new Headers(stageOne.headers),
76
- config: config,
77
- request,
78
- };
79
- if (stageOne.status >= 200 && stageOne.status !== 204) {
80
- switch (config.responseType) {
81
- case 'arraybuffer':
82
- response.data = yield stageOne.arrayBuffer();
83
- break;
84
- case 'blob':
85
- response.data = yield stageOne.blob();
86
- break;
87
- case 'json':
88
- response.data = yield stageOne.json();
89
- break;
90
- // TODO: the next option does not exist in response type
91
- // case 'formData':
92
- // response.data = await stageOne.formData();
93
- // break;
94
- default:
95
- response.data = yield stageOne.text();
96
- break;
97
- }
98
- }
99
- return response;
100
- }
101
- catch (e) {
102
- return createError('Network Error', config, 'ERR_NETWORK', request);
103
- }
104
- });
105
- }
106
- /**
107
- * This function will create a Request object based on configuration's axios
108
- */
109
- function createRequest(config) {
110
- var _a;
111
- const headers = new Headers(config.headers);
112
- // HTTP basic authentication
113
- if (config.auth) {
114
- const username = config.auth.username || '';
115
- const password = config.auth.password
116
- ? decodeURI(encodeURIComponent(config.auth.password))
117
- : '';
118
- headers.set('Authorization', `Basic ${btoa(username + ':' + password)}`);
119
- }
120
- const method = (_a = config.method) === null || _a === void 0 ? void 0 : _a.toUpperCase();
121
- const options = {
122
- headers: headers,
123
- method,
124
- };
125
- if (method !== 'GET' && method !== 'HEAD') {
126
- options.body = config.data;
127
- // In these cases the browser will automatically set the correct Content-Type,
128
- // but only if that header hasn't been set yet. So that's why we're deleting it.
129
- if (isFormData(options.body) && isStandardBrowserEnv()) {
130
- headers.delete('Content-Type');
131
- }
132
- }
133
- const c = config;
134
- if ('mode' in c) {
135
- options.mode = c.mode;
136
- }
137
- if ('cache' in c) {
138
- options.cache = c.cache;
139
- }
140
- if ('integrity' in c) {
141
- options.integrity = c.integrity;
142
- }
143
- if ('redirect' in c) {
144
- options.redirect = c.redirect;
145
- }
146
- if ('referrer' in c) {
147
- options.referrer = c.referrer;
148
- }
149
- // This config is similar to XHR’s withCredentials flag, but with three available values instead of two.
150
- // So if withCredentials is not set, default value 'same-origin' will be used
151
- if (!isUndefined(c.withCredentials)) {
152
- options.credentials = c.withCredentials ? 'include' : 'omit';
153
- }
154
- const fullPath = buildFullPath(c.baseURL, c.url);
155
- const url = buildURL(fullPath, c.params, c.paramsSerializer);
156
- // Expected browser to throw error if there is any wrong configuration value
157
- return new Request(url, options);
158
- }
159
- /**
160
- * Note:
161
- *
162
- * From version >= 0.27.0, createError function is replaced by AxiosError class.
163
- * So I copy the old createError function here for backward compatible.
164
- *
165
- *
166
- *
167
- * Create an Error with the specified message, config, error code, request and response.
168
- *
169
- * @param {string} message The error message.
170
- * @param {Object} config The config.
171
- * @param {string} [code] The error code (for example, 'ECONNABORTED').
172
- * @param {Object} [request] The request.
173
- * @param {Object} [response] The response.
174
- * @returns {Error} The created error.
175
- */
176
- function createError(message, config, code, request, response) {
177
- // TODO: this code never runs
178
- // if ('AxiosError' in axios && axios.AxiosError && typeof axios.AxiosError === 'function' && isConstructor(axios.AxiosError)) {
179
- // return new axios.AxiosError(message, axios.AxiosError[code], config, request, response);
180
- // }
181
- const error = new Error(message);
182
- return enhanceError(error, config, code, request, response);
183
- }
184
- /**
185
- *
186
- * Note:
187
- *
188
- * This function is for backward compatible.
189
- *
190
- *
191
- * Update an Error with the specified config, error code, and response.
192
- *
193
- * @param {Error} error The error to update.
194
- * @param {Object} config The config.
195
- * @param {string} [code] The error code (for example, 'ECONNABORTED').
196
- * @param {Object} [request] The request.
197
- * @param {Object} [response] The response.
198
- * @returns {Error} The error.
199
- */
200
- function enhanceError(error, config, code, request, response) {
201
- error.config = config;
202
- if (code) {
203
- error.code = code;
204
- }
205
- error.request = request;
206
- error.response = response;
207
- error.isAxiosError = true;
208
- error.toJSON = function toJSON() {
209
- return {
210
- // Standard
211
- message: this.message,
212
- name: this.name,
213
- // Microsoft
214
- description: 'description' in this ? this.description : undefined,
215
- number: 'number' in this ? this.number : undefined,
216
- // Mozilla
217
- fileName: 'fileName' in this ? this.fileName : undefined,
218
- lineNumber: 'lineNumber' in this ? this.lineNumber : undefined,
219
- columnNumber: 'columnNumber' in this ? this.columnNumber : undefined,
220
- stack: this.stack,
221
- // Axios
222
- config: this.config,
223
- code: this.code,
224
- status: this.response && this.response.status ? this.response.status : null,
225
- };
226
- };
227
- return error;
228
- }
@@ -1,7 +0,0 @@
1
- import { AxiosRequestConfig, AxiosResponse } from 'axios';
2
- /**
3
- * - Create a request object
4
- * - Get response body
5
- * - Check if timeout
6
- */
7
- export default function fetchAdapter(config: AxiosRequestConfig): Promise<AxiosResponse>;