webhook-action/src/http.ts

29 lines
659 B
TypeScript
Raw Normal View History

const fetch = require('node-fetch');
2020-08-26 00:00:45 +00:00
class Http {
make(url: string, headers: string, body: string): Promise<any> {
return new Promise((resolve, reject) => {
fetch(url, this.getOptions('post', headers, body))
.then((res: Response) => resolve(res));
});
}
getOptions(method: string, headers: string, body: string) {
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;
}
// set these headers
options.headers['content-type'] = 'application/json';
return options;
}
}
export const http = new Http();