Skip to content

Instantly share code, notes, and snippets.

@aztack
Created November 30, 2022 07:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aztack/056c76f4c960476cfd4feba194048928 to your computer and use it in GitHub Desktop.
Save aztack/056c76f4c960476cfd4feba194048928 to your computer and use it in GitHub Desktop.
https-fetch.js
import https from 'https';
function fetch(hostname: string, search: string) {
const options = {
hostname,
port: 443,
path: search,
method: 'GET',
};
let buf = '';
return new Promise((resolve, reject) => {
const req = https.request(options, res => {
res.on('data', d => {
buf += String(d);
});
res.on('end', () => {
try {
resolve({
text() {
return buf;
},
json() {
return JSON.parse(buf);
}
});
} catch (e) {
reject(JSON.stringify({ error: e }));
}
});
});
req.on('error', error => {
buf = JSON.stringify({ error });
reject(buf);
});
req.end();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment