mirror of
https://github.com/fjogeleit/http-request-action.git
synced 2026-02-04 16:45:52 +08:00
add persist handler
Signed-off-by: Frank Jogeleit <frank.jogeleit@lovoo.com>
This commit is contained in:
@@ -7,6 +7,10 @@ class GithubActions {
|
||||
core.debug(message)
|
||||
}
|
||||
|
||||
info(message) {
|
||||
core.info(message)
|
||||
}
|
||||
|
||||
warning(message) {
|
||||
core.warning(message)
|
||||
}
|
||||
@@ -21,6 +25,10 @@ class GithubActions {
|
||||
}
|
||||
|
||||
class LogActions {
|
||||
info(message) {
|
||||
console.info(message)
|
||||
}
|
||||
|
||||
debug(message) {
|
||||
console.info(message)
|
||||
}
|
||||
|
||||
30
src/handler/persist.js
Normal file
30
src/handler/persist.js
Normal file
@@ -0,0 +1,30 @@
|
||||
'use strict'
|
||||
|
||||
const axios = require('axios');
|
||||
const fs = require('fs');
|
||||
const { GithubActions } = require('../githubActions');
|
||||
|
||||
/**
|
||||
* @param {string} filePath
|
||||
* @param {GithubActions} actions
|
||||
*
|
||||
* @returns {(response: axios.AxiosResponse) => void}
|
||||
*/
|
||||
const createPersistHandler = (filePath, actions) => (response) => {
|
||||
let data = response.data
|
||||
|
||||
if (typeof data == 'object') {
|
||||
data = JSON.stringify(data)
|
||||
}
|
||||
|
||||
fs.writeFile(filePath, data, err => {
|
||||
if (!err) {
|
||||
actions.info(`response persisted successfully at ${filePath}`)
|
||||
return
|
||||
}
|
||||
|
||||
actions.warning(JSON.stringify({ message: error.message, data: response.data }))
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = { createPersistHandler }
|
||||
@@ -4,6 +4,7 @@ const axios = require('axios');
|
||||
const FormData = require('form-data')
|
||||
const fs = require('fs')
|
||||
const url = require('url');
|
||||
const { GithubActions } = require('./githubActions');
|
||||
|
||||
const METHOD_GET = 'GET'
|
||||
const METHOD_POST = 'POST'
|
||||
@@ -19,12 +20,12 @@ const CONTENT_TYPE_URLENCODED = 'application/x-www-form-urlencoded'
|
||||
* @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 {string} param0.file Single request file (absolute path)
|
||||
* @param {*} param0.actions
|
||||
* @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
|
||||
*
|
||||
* @returns {Promise<void>}
|
||||
* @returns {Promise<axios.AxiosResponse>}
|
||||
*/
|
||||
const request = async({ method, instanceConfig, data, files, file, actions, ignoredCodes, preventFailureOnNoResponse, escapeData }) => {
|
||||
try {
|
||||
@@ -47,7 +48,7 @@ const request = async({ method, instanceConfig, data, files, file, actions, igno
|
||||
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 })
|
||||
actions.setFailed(JSON.stringify({ message: `Unable to convert Data and Files into FormData: ${error.message}`, data: dataJson, files: filesJson }))
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -75,6 +76,7 @@ const request = async({ method, instanceConfig, data, files, file, actions, igno
|
||||
|
||||
actions.debug('Instance Configuration: ' + JSON.stringify(instanceConfig))
|
||||
|
||||
/** @type {axios.AxiosInstance} */
|
||||
const instance = axios.create(instanceConfig);
|
||||
|
||||
actions.debug('Request Data: ' + JSON.stringify(requestData))
|
||||
@@ -84,6 +86,8 @@ const request = async({ method, instanceConfig, data, files, file, actions, igno
|
||||
actions.setOutput('response', JSON.stringify(response.data))
|
||||
|
||||
actions.setOutput('headers', response.headers)
|
||||
|
||||
return response
|
||||
} catch (error) {
|
||||
if ((typeof error === 'object') && (error.isAxiosError === true)) {
|
||||
const { name, message, code, response } = error
|
||||
|
||||
19
src/index.js
19
src/index.js
@@ -5,6 +5,7 @@ const axios = require('axios');
|
||||
const https = require('https');
|
||||
const { request, METHOD_POST } = require('./httpClient');
|
||||
const { GithubActions } = require('./githubActions');
|
||||
const { createPersistHandler } = require('./handler/persist');
|
||||
|
||||
let customHeaders = {}
|
||||
|
||||
@@ -45,15 +46,27 @@ if (!!core.getInput('username') || !!core.getInput('password')) {
|
||||
const data = core.getInput('data') || '{}';
|
||||
const files = core.getInput('files') || '{}';
|
||||
const file = core.getInput('file')
|
||||
const responseFile = core.getInput('responseFile')
|
||||
const method = core.getInput('method') || METHOD_POST;
|
||||
const preventFailureOnNoResponse = core.getInput('preventFailureOnNoResponse') === 'true';
|
||||
const escapeData = core.getInput('escapeData') === 'true';
|
||||
|
||||
const ignoreStatusCodes = core.getInput('ignoreStatusCodes')
|
||||
let ignoredCodes = []
|
||||
const ignoreStatusCodes = core.getInput('ignoreStatusCodes');
|
||||
let ignoredCodes = [];
|
||||
|
||||
if (typeof ignoreStatusCodes === 'string' && ignoreStatusCodes.length > 0) {
|
||||
ignoredCodes = ignoreStatusCodes.split(',').map(statusCode => parseInt(statusCode.trim()))
|
||||
}
|
||||
|
||||
request({ data, method, instanceConfig, preventFailureOnNoResponse, escapeData, files, file, ignoredCodes, actions: new GithubActions() })
|
||||
const handler = [];
|
||||
const actions = new GithubActions();
|
||||
|
||||
if (!!responseFile) {
|
||||
handler.push(createPersistHandler(responseFile, actions))
|
||||
}
|
||||
|
||||
request({ data, method, instanceConfig, preventFailureOnNoResponse, escapeData, files, file, ignoredCodes, actions }).then(response => {
|
||||
if (typeof response == 'object') {
|
||||
handler.forEach(h => h(response))
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user