0.0.14 • Published 3 years ago

@sunknudsen/http v0.0.14

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

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

Sun Knudsen

Licence

MIT

0.0.14

3 years ago

0.0.13

4 years ago

0.0.12

4 years ago

0.0.10

4 years ago

0.0.11

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.5

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago