mirror of
https://git.mirrors.martin98.com/https://github.com/cyberman54/curl
synced 2025-11-18 16:21:05 +08:00
61 lines
1.8 KiB
TypeScript
61 lines
1.8 KiB
TypeScript
import * as core from '@actions/core'
|
|
import axios, { AxiosResponse, AxiosRequestConfig } from 'axios'
|
|
import setOutput from './output'
|
|
|
|
export const validateStatusCode = (actualStatusCode: string): void => {
|
|
const acceptedStatusCode: string[] = core.getInput('accept')
|
|
.split(",").filter(x => x !== "")
|
|
.map(x => x.trim());
|
|
if (!acceptedStatusCode.includes(actualStatusCode)) {
|
|
throw new Error(`The accepted status code is ${acceptedStatusCode} but got ${actualStatusCode}`)
|
|
}
|
|
}
|
|
|
|
export const buildOutput = (res: AxiosResponse<any>): string => {
|
|
return JSON.stringify({
|
|
"status_code": res.status,
|
|
"data": res.data,
|
|
"headers": res.headers
|
|
})
|
|
}
|
|
|
|
export const sendRequestWithRetry = (config: AxiosRequestConfig): void => {
|
|
var exit = false
|
|
var countRetry = 0
|
|
const retryArr: string[] = core.getInput('retry').split('/')
|
|
const numberOfRetry: number = Number(retryArr[0])
|
|
const backoff: number = Number(retryArr[1])
|
|
process.on('uncaughtException', function (err) {
|
|
countRetry += 1
|
|
core.info(`retry: ${countRetry}`)
|
|
if (countRetry <= numberOfRetry) {
|
|
sleep(backoff)
|
|
} else {
|
|
core.setFailed(err.message)
|
|
}
|
|
process.exit(0)
|
|
})
|
|
do {
|
|
axios(config)
|
|
.then(res => {
|
|
exit = true
|
|
setOutput(res)
|
|
})
|
|
.catch(err => {
|
|
countRetry += 1
|
|
core.info(`retry: ${countRetry}`)
|
|
if (countRetry <= numberOfRetry) {
|
|
sleep(backoff)
|
|
} else {
|
|
exit = true
|
|
core.setFailed(err)
|
|
}
|
|
})
|
|
} while (!exit)
|
|
}
|
|
|
|
function sleep(ms: number) {
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
}
|
|
|