webhook-action/src/http.ts

48 lines
1.0 KiB
TypeScript
Raw Normal View History

2021-02-26 04:00:44 +00:00
const fetch = require('node-fetch')
var https = require('https')
2020-08-26 00:00:45 +00:00
class Http {
2021-02-26 04:00:44 +00:00
make(
url: string,
headers: string | null,
body: string | null,
ignoreCertificate: boolean | null
): Promise<any> {
return new Promise((resolve, reject) => {
2021-02-26 04:00:44 +00:00
fetch(
url,
this.getOptions('post', headers, body, ignoreCertificate)
).then((res: Response) => resolve(res))
})
}
2021-02-26 04:00:44 +00:00
getOptions(
method: string,
headers: string | null,
body: string | null,
ignoreCertificate: boolean | null
) {
const options: any = {
2020-08-26 00:41:58 +00:00
headers: headers ? JSON.parse(headers) : {},
method
2021-02-26 04:00:44 +00:00
}
2020-08-26 00:41:58 +00:00
if (body) {
// parse the body
2021-02-26 04:00:44 +00:00
options.body = body
2020-08-26 00:41:58 +00:00
}
if (ignoreCertificate) {
// ignore the certificate by not rejecting authorized servers
2021-02-26 04:00:44 +00:00
options.agent = new https.Agent({rejectUnauthorized: false})
}
// set these headers
2021-02-26 04:00:44 +00:00
options.headers['content-type'] = 'application/json'
2021-02-26 04:00:44 +00:00
return options
}
}
2021-02-26 04:00:44 +00:00
export const http = new Http()