1.8.3 • Published 6 months ago

myfetchapi v1.8.3

Weekly downloads
-
License
ISC
Repository
github
Last release
6 months ago

myfetchapi

A simple fetch wrapper with concurrency control and retry functionality that works on both nodejs, browser and edge environments.

Installation

You can install this package via npm:

npm install myfetchapi
yarn add myfetchapi
bun add myfetchapi

Usage

const { myFetch, SET_MAX_CONCURRENT_REQUESTS } = require("myfetchapi");

// Set maximum concurrent requests. default is 20
SET_MAX_CONCURRENT_REQUESTS(20);

// Make a request
myFetch(
  "https://example.com/api/data",
  {
    method: "GET"
    maxRetries: 5 /* default is 3. To disable retries, set it to `null` or `0` */,
  }
)
  .then((response) => console.log(response))
  .catch((error) => console.error(error));

API

myFetch

The main function to make HTTP requests.

  • Parameters:
    • input: RequestInfo - The URL or Request object.
    • init (optional): RequestInit - The request Options for the HTTP(S) request
      • Additional options to init.
        • timeout (optional): number - Request timeout in milliseconds.
        • throwHttpError (optional): boolean - Set this to false if you don't want to throw an error when http status is not ok. Default: true.
        • waitForBodyUsed (optional): boolean - Only remove the request from the concurrent request queue after the response body has been fully consumed. This is useful when you need to ensure that the connection is kept alive until the body is read. Default: false.
        • useNodeFetch (optional): boolean - This option is choosen automatically according to the environment it is running on. Set to true to use Node.js Options, e.g agents, and if false it uses the environment's default fetch implementation.
        • maxRetries (optional): number - How many times a request will retry if it failed. Default is 1.
        • retryCb (optional): function - Callback function that is called when the HTTP(S) request is retrying.
          • Parameters:
            • err: Error object.
            • count: Retry count.
            • max: Max retry count.
        • retryCondition (optional): function - Function to override and set your own condition for retrying.
          • Parameters:
            • response: HTTP(S) Response object.
          • Returns: boolean | Promise
  • Returns: Promise

SET_MAX_CONCURRENT_REQUESTS

Function to set the maximum number of concurrent requests.

  • Parameters:
    • max: number - The maximum number of concurrent requests.

License

This project is licensed under the MIT License

1.8.2

6 months ago

1.8.1

7 months ago

1.8.0

7 months ago

1.7.9

7 months ago

1.7.8

7 months ago

1.8.3

6 months ago

1.7.3

12 months ago

1.7.2

12 months ago

1.7.1

12 months ago

1.7.0

12 months ago

1.7.7

11 months ago

1.7.6

11 months ago

1.7.5

11 months ago

1.7.4

11 months ago

1.6.0

1 year ago

1.5.0

1 year ago

1.4.0

1 year ago

1.2.0

1 year ago

1.3.0

1 year ago

1.1.0

1 year ago

1.0.0

1 year ago