Reqi
Reqi is a Promisified request library with built in functionality for retries, redirects, and body parsing.
Installation
npm install reqi
Usage
Using Reqi is like using your favorite request libraries, but with promises. Require reqi
, instantiate a client, and fire away!
const client = new json: true let data = await client
Reqi also includes convience methods for quick requests, such as get
and put
.
; { // create a new reqi client with json parsing const jsonClient = new json: true try // grab a post let body = await jsonClient // soup up the original post a bit let shnazzyTitle = bodytitle + '!!!' let excitingContent = bodybody const recycledPost = userId: 1 id: 1 title: shnazzyTitle body: excitingContent // publish our new post let res = await jsonClient console catch err console }
See Options for more information regarding clientOptions
and requestOptions
.
API
new ReqiClient([clientOptions])
clientOptions
:<Object>
See clientOptions
Creates a new client with the supplied clientOptions
overriding defaults.
request(requestOptions[, body=null])
requestOptions
:<Object>
See requestOptionsbody
:<Object>
|<string>
|<Buffer>
|<stream>
An acceptable request body.
request(url[, body=null])
url
:<string>
|<URL>
A parsable WHATWG input URL, or URL class.body
:<Object>
|<string>
|<Buffer>
|<stream>
An acceptable request body.
Options
clientOptions
clientOptions
:<Object>
redirect
:<boolean>
|<number>
Controls request redirection. If a positivenumber
is passed, the redirect option will delimit the maximum redirect amount. Upon being set to 'true
', all redirects will be followed.retry
:<boolean>
|<number>
Control request retries. If a positivenumber
is passed, the retry option will delimit the maximum retry amount. Upon being set to 'true
', retry attempts will be made until a success code is recieved.retryCodes
:number
|<number[]>
HTTP response code(s) to retry upon i.e.101
or[426, 429]
.maxWait
:<number>
The maximum number of seconds to wait before retrying a request. Deals primarily with rate limits.json
:<boolean>
Enables or disables automatic request and response body parsing. If enabled, 'true
', both bodies sent and recieved by the client are serialized and parsed, respectively.
Reqi gives you the option to control retries and redirects, as well as the ability to enable automatic body parsing.
const retryClient = retry: 3 retryCodes: 429
By default, retries, redirects, and body parsing are disabled.
Client options are mutable too!
// for exampleclientclientOptionsretry = 1
Default ReqiClient Configuration:
thisclientOptions = redirect: false retry: false retryCodes: maxWait: 3 // seconds json: false
These clientOptions
are bound to the request client and allow for subsequent requests without reconfiguration.
requestOptions
requestOptions
:<Object>
url
:<string>
|<URL>
(Required) A parsable WHATWG input URL, or URL class.
Just like what you're used to with Node Core, Reqi supports all core HTTP/HTTPS request options, with a few defaults:
method
: the desired HTTP request method, defaulting to 'GET
'.port
: the desired HTTP host port. If no port is provided, Reqi will use either a80
or443
value depending on the request protocol.
For more information on the supported requestOptions
, consult the following Node.js documentation:
Contributing
License
Licensed under the MIT License. See the LICENSE file for more details.