typescript lol
This commit is contained in:
@ -1,34 +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 = typeof options.body === "object" ? querystring.stringify(options.body) : options.body;
|
||||
delete options.body;
|
||||
options.headers = {...{
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
"Content-Length": Buffer.byteLength(body)
|
||||
}, ...options.headers};
|
||||
}
|
||||
(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);
|
||||
});
|
87
src/index.ts
Normal file
87
src/index.ts
Normal file
@ -0,0 +1,87 @@
|
||||
import http from "http";
|
||||
import https from "https";
|
||||
import { URL } from "url";
|
||||
import querystring from "querystring";
|
||||
|
||||
type ResponseData = {
|
||||
body: http.IncomingMessage;
|
||||
headers: http.IncomingHttpHeaders;
|
||||
text: () => Promise<string>;
|
||||
json: () => Promise<any>;
|
||||
buffer: () => Promise<Buffer>;
|
||||
arrayBuffer: () => Promise<ArrayBuffer>;
|
||||
};
|
||||
|
||||
interface ExtendedRequestOptions extends http.RequestOptions {
|
||||
body?: any;
|
||||
}
|
||||
|
||||
const readData = (
|
||||
res: http.IncomingMessage,
|
||||
mode: "text" | "json" | "buffer" | "arraybuffer"
|
||||
): Promise<string | any | Buffer | ArrayBuffer> =>
|
||||
new Promise((resolve, reject) => {
|
||||
const chunks: Buffer[] = [];
|
||||
res
|
||||
.on("data", chunk => chunks.push(chunk))
|
||||
.on("end", () => {
|
||||
const data = Buffer.concat(chunks);
|
||||
try {
|
||||
if(mode === "json")
|
||||
resolve(JSON.parse(data.toString("utf8")));
|
||||
else if(mode === "buffer")
|
||||
resolve(data);
|
||||
else if(mode === "arraybuffer")
|
||||
resolve(new Uint8Array(data).buffer);
|
||||
else
|
||||
resolve(data.toString("utf8"));
|
||||
}
|
||||
catch(err: any) {
|
||||
reject(err);
|
||||
}
|
||||
})
|
||||
.on("error", reject);
|
||||
});
|
||||
|
||||
export default async (urlString: string, options: ExtendedRequestOptions = {}): Promise<ResponseData> => {
|
||||
const { protocol, hostname, pathname, search, port } = new URL(urlString);
|
||||
|
||||
const body =
|
||||
options.method === "POST" && options.body
|
||||
? options.headers?.["Content-Type"] === "application/json"
|
||||
? JSON.stringify(options.body)
|
||||
: querystring.stringify(options.body as querystring.ParsedUrlQueryInput)
|
||||
: null;
|
||||
|
||||
const requestOptions: http.RequestOptions = {
|
||||
hostname,
|
||||
port: port || (protocol === "https:" ? 443 : 80),
|
||||
path: pathname + search,
|
||||
method: options.method || "GET",
|
||||
headers: {
|
||||
...options.headers,
|
||||
...(body ? { "Content-Length": Buffer.byteLength(body) } : {}),
|
||||
}
|
||||
};
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const requester = protocol === "https:" ? https : http;
|
||||
const req = requester.request(requestOptions, res => {
|
||||
if(res.statusCode && (res.statusCode < 200 || res.statusCode >= 300))
|
||||
return reject(new Error(`Request failed with status code ${res.statusCode}`));
|
||||
resolve({
|
||||
body: res,
|
||||
headers: res.headers,
|
||||
text: () => readData(res, "text"),
|
||||
json: () => readData(res, "json"),
|
||||
buffer: () => readData(res, "buffer"),
|
||||
arrayBuffer: () => readData(res, "arraybuffer")
|
||||
});
|
||||
});
|
||||
|
||||
req.on("error", reject);
|
||||
if(body)
|
||||
req.write(body);
|
||||
req.end();
|
||||
});
|
||||
};
|
Reference in New Issue
Block a user