2020-07-30 16:27:27 +00:00
|
|
|
const axios = require("axios");
|
|
|
|
|
|
|
|
const METHOD_GET = 'GET'
|
|
|
|
const METHOD_POST = 'POST'
|
|
|
|
|
2020-10-07 16:29:38 +00:00
|
|
|
const request = async({ method, instanceConfig, data, 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
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
request,
|
|
|
|
METHOD_POST,
|
|
|
|
METHOD_GET,
|
|
|
|
}
|