diff --git a/package.json b/package.json new file mode 100644 index 0000000..bdc44d3 --- /dev/null +++ b/package.json @@ -0,0 +1,22 @@ +{ + "name": "flumm-fetch", + "version": "1.0.0", + "description": "", + "main": "src/index.mjs", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/kein-Bot/flumm-fetch.git" + }, + "keywords": [ + "fetch" + ], + "author": "Flummi", + "license": "MIT", + "bugs": { + "url": "https://github.com/kein-Bot/flumm-fetch/issues" + }, + "homepage": "https://github.com/kein-Bot/flumm-fetch#readme" +} diff --git a/src/index.mjs b/src/index.mjs new file mode 100644 index 0000000..9fbd39e --- /dev/null +++ b/src/index.mjs @@ -0,0 +1,34 @@ +import http from "http"; +import https from "https"; +import url from "url"; +import querystring from "querystring"; + +const readdata = (res, mode, data = "") => new Promise((resolve, reject) => res + .setEncoding("utf8") + .on("data", chunk => data += chunk) + .on("end", () => { + switch(mode) { + case "text": resolve(data); break; + case "json": try { resolve(JSON.parse(data)); } catch(err) { reject(data); } break; + case "buffer": resolve(new Buffer.from(data)); break; + default: reject("lol no D:"); break; + } + })); +export default (a, options = {}, link = url.parse(a), body = "") => new Promise((resolve, reject) => { + options = {...{ hostname: link.hostname, path: link.path, method: "GET" }, ...options}; + if(options.method === "POST") { + body = querystring.stringify(options.body); + delete options.body; + options.headers = {...options.headers, ...{ + "Content-Type": "application/x-www-form-urlencoded", + "Content-Length": Buffer.byteLength(body) + }}; + } + (link.protocol === "https:"?https:http).request(options, res => resolve({ + body: res, + headers: res.headers, + text: () => readdata(res, "text"), + json: () => readdata(res, "json"), + buffer: () => readdata(res, "buffer") + })).on("error", err => reject(err)).end(body); +});