|
| 1 | +import https from 'https'; |
| 2 | +import { OutgoingHttpHeaders, IncomingMessage, ClientRequest } from 'http'; |
| 3 | +import http from 'http'; |
| 4 | + |
| 5 | +export function httpGet(url: string, headers?: OutgoingHttpHeaders): Promise<any> { |
| 6 | + return new Promise((resolve, reject) => { |
| 7 | + const options: https.RequestOptions = {} |
| 8 | + if (headers) { |
| 9 | + options.headers = headers; |
| 10 | + } |
| 11 | + https.get(url, options, (resp) => { |
| 12 | + let data: string = ''; |
| 13 | + resp.on('data', (chunk) => { |
| 14 | + data += chunk; |
| 15 | + }); |
| 16 | + |
| 17 | + resp.on('end', () => { |
| 18 | + resolve(formatResponseData(resp, data)); |
| 19 | + }); |
| 20 | + }).on('error', (err) => { |
| 21 | + reject(err); |
| 22 | + }); |
| 23 | + }); |
| 24 | +} |
| 25 | + |
| 26 | +export function httpPost(url: string, body: any, headers?: object): Promise<any> { |
| 27 | + return new Promise((resolve, reject) => { |
| 28 | + const data = typeof body !== 'string' ? JSON.stringify(body) : body; |
| 29 | + |
| 30 | + const {hostname, port, pathname} = new URL(url); |
| 31 | + const options = { |
| 32 | + hostname, port, pathname, |
| 33 | + method: 'POST', |
| 34 | + headers: { |
| 35 | + 'Content-Type': 'application/json', |
| 36 | + 'Content-Length': data.length |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + if (headers) { |
| 41 | + Object.assign(options.headers, headers); |
| 42 | + } |
| 43 | + |
| 44 | + const req = createReq(options, resolve, reject); |
| 45 | + req.write(data); |
| 46 | + req.end(); |
| 47 | + }) |
| 48 | +} |
| 49 | + |
| 50 | +export function httpPut(url: string, body: any, headers?: object): Promise<any> { |
| 51 | + return new Promise((resolve, reject) => { |
| 52 | + const data = typeof body !== 'string' ? JSON.stringify(body) : body; |
| 53 | + |
| 54 | + const {hostname, port, pathname} = new URL(url); |
| 55 | + |
| 56 | + const options = { |
| 57 | + hostname, port: 443, pathname, |
| 58 | + method: 'PUT', |
| 59 | + headers: { |
| 60 | + 'Content-Type': 'application/json', |
| 61 | + 'Content-Length': data.length |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if (headers) { |
| 66 | + Object.assign(options.headers, headers); |
| 67 | + } |
| 68 | + |
| 69 | + const req = createReq(options, resolve, reject); |
| 70 | + req.write(data); |
| 71 | + req.end(); |
| 72 | + }) |
| 73 | +} |
| 74 | + |
| 75 | +export function httpDelete(url: string, headers?: object): Promise<any> { |
| 76 | + return new Promise((resolve, reject) => { |
| 77 | + const {hostname, port, pathname} = new URL(url); |
| 78 | + |
| 79 | + const options: any = { |
| 80 | + hostname, port, pathname, |
| 81 | + method: 'DELETE', |
| 82 | + } |
| 83 | + |
| 84 | + if (headers) { |
| 85 | + Object.assign(options.headers, headers); |
| 86 | + } |
| 87 | + |
| 88 | + const req = createReq(options, resolve, reject); |
| 89 | + req.end(); |
| 90 | + }) |
| 91 | +} |
| 92 | + |
| 93 | +function createReq(options: https.RequestOptions, resolve: Function, reject: Function): ClientRequest { |
| 94 | + const req = http.request(options, (res) => { |
| 95 | + let data: string = ''; |
| 96 | + res.on('data', (chunk) => { |
| 97 | + data += chunk; |
| 98 | + }); |
| 99 | + |
| 100 | + res.on('end', () => { |
| 101 | + resolve(formatResponseData(res, data)); |
| 102 | + }); |
| 103 | + }) |
| 104 | + |
| 105 | + req.on('error', (error) => { |
| 106 | + reject(error); |
| 107 | + }); |
| 108 | + |
| 109 | + return req; |
| 110 | +} |
| 111 | + |
| 112 | +function formatResponseData(resp: IncomingMessage, data: string) { |
| 113 | + if (resp.headers['content-type']?.includes('application/json')) { |
| 114 | + try { |
| 115 | + data = JSON.parse(data); |
| 116 | + } catch (e) { |
| 117 | + console.error(e); |
| 118 | + } finally { |
| 119 | + return data |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return data; |
| 124 | +} |
0 commit comments