From 6c3109100b854c84bf3e11e9960d68151d49cc76 Mon Sep 17 00:00:00 2001 From: Lucas Caresia Date: Wed, 22 Feb 2023 10:18:37 -0300 Subject: [PATCH 01/11] Added all standart params to axios call --- platform/dist/axios.js | 6 +++++- platform/lib/axios.ts | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/platform/dist/axios.js b/platform/dist/axios.js index 3930206e6ce83..158b3cb9fdf90 100644 --- a/platform/dist/axios.js +++ b/platform/dist/axios.js @@ -48,6 +48,7 @@ function oauth1ParamsSerializer(p) { } // XXX warn about mutating config object... or clone? async function default_1(step, config, signConfig) { + const rawConfig = config; cleanObject(config.headers); cleanObject(config.params); if (typeof config.data === "object") { @@ -96,7 +97,10 @@ async function default_1(step, config, signConfig) { if (config.debug) { stepExport(step, config, "debug_config"); } - const { data } = await axios_1.default(config); + const { data } = await axios_1.default({ + ...rawConfig, + ...config, + }); if (config.debug) { stepExport(step, data, "debug_response"); } diff --git a/platform/lib/axios.ts b/platform/lib/axios.ts index 46741b5362728..3046f4b0e36eb 100644 --- a/platform/lib/axios.ts +++ b/platform/lib/axios.ts @@ -50,6 +50,7 @@ function oauth1ParamsSerializer(p: any) { // XXX warn about mutating config object... or clone? export default async function (step: any, config: AxiosRequestConfig, signConfig?: any) { + const rawConfig = config; cleanObject(config.headers); cleanObject(config.params); if (typeof config.data === "object") { @@ -100,7 +101,10 @@ export default async function (step: any, config: AxiosRequestConfig, signConfig if (config.debug) { stepExport(step, config, "debug_config"); } - const { data } = await axios(config); + const { data } = await axios({ + ...rawConfig, + ...config, + }); if (config.debug) { stepExport(step, data, "debug_response"); } From 16c6546c58b25b9480c3f81530243116fb6cdc82 Mon Sep 17 00:00:00 2001 From: Lucas Caresia Date: Tue, 7 Mar 2023 10:02:20 -0300 Subject: [PATCH 02/11] Added method to create an axios instance --- platform/dist/axios.js | 14 +++++++------- platform/dist/index.js | 3 ++- platform/lib/axios.ts | 21 +++++++++++++-------- platform/lib/index.ts | 7 ++++--- 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/platform/dist/axios.js b/platform/dist/axios.js index 158b3cb9fdf90..29c2fb812adc2 100644 --- a/platform/dist/axios.js +++ b/platform/dist/axios.js @@ -1,5 +1,6 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); +exports.createAxiosInstance = void 0; const axios_1 = require("axios"); const buildURL = require("axios/lib/helpers/buildURL"); const querystring = require("querystring"); @@ -48,7 +49,6 @@ function oauth1ParamsSerializer(p) { } // XXX warn about mutating config object... or clone? async function default_1(step, config, signConfig) { - const rawConfig = config; cleanObject(config.headers); cleanObject(config.params); if (typeof config.data === "object") { @@ -97,14 +97,13 @@ async function default_1(step, config, signConfig) { if (config.debug) { stepExport(step, config, "debug_config"); } - const { data } = await axios_1.default({ - ...rawConfig, - ...config, - }); + const response = await axios_1.default(config); if (config.debug) { - stepExport(step, data, "debug_response"); + stepExport(step, response.data, "debug_response"); } - return data; + return config.returnRawResponse + ? response + : response.data; } catch (err) { if (err.response) { @@ -132,3 +131,4 @@ function convertAxiosError(err) { err.message = JSON.stringify(err.response.data); return err; } +exports.createAxiosInstance = axios_1.default.create; diff --git a/platform/dist/index.js b/platform/dist/index.js index 841f05f5329a5..df61b7a889fc2 100644 --- a/platform/dist/index.js +++ b/platform/dist/index.js @@ -1,9 +1,10 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.$sendConfigRuntimeTypeChecker = exports.$send = exports.$end = exports.END_NEEDLE = exports.$event = exports.sendTypeMap = exports.SendConfigSSE = exports.SendConfigSnowflake = exports.SendConfigSQL = exports.SendConfigS3 = exports.SendConfigHTTP = exports.HTTP_METHODS = exports.SendConfigEmit = exports.SendConfigEmit_optional = exports.SendConfigEmit_required = exports.SendConfigEmail = exports.axios = void 0; +exports.$sendConfigRuntimeTypeChecker = exports.$send = exports.$end = exports.END_NEEDLE = exports.$event = exports.sendTypeMap = exports.SendConfigSSE = exports.SendConfigSnowflake = exports.SendConfigSQL = exports.SendConfigS3 = exports.SendConfigHTTP = exports.HTTP_METHODS = exports.SendConfigEmit = exports.SendConfigEmit_optional = exports.SendConfigEmit_required = exports.SendConfigEmail = exports.createAxiosInstance = exports.axios = void 0; const t = require("io-ts"); const axios_1 = require("./axios"); exports.axios = axios_1.default; +Object.defineProperty(exports, "createAxiosInstance", { enumerable: true, get: function () { return axios_1.createAxiosInstance; } }); var utils_1 = require("./utils"); Object.defineProperty(exports, "cloneSafe", { enumerable: true, get: function () { return utils_1.cloneSafe; } }); Object.defineProperty(exports, "jsonStringifySafe", { enumerable: true, get: function () { return utils_1.jsonStringifySafe; } }); diff --git a/platform/lib/axios.ts b/platform/lib/axios.ts index 3046f4b0e36eb..0ce9cf582ba1a 100644 --- a/platform/lib/axios.ts +++ b/platform/lib/axios.ts @@ -5,6 +5,10 @@ import * as querystring from "querystring"; import { cloneSafe } from "./utils"; import { ConfigurationError } from "./errors"; +interface PipedreamAxiosRequestConfig extends AxiosRequestConfig { + returnRawResponse?: boolean; +} + function cleanObject(o: { string: any; }) { for (const k in o || {}) { if (typeof o[k] === "undefined") { @@ -49,8 +53,7 @@ function oauth1ParamsSerializer(p: any) { } // XXX warn about mutating config object... or clone? -export default async function (step: any, config: AxiosRequestConfig, signConfig?: any) { - const rawConfig = config; +export default async function (step: any, config: PipedreamAxiosRequestConfig, signConfig?: any) { cleanObject(config.headers); cleanObject(config.params); if (typeof config.data === "object") { @@ -101,14 +104,14 @@ export default async function (step: any, config: AxiosRequestConfig, signConfig if (config.debug) { stepExport(step, config, "debug_config"); } - const { data } = await axios({ - ...rawConfig, - ...config, - }); + const response = await axios(config); if (config.debug) { - stepExport(step, data, "debug_response"); + stepExport(step, response.data, "debug_response"); } - return data; + + return config.returnRawResponse + ? response + : response.data; } catch (err) { if (err.response) { convertAxiosError(err); @@ -138,3 +141,5 @@ function convertAxiosError(err) { err.message = JSON.stringify(err.response.data); return err; } + +export const createAxiosInstance = axios.create; diff --git a/platform/lib/index.ts b/platform/lib/index.ts index a1c594da20a02..d56706da17889 100644 --- a/platform/lib/index.ts +++ b/platform/lib/index.ts @@ -1,10 +1,11 @@ import * as t from "io-ts"; -import axios from "./axios"; +import axios, { createAxiosInstance } from "./axios"; import { AxiosRequestConfig as AxiosConfig } from "axios"; export { axios, + createAxiosInstance, }; export { cloneSafe, jsonStringifySafe, @@ -15,8 +16,8 @@ export { } from "./errors"; export { - DEFAULT_POLLING_SOURCE_TIMER_INTERVAL -} from "./constants" + DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, +} from "./constants"; const SendPayload = t.union([ t.string, From 07b17125e18b56cd8feb490aaf611b56d76a131a Mon Sep 17 00:00:00 2001 From: Lucas Caresia Date: Tue, 7 Mar 2023 15:40:27 -0300 Subject: [PATCH 03/11] Fixed --- platform/dist/axios.js | 9 ++++----- platform/dist/index.js | 3 +-- platform/lib/axios.ts | 11 ++++------- platform/lib/index.ts | 4 ++-- test.js | 35 +++++++++++++++++++++++++++++++++++ 5 files changed, 46 insertions(+), 16 deletions(-) create mode 100644 test.js diff --git a/platform/dist/axios.js b/platform/dist/axios.js index 29c2fb812adc2..cd450a30f5088 100644 --- a/platform/dist/axios.js +++ b/platform/dist/axios.js @@ -1,6 +1,5 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.createAxiosInstance = void 0; const axios_1 = require("axios"); const buildURL = require("axios/lib/helpers/buildURL"); const querystring = require("querystring"); @@ -48,7 +47,7 @@ function oauth1ParamsSerializer(p) { .replace(/\*/g, "%2A"); } // XXX warn about mutating config object... or clone? -async function default_1(step, config, signConfig) { +async function callAxios(step, config, signConfig) { cleanObject(config.headers); cleanObject(config.params); if (typeof config.data === "object") { @@ -101,7 +100,7 @@ async function default_1(step, config, signConfig) { if (config.debug) { stepExport(step, response.data, "debug_response"); } - return config.returnRawResponse + return config.returnFullResponse ? response : response.data; } @@ -113,7 +112,6 @@ async function default_1(step, config, signConfig) { throw err; } } -exports.default = default_1; function stepExport(step, message, key) { message = utils_1.cloneSafe(message); if (step) { @@ -131,4 +129,5 @@ function convertAxiosError(err) { err.message = JSON.stringify(err.response.data); return err; } -exports.createAxiosInstance = axios_1.default.create; +callAxios.create = axios_1.default.create; +exports.default = callAxios; diff --git a/platform/dist/index.js b/platform/dist/index.js index df61b7a889fc2..841f05f5329a5 100644 --- a/platform/dist/index.js +++ b/platform/dist/index.js @@ -1,10 +1,9 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.$sendConfigRuntimeTypeChecker = exports.$send = exports.$end = exports.END_NEEDLE = exports.$event = exports.sendTypeMap = exports.SendConfigSSE = exports.SendConfigSnowflake = exports.SendConfigSQL = exports.SendConfigS3 = exports.SendConfigHTTP = exports.HTTP_METHODS = exports.SendConfigEmit = exports.SendConfigEmit_optional = exports.SendConfigEmit_required = exports.SendConfigEmail = exports.createAxiosInstance = exports.axios = void 0; +exports.$sendConfigRuntimeTypeChecker = exports.$send = exports.$end = exports.END_NEEDLE = exports.$event = exports.sendTypeMap = exports.SendConfigSSE = exports.SendConfigSnowflake = exports.SendConfigSQL = exports.SendConfigS3 = exports.SendConfigHTTP = exports.HTTP_METHODS = exports.SendConfigEmit = exports.SendConfigEmit_optional = exports.SendConfigEmit_required = exports.SendConfigEmail = exports.axios = void 0; const t = require("io-ts"); const axios_1 = require("./axios"); exports.axios = axios_1.default; -Object.defineProperty(exports, "createAxiosInstance", { enumerable: true, get: function () { return axios_1.createAxiosInstance; } }); var utils_1 = require("./utils"); Object.defineProperty(exports, "cloneSafe", { enumerable: true, get: function () { return utils_1.cloneSafe; } }); Object.defineProperty(exports, "jsonStringifySafe", { enumerable: true, get: function () { return utils_1.jsonStringifySafe; } }); diff --git a/platform/lib/axios.ts b/platform/lib/axios.ts index 0ce9cf582ba1a..14c3c35b15b3e 100644 --- a/platform/lib/axios.ts +++ b/platform/lib/axios.ts @@ -5,10 +5,6 @@ import * as querystring from "querystring"; import { cloneSafe } from "./utils"; import { ConfigurationError } from "./errors"; -interface PipedreamAxiosRequestConfig extends AxiosRequestConfig { - returnRawResponse?: boolean; -} - function cleanObject(o: { string: any; }) { for (const k in o || {}) { if (typeof o[k] === "undefined") { @@ -53,7 +49,7 @@ function oauth1ParamsSerializer(p: any) { } // XXX warn about mutating config object... or clone? -export default async function (step: any, config: PipedreamAxiosRequestConfig, signConfig?: any) { +async function callAxios(step: any, config: AxiosRequestConfig, signConfig?: any) { cleanObject(config.headers); cleanObject(config.params); if (typeof config.data === "object") { @@ -109,7 +105,7 @@ export default async function (step: any, config: PipedreamAxiosRequestConfig, s stepExport(step, response.data, "debug_response"); } - return config.returnRawResponse + return config.returnFullResponse ? response : response.data; } catch (err) { @@ -142,4 +138,5 @@ function convertAxiosError(err) { return err; } -export const createAxiosInstance = axios.create; +callAxios.create = axios.create; +export default callAxios; diff --git a/platform/lib/index.ts b/platform/lib/index.ts index d56706da17889..3c1443bc32baa 100644 --- a/platform/lib/index.ts +++ b/platform/lib/index.ts @@ -1,11 +1,10 @@ import * as t from "io-ts"; -import axios, { createAxiosInstance } from "./axios"; +import axios from "./axios"; import { AxiosRequestConfig as AxiosConfig } from "axios"; export { axios, - createAxiosInstance, }; export { cloneSafe, jsonStringifySafe, @@ -193,4 +192,5 @@ export const $sendConfigRuntimeTypeChecker = (function () { export interface AxiosRequestConfig extends AxiosConfig { debug?: boolean; body?: any; + returnFullResponse?: boolean; } diff --git a/test.js b/test.js new file mode 100644 index 0000000000000..ddfc1e25cafe6 --- /dev/null +++ b/test.js @@ -0,0 +1,35 @@ +const { axios } = require("./platform/dist"); + +async function test1() { + const createdAxios = axios.create({ + baseURL: "https://random-data-api.com/api/v2", + }); + + const response = await createdAxios.get("users"); + + console.log("createdAxios:", response); +} + +async function test2() { + const data = await axios(this, { + url: "https://random-data-api.com/api/v2/users", + returnRawResponse: false, + }); + + console.log("onlyData:", data); +} + +async function test3() { + // const axios = createAxiosInstance({}); + + const response = await axios(this, { + url: "https://random-data-api.com/api/v2/users", + returnFullResponse: true, + }); + + console.log("returnFullResponse:", response); +} + +test1(); +test2(); +test3(); From 7bf47780bb46a24c0eb4c4f2dcce96f61f06dbcf Mon Sep 17 00:00:00 2001 From: Lucas Caresia Date: Mon, 13 Mar 2023 10:13:38 -0300 Subject: [PATCH 04/11] Some fixes, coding yet --- platform/dist/axios.js | 88 +++++++++++++++++++++++------------ platform/lib/axios.ts | 103 ++++++++++++++++++++++++++++------------- 2 files changed, 128 insertions(+), 63 deletions(-) diff --git a/platform/dist/axios.js b/platform/dist/axios.js index cd450a30f5088..a9ecab1847753 100644 --- a/platform/dist/axios.js +++ b/platform/dist/axios.js @@ -46,6 +46,37 @@ function oauth1ParamsSerializer(p) { .replace(/\)/g, "%29") .replace(/\*/g, "%2A"); } +async function getOauthSignature(config, signConfig) { + const { oauthSignerUri, token, } = signConfig; + const { baseURL, url, } = config; + const newUrl = buildURL((baseURL !== null && baseURL !== void 0 ? baseURL : "") + url, config.params, oauth1ParamsSerializer); // build url as axios will + const requestData = { + method: config.method || "get", + url: newUrl, + }; + // the OAuth specification explicitly states that only form-encoded data should be included + let hasContentType = false; + let formEncodedContentType = false; + for (const k in config.headers || {}) { + if (/content-type/i.test(k)) { + hasContentType = true; + formEncodedContentType = config.headers[k] === "application/x-www-form-urlencoded"; + break; + } + } + if (config.data && typeof config.data === "object" && formEncodedContentType) { + requestData.data = config.data; + } + else if (typeof config.data === "string" && (!hasContentType || formEncodedContentType)) { + requestData.data = querystring.parse(config.data); + } + config.paramsSerializer = oauth1ParamsSerializer; + const payload = { + requestData, + token, + }; + return (await axios_1.default.post(oauthSignerUri, payload)).data; +} // XXX warn about mutating config object... or clone? async function callAxios(step, config, signConfig) { cleanObject(config.headers); @@ -59,35 +90,7 @@ async function callAxios(step, config, signConfig) { removeSearchFromUrl(config); // OAuth1 request if (signConfig) { - const { oauthSignerUri, token, } = signConfig; - const { baseURL, url, } = config; - const newUrl = buildURL((baseURL !== null && baseURL !== void 0 ? baseURL : "") + url, config.params, oauth1ParamsSerializer); // build url as axios will - const requestData = { - method: config.method || "get", - url: newUrl, - }; - // the OAuth specification explicitly states that only form-encoded data should be included - let hasContentType = false; - let formEncodedContentType = false; - for (const k in config.headers || {}) { - if (/content-type/i.test(k)) { - hasContentType = true; - formEncodedContentType = config.headers[k] === "application/x-www-form-urlencoded"; - break; - } - } - if (config.data && typeof config.data === "object" && formEncodedContentType) { - requestData.data = config.data; - } - else if (typeof config.data === "string" && (!hasContentType || formEncodedContentType)) { - requestData.data = querystring.parse(config.data); - } - config.paramsSerializer = oauth1ParamsSerializer; - const payload = { - requestData, - token, - }; - const oauthSignature = (await axios_1.default.post(oauthSignerUri, payload)).data; + const oauthSignature = await getOauthSignature(config, signConfig); if (!config.headers) config.headers = {}; config.headers.Authorization = oauthSignature; @@ -129,5 +132,30 @@ function convertAxiosError(err) { err.message = JSON.stringify(err.response.data); return err; } -callAxios.create = axios_1.default.create; +function create(config, signConfig) { + const axiosInstance = axios_1.default.create(config); + axiosInstance.interceptors.request.use(async (config) => { + if (signConfig) { + const oauthSignature = await getOauthSignature(config, signConfig); + if (!config.headers) + config.headers = {}; + config.headers.Authorization = oauthSignature; + } + cleanObject(config.headers); + cleanObject(config.params); + if (typeof config.data === "object") { + cleanObject(config.data); + } + removeSearchFromUrl(config); + return config; + }, (error) => { + if (error.response) { + convertAxiosError(error); + stepExport(this, error.response, "debug"); + } + throw error; + }); + return axiosInstance; +} +callAxios.create = create; exports.default = callAxios; diff --git a/platform/lib/axios.ts b/platform/lib/axios.ts index 14c3c35b15b3e..d699c63209d25 100644 --- a/platform/lib/axios.ts +++ b/platform/lib/axios.ts @@ -1,5 +1,6 @@ import axios from "axios"; import { AxiosRequestConfig } from "./index"; +import { AxiosRequestConfig as AxiosConfig } from "axios"; import * as buildURL from "axios/lib/helpers/buildURL"; import * as querystring from "querystring"; import { cloneSafe } from "./utils"; @@ -48,6 +49,41 @@ function oauth1ParamsSerializer(p: any) { .replace(/\*/g, "%2A"); } +async function getOauthSignature(config: AxiosRequestConfig, signConfig?: any) { + const { + oauthSignerUri, token, + } = signConfig; + const { + baseURL, url, + } = config; + const newUrl: string = buildURL((baseURL ?? "") + url, config.params, oauth1ParamsSerializer); // build url as axios will + const requestData = { + method: config.method || "get", + url: newUrl, + }; + // the OAuth specification explicitly states that only form-encoded data should be included + let hasContentType = false; + let formEncodedContentType = false; + for (const k in config.headers || {}) { + if (/content-type/i.test(k)) { + hasContentType = true; + formEncodedContentType = config.headers[k] === "application/x-www-form-urlencoded"; + break; + } + } + if (config.data && typeof config.data === "object" && formEncodedContentType) { + (requestData as any).data = config.data; + } else if (typeof config.data === "string" && (!hasContentType || formEncodedContentType)) { + (requestData as any).data = querystring.parse(config.data); + } + config.paramsSerializer = oauth1ParamsSerializer; + const payload = { + requestData, + token, + }; + return (await axios.post(oauthSignerUri, payload)).data; +} + // XXX warn about mutating config object... or clone? async function callAxios(step: any, config: AxiosRequestConfig, signConfig?: any) { cleanObject(config.headers); @@ -59,43 +95,14 @@ async function callAxios(step: any, config: AxiosRequestConfig, signConfig?: any throw new ConfigurationError("unexpected body, use only data instead"); } removeSearchFromUrl(config); + // OAuth1 request if (signConfig) { - const { - oauthSignerUri, token, - } = signConfig; - const { - baseURL, url, - } = config; - const newUrl: string = buildURL((baseURL ?? "") + url, config.params, oauth1ParamsSerializer); // build url as axios will - const requestData = { - method: config.method || "get", - url: newUrl, - }; - // the OAuth specification explicitly states that only form-encoded data should be included - let hasContentType = false; - let formEncodedContentType = false; - for (const k in config.headers || {}) { - if (/content-type/i.test(k)) { - hasContentType = true; - formEncodedContentType = config.headers[k] === "application/x-www-form-urlencoded"; - break; - } - } - if (config.data && typeof config.data === "object" && formEncodedContentType) { - (requestData as any).data = config.data; - } else if (typeof config.data === "string" && (!hasContentType || formEncodedContentType)) { - (requestData as any).data = querystring.parse(config.data); - } - config.paramsSerializer = oauth1ParamsSerializer; - const payload = { - requestData, - token, - }; - const oauthSignature = (await axios.post(oauthSignerUri, payload)).data; + const oauthSignature = await getOauthSignature(config, signConfig); if (!config.headers) config.headers = {}; config.headers.Authorization = oauthSignature; } + try { if (config.debug) { stepExport(step, config, "debug_config"); @@ -138,5 +145,35 @@ function convertAxiosError(err) { return err; } -callAxios.create = axios.create; +function create(config?: AxiosConfig, signConfig?: any) { + const axiosInstance = axios.create(config); + + axiosInstance.interceptors.request.use(async (config) => { + if (signConfig) { + const oauthSignature = await getOauthSignature(config, signConfig); + if (!config.headers) config.headers = {}; + config.headers.Authorization = oauthSignature; + } + + cleanObject(config.headers); + cleanObject(config.params); + if (typeof config.data === "object") { + cleanObject(config.data); + } + removeSearchFromUrl(config); + + return config; + }, (error) => { + if (error.response) { + convertAxiosError(error); + stepExport(this, error.response, "debug"); + } + + throw error; + }); + + return axiosInstance; +} + +callAxios.create = create; export default callAxios; From 61df6b7444d95148865107bb0502efaca03806d5 Mon Sep 17 00:00:00 2001 From: Lucas Caresia Date: Mon, 13 Mar 2023 14:40:03 -0300 Subject: [PATCH 05/11] Update platform/lib/axios.ts Co-authored-by: Andrew Chuang --- platform/lib/axios.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platform/lib/axios.ts b/platform/lib/axios.ts index d699c63209d25..c59d975ee550c 100644 --- a/platform/lib/axios.ts +++ b/platform/lib/axios.ts @@ -49,7 +49,7 @@ function oauth1ParamsSerializer(p: any) { .replace(/\*/g, "%2A"); } -async function getOauthSignature(config: AxiosRequestConfig, signConfig?: any) { +async function getOauthSignature(config: AxiosRequestConfig, signConfig: any) { const { oauthSignerUri, token, } = signConfig; From 7a8aa7308dd72f8e42eb9fd4570efa98e8c076e6 Mon Sep 17 00:00:00 2001 From: Lucas Caresia Date: Mon, 13 Mar 2023 14:48:20 -0300 Subject: [PATCH 06/11] Added interceptor to response too --- platform/lib/axios.ts | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/platform/lib/axios.ts b/platform/lib/axios.ts index d699c63209d25..0cfdf37548753 100644 --- a/platform/lib/axios.ts +++ b/platform/lib/axios.ts @@ -145,9 +145,13 @@ function convertAxiosError(err) { return err; } -function create(config?: AxiosConfig, signConfig?: any) { +function create(config?: AxiosRequestConfig, signConfig?: any) { const axiosInstance = axios.create(config); + if (config?.debug) { + stepExport(this, config, "debug_config"); + } + axiosInstance.interceptors.request.use(async (config) => { if (signConfig) { const oauthSignature = await getOauthSignature(config, signConfig); @@ -172,6 +176,21 @@ function create(config?: AxiosConfig, signConfig?: any) { throw error; }); + axiosInstance.interceptors.response.use((response) => { + if (config?.debug) { + stepExport(this, response.data, "debug_response"); + } + + return response; + }, (error) => { + if (error.response) { + convertAxiosError(error); + stepExport(this, error.response, "debug"); + } + + throw error; + }); + return axiosInstance; } From 9cfe6e4a7bdfe40396d428a5bf74c3ddba0a12f7 Mon Sep 17 00:00:00 2001 From: Lucas Caresia Date: Mon, 13 Mar 2023 16:20:34 -0300 Subject: [PATCH 07/11] Some fixes --- platform/dist/axios.js | 16 ++++++++++++++++ platform/lib/axios.ts | 4 +++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/platform/dist/axios.js b/platform/dist/axios.js index a9ecab1847753..ab67a96f22a73 100644 --- a/platform/dist/axios.js +++ b/platform/dist/axios.js @@ -134,6 +134,9 @@ function convertAxiosError(err) { } function create(config, signConfig) { const axiosInstance = axios_1.default.create(config); + if (config === null || config === void 0 ? void 0 : config.debug) { + stepExport(this, config, "debug_config"); + } axiosInstance.interceptors.request.use(async (config) => { if (signConfig) { const oauthSignature = await getOauthSignature(config, signConfig); @@ -155,6 +158,19 @@ function create(config, signConfig) { } throw error; }); + axiosInstance.interceptors.response.use((response) => { + if (config === null || config === void 0 ? void 0 : config.debug) { + stepExport(this, response.data, "debug_response"); + } + return (config === null || config === void 0 ? void 0 : config.returnFullResponse) ? response + : response.data; + }, (error) => { + if (error.response) { + convertAxiosError(error); + stepExport(this, error.response, "debug"); + } + throw error; + }); return axiosInstance; } callAxios.create = create; diff --git a/platform/lib/axios.ts b/platform/lib/axios.ts index cbd659213761f..99710c3de99ab 100644 --- a/platform/lib/axios.ts +++ b/platform/lib/axios.ts @@ -181,7 +181,9 @@ function create(config?: AxiosRequestConfig, signConfig?: any) { stepExport(this, response.data, "debug_response"); } - return response; + return config?.returnFullResponse + ? response + : response.data; }, (error) => { if (error.response) { convertAxiosError(error); From 167e1fb8c2e692ad7ed480a58e14a6a1cc5a75d4 Mon Sep 17 00:00:00 2001 From: Lucas Caresia Date: Mon, 13 Mar 2023 16:53:56 -0300 Subject: [PATCH 08/11] Fixed bug on config --- platform/lib/axios.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/platform/lib/axios.ts b/platform/lib/axios.ts index 99710c3de99ab..18db09e9bb2a3 100644 --- a/platform/lib/axios.ts +++ b/platform/lib/axios.ts @@ -177,6 +177,8 @@ function create(config?: AxiosRequestConfig, signConfig?: any) { }); axiosInstance.interceptors.response.use((response) => { + const config: AxiosRequestConfig = response.config; + if (config?.debug) { stepExport(this, response.data, "debug_response"); } From b957bf65a69e6dca1942e01fa791506088e18afb Mon Sep 17 00:00:00 2001 From: Lucas Caresia Date: Mon, 13 Mar 2023 16:59:02 -0300 Subject: [PATCH 09/11] tipo --- platform/dist/axios.js | 1 + platform/lib/axios.ts | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/platform/dist/axios.js b/platform/dist/axios.js index ab67a96f22a73..987411c7ccfc8 100644 --- a/platform/dist/axios.js +++ b/platform/dist/axios.js @@ -159,6 +159,7 @@ function create(config, signConfig) { throw error; }); axiosInstance.interceptors.response.use((response) => { + const config = response.config; if (config === null || config === void 0 ? void 0 : config.debug) { stepExport(this, response.data, "debug_response"); } diff --git a/platform/lib/axios.ts b/platform/lib/axios.ts index 18db09e9bb2a3..93d4535cf4360 100644 --- a/platform/lib/axios.ts +++ b/platform/lib/axios.ts @@ -178,12 +178,12 @@ function create(config?: AxiosRequestConfig, signConfig?: any) { axiosInstance.interceptors.response.use((response) => { const config: AxiosRequestConfig = response.config; - - if (config?.debug) { + + if (config.debug) { stepExport(this, response.data, "debug_response"); } - return config?.returnFullResponse + return config.returnFullResponse ? response : response.data; }, (error) => { From 81376a6179ef9ea9fc3ec46f911ca7bc93c4397d Mon Sep 17 00:00:00 2001 From: Lucas Caresia Date: Mon, 13 Mar 2023 17:40:51 -0300 Subject: [PATCH 10/11] tipo --- platform/dist/axios.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/platform/dist/axios.js b/platform/dist/axios.js index 987411c7ccfc8..3bc0a9632102e 100644 --- a/platform/dist/axios.js +++ b/platform/dist/axios.js @@ -160,10 +160,11 @@ function create(config, signConfig) { }); axiosInstance.interceptors.response.use((response) => { const config = response.config; - if (config === null || config === void 0 ? void 0 : config.debug) { + if (config.debug) { stepExport(this, response.data, "debug_response"); } - return (config === null || config === void 0 ? void 0 : config.returnFullResponse) ? response + return config.returnFullResponse + ? response : response.data; }, (error) => { if (error.response) { From 0b3e384e22016f25781e795078d9f2b7eb933233 Mon Sep 17 00:00:00 2001 From: Andrew Chuang Date: Mon, 13 Mar 2023 17:43:49 -0300 Subject: [PATCH 11/11] remove test.js --- test.js | 35 ----------------------------------- 1 file changed, 35 deletions(-) delete mode 100644 test.js diff --git a/test.js b/test.js deleted file mode 100644 index ddfc1e25cafe6..0000000000000 --- a/test.js +++ /dev/null @@ -1,35 +0,0 @@ -const { axios } = require("./platform/dist"); - -async function test1() { - const createdAxios = axios.create({ - baseURL: "https://random-data-api.com/api/v2", - }); - - const response = await createdAxios.get("users"); - - console.log("createdAxios:", response); -} - -async function test2() { - const data = await axios(this, { - url: "https://random-data-api.com/api/v2/users", - returnRawResponse: false, - }); - - console.log("onlyData:", data); -} - -async function test3() { - // const axios = createAxiosInstance({}); - - const response = await axios(this, { - url: "https://random-data-api.com/api/v2/users", - returnFullResponse: true, - }); - - console.log("returnFullResponse:", response); -} - -test1(); -test2(); -test3();