Libraries
    Preparing search index...

    Function createClient

    • Creates a fully typed HTTP client from an API contract.

      The returned object mirrors the shape of the contract: each group becomes a namespace and each endpoint within that group becomes an async callable function.

      At runtime, endpoint metadata (HTTP method, path template, schemas) is read from each EndpointBuilder via .introspect(). Path parameters are serialized through the ParseStringSchemaBuilder.serialize() method when a route() template is used; query parameters are serialized to a query string; and the request body is JSON-encoded for methods that accept a body.

      Type Parameters

      • T extends ApiContract

        The exact API contract type, preserving all endpoint builder generics so that params, body, query, and response types are fully inferred.

      Parameters

      • contract: T

        The API contract created with defineApi().

      • options: ClientOptions = {}

        Client configuration (base URL, auth token, custom fetch, etc.).

      Returns TypedClient<T>

      A TypedClient whose shape mirrors the contract.

      import { api } from 'todo-shared';
      import { createClient } from '@cleverbrush/web';

      const client = createClient(api, {
      baseUrl: 'https://api.example.com',
      getToken: () => localStorage.getItem('token'),
      onUnauthorized: () => { window.location.href = '/login'; },
      });

      // Fully typed — params, body, and response types are inferred.
      const todos = await client.todos.list();
      const todo = await client.todos.get({ params: { id: 1 } });
      const created = await client.todos.create({ body: { title: 'Buy milk' } });