A load balanced fetch.
npm i lb-fetchimport lbFetch from 'lb-fetch';
const response = await lbFetch(
[
"https://server1.example.com/api/endpoint",
new URL("https://server2.example.com/api/endpoint")
],
{
method: 'POST',
body: new URLSearchParams({
foo: 'bar',
baz: '42'
})
}
);Tries to fetch one of the inputs in some order until successful.
Returns a Promise of a
Response.
Type: (string | URL)[] or generic InputType[]
The URLs to try. The input may also be an array of anything else, then
options.balancer must be defined.
Type: RequestInit | undefined
Default: undefined
Same as
fetch options.
Type: typeof fetch
Default: the global fetch method
Any method that conforms to the Fetch API.
Type: Balancer<InputType = string | URL>
type Balancer<InputType = string | URL> = (
inputs: InputType[],
init: RequestInit | undefined
) => Promise<(string | URL)[]> | (string | URL)[];Default: randomBalancer
This method decides the order in which the entries of input are tried.
Note: the default value only works for inputs consisting of strings and
URLs.
Type: SuccessPredicate
type SuccessPredicate = (response: Response) => boolean;Default: reject500s
This method decides whether an attempt was successful.
If a request to an input throws an exception, it is also considered unsuccessful.
The default balancer returns a shuffled shallow copy of its input.
Type: (string | URL)[]
The default SuccessPredicate accepts a response, if its status is less than
500 – i.e. if the response was not a server error.