Skip to content

Instantly share code, notes, and snippets.

@gjuoun
Last active April 16, 2022 10:04
Show Gist options
  • Save gjuoun/f08f5f0298be14f88f32ffb46315e0dd to your computer and use it in GitHub Desktop.
Save gjuoun/f08f5f0298be14f88f32ffb46315e0dd to your computer and use it in GitHub Desktop.
fetch nodejs polyfill
const https = require("https");
const fs = require("fs");
const fetch = (url, options) => {
return new Promise((resolve, reject) => {
const urlObj = new URL(url);
const req = https.request(urlObj, options, (res) => {
const response = new Response({
statusCode: res.statusCode,
headers: res.headers,
url: res.url,
});
const buffers = [];
res.on("data", (data) => {
buffers.push(data);
});
res.on("end", () => {
response.setBody(buffers);
resolve(response);
});
});
req.on("error", (e) => {
reject(e);
});
req.end();
});
};
class Response {
headers;
#body;
url;
statusCode;
constructor({ headers, body, statusCode, url }) {
this.headers = headers;
this.body = body;
this.statusCode = statusCode;
this.url = url;
}
async json() {
return JSON.parse(Buffer.concat(this.body).toString());
}
async text() {
return Buffer.concat(this.body).toString();
}
async blob() {
return Buffer.concat(this.body);
}
setBody(body) {
this.body = body;
}
}
async function main() {
try {
const res = await fetch(
"https://cdn.pixabay.com/photo/2022/04/02/12/32/easter-tree-7106933__480.jpg",
{
method: "GET",
}
);
// const data = await res.json();
const data = await res.blob();
fs.writeFileSync("./picture.jpg", data);
} catch (e) {
console.log(e.message);
}
}
main();
module.exports = { fetch };
export const fetch = (url: string, options: RequestOptions) => {
return new Promise<Response>((resolve, reject) => {
const urlObj = new URL(url);
const req = https.request(urlObj, options, (res) => {
const response = new Response({
statusCode: res.statusCode,
headers: res.headers,
url: res.url,
});
const buffers: Buffer[] = [];
res.on("data", (data) => {
buffers.push(data);
});
res.on("end", () => {
response.setBody(buffers);
resolve(response);
});
});
req.on("error", (e) => {
reject(e);
});
req.end();
});
};
class Response {
headers: IncomingHttpHeaders;
private body: Buffer[];
url?: string;
statusCode?: number;
constructor({
headers,
body,
statusCode,
url,
}: {
headers: any;
body?: any;
statusCode?: number;
url?: string;
}) {
this.headers = headers;
this.body = body;
this.statusCode = statusCode;
this.url = url;
}
async json() {
return JSON.parse(Buffer.concat(this.body).toString());
}
async text() {
return Buffer.concat(this.body).toString();
}
async blob() {
return Buffer.concat(this.body);
}
setBody(body: Buffer[]) {
this.body = body;
}
}
async function main() {
try {
const res = await fetch(
"https://cdn.pixabay.com/photo/2022/04/02/12/32/easter-tree-7106933__480.jpg",
{
method: "GET",
}
);
// const data = await res.json();
const data = await res.blob();
fs.writeFileSync("./picture.jpg", data);
} catch (e: any) {
console.log(e.message);
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment