The APIClient class provides a basic HTTP client for making API requests. It includes methods for making GET, POST, PUT, PATCH, and DELETE requests.

Subclasses should define the baseURL to specify the root URL for all requests.

The class allows for customization of the request and response through the before and after hooks, which can be overridden in subclasses to modify behavior before sending a request or after receiving a response.

class MyAPIClient extends APIClient {
readonly baseURL = new URL("https://api.example.com/");

async before(request: Request) {
const token = await fetchToken();
request.headers.set('Authorization', `Bearer ${token}`);
return request;
}
}

Constructors

Properties

Methods

Constructors

Properties

baseURL: URL

The base URL for all API requests. Must be defined in subclasses.

Methods

  • This method is called after the response is received. It can be used to process or inspect the response before returning it.

    Parameters

    • response: Response

      The response object that was received.

    Returns Promise<Response>

    A promise that resolves to the (possibly processed) response.

  • This method is called before the request is sent. It can be used to modify the request before it is sent (e.g., adding headers).

    Parameters

    • request: Request

      The request object that will be sent.

    Returns Promise<Request>

    A promise that resolves to the (possibly modified) request.

  • Sends a DELETE request to the specified path.

    Parameters

    • path: string

      The path to send the request to, relative to baseURL.

    • Optionalinit: Omit<RequestInit, "method">

      Optional request options, excluding the HTTP method.

    Returns Promise<Response>

    A promise that resolves to the response from the request.

  • Sends a fetch request to the specified path, using the provided RequestInit options.

    Parameters

    • path: string

      The path to send the request to, relative to baseURL.

    • Optionalinit: RequestInit

      Optional request options.

    Returns Promise<Response>

    A promise that resolves to the response from the request.

  • Sends a GET request to the specified path.

    Parameters

    • path: string

      The path to send the request to, relative to baseURL.

    • Optionalinit: Omit<RequestInit, "method">

      Optional request options, excluding the HTTP method.

    Returns Promise<Response>

    A promise that resolves to the response from the request.

  • Sends a PATCH request to the specified path.

    Parameters

    • path: string

      The path to send the request to, relative to baseURL.

    • Optionalinit: Omit<RequestInit, "method">

      Optional request options, excluding the HTTP method.

    Returns Promise<Response>

    A promise that resolves to the response from the request.

  • Sends a POST request to the specified path.

    Parameters

    • path: string

      The path to send the request to, relative to baseURL.

    • Optionalinit: Omit<RequestInit, "method">

      Optional request options, excluding the HTTP method.

    Returns Promise<Response>

    A promise that resolves to the response from the request.

  • Sends a PUT request to the specified path.

    Parameters

    • path: string

      The path to send the request to, relative to baseURL.

    • Optionalinit: Omit<RequestInit, "method">

      Optional request options, excluding the HTTP method.

    Returns Promise<Response>

    A promise that resolves to the response from the request.