@pipedream/platform
Advanced tools
Comparing version 1.3.0 to 1.4.0
@@ -49,4 +49,35 @@ "use strict"; | ||
} | ||
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 default_1(step, config, signConfig) { | ||
async function callAxios(step, config, signConfig) { | ||
cleanObject(config.headers); | ||
@@ -63,31 +94,3 @@ cleanObject(config.params); | ||
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) | ||
@@ -101,7 +104,9 @@ config.headers = {}; | ||
} | ||
const { data } = await axios_1.default(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.returnFullResponse | ||
? response | ||
: response.data; | ||
} | ||
@@ -116,3 +121,2 @@ catch (err) { | ||
} | ||
exports.default = default_1; | ||
function stepExport(step, message, key) { | ||
@@ -135,1 +139,46 @@ message = utils_1.cloneSafe(message); | ||
} | ||
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); | ||
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; | ||
}); | ||
axiosInstance.interceptors.response.use((response) => { | ||
const config = response.config; | ||
if (config.debug) { | ||
stepExport(this, response.data, "debug_response"); | ||
} | ||
return config.returnFullResponse | ||
? response | ||
: response.data; | ||
}, (error) => { | ||
if (error.response) { | ||
convertAxiosError(error); | ||
stepExport(this, error.response, "debug"); | ||
} | ||
throw error; | ||
}); | ||
return axiosInstance; | ||
} | ||
callAxios.create = create; | ||
exports.default = callAxios; |
138
lib/axios.ts
import axios from "axios"; | ||
import { AxiosRequestConfig } from "./index"; | ||
import { AxiosRequestConfig as AxiosConfig } from "axios"; | ||
import * as buildURL from "axios/lib/helpers/buildURL"; | ||
@@ -51,4 +52,39 @@ import * as querystring from "querystring"; | ||
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? | ||
export default async function (step: any, config: AxiosRequestConfig, signConfig?: any) { | ||
async function callAxios(step: any, config: AxiosRequestConfig, signConfig?: any) { | ||
cleanObject(config.headers); | ||
@@ -63,39 +99,10 @@ cleanObject(config.params); | ||
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 { | ||
@@ -105,7 +112,10 @@ if (config.debug) { | ||
} | ||
const { data } = await axios(config); | ||
const response = await axios(config); | ||
if (config.debug) { | ||
stepExport(step, data, "debug_response"); | ||
stepExport(step, response.data, "debug_response"); | ||
} | ||
return data; | ||
return config.returnFullResponse | ||
? response | ||
: response.data; | ||
} catch (err) { | ||
@@ -140,1 +150,57 @@ if (err.response) { | ||
} | ||
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); | ||
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; | ||
}); | ||
axiosInstance.interceptors.response.use((response) => { | ||
const config: AxiosRequestConfig = response.config; | ||
if (config.debug) { | ||
stepExport(this, response.data, "debug_response"); | ||
} | ||
return config.returnFullResponse | ||
? response | ||
: response.data; | ||
}, (error) => { | ||
if (error.response) { | ||
convertAxiosError(error); | ||
stepExport(this, error.response, "debug"); | ||
} | ||
throw error; | ||
}); | ||
return axiosInstance; | ||
} | ||
callAxios.create = create; | ||
export default callAxios; |
@@ -18,4 +18,4 @@ import * as t from "io-ts"; | ||
export { | ||
DEFAULT_POLLING_SOURCE_TIMER_INTERVAL | ||
} from "./constants" | ||
DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, | ||
} from "./constants"; | ||
@@ -196,2 +196,3 @@ const SendPayload = t.union([ | ||
body?: any; | ||
returnFullResponse?: boolean; | ||
} |
{ | ||
"name": "@pipedream/platform", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Pipedream platform globals (typing and runtime type checking)", | ||
@@ -5,0 +5,0 @@ "homepage": "https://pipedream.com", |
41554
1109