update flumm-fetch

This commit is contained in:
Flummi 2019-11-25 15:41:12 +01:00
parent 671fc3c746
commit fdd99dfbd9
3 changed files with 6 additions and 38 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "flumm-fetch-cookies", "name": "flumm-fetch-cookies",
"version": "1.1.1", "version": "1.1.2",
"description": "flumm-fetch wrapper that adds support for cookie-jars", "description": "flumm-fetch wrapper that adds support for cookie-jars",
"main": "src/index.mjs", "main": "src/index.mjs",
"engines": { "engines": {
@ -24,5 +24,8 @@
"bugs": { "bugs": {
"url": "https://github.com/kein-Bot/flumm-fetch-cookies/issues" "url": "https://github.com/kein-Bot/flumm-fetch-cookies/issues"
}, },
"homepage": "https://github.com/kein-Bot/flumm-fetch-cookies#readme" "homepage": "https://github.com/kein-Bot/flumm-fetch-cookies#readme",
"dependencies": {
"flumm-fetch": "^1.0.0"
}
} }

View File

@ -1,35 +0,0 @@
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);
});

View File

@ -1,4 +1,4 @@
import fetch from "./fetch.mjs"; import fetch from "flumm-fetch";
import CookieJar from "./cookie-jar.mjs"; import CookieJar from "./cookie-jar.mjs";
import Cookie from "./cookie.mjs"; import Cookie from "./cookie.mjs";