class WebClient

An HTTP client built on fetch with automatic retries and error handling.

Examples

Example 1

const client = WebClient.create()
  .withBaseUrl("https://api.example.com")
  .withHeaders({ Authorization: "Bearer token" })
  .withTimeout(10_000)
  .withRetry({ attempts: 3, delay: (n) => 1000 * 2 ** n });

const result = await client.get("/users/1").run();
console.log(result.data);

Constructors

Static Methods

Creates a new WebClient with all defaults.

Properties

private
readonly
config: ResolvedConfig

Methods

delete(
path: string,
options?: RequestOptions,
): Task<ResponseResult, HttpError>

Make a DELETE request.

get(
path: string,
options?: RequestOptions,
): Task<ResponseResult, HttpError>

Make a GET request.

patch(
path: string,
body: unknown,
options?: RequestOptions,
): Task<ResponseResult, HttpError>

Make a PATCH request with a body.

post(
path: string,
body: unknown,
options?: RequestOptions,
): Task<ResponseResult, HttpError>

Make a POST request with a body.

put(
path: string,
body: unknown,
options?: RequestOptions,
): Task<ResponseResult, HttpError>

Make a PUT request with a body.

request(
path: string,
method: Method,
options?: RequestOptions,
body?: unknown,
): Task<ResponseResult, HttpError>

Make an HTTP request with the specified method.

withBaseUrl(url: string | URL): WebClient

Returns a new WebClient with the given base URL.

withFetch(fetch: globalThis.fetch): WebClient

Returns a new WebClient using the given fetch function.

withHeaders(headers: Record<string, string>): WebClient

Returns a new WebClient with the given headers merged into existing ones.

Returns a new WebClient with retry options field-merged into existing ones.

Returns a new WebClient with the given timeout in milliseconds.