@taquito/http-utils 18.0.0-RC.0 → 19.0.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,8 +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
- //# sourceMappingURL=errors.js.map
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;
@@ -319,5 +319,4 @@ var STATUS_CODE;
319
319
  * to require agreement to Terms of Service before granting full Internet access via a Wi-Fi hotspot).
320
320
  */
321
321
  STATUS_CODE[STATUS_CODE["NETWORK_AUTHENTICATION_REQUIRED"] = 511] = "NETWORK_AUTHENTICATION_REQUIRED";
322
- })(STATUS_CODE = exports.STATUS_CODE || (exports.STATUS_CODE = {}));
323
- //# sourceMappingURL=status_code.js.map
322
+ })(STATUS_CODE || (exports.STATUS_CODE = STATUS_CODE = {}));
@@ -5,7 +5,11 @@
5
5
  */
6
6
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
7
7
  if (k2 === undefined) k2 = k;
8
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
8
+ var desc = Object.getOwnPropertyDescriptor(m, k);
9
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
10
+ desc = { enumerable: true, get: function() { return m[k]; } };
11
+ }
12
+ Object.defineProperty(o, k2, desc);
9
13
  }) : (function(o, m, k, k2) {
10
14
  if (k2 === undefined) k2 = k;
11
15
  o[k2] = m[k];
@@ -24,23 +28,22 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
24
28
  };
25
29
  var _a;
26
30
  Object.defineProperty(exports, "__esModule", { value: true });
27
- exports.HttpBackend = exports.HttpResponseError = exports.HttpRequestFailed = exports.VERSION = void 0;
28
- const fetch_adapter_1 = require("./fetch-adapter");
29
- const axios_1 = require("axios");
30
- 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
31
35
  const isNode = typeof process !== 'undefined' && !!((_a = process === null || process === void 0 ? void 0 : process.versions) === null || _a === void 0 ? void 0 : _a.node);
32
- const adapter = isNode ? undefined : fetch_adapter_1.default;
36
+ if (isNode) {
37
+ fetch = require('node-fetch');
38
+ }
39
+ const errors_1 = require("./errors");
33
40
  __exportStar(require("./status_code"), exports);
34
41
  var version_1 = require("./version");
35
42
  Object.defineProperty(exports, "VERSION", { enumerable: true, get: function () { return version_1.VERSION; } });
36
43
  var errors_2 = require("./errors");
37
44
  Object.defineProperty(exports, "HttpRequestFailed", { enumerable: true, get: function () { return errors_2.HttpRequestFailed; } });
38
45
  Object.defineProperty(exports, "HttpResponseError", { enumerable: true, get: function () { return errors_2.HttpResponseError; } });
39
- var ResponseType;
40
- (function (ResponseType) {
41
- ResponseType["TEXT"] = "text";
42
- ResponseType["JSON"] = "json";
43
- })(ResponseType || (ResponseType = {}));
46
+ Object.defineProperty(exports, "HttpTimeoutError", { enumerable: true, get: function () { return errors_2.HttpTimeoutError; } });
44
47
  class HttpBackend {
45
48
  constructor(timeout = 30000) {
46
49
  this.timeout = timeout;
@@ -82,53 +85,56 @@ class HttpBackend {
82
85
  /**
83
86
  *
84
87
  * @param options contains options to be passed for the HTTP request (url, method and timeout)
85
- * @throws {@link HttpRequestFailed} | {@link HttpResponseError}
88
+ * @throws {@link HttpRequestFailed} | {@link HttpResponseError} | {@link HttpTimeoutError}
86
89
  */
87
90
  createRequest({ url, method, timeout = this.timeout, query, headers = {}, json = true }, data) {
88
91
  return __awaiter(this, void 0, void 0, function* () {
92
+ // Serializes query params
89
93
  const urlWithQuery = url + this.serialize(query);
90
- let resType;
91
- let transformResponse = undefined;
94
+ // Adds default header entry if there aren't any Content-Type header
92
95
  if (!headers['Content-Type']) {
93
96
  headers['Content-Type'] = 'application/json';
94
97
  }
95
- if (!json) {
96
- resType = ResponseType.TEXT;
97
- transformResponse = [(v) => v];
98
- }
99
- else {
100
- resType = ResponseType.JSON;
101
- }
98
+ // Creates a new AbortController instance to handle timeouts
99
+ const controller = new AbortController();
100
+ const t = setTimeout(() => controller.abort(), timeout);
102
101
  try {
103
- const response = yield axios_1.default.request({
104
- url: urlWithQuery,
105
- method: method !== null && method !== void 0 ? method : 'GET',
106
- headers: headers,
107
- responseType: resType,
108
- transformResponse,
109
- timeout: timeout,
110
- data: data,
111
- adapter,
102
+ const response = yield fetch(urlWithQuery, {
103
+ method,
104
+ headers,
105
+ body: JSON.stringify(data),
106
+ signal: controller.signal,
112
107
  });
113
- 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
+ }
114
122
  }
115
- catch (err) {
116
- if ((axios_1.default.isAxiosError(err) && err.response) || (!isNode && err.response)) {
117
- let errorData;
118
- if (typeof err.response.data === 'object') {
119
- errorData = JSON.stringify(err.response.data);
120
- }
121
- else {
122
- errorData = err.response.data;
123
- }
124
- 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;
125
129
  }
126
130
  else {
127
- throw new errors_1.HttpRequestFailed(String(method), urlWithQuery, err);
131
+ throw new errors_1.HttpRequestFailed(String(method), urlWithQuery, e);
128
132
  }
129
133
  }
134
+ finally {
135
+ clearTimeout(t);
136
+ }
130
137
  });
131
138
  }
132
139
  }
133
140
  exports.HttpBackend = HttpBackend;
134
- //# sourceMappingURL=taquito-http-utils.js.map
@@ -3,7 +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": "21f25a09b87809102b0214544d2c5396eeb5872e",
7
- "version": "18.0.0-RC.0"
6
+ "commitHash": "801b9525f497845e778696ccac8df3a7b0f299d8",
7
+ "version": "19.0.0"
8
8
  };
9
- //# sourceMappingURL=version.js.map