0.0.14 • Published 3 years ago
@sunknudsen/http v0.0.14
http
HTTP request utility used by a few of my projects.
This package abstracts got and implements rate limiting.
Installation
npm install @sunknudsen/http --save
Usage
By default, rate limiting is disabled, responseType
is set to json
and retry
is set to { limit: 0, methods: ["POST", "GET", "PATCH", "PUT", "DELETE"] }
.
Set instance options
Example with all available instance options
import http, { HTTPError as httpError } from "@sunknudsen/http"
// or `const http = require("@sunknudsen/http").default` and `const httpError = require("@sunknudsen/http").HTTPError` for CommonJS
class customError extends httpError {}
const httpInstance = new http({
rateLimit: {
requests: 10,
interval: "second",
},
errorClass: customError,
hooks: {
rateLimitReached: () => {
console.log("Rate limit reached hook fired")
},
beforeRetry: (request, error, retryCount) => {
console.log("Before retry hook fired", request, error, retryCount)
},
},
prefixUrl: "https://api.example.net",
headers: {
"user-agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:74.0) Gecko/20100101 Firefox/74.0",
},
responseType: "json",
retry: {
limit: 10,
methods: ["POST", "GET", "PATCH", "PUT", "DELETE"],
},
})
Send POST
, GET
, PATCH
, PUT
or DELETE
request
Example with all available request options
import http from "@sunknudsen/http"
const httpInstance = new http()
httpInstance.get(
"https://api.example.net",
{
foo: "bar",
},
{
headers: {
"user-agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:74.0) Gecko/20100101 Firefox/74.0",
},
responseType: "json",
retry: {
limit: 10,
},
}
)
Contributors
Licence
MIT