APIProviderclass

new APIProvider<P, R>()
Return
APIProvider<P, R>
Abstract base for API providers that send requests to a set of Endpoint definitions rooted at a common base URL.
PropertyType
.urlURL
The common base URL that every endpoint request is resolved against. required readonly

Abstract base for API providers that send requests to a set of Endpoint definitions rooted at a common base URL.

  • Concrete subclasses implement renderURL(), createRequest(), fetch(), and parseResponse(); call() orchestrates them.
  • Implements AsyncDisposable so providers can be wrapped and disposed in a chain.

The abstract base class every API provider implements. APIProvider<P, R> defines the surface that endpoint calls go through: call(endpoint, payload) to execute a typed endpoint, and fetch(request) as the lower-level transport step that wrapper providers intercept.

You never instantiate APIProvider directly — use the concrete ClientAPIProvider (or a mock), optionally wrapped in ThroughAPIProvider subclasses. APIProvider implements AsyncDisposable.

Usage

Code that accepts "any provider" should type against APIProvider so a bare client, a validated chain, or a mock are all interchangeable:

ts
import type { APIProvider } from "shelving/api"

async function loadUser(provider: APIProvider, id: string) {
  return provider.call(getUser, { id }); // works with any provider in the chain
}

Examples

const provider = new ClientAPIProvider({ url: "https://api.example.com" });
const user = await provider.call(getUser, { id: "abc" });

Methods

Go

APIProvider.renderURL()method

Render the full final URL for an API request to a given endpoint with a given payload.

renderURL(endpoint: Endpoint<PP, RR>, payload: PP, caller?: AnyCaller): URL
Go

APIProvider.createRequest()method

Create a Request that targets this endpoint with a given base URL.

createRequest(endpoint: Endpoint<PP, RR>, payload: PP, options?: RequestOptions, caller?: AnyCaller): Request
Go

APIProvider.fetch()method

Send a Request and return its Response (defaults to the JavaScript fetch() API).

fetch(request: Request): Promise<Response>
Go

APIProvider.parseResponse()method

Parse an HTTP Response for this endpoint into a result value.

parseResponse(_endpoint: Endpoint<PP, RR>, response: Response, caller?: AnyCaller): Promise<RR>
Go

APIProvider.call()method

Send a payload to an Endpoint and retrieve the parsed result.

call(endpoint: Endpoint<PP, RR>, payload: PP, options?: RequestOptions, caller?: AnyCaller): Promise<RR>