webhook-action/src/http.ts

35 lines
980 B
TypeScript
Raw Normal View History

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