2020-07-30 16:27:27 +00:00
|
|
|
const axios = require("axios");
|
2021-01-24 12:14:13 +00:00
|
|
|
const FormData = require('form-data')
|
|
|
|
const fs = require('fs')
|
2020-07-30 16:27:27 +00:00
|
|
|
|
|
|
|
const METHOD_GET = 'GET'
|
|
|
|
const METHOD_POST = 'POST'
|
|
|
|
|
2021-03-19 16:28:53 +00:00
|
|
|
/**
|
|
|
|
* @param {Object} param0
|
|
|
|
* @param {string} param0.method HTTP Method
|
|
|
|
* @param {{ baseURL: string; timeout: number; headers: { [name: string]: string } }} param0.instanceConfig
|
|
|
|
* @param {string} param0.data Request Body as string, default {}
|
|
|
|
* @param {string} param0.files Map of Request Files (name: absolute path) as JSON String, default: {}
|
|
|
|
* @param {{ username: string; password: string }|undefined} param0.auth Optional HTTP Basic Auth
|
|
|
|
* @param {*} param0.actions
|
|
|
|
* @param {number[]} param0.ignoredCodes Prevent Action to fail if the API response with one of this StatusCodes
|
|
|
|
* @param {boolean} param0.preventFailureOnNoResponse Prevent Action to fail if the API respond without Response
|
|
|
|
* @param {boolean} param0.escapeData Escape unescaped JSON content in data
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
const request = async({ method, instanceConfig, data, files, auth, actions, ignoredCodes, preventFailureOnNoResponse, escapeData }) => {
|
2020-07-30 16:27:27 +00:00
|
|
|
try {
|
2020-10-07 16:29:38 +00:00
|
|
|
if (escapeData) {
|
|
|
|
data = data.replace(/"[^"]*"/g, (match) => {
|
2021-02-19 10:36:10 +00:00
|
|
|
return match.replace(/[\n\r]\s*/g, "\\n");
|
2020-10-07 16:29:38 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-16 20:37:11 +00:00
|
|
|
if (method === METHOD_GET) {
|
|
|
|
data = undefined;
|
|
|
|
}
|
2020-07-30 16:27:27 +00:00
|
|
|
|
2021-01-24 12:14:13 +00:00
|
|
|
if (files && files !== '{}') {
|
|
|
|
filesJson = convertToJSON(files)
|
|
|
|
dataJson = convertToJSON(data)
|
|
|
|
|
|
|
|
if (Object.keys(filesJson).length > 0) {
|
|
|
|
try {
|
|
|
|
data = convertToFormData(dataJson, filesJson)
|
|
|
|
instanceConfig = await updateConfig(instanceConfig, data, actions)
|
|
|
|
} catch(error) {
|
|
|
|
actions.setFailed({ message: `Unable to convert Data and Files into FormData: ${error.message}`, data: dataJson, files: filesJson })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-30 16:27:27 +00:00
|
|
|
const requestData = {
|
|
|
|
auth,
|
|
|
|
method,
|
2021-02-19 10:36:10 +00:00
|
|
|
data,
|
|
|
|
maxContentLength: Infinity,
|
|
|
|
maxBodyLength: Infinity
|
2020-07-30 16:27:27 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 12:52:05 +00:00
|
|
|
actions.debug('Instance Configuration: ' + JSON.stringify(instanceConfig))
|
|
|
|
|
|
|
|
const instance = axios.create(instanceConfig);
|
|
|
|
|
2020-07-30 16:27:27 +00:00
|
|
|
actions.debug('Request Data: ' + JSON.stringify(requestData))
|
|
|
|
|
|
|
|
const response = await instance.request(requestData)
|
|
|
|
|
|
|
|
actions.setOutput('response', JSON.stringify(response.data))
|
|
|
|
} catch (error) {
|
|
|
|
if (error.toJSON) {
|
2021-03-19 16:28:53 +00:00
|
|
|
actions.setOutput('requestError', JSON.stringify(error.toJSON()));
|
2020-07-30 16:27:27 +00:00
|
|
|
}
|
|
|
|
|
2021-03-19 16:28:53 +00:00
|
|
|
if (error.response && ignoredCodes.includes(error.response.status)) {
|
|
|
|
actions.warning(JSON.stringify({ code: error.response.status, message: error.response.data }))
|
|
|
|
} else if (error.response) {
|
|
|
|
actions.setFailed(JSON.stringify({ code: error.response.status, message: error.response.data }))
|
2020-07-30 16:27:27 +00:00
|
|
|
} else if (error.request && !preventFailureOnNoResponse) {
|
|
|
|
actions.setFailed(JSON.stringify({ error: "no response received" }));
|
|
|
|
} else if (error.request && preventFailureOnNoResponse) {
|
|
|
|
actions.warning(JSON.stringify(error));
|
|
|
|
} else {
|
2020-10-07 16:29:38 +00:00
|
|
|
actions.setFailed(JSON.stringify({ message: error.message, data }));
|
2020-07-30 16:27:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-19 16:28:53 +00:00
|
|
|
/**
|
|
|
|
* @param {string} value
|
|
|
|
*
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
2021-01-24 12:14:13 +00:00
|
|
|
const convertToJSON = (value) => {
|
|
|
|
try {
|
|
|
|
return JSON.parse(value)
|
|
|
|
} catch(e) {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-19 16:28:53 +00:00
|
|
|
/**
|
|
|
|
* @param {Object} data
|
|
|
|
* @param {Object} files
|
|
|
|
*
|
|
|
|
* @returns {FormData}
|
|
|
|
*/
|
2021-01-24 12:14:13 +00:00
|
|
|
const convertToFormData = (data, files) => {
|
|
|
|
formData = new FormData()
|
|
|
|
|
|
|
|
for (const [key, value] of Object.entries(data)) {
|
|
|
|
formData.append(key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const [key, value] of Object.entries(files)) {
|
|
|
|
formData.append(key, fs.createReadStream(value))
|
|
|
|
}
|
|
|
|
|
|
|
|
return formData
|
|
|
|
}
|
|
|
|
|
2021-03-19 16:28:53 +00:00
|
|
|
/**
|
|
|
|
* @param {{ baseURL: string; timeout: number; headers: { [name: string]: string } }} instanceConfig
|
|
|
|
* @param {FormData} formData
|
|
|
|
* @param {*} actions
|
|
|
|
*
|
|
|
|
* @returns {{ baseURL: string; timeout: number; headers: { [name: string]: string } }}
|
|
|
|
*/
|
2021-01-24 12:14:13 +00:00
|
|
|
const updateConfig = async (instanceConfig, formData, actions) => {
|
|
|
|
try {
|
2021-01-24 12:52:05 +00:00
|
|
|
const formHeaders = formData.getHeaders()
|
|
|
|
const contentType = formHeaders['content-type']
|
|
|
|
|
|
|
|
delete formHeaders['content-type']
|
|
|
|
|
2021-01-24 12:14:13 +00:00
|
|
|
return {
|
|
|
|
...instanceConfig,
|
|
|
|
headers: {
|
|
|
|
...instanceConfig.headers,
|
2021-01-24 12:52:05 +00:00
|
|
|
...formHeaders,
|
|
|
|
'Content-Length': await contentLength(formData),
|
|
|
|
'Content-Type': contentType
|
2021-01-24 12:14:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch(error) {
|
|
|
|
actions.setFailed({ message: `Unable to read Content-Length: ${error.message}`, data, files })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-19 16:28:53 +00:00
|
|
|
/**
|
|
|
|
* @param {FormData} formData
|
|
|
|
*
|
|
|
|
* @returns {Promise<number>}
|
|
|
|
*/
|
2021-01-24 12:14:13 +00:00
|
|
|
const contentLength = (formData) => new Promise((resolve, reject) => {
|
|
|
|
formData.getLength((err, length) => {
|
|
|
|
if (err) {
|
|
|
|
reject (err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(length)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-07-30 16:27:27 +00:00
|
|
|
module.exports = {
|
|
|
|
request,
|
|
|
|
METHOD_POST,
|
|
|
|
METHOD_GET,
|
|
|
|
}
|