2020-08-26 00:20:01 +00:00
|
|
|
const fetch = require('node-fetch');
|
2020-08-26 00:00:45 +00:00
|
|
|
|
2020-08-25 23:36:04 +00:00
|
|
|
class Http {
|
2020-08-26 00:31:06 +00:00
|
|
|
make(url: string, headers: string, body: string): Promise<any> {
|
2020-08-26 00:34:42 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fetch(url, this.getOptions('post', headers, body))
|
|
|
|
.then((res: Response) => resolve(res));
|
2020-08-25 23:36:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-26 00:20:01 +00:00
|
|
|
getOptions(method: string, headers: string, body: string) {
|
|
|
|
const options: any = {
|
2020-08-26 00:41:58 +00:00
|
|
|
headers: headers ? JSON.parse(headers) : {},
|
2020-08-26 00:20:01 +00:00
|
|
|
method
|
|
|
|
};
|
|
|
|
|
2020-08-26 00:41:58 +00:00
|
|
|
if (body) {
|
|
|
|
// parse the body
|
|
|
|
options.body = body;
|
|
|
|
}
|
2020-08-25 23:36:04 +00:00
|
|
|
|
|
|
|
// set these headers
|
2020-08-26 00:20:01 +00:00
|
|
|
options.headers['content-type'] = 'application/json';
|
2020-08-25 23:36:04 +00:00
|
|
|
|
2020-08-26 00:20:01 +00:00
|
|
|
return options;
|
2020-08-25 23:36:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const http = new Http();
|