@huksley/request
Advanced tools
Comparing version 1.0.1 to 1.0.2
{ | ||
"name": "@huksley/request", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "[gist-sized] Zero dependency, single-file NodeJS 14+ HTTP(s) request module", | ||
"main": "request.js", | ||
"type": "module", | ||
"repository": "https://github.com/huksley/node-request", | ||
@@ -7,0 +8,0 @@ "bugs": "https://github.com/huksley/node-request/issues", |
@@ -11,4 +11,5 @@ # HTTP(s) Request | ||
const res = await request("https://jsonip.com"); | ||
console.info("Your IP", res.body.ip, "headers", res.headers); | ||
request("https://jsonip.com").then((res) => { | ||
console.info("Your IP", res.body.ip, "headers", res.headers); | ||
}); | ||
``` | ||
@@ -15,0 +16,0 @@ |
import { request as httpsRequest } from "https"; | ||
import { IncomingMessage, request as httpRequest } from "http"; | ||
import * as zlib from "zlib"; | ||
import { inspect } from "util"; | ||
/** | ||
* Makes HTTP(s) request and returns response with parsed body. | ||
* | ||
* Makes HTTP(s) request and returns response with parsed body. | ||
* | ||
* ``` | ||
@@ -31,3 +32,3 @@ * const res = await request("https://jsonip.com") | ||
servername: u.host, | ||
rejectUnauthorized: options.rejectUnauthorized || true, | ||
rejectUnauthorized: options?.rejectUnauthorized || true, | ||
method: method || "GET", | ||
@@ -56,6 +57,12 @@ encoding: encoding !== undefined ? null : encoding, | ||
res.on("data", (chunk) => (data = Buffer.concat([data, chunk]))); | ||
res.on("error", (err) => { | ||
const e = new Error("HTTP request failed: " + url); | ||
e.error = err; | ||
reject(e); | ||
res.on("error", (e) => { | ||
const err = new Error("HTTP request failed: " + url); | ||
err.cause = e; | ||
// Add properties but disallow too verbose dumping | ||
res[inspect.custom] = () => { | ||
return "**Response**"; | ||
}; | ||
// This disallows dump of response in error log and in | ||
Object.defineProperty(err, "response", { enumerable: true, get: () => res }); | ||
reject(err); | ||
}); | ||
@@ -79,7 +86,12 @@ | ||
"end", | ||
handler((err, data) => { | ||
if (err) { | ||
const e = new Error("HTTP request failed: " + url); | ||
e.error = err; | ||
reject(e); | ||
handler((e, data) => { | ||
if (e) { | ||
const err = new Error("HTTP request failed: " + url); | ||
err.cause = e; | ||
// Add properties but disallow too verbose dumping | ||
res[inspect.custom] = () => { | ||
return "**Response**"; | ||
}; | ||
Object.defineProperty(err, "response", { enumerable: true, get: () => res }); | ||
reject(err); | ||
return; | ||
@@ -98,6 +110,11 @@ } | ||
} | ||
} catch (err) { | ||
const e = new Error("HTTP request failed: " + url); | ||
e.error = err; | ||
reject(e); | ||
} catch (e) { | ||
const err = new Error("HTTP request failed: " + url); | ||
err.cause = e; | ||
// Add properties but disallow too verbose dumping | ||
res[inspect.custom] = () => { | ||
return "**Response**"; | ||
}; | ||
Object.defineProperty(err, "response", { enumerable: true, get: () => res }); | ||
reject(err); | ||
} | ||
@@ -109,7 +126,11 @@ | ||
if (verbose) { | ||
console.info("HTTP request failed", url, res.statusCode, data); | ||
console.info("HTTP request failed", url, res.statusCode); | ||
} | ||
const err = new Error("HTTP request failed: " + url); | ||
err.statusCode = res.statusCode; | ||
err.response = res; | ||
// Add properties but disallow too verbose dumping | ||
res[inspect.custom] = () => { | ||
return "**Response**"; | ||
}; | ||
Object.defineProperty(err, "response", { enumerable: true, get: () => res }); | ||
reject(err); | ||
@@ -116,0 +137,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13930
274
35
Yes