@fewlines-education/request
v0.0.1
Published
Simple wrapper around node-fetch with a callback API
Readme
Request
Installation
With Yarn:
yarn add @fewlines-education/requestWith NPM:
npm install @fewlines-education/requestCommon Usage
With TypeScript or using ESM:
import request from "@fewlines-education/request";With CommonJS:
const request = require("@fewlines-education/request").default;
```
### Plain text or HTML
```js
import request from "@fewlines-education/request";
request("https://github.com/", (error, body) => {
console.log(body);
});JSON
import request from "@fewlines-education/request";
reqest("https://api.github.com/users/github", (error, body) => {
const data = JSON.parse(body);
console.log(data);
});Simple Post
import request from "@fewlines-education/request";
request(
"https://httpbin.org/post",
{
method: "POST",
body: "a=1",
},
(error, body) => {
const data = JSON.parse(body);
console.log(data);
}
);Post with JSON
import request from "@fewlines-education/request";
const body = { a: 1 };
request(
"https://httpbin.org/post",
{
method: "post",
body: JSON.stringify(body),
headers: { "Content-Type": "application/json" },
},
(error, body) => {
const data = JSON.parse(body);
console.log(data);
}
);Post with form parameters
URLSearchParams is available on the global object in Node.js as of v10.0.0. See official documentation for more usage methods.
NOTE: The Content-Type header is only set automatically to x-www-form-urlencoded when an instance of URLSearchParams is given as such:
import request from "@fewlines-education/request";
const params = new URLSearchParams();
params.append("a", 1);
request(
"https://httpbin.org/post",
{
method: "POST",
body: params,
},
(error, body) => {
const data = JSON.parse(body);
console.log(data);
}
);Handling exceptions
NOTE: 3xx-5xx responses are NOT exceptions, and should be handled with the Response
import request from "@fewlines-education/request";
request("https://domain.invalid/", (error) => {
console.log(error);
});Handling client and server errors
It is common to create a helper function to check that the response contains no client (4xx) or server (5xx) error responses:
import request from "@fewlines-education/request";
class HTTPResponseError extends Error {
constructor(response, ...args) {
super(
`HTTP Error Response: ${response.status} ${response.statusText}`,
...args
);
this.response = response;
}
}
const checkStatus = (response) => {
if (response.ok) {
// response.status >= 200 && response.status < 300
return response;
} else {
throw new HTTPResponseError(response);
}
};
request("https://httpbin.org/status/400", (error, body, response) => {
try {
checkStatus(response);
} catch (error) {
console.error(error);
const errorBody = await error.response.text();
console.error(`Error body: ${errorBody}`);
}
});Accessing Headers and other Metadata
import request from "@fewlines-education/request";
request("https://github.com/", (error, body, response) => {
console.log(response.ok);
console.log(response.status);
console.log(response.statusText);
console.log(response.headers.raw());
console.log(response.headers.get("content-type"));
});API
request(url[, options], callback)
urlA string representing the URL for fetchingoptionsOptions for the HTTP(S) requestcallbackA function that will be called with:errorIf an error occured, it will contain the error, otherwise this will be nullbodyThe body of the response as a stringresponseThe Response of the request
Perform an HTTP(S) fetch.
url should be an absolute URL, such as https://example.com/. A path-relative URL (/file/under/root) or protocol-relative URL (//can-be-http-or-https.com/) will result in an error.
Options
The default values are shown after each option key.
{
// These properties are part of the Fetch Standard
method: 'GET',
headers: {}, // Request headers. format is the identical to that accepted by the Headers constructor (see below)
body: null, // Request body. can be null, or a Node.js Readable stream
redirect: 'follow', // Set to `manual` to extract redirect headers, `error` to reject redirect
signal: null, // Pass an instance of AbortSignal to optionally abort requests
// The following properties are node-fetch extensions
follow: 20, // maximum redirect count. 0 to not follow redirect
compress: true, // support gzip/deflate content encoding. false to disable
size: 0, // maximum response body size in bytes. 0 to disable
agent: null, // http(s).Agent instance or function that returns an instance (see below)
highWaterMark: 16384, // the maximum number of bytes to store in the internal buffer before ceasing to read from the underlying resource.
insecureHTTPParser: false // Use an insecure HTTP parser that accepts invalid HTTP headers when `true`.
}Default Headers
If no values are set, the following request headers will be sent automatically:
| Header | Value |
| ------------------- | ------------------------------------------------------ |
| Accept-Encoding | gzip,deflate,br (when options.compress === true) |
| Accept | */* |
| Connection | close (when no options.agent is present) |
| Content-Length | (automatically calculated, if possible) |
| Host | (host and port information from the target URI) |
| Transfer-Encoding | chunked (when req.body is a stream) |
| User-Agent | node-fetch |
Note: when body is a Stream, Content-Length is not set automatically.
Custom Agent
The agent option allows you to specify networking related options which are out of the scope of Fetch, including and not limited to the following:
- Support self-signed certificate
- Use only IPv4 or IPv6
- Custom DNS Lookup
See http.Agent for more information.
In addition, the agent option accepts a function that returns http(s).Agent instance given current URL, this is useful during a redirection chain across HTTP and HTTPS protocol.
import http from "node:http";
import https from "node:https";
const httpAgent = new http.Agent({
keepAlive: true,
});
const httpsAgent = new https.Agent({
keepAlive: true,
});
const options = {
agent: function (_parsedURL) {
if (_parsedURL.protocol == "http:") {
return httpAgent;
} else {
return httpsAgent;
}
},
};Class: Request
An HTTP(S) request containing information about URL, method, headers, and the body. This class implements the Body interface.
Due to the nature of Node.js, the following properties are not implemented at this moment:
typedestinationmodecredentialscacheintegritykeepalive
The following extension properties are provided:
followcompresscounteragenthighWaterMark
See options for exact meaning of these extensions.
new Request(input[, options])
(spec-compliant)
inputA string representing a URL, or anotherRequest(which will be cloned)options[Options][#fetch-options] for the HTTP(S) request
Constructs a new Request object. The constructor is identical to that in the browser.
In most cases, directly request(url, options, callback) is simpler than creating a Request object.
Class: Response
An HTTP(S) response. This class implements the Body interface.
The following properties are not implemented at this moment:
trailer
new Response([body[, options]])
(spec-compliant)
bodyAStringor [Readablestream][node-readable]optionsA [ResponseInit][response-init] options dictionary
Constructs a new Response object. The constructor is identical to that in the browser.
Because Node.js does not implement service workers (for which this class was designed), one rarely has to construct a Response directly.
response.ok
(spec-compliant)
Convenience property representing if the request ended normally. Will evaluate to true if the response status was greater than or equal to 200 but smaller than 300.
response.redirected
(spec-compliant)
Convenience property representing if the request has been redirected at least once. Will evaluate to true if the internal redirect counter is greater than 0.
response.type
(deviation from spec)
Convenience property representing the response's type. request only supports 'default' and 'error' and does not make use of filtered responses.
Class: Headers
This class allows manipulating and iterating over a set of HTTP headers. All methods specified in the [Fetch Standard][whatwg-fetch] are implemented.
new Headers([init])
(spec-compliant)
initOptional argument to pre-fill theHeadersobject
Construct a new Headers object. init can be either null, a Headers object, an key-value map object or any iterable object.
// Example adapted from https://fetch.spec.whatwg.org/#example-headers-class
import { Headers } from "@fewlines-education/request";
const meta = {
"Content-Type": "text/xml",
};
const headers = new Headers(meta);
// The above is equivalent to
const meta = [["Content-Type", "text/xml"]];
const headers = new Headers(meta);
// You can in fact use any iterable objects, like a Map or even another Headers
const meta = new Map();
meta.set("Content-Type", "text/xml");
const headers = new Headers(meta);
const copyOfHeaders = new Headers(headers);Interface: Body
Body is an abstract interface with methods that are applicable to both Request and Response classes.
body.body
(deviation from spec)
- Node.js [
Readablestream][node-readable]
Data are encapsulated in the Body object. Note that while the [Fetch Standard][whatwg-fetch] requires the property to always be a WHATWG ReadableStream, in request it is a Node.js [Readable stream][node-readable].
body.bodyUsed
(spec-compliant)
Boolean
A boolean property for if this body has been consumed. Per the specs, a consumed body cannot be used again.
Class: FetchError
(node-fetch extension)
An operational error in the fetching process.
TypeScript
types are bundled with @fewlines-education/request, so you don't need to install any additional packages.
