mirror of
https://git.mirrors.martin98.com/https://github.com/cyberman54/curl
synced 2026-04-06 10:53:18 +08:00
first commit
This commit is contained in:
29
src/index.ts
Normal file
29
src/index.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
|
||||
import axios from 'axios'
|
||||
import config from './requestconf'
|
||||
import * as core from '@actions/core'
|
||||
import { AxiosRequestConfig } from 'axios'
|
||||
import setOutput from './output'
|
||||
import * as fs from 'fs'
|
||||
|
||||
|
||||
try {
|
||||
if(core.getInput('custom-config')){
|
||||
const configPath = core.getInput('custom-config');
|
||||
const basePath = process.env.GITHUB_WORKSPACE;
|
||||
const path = `${basePath}/${configPath}`;
|
||||
core.info(`Path is ${path}`);
|
||||
if(configPath.split('.').pop() !== 'json'){
|
||||
throw new Error('Config must be json file')
|
||||
}
|
||||
if(!fs.existsSync(path)){
|
||||
throw new Error('Config file not found, meybe you need to use action/checkout before this step or there is typo on file name')
|
||||
}
|
||||
let customConfig: AxiosRequestConfig = JSON.parse(fs.readFileSync(path).toString()) as AxiosRequestConfig;
|
||||
axios(customConfig).then(res => setOutput(res)).catch(err => core.setFailed(err.message))
|
||||
}else{
|
||||
axios(config).then(res => setOutput(res)).catch(err => core.setFailed(err.message))
|
||||
}
|
||||
} catch (err) {
|
||||
core.setFailed(err.message);
|
||||
}
|
||||
17
src/output.ts
Normal file
17
src/output.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import * as core from '@actions/core'
|
||||
import * as util from './util'
|
||||
import { AxiosResponse } from 'axios'
|
||||
|
||||
const setOutput = (res: void | AxiosResponse <any>) => {
|
||||
if(!res){
|
||||
throw new Error('No response from request')
|
||||
}
|
||||
util.validateStatusCode(res.status.toString());
|
||||
if(core.getInput('is_debug') === 'true'){
|
||||
core.info(util.buildOutput(res));
|
||||
}
|
||||
core.setOutput('response', util.buildOutput(res));
|
||||
}
|
||||
|
||||
|
||||
export default setOutput
|
||||
92
src/requestconf.ts
Normal file
92
src/requestconf.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
|
||||
import axios from 'axios'
|
||||
import * as core from '@actions/core'
|
||||
import { AxiosRequestConfig, Method, AxiosBasicCredentials, AxiosProxyConfig} from 'axios'
|
||||
|
||||
// builder for request config
|
||||
|
||||
const builder = {
|
||||
basicAuth: (): AxiosBasicCredentials => {
|
||||
let authArr: string[] = core.getInput('basic-auth').trim().split(':');
|
||||
if(authArr.length !== 2){
|
||||
throw new Error('basic-auth format is invalid. The valid format should be username:password.');
|
||||
}
|
||||
return {
|
||||
username: authArr[0],
|
||||
password: authArr[1]
|
||||
}
|
||||
},
|
||||
bearerToken: (): string => {
|
||||
return `Bearer ${core.getInput('bearer-token')}`;
|
||||
},
|
||||
proxy: (): AxiosProxyConfig => {
|
||||
let proxy: AxiosProxyConfig;
|
||||
if(core.getInput('proxy-url').includes('//')){
|
||||
const proxyUrlArr: string[] = core.getInput('proxy-url').replace('//', '').trim().split(':');
|
||||
if(proxyUrlArr.length !== 3){
|
||||
throw new Error('proxy-url format is invalid. The valid format should be host:port.');
|
||||
}
|
||||
proxy = {
|
||||
protocol: proxyUrlArr[0],
|
||||
host: proxyUrlArr[1],
|
||||
port: Number(proxyUrlArr[2])
|
||||
}
|
||||
}else{
|
||||
const proxyUrlArr: string[] = core.getInput('proxy-url').trim().split(':');
|
||||
if(proxyUrlArr.length !== 2){
|
||||
throw new Error('proxy-url format is invalid. The valid format should be host:port.');
|
||||
}
|
||||
proxy = {
|
||||
host: proxyUrlArr[0],
|
||||
port: Number(proxyUrlArr[1])
|
||||
}
|
||||
}
|
||||
if(core.getInput('proxy-auth')){
|
||||
const proxyAuthArr: string[] = core.getInput('proxy-auth').trim().split(':');
|
||||
if(proxyAuthArr.length !== 2){
|
||||
throw new Error('proxy-auth format is invalid. The valid format should be username:password.');
|
||||
}
|
||||
proxy.auth = {
|
||||
username: proxyAuthArr[0],
|
||||
password: proxyAuthArr[1]
|
||||
}
|
||||
}
|
||||
return proxy;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Request config
|
||||
|
||||
const config: AxiosRequestConfig = {
|
||||
url: core.getInput('url'),
|
||||
method: core.getInput('method') as Method,
|
||||
timeout: Number(core.getInput('timeout'))
|
||||
}
|
||||
|
||||
if(core.getInput('basic-auth')){
|
||||
config.auth = builder.basicAuth()
|
||||
}
|
||||
|
||||
if(core.getInput('headers')){
|
||||
config.headers = JSON.parse(core.getInput('headers'))
|
||||
}
|
||||
|
||||
if(core.getInput('params')){
|
||||
config.params = JSON.parse(core.getInput('params'))
|
||||
}
|
||||
|
||||
if(core.getInput('body')){
|
||||
config.data = core.getInput('body')
|
||||
}
|
||||
|
||||
if(core.getInput('bearer-token')){
|
||||
config.headers = { ...config.headers, Authorization: builder.bearerToken() }
|
||||
}
|
||||
|
||||
if(core.getInput('proxy-url')){
|
||||
config.proxy = builder.proxy()
|
||||
}
|
||||
|
||||
|
||||
export default config
|
||||
21
src/util.ts
Normal file
21
src/util.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import * as core from '@actions/core'
|
||||
import { AxiosResponse } from 'axios'
|
||||
|
||||
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
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user