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-01-24 12:14:13 +00:00
|
|
|
const request = async({ method, instanceConfig, data, files, auth, actions, preventFailureOnNoResponse, escapeData }) => {
|
2020-07-30 16:27:27 +00:00
|
|
|
try {
|
|
|
|
const instance = axios.create(instanceConfig);
|
|
|
|
|
2020-10-07 16:29:38 +00:00
|
|
|
if (escapeData) {
|
|
|
|
data = data.replace(/"[^"]*"/g, (match) => {
|
|
|
|
return match.replace(/[\n\r]\s*/g, "\\n");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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,
|
2020-12-16 20:37:11 +00:00
|
|
|
data
|
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) {
|
|
|
|
actions.setOutput(JSON.stringify(error.toJSON()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error.response) {
|
|
|
|
actions.setFailed(JSON.stringify({ code: error.response.code, message: error.response.data }))
|
|
|
|
} 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-01-24 12:14:13 +00:00
|
|
|
const convertToJSON = (value) => {
|
|
|
|
try {
|
|
|
|
return JSON.parse(value)
|
|
|
|
} catch(e) {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
const updateConfig = async (instanceConfig, formData, actions) => {
|
|
|
|
try {
|
|
|
|
return {
|
|
|
|
...instanceConfig,
|
|
|
|
headers: {
|
|
|
|
...instanceConfig.headers,
|
|
|
|
...formData.getHeaders(),
|
|
|
|
'Content-Length': await contentLength(formData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch(error) {
|
|
|
|
actions.setFailed({ message: `Unable to read Content-Length: ${error.message}`, data, files })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|