Last active
April 16, 2022 10:04
-
-
Save gjuoun/f08f5f0298be14f88f32ffb46315e0dd to your computer and use it in GitHub Desktop.
fetch nodejs polyfill
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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