mirror of
https://github.com/fjogeleit/http-request-action.git
synced 2026-02-05 17:45:55 +08:00
Compare commits
22 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
eab8015483 | ||
|
|
f62db86b8d | ||
|
|
ef8ec33f46 | ||
|
|
c431724973 | ||
|
|
a9fc010566 | ||
|
|
8cbbec6c93 | ||
|
|
20c7f757a1 | ||
|
|
5e4203c2ac | ||
|
|
6659346e66 | ||
|
|
fc0207c5aa | ||
|
|
394beeafea | ||
|
|
e8dd067b83 | ||
|
|
a7bd4f21e5 | ||
|
|
2e2dec74b5 | ||
|
|
5f7d5f7c54 | ||
|
|
991c07d6d9 | ||
|
|
5500a62817 | ||
|
|
81ecdf1750 | ||
|
|
04b426e25f | ||
|
|
fc435761fc | ||
|
|
12f71e545a | ||
|
|
edf33a9d70 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
||||
node_modules
|
||||
.vscode
|
||||
@@ -43,6 +43,8 @@ jobs:
|
||||
|ignoreStatusCodes| Prevent this Action to fail if the request respond with one of the configured Status Codes. Example: '404,401' ||
|
||||
|httpsCA| Certificate authority as string in PEM format ||
|
||||
|responseFile| Persist the response data to the specified file path ||
|
||||
|retry| optional amount of retries if the request is failing, does not retry if the status code is ignored ||
|
||||
|retryWait| time between each retry in millseconds | 3000 |
|
||||
|
||||
### Response
|
||||
|
||||
|
||||
@@ -53,6 +53,12 @@ inputs:
|
||||
responseFile:
|
||||
description: 'Persist the response data to the specified file path'
|
||||
required: false
|
||||
retry:
|
||||
description: 'optional amount of retries if the request fails'
|
||||
required: false
|
||||
retryWait:
|
||||
description: 'wait time between retries in milliseconds'
|
||||
required: false
|
||||
outputs:
|
||||
response:
|
||||
description: 'HTTP Response Content'
|
||||
|
||||
@@ -42,10 +42,14 @@ request({
|
||||
data: argv.data,
|
||||
method: argv.method,
|
||||
instanceConfig,
|
||||
preventFailureOnNoResponse: false,
|
||||
escapeData: false,
|
||||
files: argv.files,
|
||||
file: argv.file,
|
||||
ignoredCodes: [],
|
||||
actions: new LogActions()
|
||||
actions: new LogActions(),
|
||||
options: {
|
||||
ignoredCodes: [],
|
||||
escapeData: false,
|
||||
preventFailureOnNoResponse: false,
|
||||
retry: 0,
|
||||
retryWait: 0
|
||||
}
|
||||
})
|
||||
|
||||
374
dist/index.js
vendored
374
dist/index.js
vendored
@@ -1726,6 +1726,10 @@ function checkBypass(reqUrl) {
|
||||
if (!reqUrl.hostname) {
|
||||
return false;
|
||||
}
|
||||
const reqHost = reqUrl.hostname;
|
||||
if (isLoopbackAddress(reqHost)) {
|
||||
return true;
|
||||
}
|
||||
const noProxy = process.env['no_proxy'] || process.env['NO_PROXY'] || '';
|
||||
if (!noProxy) {
|
||||
return false;
|
||||
@@ -1751,13 +1755,24 @@ function checkBypass(reqUrl) {
|
||||
.split(',')
|
||||
.map(x => x.trim().toUpperCase())
|
||||
.filter(x => x)) {
|
||||
if (upperReqHosts.some(x => x === upperNoProxyItem)) {
|
||||
if (upperNoProxyItem === '*' ||
|
||||
upperReqHosts.some(x => x === upperNoProxyItem ||
|
||||
x.endsWith(`.${upperNoProxyItem}`) ||
|
||||
(upperNoProxyItem.startsWith('.') &&
|
||||
x.endsWith(`${upperNoProxyItem}`)))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
exports.checkBypass = checkBypass;
|
||||
function isLoopbackAddress(host) {
|
||||
const hostLower = host.toLowerCase();
|
||||
return (hostLower === 'localhost' ||
|
||||
hostLower.startsWith('127.') ||
|
||||
hostLower.startsWith('[::1]') ||
|
||||
hostLower.startsWith('[0:0:0:0:0:0:0:1]'));
|
||||
}
|
||||
//# sourceMappingURL=proxy.js.map
|
||||
|
||||
/***/ }),
|
||||
@@ -5056,6 +5071,91 @@ const createPersistHandler = (filePath, actions) => (response) => {
|
||||
|
||||
module.exports = { createPersistHandler }
|
||||
|
||||
/***/ }),
|
||||
|
||||
/***/ 6989:
|
||||
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {
|
||||
|
||||
"use strict";
|
||||
|
||||
|
||||
const { GithubActions } = __nccwpck_require__(8169);
|
||||
const FormData = __nccwpck_require__(4334);
|
||||
const fs = __nccwpck_require__(7147);
|
||||
|
||||
/**
|
||||
* @param {string} value
|
||||
*
|
||||
* @returns {Object}
|
||||
*/
|
||||
const convertToJSON = (value) => {
|
||||
try {
|
||||
return JSON.parse(value) || {};
|
||||
} catch (e) {
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {{ [key: string]: string }} data
|
||||
* @param {{ [key: string]: string }} files
|
||||
* @param {boolean} convertPaths
|
||||
*
|
||||
* @returns {FormData}
|
||||
*/
|
||||
const convertToFormData = (data, files, convertPaths) => {
|
||||
const 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;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {() => Promise} callback
|
||||
* @param {{ retry: number; sleep: number; actions: GithubActions }} options
|
||||
*
|
||||
* @returns {Promise}
|
||||
*/
|
||||
const retry = async (callback, options) => {
|
||||
let lastErr = null;
|
||||
let i = 0;
|
||||
|
||||
do {
|
||||
try {
|
||||
return await callback();
|
||||
} catch (err) {
|
||||
lastErr = err;
|
||||
}
|
||||
|
||||
if (i < options.retries) {
|
||||
options.actions.warning(`#${i + 1} request failed: ${err}`);
|
||||
await sleep(options.sleep);
|
||||
}
|
||||
|
||||
i++;
|
||||
} while (i <= options.retry);
|
||||
|
||||
throw lastErr;
|
||||
};
|
||||
|
||||
function sleep(milliseconds) {
|
||||
return new Promise((resolve) => setTimeout(resolve, milliseconds));
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
convertToJSON,
|
||||
convertToFormData,
|
||||
retry,
|
||||
};
|
||||
|
||||
|
||||
/***/ }),
|
||||
|
||||
/***/ 9082:
|
||||
@@ -5065,10 +5165,11 @@ module.exports = { createPersistHandler }
|
||||
|
||||
|
||||
const axios = __nccwpck_require__(8757);
|
||||
const FormData = __nccwpck_require__(4334)
|
||||
const fs = __nccwpck_require__(7147)
|
||||
const FormData = __nccwpck_require__(4334);
|
||||
const fs = __nccwpck_require__(7147);
|
||||
const url = __nccwpck_require__(7310);
|
||||
const { GithubActions } = __nccwpck_require__(8169);
|
||||
const { convertToJSON, convertToFormData, retry } = __nccwpck_require__(6989);
|
||||
|
||||
const METHOD_GET = 'GET'
|
||||
const METHOD_POST = 'POST'
|
||||
@@ -5085,15 +5186,21 @@ const CONTENT_TYPE_URLENCODED = 'application/x-www-form-urlencoded'
|
||||
* @param {string} param0.files Map of Request Files (name: absolute path) as JSON String, default: {}
|
||||
* @param {string} param0.file Single request file (absolute path)
|
||||
* @param {GithubActions} 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
|
||||
* @param {{
|
||||
* ignoredCodes: number[];
|
||||
* preventFailureOnNoResponse: boolean,
|
||||
* escapeData: boolean;
|
||||
* retry: number;
|
||||
* retryWait: number;
|
||||
* }} param0.options
|
||||
*
|
||||
* @returns {Promise<axios.AxiosResponse>}
|
||||
*/
|
||||
const request = async({ method, instanceConfig, data, files, file, actions, ignoredCodes, preventFailureOnNoResponse, escapeData }) => {
|
||||
const request = async({ method, instanceConfig, data, files, file, actions, options }) => {
|
||||
actions.debug(`options: ${JSON.stringify(options)}`)
|
||||
|
||||
try {
|
||||
if (escapeData) {
|
||||
if (options.escapeData) {
|
||||
data = data.replace(/"[^"]*"/g, (match) => {
|
||||
return match.replace(/[\n\r]\s*/g, "\\n");
|
||||
});
|
||||
@@ -5145,10 +5252,38 @@ const request = async({ method, instanceConfig, data, files, file, actions, igno
|
||||
|
||||
actions.debug('Request Data: ' + JSON.stringify(requestData))
|
||||
|
||||
const response = await instance.request(requestData)
|
||||
const execRequest = async () => {
|
||||
try {
|
||||
return await instance.request(requestData)
|
||||
} catch(error) {
|
||||
if (error.response && options.ignoredCodes.includes(error.response.status)) {
|
||||
actions.warning(`ignored status code: ${JSON.stringify({ code: error.response.status, message: error.response.data })}`)
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
if (!error.response && error.request && options.preventFailureOnNoResponse) {
|
||||
actions.warning(`no response received: ${JSON.stringify(error)}`);
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
/** @type {axios.AxiosResponse|null} */
|
||||
const response = await retry(execRequest, {
|
||||
actions,
|
||||
retry: options.retry || 0,
|
||||
sleep: options.retryWait // wait time after each retry
|
||||
})
|
||||
|
||||
if (!response) {
|
||||
return null
|
||||
}
|
||||
|
||||
actions.setOutput('response', JSON.stringify(response.data))
|
||||
|
||||
actions.setOutput('headers', response.headers)
|
||||
|
||||
return response
|
||||
@@ -5158,53 +5293,16 @@ const request = async({ method, instanceConfig, data, files, file, actions, igno
|
||||
actions.setOutput('requestError', JSON.stringify({ name, message, code, status: response && response.status ? response.status : null }));
|
||||
}
|
||||
|
||||
if (error.response && ignoredCodes.includes(error.response.status)) {
|
||||
actions.warning(JSON.stringify({ code: error.response.status, message: error.response.data }))
|
||||
} else if (error.response) {
|
||||
if (error.response) {
|
||||
actions.setFailed(JSON.stringify({ code: error.response.status, message: error.response.data }))
|
||||
} else if (error.request && !preventFailureOnNoResponse) {
|
||||
} else if (error.request) {
|
||||
actions.setFailed(JSON.stringify({ error: "no response received" }));
|
||||
} else if (error.request && preventFailureOnNoResponse) {
|
||||
actions.warning(JSON.stringify(error));
|
||||
} else {
|
||||
actions.setFailed(JSON.stringify({ message: error.message, data }));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} value
|
||||
*
|
||||
* @returns {Object}
|
||||
*/
|
||||
const convertToJSON = (value) => {
|
||||
try {
|
||||
return JSON.parse(value) || {}
|
||||
} catch(e) {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Object} data
|
||||
* @param {Object} files
|
||||
*
|
||||
* @returns {FormData}
|
||||
*/
|
||||
const convertToFormData = (data, files) => {
|
||||
const 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
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {{ baseURL: string; timeout: number; headers: { [name: string]: string } }} instanceConfig
|
||||
* @param {FormData} formData
|
||||
@@ -5406,7 +5504,7 @@ module.exports = require("zlib");
|
||||
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {
|
||||
|
||||
"use strict";
|
||||
// Axios v1.3.2 Copyright (c) 2023 Matt Zabriskie and contributors
|
||||
// Axios v1.4.0 Copyright (c) 2023 Matt Zabriskie and contributors
|
||||
|
||||
|
||||
const FormData$1 = __nccwpck_require__(4334);
|
||||
@@ -5624,12 +5722,16 @@ const isStream = (val) => isObject(val) && isFunction(val.pipe);
|
||||
* @returns {boolean} True if value is an FormData, otherwise false
|
||||
*/
|
||||
const isFormData = (thing) => {
|
||||
const pattern = '[object FormData]';
|
||||
let kind;
|
||||
return thing && (
|
||||
(typeof FormData === 'function' && thing instanceof FormData) ||
|
||||
toString.call(thing) === pattern ||
|
||||
(isFunction(thing.toString) && thing.toString() === pattern)
|
||||
);
|
||||
(typeof FormData === 'function' && thing instanceof FormData) || (
|
||||
isFunction(thing.append) && (
|
||||
(kind = kindOf(thing)) === 'formdata' ||
|
||||
// detect form-data instance
|
||||
(kind === 'object' && isFunction(thing.toString) && thing.toString() === '[object FormData]')
|
||||
)
|
||||
)
|
||||
)
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -6094,6 +6196,11 @@ const toJSONObject = (obj) => {
|
||||
return visit(obj, 0);
|
||||
};
|
||||
|
||||
const isAsyncFn = kindOfTest('AsyncFunction');
|
||||
|
||||
const isThenable = (thing) =>
|
||||
thing && (isObject(thing) || isFunction(thing)) && isFunction(thing.then) && isFunction(thing.catch);
|
||||
|
||||
const utils = {
|
||||
isArray,
|
||||
isArrayBuffer,
|
||||
@@ -6143,7 +6250,9 @@ const utils = {
|
||||
ALPHABET,
|
||||
generateString,
|
||||
isSpecCompliantForm,
|
||||
toJSONObject
|
||||
toJSONObject,
|
||||
isAsyncFn,
|
||||
isThenable
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -6985,15 +7094,17 @@ function parseTokens(str) {
|
||||
return tokens;
|
||||
}
|
||||
|
||||
function isValidHeaderName(str) {
|
||||
return /^[-_a-zA-Z]+$/.test(str.trim());
|
||||
}
|
||||
const isValidHeaderName = (str) => /^[-_a-zA-Z0-9^`|~,!#$%&'*+.]+$/.test(str.trim());
|
||||
|
||||
function matchHeaderValue(context, value, header, filter) {
|
||||
function matchHeaderValue(context, value, header, filter, isHeaderNameFilter) {
|
||||
if (utils.isFunction(filter)) {
|
||||
return filter.call(this, value, header);
|
||||
}
|
||||
|
||||
if (isHeaderNameFilter) {
|
||||
value = header;
|
||||
}
|
||||
|
||||
if (!utils.isString(value)) return;
|
||||
|
||||
if (utils.isString(filter)) {
|
||||
@@ -7137,7 +7248,7 @@ class AxiosHeaders {
|
||||
|
||||
while (i--) {
|
||||
const key = keys[i];
|
||||
if(!matcher || matchHeaderValue(this, this[key], key, matcher)) {
|
||||
if(!matcher || matchHeaderValue(this, this[key], key, matcher, true)) {
|
||||
delete this[key];
|
||||
deleted = true;
|
||||
}
|
||||
@@ -7356,7 +7467,7 @@ function buildFullPath(baseURL, requestedURL) {
|
||||
return requestedURL;
|
||||
}
|
||||
|
||||
const VERSION = "1.3.2";
|
||||
const VERSION = "1.4.0";
|
||||
|
||||
function parseProtocol(url) {
|
||||
const match = /^([-+\w]{1,25})(:?\/\/|:)/.exec(url);
|
||||
@@ -7826,6 +7937,21 @@ class ZlibHeaderTransformStream extends stream__default["default"].Transform {
|
||||
|
||||
const ZlibHeaderTransformStream$1 = ZlibHeaderTransformStream;
|
||||
|
||||
const callbackify = (fn, reducer) => {
|
||||
return utils.isAsyncFn(fn) ? function (...args) {
|
||||
const cb = args.pop();
|
||||
fn.apply(this, args).then((value) => {
|
||||
try {
|
||||
reducer ? cb(null, ...reducer(value)) : cb(null, value);
|
||||
} catch (err) {
|
||||
cb(err);
|
||||
}
|
||||
}, cb);
|
||||
} : fn;
|
||||
};
|
||||
|
||||
const callbackify$1 = callbackify;
|
||||
|
||||
const zlibOptions = {
|
||||
flush: zlib__default["default"].constants.Z_SYNC_FLUSH,
|
||||
finishFlush: zlib__default["default"].constants.Z_SYNC_FLUSH
|
||||
@@ -7918,26 +8044,58 @@ function setProxy(options, configProxy, location) {
|
||||
|
||||
const isHttpAdapterSupported = typeof process !== 'undefined' && utils.kindOf(process) === 'process';
|
||||
|
||||
// temporary hotfix
|
||||
|
||||
const wrapAsync = (asyncExecutor) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
let onDone;
|
||||
let isDone;
|
||||
|
||||
const done = (value, isRejected) => {
|
||||
if (isDone) return;
|
||||
isDone = true;
|
||||
onDone && onDone(value, isRejected);
|
||||
};
|
||||
|
||||
const _resolve = (value) => {
|
||||
done(value);
|
||||
resolve(value);
|
||||
};
|
||||
|
||||
const _reject = (reason) => {
|
||||
done(reason, true);
|
||||
reject(reason);
|
||||
};
|
||||
|
||||
asyncExecutor(_resolve, _reject, (onDoneHandler) => (onDone = onDoneHandler)).catch(_reject);
|
||||
})
|
||||
};
|
||||
|
||||
/*eslint consistent-return:0*/
|
||||
const httpAdapter = isHttpAdapterSupported && function httpAdapter(config) {
|
||||
/*eslint no-async-promise-executor:0*/
|
||||
return new Promise(async function dispatchHttpRequest(resolvePromise, rejectPromise) {
|
||||
let data = config.data;
|
||||
const responseType = config.responseType;
|
||||
const responseEncoding = config.responseEncoding;
|
||||
return wrapAsync(async function dispatchHttpRequest(resolve, reject, onDone) {
|
||||
let {data, lookup, family} = config;
|
||||
const {responseType, responseEncoding} = config;
|
||||
const method = config.method.toUpperCase();
|
||||
let isFinished;
|
||||
let isDone;
|
||||
let rejected = false;
|
||||
let req;
|
||||
|
||||
if (lookup && utils.isAsyncFn(lookup)) {
|
||||
lookup = callbackify$1(lookup, (entry) => {
|
||||
if(utils.isString(entry)) {
|
||||
entry = [entry, entry.indexOf('.') < 0 ? 6 : 4];
|
||||
} else if (!utils.isArray(entry)) {
|
||||
throw new TypeError('lookup async function must return an array [ip: string, family: number]]')
|
||||
}
|
||||
return entry;
|
||||
});
|
||||
}
|
||||
|
||||
// temporary internal emitter until the AxiosRequest class will be implemented
|
||||
const emitter = new EventEmitter__default["default"]();
|
||||
|
||||
function onFinished() {
|
||||
if (isFinished) return;
|
||||
isFinished = true;
|
||||
|
||||
const onFinished = () => {
|
||||
if (config.cancelToken) {
|
||||
config.cancelToken.unsubscribe(abort);
|
||||
}
|
||||
@@ -7947,28 +8105,15 @@ const httpAdapter = isHttpAdapterSupported && function httpAdapter(config) {
|
||||
}
|
||||
|
||||
emitter.removeAllListeners();
|
||||
}
|
||||
|
||||
function done(value, isRejected) {
|
||||
if (isDone) return;
|
||||
};
|
||||
|
||||
onDone((value, isRejected) => {
|
||||
isDone = true;
|
||||
|
||||
if (isRejected) {
|
||||
rejected = true;
|
||||
onFinished();
|
||||
}
|
||||
|
||||
isRejected ? rejectPromise(value) : resolvePromise(value);
|
||||
}
|
||||
|
||||
const resolve = function resolve(value) {
|
||||
done(value);
|
||||
};
|
||||
|
||||
const reject = function reject(value) {
|
||||
done(value, true);
|
||||
};
|
||||
});
|
||||
|
||||
function abort(reason) {
|
||||
emitter.emit('abort', !reason || reason.type ? new CanceledError(null, config, req) : reason);
|
||||
@@ -8066,7 +8211,7 @@ const httpAdapter = isHttpAdapterSupported && function httpAdapter(config) {
|
||||
if (!headers.hasContentLength()) {
|
||||
try {
|
||||
const knownLength = await util__default["default"].promisify(data.getLength).call(data);
|
||||
headers.setContentLength(knownLength);
|
||||
Number.isFinite(knownLength) && knownLength >= 0 && headers.setContentLength(knownLength);
|
||||
/*eslint no-empty:0*/
|
||||
} catch (e) {
|
||||
}
|
||||
@@ -8170,6 +8315,8 @@ const httpAdapter = isHttpAdapterSupported && function httpAdapter(config) {
|
||||
agents: { http: config.httpAgent, https: config.httpsAgent },
|
||||
auth,
|
||||
protocol,
|
||||
family,
|
||||
lookup,
|
||||
beforeRedirect: dispatchBeforeRedirect,
|
||||
beforeRedirects: {}
|
||||
};
|
||||
@@ -8599,8 +8746,12 @@ const xhrAdapter = isXHRAdapterSupported && function (config) {
|
||||
}
|
||||
}
|
||||
|
||||
if (utils.isFormData(requestData) && (platform.isStandardBrowserEnv || platform.isStandardBrowserWebWorkerEnv)) {
|
||||
requestHeaders.setContentType(false); // Let the browser set it
|
||||
if (utils.isFormData(requestData)) {
|
||||
if (platform.isStandardBrowserEnv || platform.isStandardBrowserWebWorkerEnv) {
|
||||
requestHeaders.setContentType(false); // Let the browser set it
|
||||
} else {
|
||||
requestHeaders.setContentType('multipart/form-data;', false); // mobile/desktop app frameworks
|
||||
}
|
||||
}
|
||||
|
||||
let request = new XMLHttpRequest();
|
||||
@@ -9006,7 +9157,7 @@ function mergeConfig(config1, config2) {
|
||||
headers: (a, b) => mergeDeepProperties(headersToObject(a), headersToObject(b), true)
|
||||
};
|
||||
|
||||
utils.forEach(Object.keys(config1).concat(Object.keys(config2)), function computeConfigValue(prop) {
|
||||
utils.forEach(Object.keys(Object.assign({}, config1, config2)), function computeConfigValue(prop) {
|
||||
const merge = mergeMap[prop] || mergeDeepProperties;
|
||||
const configValue = merge(config1[prop], config2[prop], prop);
|
||||
(utils.isUndefined(configValue) && merge !== mergeDirectKeys) || (config[prop] = configValue);
|
||||
@@ -9150,11 +9301,17 @@ class Axios {
|
||||
}, false);
|
||||
}
|
||||
|
||||
if (paramsSerializer !== undefined) {
|
||||
validator.assertOptions(paramsSerializer, {
|
||||
encode: validators.function,
|
||||
serialize: validators.function
|
||||
}, true);
|
||||
if (paramsSerializer != null) {
|
||||
if (utils.isFunction(paramsSerializer)) {
|
||||
config.paramsSerializer = {
|
||||
serialize: paramsSerializer
|
||||
};
|
||||
} else {
|
||||
validator.assertOptions(paramsSerializer, {
|
||||
encode: validators.function,
|
||||
serialize: validators.function
|
||||
}, true);
|
||||
}
|
||||
}
|
||||
|
||||
// Set config.method
|
||||
@@ -9653,7 +9810,8 @@ if (!!core.getInput('customHeaders')) {
|
||||
try {
|
||||
customHeaders = JSON.parse(core.getInput('customHeaders'));
|
||||
} catch(error) {
|
||||
core.error('Could not parse customHeaders string value')
|
||||
core.debug(`Invalid customHeaders string: ${core.getInput('customHeaders')}`)
|
||||
core.error(`Could not parse customHeaders string value: ${error}`)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9683,6 +9841,16 @@ if (!!core.getInput('username') || !!core.getInput('password')) {
|
||||
}
|
||||
}
|
||||
|
||||
let retry = 0
|
||||
if (!!core.getInput('retry')) {
|
||||
retry = parseInt(core.getInput('retry'))
|
||||
}
|
||||
|
||||
let retryWait = 3000
|
||||
if (!!core.getInput('retryWait')) {
|
||||
retry = parseInt(core.getInput('retryWait'))
|
||||
}
|
||||
|
||||
const data = core.getInput('data') || '{}';
|
||||
const files = core.getInput('files') || '{}';
|
||||
const file = core.getInput('file')
|
||||
@@ -9705,7 +9873,15 @@ if (!!responseFile) {
|
||||
handler.push(createPersistHandler(responseFile, actions))
|
||||
}
|
||||
|
||||
request({ data, method, instanceConfig, preventFailureOnNoResponse, escapeData, files, file, ignoredCodes, actions }).then(response => {
|
||||
const options = {
|
||||
ignoredCodes,
|
||||
preventFailureOnNoResponse,
|
||||
escapeData,
|
||||
retry,
|
||||
retryWait
|
||||
}
|
||||
|
||||
request({ data, method, instanceConfig, files, file, actions, options }).then(response => {
|
||||
if (typeof response == 'object') {
|
||||
handler.forEach(h => h(response))
|
||||
}
|
||||
|
||||
40
package-lock.json
generated
40
package-lock.json
generated
@@ -13,9 +13,9 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@vercel/ncc": "^0.36.1",
|
||||
"axios": "^1.2",
|
||||
"axios": "^1.4",
|
||||
"form-data": "^4.0.0",
|
||||
"yargs": "^17.6.2"
|
||||
"yargs": "^17.7.2"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=16.0.0"
|
||||
@@ -31,9 +31,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/@actions/http-client": {
|
||||
"version": "2.0.1",
|
||||
"resolved": "https://registry.npmjs.org/@actions/http-client/-/http-client-2.0.1.tgz",
|
||||
"integrity": "sha512-PIXiMVtz6VvyaRsGY268qvj57hXQEpsYogYOu2nrQhlf+XCGmZstmuZBbAybUl1nQGnvS1k1eEsQ69ZoD7xlSw==",
|
||||
"version": "2.1.0",
|
||||
"resolved": "https://registry.npmjs.org/@actions/http-client/-/http-client-2.1.0.tgz",
|
||||
"integrity": "sha512-BonhODnXr3amchh4qkmjPMUO8mFi/zLaaCeCAJZqch8iQqyDnVIkySjB38VHAC8IJ+bnlgfOqlhpyCUZHlQsqw==",
|
||||
"dependencies": {
|
||||
"tunnel": "^0.0.6"
|
||||
}
|
||||
@@ -78,9 +78,9 @@
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/axios": {
|
||||
"version": "1.3.2",
|
||||
"resolved": "https://registry.npmjs.org/axios/-/axios-1.3.2.tgz",
|
||||
"integrity": "sha512-1M3O703bYqYuPhbHeya5bnhpYVsDDRyQSabNja04mZtboLNSuZ4YrltestrLXfHgmzua4TpUqRiVKbiQuo2epw==",
|
||||
"version": "1.4.0",
|
||||
"resolved": "https://registry.npmjs.org/axios/-/axios-1.4.0.tgz",
|
||||
"integrity": "sha512-S4XCWMEmzvo64T9GfvQDOXgYRDJ/wsSZc7Jvdgx5u1sd0JwsuPLqb3SYmusag+edF6ziyMensPVqLTSc1PiSEA==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"follow-redirects": "^1.15.0",
|
||||
@@ -313,9 +313,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/yargs": {
|
||||
"version": "17.6.2",
|
||||
"resolved": "https://registry.npmjs.org/yargs/-/yargs-17.6.2.tgz",
|
||||
"integrity": "sha512-1/9UrdHjDZc0eOU0HxOHoS78C69UD3JRMvzlJ7S79S2nTaWRA/whGCTV8o9e/N/1Va9YIV7Q4sOxD8VV4pCWOw==",
|
||||
"version": "17.7.2",
|
||||
"resolved": "https://registry.npmjs.org/yargs/-/yargs-17.7.2.tgz",
|
||||
"integrity": "sha512-7dSzzRQ++CKnNI/krKnYRV7JKKPUXMEh61soaHKg9mrWEhzFWhFnxPxGl+69cD1Ou63C13NUPCnmIcrvqCuM6w==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"cliui": "^8.0.1",
|
||||
@@ -351,9 +351,9 @@
|
||||
}
|
||||
},
|
||||
"@actions/http-client": {
|
||||
"version": "2.0.1",
|
||||
"resolved": "https://registry.npmjs.org/@actions/http-client/-/http-client-2.0.1.tgz",
|
||||
"integrity": "sha512-PIXiMVtz6VvyaRsGY268qvj57hXQEpsYogYOu2nrQhlf+XCGmZstmuZBbAybUl1nQGnvS1k1eEsQ69ZoD7xlSw==",
|
||||
"version": "2.1.0",
|
||||
"resolved": "https://registry.npmjs.org/@actions/http-client/-/http-client-2.1.0.tgz",
|
||||
"integrity": "sha512-BonhODnXr3amchh4qkmjPMUO8mFi/zLaaCeCAJZqch8iQqyDnVIkySjB38VHAC8IJ+bnlgfOqlhpyCUZHlQsqw==",
|
||||
"requires": {
|
||||
"tunnel": "^0.0.6"
|
||||
}
|
||||
@@ -386,9 +386,9 @@
|
||||
"dev": true
|
||||
},
|
||||
"axios": {
|
||||
"version": "1.3.2",
|
||||
"resolved": "https://registry.npmjs.org/axios/-/axios-1.3.2.tgz",
|
||||
"integrity": "sha512-1M3O703bYqYuPhbHeya5bnhpYVsDDRyQSabNja04mZtboLNSuZ4YrltestrLXfHgmzua4TpUqRiVKbiQuo2epw==",
|
||||
"version": "1.4.0",
|
||||
"resolved": "https://registry.npmjs.org/axios/-/axios-1.4.0.tgz",
|
||||
"integrity": "sha512-S4XCWMEmzvo64T9GfvQDOXgYRDJ/wsSZc7Jvdgx5u1sd0JwsuPLqb3SYmusag+edF6ziyMensPVqLTSc1PiSEA==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"follow-redirects": "^1.15.0",
|
||||
@@ -553,9 +553,9 @@
|
||||
"dev": true
|
||||
},
|
||||
"yargs": {
|
||||
"version": "17.6.2",
|
||||
"resolved": "https://registry.npmjs.org/yargs/-/yargs-17.6.2.tgz",
|
||||
"integrity": "sha512-1/9UrdHjDZc0eOU0HxOHoS78C69UD3JRMvzlJ7S79S2nTaWRA/whGCTV8o9e/N/1Va9YIV7Q4sOxD8VV4pCWOw==",
|
||||
"version": "17.7.2",
|
||||
"resolved": "https://registry.npmjs.org/yargs/-/yargs-17.7.2.tgz",
|
||||
"integrity": "sha512-7dSzzRQ++CKnNI/krKnYRV7JKKPUXMEh61soaHKg9mrWEhzFWhFnxPxGl+69cD1Ou63C13NUPCnmIcrvqCuM6w==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"cliui": "^8.0.1",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "http-request-action",
|
||||
"version": "1.11.2",
|
||||
"version": "1.14.1",
|
||||
"description": "",
|
||||
"main": "src/index.js",
|
||||
"private": false,
|
||||
@@ -20,9 +20,9 @@
|
||||
"homepage": "https://github.com/fjogeleit/http-request-action#readme",
|
||||
"devDependencies": {
|
||||
"@vercel/ncc": "^0.36.1",
|
||||
"axios": "^1.2",
|
||||
"axios": "^1.4",
|
||||
"form-data": "^4.0.0",
|
||||
"yargs": "^17.6.2"
|
||||
"yargs": "^17.7.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"@actions/core": "^1.10.0"
|
||||
|
||||
77
src/helper.js
Normal file
77
src/helper.js
Normal file
@@ -0,0 +1,77 @@
|
||||
'use strict';
|
||||
|
||||
const { GithubActions } = require('./githubActions');
|
||||
const FormData = require('form-data');
|
||||
const fs = require('fs');
|
||||
|
||||
/**
|
||||
* @param {string} value
|
||||
*
|
||||
* @returns {Object}
|
||||
*/
|
||||
const convertToJSON = (value) => {
|
||||
try {
|
||||
return JSON.parse(value) || {};
|
||||
} catch (e) {
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {{ [key: string]: string }} data
|
||||
* @param {{ [key: string]: string }} files
|
||||
* @param {boolean} convertPaths
|
||||
*
|
||||
* @returns {FormData}
|
||||
*/
|
||||
const convertToFormData = (data, files, convertPaths) => {
|
||||
const 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;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {() => Promise} callback
|
||||
* @param {{ retry: number; sleep: number; actions: GithubActions }} options
|
||||
*
|
||||
* @returns {Promise}
|
||||
*/
|
||||
const retry = async (callback, options) => {
|
||||
let lastErr = null;
|
||||
let i = 0;
|
||||
|
||||
do {
|
||||
try {
|
||||
return await callback();
|
||||
} catch (err) {
|
||||
lastErr = err;
|
||||
}
|
||||
|
||||
if (i < options.retries) {
|
||||
options.actions.warning(`#${i + 1} request failed: ${err}`);
|
||||
await sleep(options.sleep);
|
||||
}
|
||||
|
||||
i++;
|
||||
} while (i <= options.retry);
|
||||
|
||||
throw lastErr;
|
||||
};
|
||||
|
||||
function sleep(milliseconds) {
|
||||
return new Promise((resolve) => setTimeout(resolve, milliseconds));
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
convertToJSON,
|
||||
convertToFormData,
|
||||
retry,
|
||||
};
|
||||
@@ -1,10 +1,11 @@
|
||||
'use strict'
|
||||
|
||||
const axios = require('axios');
|
||||
const FormData = require('form-data')
|
||||
const fs = require('fs')
|
||||
const FormData = require('form-data');
|
||||
const fs = require('fs');
|
||||
const url = require('url');
|
||||
const { GithubActions } = require('./githubActions');
|
||||
const { convertToJSON, convertToFormData, retry } = require('./helper');
|
||||
|
||||
const METHOD_GET = 'GET'
|
||||
const METHOD_POST = 'POST'
|
||||
@@ -21,15 +22,21 @@ const CONTENT_TYPE_URLENCODED = 'application/x-www-form-urlencoded'
|
||||
* @param {string} param0.files Map of Request Files (name: absolute path) as JSON String, default: {}
|
||||
* @param {string} param0.file Single request file (absolute path)
|
||||
* @param {GithubActions} 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
|
||||
* @param {{
|
||||
* ignoredCodes: number[];
|
||||
* preventFailureOnNoResponse: boolean,
|
||||
* escapeData: boolean;
|
||||
* retry: number;
|
||||
* retryWait: number;
|
||||
* }} param0.options
|
||||
*
|
||||
* @returns {Promise<axios.AxiosResponse>}
|
||||
*/
|
||||
const request = async({ method, instanceConfig, data, files, file, actions, ignoredCodes, preventFailureOnNoResponse, escapeData }) => {
|
||||
const request = async({ method, instanceConfig, data, files, file, actions, options }) => {
|
||||
actions.debug(`options: ${JSON.stringify(options)}`)
|
||||
|
||||
try {
|
||||
if (escapeData) {
|
||||
if (options.escapeData) {
|
||||
data = data.replace(/"[^"]*"/g, (match) => {
|
||||
return match.replace(/[\n\r]\s*/g, "\\n");
|
||||
});
|
||||
@@ -81,10 +88,38 @@ const request = async({ method, instanceConfig, data, files, file, actions, igno
|
||||
|
||||
actions.debug('Request Data: ' + JSON.stringify(requestData))
|
||||
|
||||
const response = await instance.request(requestData)
|
||||
const execRequest = async () => {
|
||||
try {
|
||||
return await instance.request(requestData)
|
||||
} catch(error) {
|
||||
if (error.response && options.ignoredCodes.includes(error.response.status)) {
|
||||
actions.warning(`ignored status code: ${JSON.stringify({ code: error.response.status, message: error.response.data })}`)
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
if (!error.response && error.request && options.preventFailureOnNoResponse) {
|
||||
actions.warning(`no response received: ${JSON.stringify(error)}`);
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
/** @type {axios.AxiosResponse|null} */
|
||||
const response = await retry(execRequest, {
|
||||
actions,
|
||||
retry: options.retry || 0,
|
||||
sleep: options.retryWait // wait time after each retry
|
||||
})
|
||||
|
||||
if (!response) {
|
||||
return null
|
||||
}
|
||||
|
||||
actions.setOutput('response', JSON.stringify(response.data))
|
||||
|
||||
actions.setOutput('headers', response.headers)
|
||||
|
||||
return response
|
||||
@@ -94,53 +129,16 @@ const request = async({ method, instanceConfig, data, files, file, actions, igno
|
||||
actions.setOutput('requestError', JSON.stringify({ name, message, code, status: response && response.status ? response.status : null }));
|
||||
}
|
||||
|
||||
if (error.response && ignoredCodes.includes(error.response.status)) {
|
||||
actions.warning(JSON.stringify({ code: error.response.status, message: error.response.data }))
|
||||
} else if (error.response) {
|
||||
if (error.response) {
|
||||
actions.setFailed(JSON.stringify({ code: error.response.status, message: error.response.data }))
|
||||
} else if (error.request && !preventFailureOnNoResponse) {
|
||||
} else if (error.request) {
|
||||
actions.setFailed(JSON.stringify({ error: "no response received" }));
|
||||
} else if (error.request && preventFailureOnNoResponse) {
|
||||
actions.warning(JSON.stringify(error));
|
||||
} else {
|
||||
actions.setFailed(JSON.stringify({ message: error.message, data }));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} value
|
||||
*
|
||||
* @returns {Object}
|
||||
*/
|
||||
const convertToJSON = (value) => {
|
||||
try {
|
||||
return JSON.parse(value) || {}
|
||||
} catch(e) {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Object} data
|
||||
* @param {Object} files
|
||||
*
|
||||
* @returns {FormData}
|
||||
*/
|
||||
const convertToFormData = (data, files) => {
|
||||
const 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
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {{ baseURL: string; timeout: number; headers: { [name: string]: string } }} instanceConfig
|
||||
* @param {FormData} formData
|
||||
|
||||
23
src/index.js
23
src/index.js
@@ -13,7 +13,8 @@ if (!!core.getInput('customHeaders')) {
|
||||
try {
|
||||
customHeaders = JSON.parse(core.getInput('customHeaders'));
|
||||
} catch(error) {
|
||||
core.error('Could not parse customHeaders string value')
|
||||
core.debug(`Invalid customHeaders string: ${core.getInput('customHeaders')}`)
|
||||
core.error(`Could not parse customHeaders string value: ${error}`)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,6 +44,16 @@ if (!!core.getInput('username') || !!core.getInput('password')) {
|
||||
}
|
||||
}
|
||||
|
||||
let retry = 0
|
||||
if (!!core.getInput('retry')) {
|
||||
retry = parseInt(core.getInput('retry'))
|
||||
}
|
||||
|
||||
let retryWait = 3000
|
||||
if (!!core.getInput('retryWait')) {
|
||||
retry = parseInt(core.getInput('retryWait'))
|
||||
}
|
||||
|
||||
const data = core.getInput('data') || '{}';
|
||||
const files = core.getInput('files') || '{}';
|
||||
const file = core.getInput('file')
|
||||
@@ -65,7 +76,15 @@ if (!!responseFile) {
|
||||
handler.push(createPersistHandler(responseFile, actions))
|
||||
}
|
||||
|
||||
request({ data, method, instanceConfig, preventFailureOnNoResponse, escapeData, files, file, ignoredCodes, actions }).then(response => {
|
||||
const options = {
|
||||
ignoredCodes,
|
||||
preventFailureOnNoResponse,
|
||||
escapeData,
|
||||
retry,
|
||||
retryWait
|
||||
}
|
||||
|
||||
request({ data, method, instanceConfig, files, file, actions, options }).then(response => {
|
||||
if (typeof response == 'object') {
|
||||
handler.forEach(h => h(response))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user