@taquito/http-utils
Advanced tools
Comparing version 18.0.0-RC.0 to 19.0.0-beta-RC.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.HttpResponseError = exports.HttpRequestFailed = void 0; | ||
exports.HttpTimeoutError = exports.HttpResponseError = exports.HttpRequestFailed = void 0; | ||
const core_1 = require("@taquito/core"); | ||
@@ -32,6 +32,19 @@ /** | ||
this.url = url; | ||
this.name = 'HttpResponse'; | ||
this.name = 'HttpResponseError'; | ||
} | ||
} | ||
exports.HttpResponseError = HttpResponseError; | ||
//# sourceMappingURL=errors.js.map | ||
/** | ||
* @category Error | ||
* @description Error | ||
*/ | ||
class HttpTimeoutError extends core_1.NetworkError { | ||
constructor(timeout, url) { | ||
super(); | ||
this.timeout = timeout; | ||
this.url = url; | ||
this.name = 'HttpTimeoutError'; | ||
this.message = `HTTP request timeout of ${timeout}ms exceeded`; | ||
} | ||
} | ||
exports.HttpTimeoutError = HttpTimeoutError; |
@@ -322,3 +322,2 @@ "use strict"; | ||
STATUS_CODE[STATUS_CODE["NETWORK_AUTHENTICATION_REQUIRED"] = 511] = "NETWORK_AUTHENTICATION_REQUIRED"; | ||
})(STATUS_CODE = exports.STATUS_CODE || (exports.STATUS_CODE = {})); | ||
//# sourceMappingURL=status_code.js.map | ||
})(STATUS_CODE || (exports.STATUS_CODE = STATUS_CODE = {})); |
@@ -8,3 +8,7 @@ "use strict"; | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
@@ -28,8 +32,11 @@ if (k2 === undefined) k2 = k; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.HttpBackend = exports.HttpResponseError = exports.HttpRequestFailed = exports.VERSION = void 0; | ||
const fetch_adapter_1 = require("./fetch-adapter"); | ||
const axios_1 = require("axios"); | ||
exports.HttpBackend = exports.HttpTimeoutError = exports.HttpResponseError = exports.HttpRequestFailed = exports.VERSION = void 0; | ||
let fetch = globalThis === null || globalThis === void 0 ? void 0 : globalThis.fetch; | ||
// Will only use browser fetch if we are in a browser environment, | ||
// default to the more stable node-fetch otherwise | ||
const isNode = typeof process !== 'undefined' && !!((_a = process === null || process === void 0 ? void 0 : process.versions) === null || _a === void 0 ? void 0 : _a.node); | ||
if (isNode) { | ||
fetch = require('node-fetch'); | ||
} | ||
const errors_1 = require("./errors"); | ||
const isNode = typeof process !== 'undefined' && !!((_a = process === null || process === void 0 ? void 0 : process.versions) === null || _a === void 0 ? void 0 : _a.node); | ||
const adapter = isNode ? undefined : fetch_adapter_1.default; | ||
__exportStar(require("./status_code"), exports); | ||
@@ -41,7 +48,3 @@ var version_1 = require("./version"); | ||
Object.defineProperty(exports, "HttpResponseError", { enumerable: true, get: function () { return errors_2.HttpResponseError; } }); | ||
var ResponseType; | ||
(function (ResponseType) { | ||
ResponseType["TEXT"] = "text"; | ||
ResponseType["JSON"] = "json"; | ||
})(ResponseType || (ResponseType = {})); | ||
Object.defineProperty(exports, "HttpTimeoutError", { enumerable: true, get: function () { return errors_2.HttpTimeoutError; } }); | ||
class HttpBackend { | ||
@@ -88,47 +91,51 @@ constructor(timeout = 30000) { | ||
* @param options contains options to be passed for the HTTP request (url, method and timeout) | ||
* @throws {@link HttpRequestFailed} | {@link HttpResponseError} | ||
* @throws {@link HttpRequestFailed} | {@link HttpResponseError} | {@link HttpTimeoutError} | ||
*/ | ||
createRequest({ url, method, timeout = this.timeout, query, headers = {}, json = true }, data) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
// Serializes query params | ||
const urlWithQuery = url + this.serialize(query); | ||
let resType; | ||
let transformResponse = undefined; | ||
// Adds default header entry if there aren't any Content-Type header | ||
if (!headers['Content-Type']) { | ||
headers['Content-Type'] = 'application/json'; | ||
} | ||
if (!json) { | ||
resType = ResponseType.TEXT; | ||
transformResponse = [(v) => v]; | ||
} | ||
else { | ||
resType = ResponseType.JSON; | ||
} | ||
// Creates a new AbortController instance to handle timeouts | ||
const controller = new AbortController(); | ||
const t = setTimeout(() => controller.abort(), timeout); | ||
try { | ||
const response = yield axios_1.default.request({ | ||
url: urlWithQuery, | ||
method: method !== null && method !== void 0 ? method : 'GET', | ||
headers: headers, | ||
responseType: resType, | ||
transformResponse, | ||
timeout: timeout, | ||
data: data, | ||
adapter, | ||
const response = yield fetch(urlWithQuery, { | ||
method, | ||
headers, | ||
body: JSON.stringify(data), | ||
signal: controller.signal, | ||
}); | ||
return response.data; | ||
if (typeof response === 'undefined') { | ||
throw new Error('Response is undefined'); | ||
} | ||
// Handle responses with status code >= 400 | ||
if (response.status >= 400) { | ||
const errorData = yield response.text(); | ||
throw new errors_1.HttpResponseError(`Http error response: (${response.status}) ${errorData}`, response.status, response.statusText, errorData, urlWithQuery); | ||
} | ||
if (json) { | ||
return response.json(); | ||
} | ||
else { | ||
return response.text(); | ||
} | ||
} | ||
catch (err) { | ||
if ((axios_1.default.isAxiosError(err) && err.response) || (!isNode && err.response)) { | ||
let errorData; | ||
if (typeof err.response.data === 'object') { | ||
errorData = JSON.stringify(err.response.data); | ||
} | ||
else { | ||
errorData = err.response.data; | ||
} | ||
throw new errors_1.HttpResponseError(`Http error response: (${err.response.status}) ${errorData}`, err.response.status, err.response.statusText, errorData, urlWithQuery); | ||
catch (e) { | ||
if (e instanceof Error && e.name === 'AbortError') { | ||
throw new errors_1.HttpTimeoutError(timeout, urlWithQuery); | ||
} | ||
else if (e instanceof errors_1.HttpResponseError) { | ||
throw e; | ||
} | ||
else { | ||
throw new errors_1.HttpRequestFailed(String(method), urlWithQuery, err); | ||
throw new errors_1.HttpRequestFailed(String(method), urlWithQuery, e); | ||
} | ||
} | ||
finally { | ||
clearTimeout(t); | ||
} | ||
}); | ||
@@ -138,2 +145,1 @@ } | ||
exports.HttpBackend = HttpBackend; | ||
//# sourceMappingURL=taquito-http-utils.js.map |
@@ -6,5 +6,4 @@ "use strict"; | ||
exports.VERSION = { | ||
"commitHash": "21f25a09b87809102b0214544d2c5396eeb5872e", | ||
"version": "18.0.0-RC.0" | ||
"commitHash": "bd52c12d05e329e4cf3a81fe55c4778a47879ccd", | ||
"version": "19.0.0-beta-RC.0" | ||
}; | ||
//# sourceMappingURL=version.js.map |
@@ -1,2 +0,1 @@ | ||
import axios from 'axios'; | ||
import { NetworkError } from '@taquito/core'; | ||
@@ -18,3 +17,5 @@ | ||
***************************************************************************** */ | ||
/* global Reflect, Promise, SuppressedError, Symbol */ | ||
function __awaiter(thisArg, _arguments, P, generator) { | ||
@@ -35,219 +36,3 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
const settle = require('axios/lib/core/settle'); | ||
const buildURL = require('axios/lib/helpers/buildURL'); | ||
const buildFullPath = require('axios/lib/core/buildFullPath'); | ||
const { isUndefined, isStandardBrowserEnv, isFormData } = require('axios/lib/utils'); | ||
/** | ||
* - Create a request object | ||
* - Get response body | ||
* - Check if timeout | ||
*/ | ||
function fetchAdapter(config) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const request = createRequest(config); | ||
const promiseChain = [getResponse(request, config)]; | ||
if (config.timeout && config.timeout > 0) { | ||
promiseChain.push(new Promise((res) => { | ||
setTimeout(() => { | ||
const message = config.timeoutErrorMessage | ||
? config.timeoutErrorMessage | ||
: 'timeout of ' + config.timeout + 'ms exceeded'; | ||
res(createError(message, config, 'ECONNABORTED', request)); | ||
}, config.timeout); | ||
})); | ||
} | ||
const data = yield Promise.race(promiseChain); | ||
return new Promise((resolve, reject) => { | ||
if (data instanceof Error) { | ||
reject(data); | ||
} | ||
else { | ||
const c = config; | ||
'settle' in c && Object.prototype.toString.call(c.settle) === '[object Function]' | ||
? c.settle(resolve, reject, data) | ||
: settle(resolve, reject, data); | ||
} | ||
}); | ||
}); | ||
} | ||
/** | ||
* Fetch API stage two is to get response body. This function tries to retrieve | ||
* response body based on response's type | ||
*/ | ||
function getResponse(request, config) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const stageOne = yield fetch(request); | ||
let response = { | ||
ok: stageOne.ok, | ||
status: stageOne.status, | ||
statusText: stageOne.statusText, | ||
headers: new Headers(stageOne.headers), | ||
config: config, | ||
request, | ||
}; | ||
if (stageOne.status >= 400) { | ||
return createError('Response Error', config, 'ERR_NETWORK', request, response); | ||
} | ||
response = { | ||
ok: stageOne.ok, | ||
status: stageOne.status, | ||
statusText: stageOne.statusText, | ||
headers: new Headers(stageOne.headers), | ||
config: config, | ||
request, | ||
}; | ||
if (stageOne.status >= 200 && stageOne.status !== 204) { | ||
switch (config.responseType) { | ||
case 'arraybuffer': | ||
response.data = yield stageOne.arrayBuffer(); | ||
break; | ||
case 'blob': | ||
response.data = yield stageOne.blob(); | ||
break; | ||
case 'json': | ||
response.data = yield stageOne.json(); | ||
break; | ||
// TODO: the next option does not exist in response type | ||
// case 'formData': | ||
// response.data = await stageOne.formData(); | ||
// break; | ||
default: | ||
response.data = yield stageOne.text(); | ||
break; | ||
} | ||
} | ||
return response; | ||
} | ||
catch (e) { | ||
return createError('Network Error', config, 'ERR_NETWORK', request); | ||
} | ||
}); | ||
} | ||
/** | ||
* This function will create a Request object based on configuration's axios | ||
*/ | ||
function createRequest(config) { | ||
var _a; | ||
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 = (_a = config.method) === null || _a === void 0 ? void 0 : _a.toUpperCase(); | ||
const options = { | ||
headers: 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'); | ||
} | ||
} | ||
const c = config; | ||
if ('mode' in c) { | ||
options.mode = c.mode; | ||
} | ||
if ('cache' in c) { | ||
options.cache = c.cache; | ||
} | ||
if ('integrity' in c) { | ||
options.integrity = c.integrity; | ||
} | ||
if ('redirect' in c) { | ||
options.redirect = c.redirect; | ||
} | ||
if ('referrer' in c) { | ||
options.referrer = c.referrer; | ||
} | ||
// 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(c.withCredentials)) { | ||
options.credentials = c.withCredentials ? 'include' : 'omit'; | ||
} | ||
const fullPath = buildFullPath(c.baseURL, c.url); | ||
const url = buildURL(fullPath, c.params, c.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) { | ||
// TODO: this code never runs | ||
// if ('AxiosError' in axios && axios.AxiosError && typeof axios.AxiosError === 'function' && isConstructor(axios.AxiosError)) { | ||
// 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: 'description' in this ? this.description : undefined, | ||
number: 'number' in this ? this.number : undefined, | ||
// Mozilla | ||
fileName: 'fileName' in this ? this.fileName : undefined, | ||
lineNumber: 'lineNumber' in this ? this.lineNumber : undefined, | ||
columnNumber: 'columnNumber' in this ? this.columnNumber : undefined, | ||
stack: this.stack, | ||
// Axios | ||
config: this.config, | ||
code: this.code, | ||
status: this.response && this.response.status ? this.response.status : null, | ||
}; | ||
}; | ||
return error; | ||
} | ||
/** | ||
* @category Error | ||
@@ -278,5 +63,18 @@ * @description Error that indicates a general failure in making the HTTP request | ||
this.url = url; | ||
this.name = 'HttpResponse'; | ||
this.name = 'HttpResponseError'; | ||
} | ||
} | ||
/** | ||
* @category Error | ||
* @description Error | ||
*/ | ||
class HttpTimeoutError extends NetworkError { | ||
constructor(timeout, url) { | ||
super(); | ||
this.timeout = timeout; | ||
this.url = url; | ||
this.name = 'HttpTimeoutError'; | ||
this.message = `HTTP request timeout of ${timeout}ms exceeded`; | ||
} | ||
} | ||
@@ -605,4 +403,4 @@ /** | ||
const VERSION = { | ||
"commitHash": "21f25a09b87809102b0214544d2c5396eeb5872e", | ||
"version": "18.0.0-RC.0" | ||
"commitHash": "bd52c12d05e329e4cf3a81fe55c4778a47879ccd", | ||
"version": "19.0.0-beta-RC.0" | ||
}; | ||
@@ -615,9 +413,9 @@ | ||
var _a; | ||
let fetch = globalThis === null || globalThis === void 0 ? void 0 : globalThis.fetch; | ||
// Will only use browser fetch if we are in a browser environment, | ||
// default to the more stable node-fetch otherwise | ||
const isNode = typeof process !== 'undefined' && !!((_a = process === null || process === void 0 ? void 0 : process.versions) === null || _a === void 0 ? void 0 : _a.node); | ||
const adapter = isNode ? undefined : fetchAdapter; | ||
var ResponseType; | ||
(function (ResponseType) { | ||
ResponseType["TEXT"] = "text"; | ||
ResponseType["JSON"] = "json"; | ||
})(ResponseType || (ResponseType = {})); | ||
if (isNode) { | ||
fetch = require('node-fetch'); | ||
} | ||
class HttpBackend { | ||
@@ -664,47 +462,51 @@ constructor(timeout = 30000) { | ||
* @param options contains options to be passed for the HTTP request (url, method and timeout) | ||
* @throws {@link HttpRequestFailed} | {@link HttpResponseError} | ||
* @throws {@link HttpRequestFailed} | {@link HttpResponseError} | {@link HttpTimeoutError} | ||
*/ | ||
createRequest({ url, method, timeout = this.timeout, query, headers = {}, json = true }, data) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
// Serializes query params | ||
const urlWithQuery = url + this.serialize(query); | ||
let resType; | ||
let transformResponse = undefined; | ||
// Adds default header entry if there aren't any Content-Type header | ||
if (!headers['Content-Type']) { | ||
headers['Content-Type'] = 'application/json'; | ||
} | ||
if (!json) { | ||
resType = ResponseType.TEXT; | ||
transformResponse = [(v) => v]; | ||
} | ||
else { | ||
resType = ResponseType.JSON; | ||
} | ||
// Creates a new AbortController instance to handle timeouts | ||
const controller = new AbortController(); | ||
const t = setTimeout(() => controller.abort(), timeout); | ||
try { | ||
const response = yield axios.request({ | ||
url: urlWithQuery, | ||
method: method !== null && method !== void 0 ? method : 'GET', | ||
headers: headers, | ||
responseType: resType, | ||
transformResponse, | ||
timeout: timeout, | ||
data: data, | ||
adapter, | ||
const response = yield fetch(urlWithQuery, { | ||
method, | ||
headers, | ||
body: JSON.stringify(data), | ||
signal: controller.signal, | ||
}); | ||
return response.data; | ||
if (typeof response === 'undefined') { | ||
throw new Error('Response is undefined'); | ||
} | ||
// Handle responses with status code >= 400 | ||
if (response.status >= 400) { | ||
const errorData = yield response.text(); | ||
throw new HttpResponseError(`Http error response: (${response.status}) ${errorData}`, response.status, response.statusText, errorData, urlWithQuery); | ||
} | ||
if (json) { | ||
return response.json(); | ||
} | ||
else { | ||
return response.text(); | ||
} | ||
} | ||
catch (err) { | ||
if ((axios.isAxiosError(err) && err.response) || (!isNode && err.response)) { | ||
let errorData; | ||
if (typeof err.response.data === 'object') { | ||
errorData = JSON.stringify(err.response.data); | ||
} | ||
else { | ||
errorData = err.response.data; | ||
} | ||
throw new HttpResponseError(`Http error response: (${err.response.status}) ${errorData}`, err.response.status, err.response.statusText, errorData, urlWithQuery); | ||
catch (e) { | ||
if (e instanceof Error && e.name === 'AbortError') { | ||
throw new HttpTimeoutError(timeout, urlWithQuery); | ||
} | ||
else if (e instanceof HttpResponseError) { | ||
throw e; | ||
} | ||
else { | ||
throw new HttpRequestFailed(String(method), urlWithQuery, err); | ||
throw new HttpRequestFailed(String(method), urlWithQuery, e); | ||
} | ||
} | ||
finally { | ||
clearTimeout(t); | ||
} | ||
}); | ||
@@ -714,3 +516,3 @@ } | ||
export { HttpBackend, HttpRequestFailed, HttpResponseError, STATUS_CODE, VERSION }; | ||
export { HttpBackend, HttpRequestFailed, HttpResponseError, HttpTimeoutError, STATUS_CODE, VERSION }; | ||
//# sourceMappingURL=taquito-http-utils.es6.js.map |
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('axios'), require('@taquito/core')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'axios', '@taquito/core'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.taquitoHttpUtils = {}, global.axios, global.core)); | ||
})(this, (function (exports, axios, core) { 'use strict'; | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@taquito/core')) : | ||
typeof define === 'function' && define.amd ? define(['exports', '@taquito/core'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.taquitoHttpUtils = {}, global.core)); | ||
})(this, (function (exports, core) { 'use strict'; | ||
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios); | ||
/****************************************************************************** | ||
@@ -25,3 +21,5 @@ Copyright (c) Microsoft Corporation. | ||
***************************************************************************** */ | ||
/* global Reflect, Promise, SuppressedError, Symbol */ | ||
function __awaiter(thisArg, _arguments, P, generator) { | ||
@@ -42,219 +40,3 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
const settle = require('axios/lib/core/settle'); | ||
const buildURL = require('axios/lib/helpers/buildURL'); | ||
const buildFullPath = require('axios/lib/core/buildFullPath'); | ||
const { isUndefined, isStandardBrowserEnv, isFormData } = require('axios/lib/utils'); | ||
/** | ||
* - Create a request object | ||
* - Get response body | ||
* - Check if timeout | ||
*/ | ||
function fetchAdapter(config) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const request = createRequest(config); | ||
const promiseChain = [getResponse(request, config)]; | ||
if (config.timeout && config.timeout > 0) { | ||
promiseChain.push(new Promise((res) => { | ||
setTimeout(() => { | ||
const message = config.timeoutErrorMessage | ||
? config.timeoutErrorMessage | ||
: 'timeout of ' + config.timeout + 'ms exceeded'; | ||
res(createError(message, config, 'ECONNABORTED', request)); | ||
}, config.timeout); | ||
})); | ||
} | ||
const data = yield Promise.race(promiseChain); | ||
return new Promise((resolve, reject) => { | ||
if (data instanceof Error) { | ||
reject(data); | ||
} | ||
else { | ||
const c = config; | ||
'settle' in c && Object.prototype.toString.call(c.settle) === '[object Function]' | ||
? c.settle(resolve, reject, data) | ||
: settle(resolve, reject, data); | ||
} | ||
}); | ||
}); | ||
} | ||
/** | ||
* Fetch API stage two is to get response body. This function tries to retrieve | ||
* response body based on response's type | ||
*/ | ||
function getResponse(request, config) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const stageOne = yield fetch(request); | ||
let response = { | ||
ok: stageOne.ok, | ||
status: stageOne.status, | ||
statusText: stageOne.statusText, | ||
headers: new Headers(stageOne.headers), | ||
config: config, | ||
request, | ||
}; | ||
if (stageOne.status >= 400) { | ||
return createError('Response Error', config, 'ERR_NETWORK', request, response); | ||
} | ||
response = { | ||
ok: stageOne.ok, | ||
status: stageOne.status, | ||
statusText: stageOne.statusText, | ||
headers: new Headers(stageOne.headers), | ||
config: config, | ||
request, | ||
}; | ||
if (stageOne.status >= 200 && stageOne.status !== 204) { | ||
switch (config.responseType) { | ||
case 'arraybuffer': | ||
response.data = yield stageOne.arrayBuffer(); | ||
break; | ||
case 'blob': | ||
response.data = yield stageOne.blob(); | ||
break; | ||
case 'json': | ||
response.data = yield stageOne.json(); | ||
break; | ||
// TODO: the next option does not exist in response type | ||
// case 'formData': | ||
// response.data = await stageOne.formData(); | ||
// break; | ||
default: | ||
response.data = yield stageOne.text(); | ||
break; | ||
} | ||
} | ||
return response; | ||
} | ||
catch (e) { | ||
return createError('Network Error', config, 'ERR_NETWORK', request); | ||
} | ||
}); | ||
} | ||
/** | ||
* This function will create a Request object based on configuration's axios | ||
*/ | ||
function createRequest(config) { | ||
var _a; | ||
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 = (_a = config.method) === null || _a === void 0 ? void 0 : _a.toUpperCase(); | ||
const options = { | ||
headers: 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'); | ||
} | ||
} | ||
const c = config; | ||
if ('mode' in c) { | ||
options.mode = c.mode; | ||
} | ||
if ('cache' in c) { | ||
options.cache = c.cache; | ||
} | ||
if ('integrity' in c) { | ||
options.integrity = c.integrity; | ||
} | ||
if ('redirect' in c) { | ||
options.redirect = c.redirect; | ||
} | ||
if ('referrer' in c) { | ||
options.referrer = c.referrer; | ||
} | ||
// 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(c.withCredentials)) { | ||
options.credentials = c.withCredentials ? 'include' : 'omit'; | ||
} | ||
const fullPath = buildFullPath(c.baseURL, c.url); | ||
const url = buildURL(fullPath, c.params, c.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) { | ||
// TODO: this code never runs | ||
// if ('AxiosError' in axios && axios.AxiosError && typeof axios.AxiosError === 'function' && isConstructor(axios.AxiosError)) { | ||
// 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: 'description' in this ? this.description : undefined, | ||
number: 'number' in this ? this.number : undefined, | ||
// Mozilla | ||
fileName: 'fileName' in this ? this.fileName : undefined, | ||
lineNumber: 'lineNumber' in this ? this.lineNumber : undefined, | ||
columnNumber: 'columnNumber' in this ? this.columnNumber : undefined, | ||
stack: this.stack, | ||
// Axios | ||
config: this.config, | ||
code: this.code, | ||
status: this.response && this.response.status ? this.response.status : null, | ||
}; | ||
}; | ||
return error; | ||
} | ||
/** | ||
* @category Error | ||
@@ -285,5 +67,18 @@ * @description Error that indicates a general failure in making the HTTP request | ||
this.url = url; | ||
this.name = 'HttpResponse'; | ||
this.name = 'HttpResponseError'; | ||
} | ||
} | ||
/** | ||
* @category Error | ||
* @description Error | ||
*/ | ||
class HttpTimeoutError extends core.NetworkError { | ||
constructor(timeout, url) { | ||
super(); | ||
this.timeout = timeout; | ||
this.url = url; | ||
this.name = 'HttpTimeoutError'; | ||
this.message = `HTTP request timeout of ${timeout}ms exceeded`; | ||
} | ||
} | ||
@@ -612,4 +407,4 @@ /** | ||
const VERSION = { | ||
"commitHash": "21f25a09b87809102b0214544d2c5396eeb5872e", | ||
"version": "18.0.0-RC.0" | ||
"commitHash": "bd52c12d05e329e4cf3a81fe55c4778a47879ccd", | ||
"version": "19.0.0-beta-RC.0" | ||
}; | ||
@@ -622,9 +417,9 @@ | ||
var _a; | ||
let fetch = globalThis === null || globalThis === void 0 ? void 0 : globalThis.fetch; | ||
// Will only use browser fetch if we are in a browser environment, | ||
// default to the more stable node-fetch otherwise | ||
const isNode = typeof process !== 'undefined' && !!((_a = process === null || process === void 0 ? void 0 : process.versions) === null || _a === void 0 ? void 0 : _a.node); | ||
const adapter = isNode ? undefined : fetchAdapter; | ||
var ResponseType; | ||
(function (ResponseType) { | ||
ResponseType["TEXT"] = "text"; | ||
ResponseType["JSON"] = "json"; | ||
})(ResponseType || (ResponseType = {})); | ||
if (isNode) { | ||
fetch = require('node-fetch'); | ||
} | ||
class HttpBackend { | ||
@@ -671,47 +466,51 @@ constructor(timeout = 30000) { | ||
* @param options contains options to be passed for the HTTP request (url, method and timeout) | ||
* @throws {@link HttpRequestFailed} | {@link HttpResponseError} | ||
* @throws {@link HttpRequestFailed} | {@link HttpResponseError} | {@link HttpTimeoutError} | ||
*/ | ||
createRequest({ url, method, timeout = this.timeout, query, headers = {}, json = true }, data) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
// Serializes query params | ||
const urlWithQuery = url + this.serialize(query); | ||
let resType; | ||
let transformResponse = undefined; | ||
// Adds default header entry if there aren't any Content-Type header | ||
if (!headers['Content-Type']) { | ||
headers['Content-Type'] = 'application/json'; | ||
} | ||
if (!json) { | ||
resType = ResponseType.TEXT; | ||
transformResponse = [(v) => v]; | ||
} | ||
else { | ||
resType = ResponseType.JSON; | ||
} | ||
// Creates a new AbortController instance to handle timeouts | ||
const controller = new AbortController(); | ||
const t = setTimeout(() => controller.abort(), timeout); | ||
try { | ||
const response = yield axios__default["default"].request({ | ||
url: urlWithQuery, | ||
method: method !== null && method !== void 0 ? method : 'GET', | ||
headers: headers, | ||
responseType: resType, | ||
transformResponse, | ||
timeout: timeout, | ||
data: data, | ||
adapter, | ||
const response = yield fetch(urlWithQuery, { | ||
method, | ||
headers, | ||
body: JSON.stringify(data), | ||
signal: controller.signal, | ||
}); | ||
return response.data; | ||
if (typeof response === 'undefined') { | ||
throw new Error('Response is undefined'); | ||
} | ||
// Handle responses with status code >= 400 | ||
if (response.status >= 400) { | ||
const errorData = yield response.text(); | ||
throw new HttpResponseError(`Http error response: (${response.status}) ${errorData}`, response.status, response.statusText, errorData, urlWithQuery); | ||
} | ||
if (json) { | ||
return response.json(); | ||
} | ||
else { | ||
return response.text(); | ||
} | ||
} | ||
catch (err) { | ||
if ((axios__default["default"].isAxiosError(err) && err.response) || (!isNode && err.response)) { | ||
let errorData; | ||
if (typeof err.response.data === 'object') { | ||
errorData = JSON.stringify(err.response.data); | ||
} | ||
else { | ||
errorData = err.response.data; | ||
} | ||
throw new HttpResponseError(`Http error response: (${err.response.status}) ${errorData}`, err.response.status, err.response.statusText, errorData, urlWithQuery); | ||
catch (e) { | ||
if (e instanceof Error && e.name === 'AbortError') { | ||
throw new HttpTimeoutError(timeout, urlWithQuery); | ||
} | ||
else if (e instanceof HttpResponseError) { | ||
throw e; | ||
} | ||
else { | ||
throw new HttpRequestFailed(String(method), urlWithQuery, err); | ||
throw new HttpRequestFailed(String(method), urlWithQuery, e); | ||
} | ||
} | ||
finally { | ||
clearTimeout(t); | ||
} | ||
}); | ||
@@ -724,7 +523,6 @@ } | ||
exports.HttpResponseError = HttpResponseError; | ||
exports.HttpTimeoutError = HttpTimeoutError; | ||
exports.VERSION = VERSION; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
})); | ||
//# sourceMappingURL=taquito-http-utils.umd.js.map |
@@ -25,1 +25,10 @@ import { NetworkError } from '@taquito/core'; | ||
} | ||
/** | ||
* @category Error | ||
* @description Error | ||
*/ | ||
export declare class HttpTimeoutError extends NetworkError { | ||
readonly timeout: number; | ||
readonly url: string; | ||
constructor(timeout: number, url: string); | ||
} |
@@ -0,0 +0,0 @@ /** |
@@ -7,4 +7,4 @@ /** | ||
export { VERSION } from './version'; | ||
export { HttpRequestFailed, HttpResponseError } from './errors'; | ||
declare type ObjectType = Record<string, any>; | ||
export { HttpRequestFailed, HttpResponseError, HttpTimeoutError } from './errors'; | ||
type ObjectType = Record<string, any>; | ||
export interface HttpRequestOptions { | ||
@@ -28,5 +28,5 @@ url: string; | ||
* @param options contains options to be passed for the HTTP request (url, method and timeout) | ||
* @throws {@link HttpRequestFailed} | {@link HttpResponseError} | ||
* @throws {@link HttpRequestFailed} | {@link HttpResponseError} | {@link HttpTimeoutError} | ||
*/ | ||
createRequest<T>({ url, method, timeout, query, headers, json }: HttpRequestOptions, data?: object | string): Promise<T>; | ||
} |
@@ -0,0 +0,0 @@ export declare const VERSION: { |
{ | ||
"name": "@taquito/http-utils", | ||
"version": "18.0.0-RC.0", | ||
"version": "19.0.0-beta-RC.0", | ||
"description": "", | ||
@@ -25,3 +25,3 @@ "keywords": [ | ||
"engines": { | ||
"node": ">=16" | ||
"node": ">=18" | ||
}, | ||
@@ -33,4 +33,4 @@ "scripts": { | ||
"version-stamp": "node ../taquito/version-stamping.js", | ||
"build": "tsc --project ./tsconfig.prod.json --module commonjs && rollup -c rollup.config.ts ", | ||
"start": "rollup -c rollup.config.ts -w" | ||
"build": "tsc --project ./tsconfig.prod.json --module commonjs && rollup -c rollup.config.ts --bundleConfigAsCjs", | ||
"start": "rollup -c rollup.config.ts --bundleConfigAsCjs -w" | ||
}, | ||
@@ -40,4 +40,3 @@ "lint-staged": { | ||
"prettier --write", | ||
"eslint --fix", | ||
"git add" | ||
"eslint --fix" | ||
] | ||
@@ -65,35 +64,35 @@ }, | ||
"dependencies": { | ||
"@taquito/core": "^18.0.0-RC.0", | ||
"axios": "0.26.0" | ||
"@taquito/core": "^19.0.0-beta-RC.0", | ||
"node-fetch": "^2.7.0" | ||
}, | ||
"devDependencies": { | ||
"@types/bluebird": "^3.5.36", | ||
"@types/jest": "^26.0.24", | ||
"@types/node": "^16", | ||
"@types/superagent": "^4.1.13", | ||
"@typescript-eslint/eslint-plugin": "^5.28.0", | ||
"@typescript-eslint/parser": "^5.28.0", | ||
"@types/bluebird": "^3.5.40", | ||
"@types/jest": "^29.5.5", | ||
"@types/node": "^20", | ||
"@types/node-fetch": "^2.6.9", | ||
"@types/superagent": "^4.1.19", | ||
"@typescript-eslint/eslint-plugin": "^6.8.0", | ||
"@typescript-eslint/parser": "^6.8.0", | ||
"colors": "^1.4.0", | ||
"coveralls": "^3.1.1", | ||
"cross-env": "^7.0.3", | ||
"eslint": "^8.17.0", | ||
"jest": "^26.6.3", | ||
"jest-config": "^26.6.3", | ||
"lint-staged": "^13.0.1", | ||
"eslint": "^8.51.0", | ||
"jest": "^29.7.0", | ||
"jest-config": "^29.7.0", | ||
"lint-staged": "^14.0.1", | ||
"lodash.camelcase": "^4.3.0", | ||
"prettier": "^2.7.0", | ||
"prettier": "^3.0.3", | ||
"prompt": "^1.3.0", | ||
"replace-in-file": "^6.3.5", | ||
"rimraf": "^3.0.2", | ||
"rollup": "^2.75.6", | ||
"replace-in-file": "^7.0.1", | ||
"rimraf": "^5.0.5", | ||
"rollup": "^4.1.4", | ||
"rollup-plugin-json": "^4.0.0", | ||
"rollup-plugin-sourcemaps": "^0.6.3", | ||
"rollup-plugin-typescript2": "^0.32.1", | ||
"rollup-plugin-typescript2": "^0.36.0", | ||
"shelljs": "^0.8.5", | ||
"ts-jest": "^26.4.4", | ||
"ts-node": "^10.4.0", | ||
"ts-jest": "^29.1.1", | ||
"ts-node": "^10.9.1", | ||
"ts-toolbelt": "^9.6.0", | ||
"typescript": "~4.1.5" | ||
"typescript": "~5.2.2" | ||
}, | ||
"gitHead": "998e588c7c72f45bb8d95bed54152b618aa18ec3" | ||
"gitHead": "959e385ba06d8932866503bb538252a3912acbc9" | ||
} |
@@ -110,3 +110,3 @@ { | ||
"engines": { | ||
"node": ">=16" | ||
"node": ">=18" | ||
}, | ||
@@ -117,3 +117,3 @@ "scripts": { | ||
"prebuild": "rimraf dist", | ||
"build": "tsc --project ./tsconfig.prod.json --module commonjs && rollup -c rollup.config.ts ", | ||
"build": "tsc --project ./tsconfig.prod.json --module commonjs && rollup -c rollup.config.ts", | ||
"start": "rollup -c rollup.config.ts -w" | ||
@@ -124,4 +124,3 @@ }, | ||
"prettier --write", | ||
"tslint --fix", | ||
"git add" | ||
"tslint --fix" | ||
] | ||
@@ -153,3 +152,3 @@ }, | ||
"@types/jest": "^26.0.16", | ||
"@types/node": "^16", | ||
"@types/node": "^18", | ||
"@types/superagent": "^4.1.10", | ||
@@ -169,3 +168,2 @@ "colors": "^1.4.0", | ||
"rollup-plugin-json": "^4.0.0", | ||
"rollup-plugin-sourcemaps": "^0.6.3", | ||
"rollup-plugin-typescript2": "^0.27.3", | ||
@@ -172,0 +170,0 @@ "shelljs": "^0.8.4", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
121897
16
2094
+ Addednode-fetch@^2.7.0
+ Added@taquito/core@19.2.1(transitive)
+ Addedjson-stringify-safe@5.0.1(transitive)
+ Addednode-fetch@2.7.0(transitive)
+ Addedtr46@0.0.3(transitive)
+ Addedwebidl-conversions@3.0.1(transitive)
+ Addedwhatwg-url@5.0.0(transitive)
- Removedaxios@0.26.0
- Removed@taquito/core@18.0.0-RC.0(transitive)
- Removedaxios@0.26.0(transitive)
- Removedfollow-redirects@1.15.9(transitive)