From f61b8441985dc53bdc6828ed93a831945700a9e5 Mon Sep 17 00:00:00 2001 From: El Jeffo Date: Wed, 4 Aug 2021 21:14:02 +0900 Subject: [PATCH 1/2] Avoid changing global axios.defaults.transformResponse --- src/apiProvider.ts | 23 +++++++++++------------ src/proxyProvider.ts | 13 ++++++------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/apiProvider.ts b/src/apiProvider.ts index 5e3fbc7b..0bee9a7c 100644 --- a/src/apiProvider.ts +++ b/src/apiProvider.ts @@ -22,11 +22,17 @@ export class ApiProvider implements IApiProvider { * @param url the URL of the Elrond Api * @param config axios request config options */ - constructor(url: string, config?: AxiosRequestConfig) { - this.url = url; - this.config = config || { - timeout: 1000, - }; + constructor(url: string, config?: AxiosRequestConfig) { + this.url = url; + // See: https://github.com/axios/axios/issues/983 + this.config = config || { + timeout: 1000, + transformResponse: [ + function(data) { + return JSONbig.parse(data); + }, + ], + }; } /** @@ -90,10 +96,3 @@ export class ApiProvider implements IApiProvider { throw new errors.ErrApiProviderGet(resourceUrl, originalErrorMessage, error); } } - -// See: https://github.com/axios/axios/issues/983 -axios.defaults.transformResponse = [ - function (data) { - return JSONbig.parse(data); - }, -]; diff --git a/src/proxyProvider.ts b/src/proxyProvider.ts index 98166076..83a3b772 100644 --- a/src/proxyProvider.ts +++ b/src/proxyProvider.ts @@ -27,8 +27,14 @@ export class ProxyProvider implements IProvider { */ constructor(url: string, config?: AxiosRequestConfig) { this.url = url; + // See: https://github.com/axios/axios/issues/983 this.config = config || { timeout: 1000, + transformResponse: [ + function(data) { + return JSONbig.parse(data); + }, + ], }; } @@ -199,10 +205,3 @@ export class ProxyProvider implements IProvider { throw new errors.ErrApiProviderGet(resourceUrl, originalErrorMessage, error); } } - -// See: https://github.com/axios/axios/issues/983 -axios.defaults.transformResponse = [ - function (data) { - return JSONbig.parse(data); - }, -]; From 24cb5fab0434ba825e218e222580cc624a9fffd9 Mon Sep 17 00:00:00 2001 From: El Jeffo Date: Thu, 19 Aug 2021 00:27:51 +0000 Subject: [PATCH 2/2] Move comment to transformResponse; Use Object.assign to merge config --- src/apiProvider.ts | 10 +++++----- src/proxyProvider.ts | 18 +++++++++--------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/apiProvider.ts b/src/apiProvider.ts index 0bee9a7c..1d52e1be 100644 --- a/src/apiProvider.ts +++ b/src/apiProvider.ts @@ -22,17 +22,17 @@ export class ApiProvider implements IApiProvider { * @param url the URL of the Elrond Api * @param config axios request config options */ - constructor(url: string, config?: AxiosRequestConfig) { + constructor(url: string, config?: AxiosRequestConfig) { this.url = url; - // See: https://github.com/axios/axios/issues/983 - this.config = config || { + this.config = Object.assign({}, config, { timeout: 1000, + // See: https://github.com/axios/axios/issues/983 regarding transformResponse transformResponse: [ - function(data) { + function(data: any) { return JSONbig.parse(data); }, ], - }; + }); } /** diff --git a/src/proxyProvider.ts b/src/proxyProvider.ts index 83a3b772..7f097d6c 100644 --- a/src/proxyProvider.ts +++ b/src/proxyProvider.ts @@ -27,15 +27,15 @@ export class ProxyProvider implements IProvider { */ constructor(url: string, config?: AxiosRequestConfig) { this.url = url; - // See: https://github.com/axios/axios/issues/983 - this.config = config || { - timeout: 1000, - transformResponse: [ - function(data) { - return JSONbig.parse(data); - }, - ], - }; + this.config = Object.assign({}, config, { + timeout: 1000, + // See: https://github.com/axios/axios/issues/983 regarding transformResponse + transformResponse: [ + function(data: any) { + return JSONbig.parse(data); + }, + ], + }); } /**