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
|
|
|
|
2020-08-25 23:36:04 +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> {
|
2020-08-26 00:34:42 +00:00
|
|
|
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))
|
|
|
|
})
|
2020-08-25 23:36:04 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 04:00:44 +00:00
|
|
|
getOptions(
|
|
|
|
method: string,
|
|
|
|
headers: string | null,
|
|
|
|
body: string | null,
|
|
|
|
ignoreCertificate: boolean | null
|
|
|
|
) {
|
2020-08-26 00:20:01 +00:00
|
|
|
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
|
2021-02-26 04:00:44 +00:00
|
|
|
}
|
2020-08-26 00:20:01 +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
|
|
|
}
|
2020-08-25 23:36:04 +00:00
|
|
|
|
2021-02-26 03:58:33 +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})
|
2021-02-26 03:58:33 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 23:36:04 +00:00
|
|
|
// set these headers
|
2021-02-26 04:00:44 +00:00
|
|
|
options.headers['content-type'] = 'application/json'
|
2020-08-25 23:36:04 +00:00
|
|
|
|
2021-02-26 04:00:44 +00:00
|
|
|
return options
|
2020-08-25 23:36:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-26 04:00:44 +00:00
|
|
|
export const http = new Http()
|