webhook-action/src/http.ts

29 lines
679 B
TypeScript
Raw Normal View History

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