Function: createClient()
createClient<
T>(contract,baseUrl,options?):Client<T["endpoints"],InferError<T>>
Defined in: src/client.ts:111
Creates a typed API client from a contract.
Type Parameters
| Type Parameter |
|---|
T extends Contract |
Parameters
| Parameter | Type | Description |
|---|---|---|
contract | T | The contract. |
baseUrl | string | The base URL for all requests. |
options | CreateClientOptions | The client options. |
Returns
Client<T["endpoints"], InferError<T>>
Example
ts
import { createClient } from 'sorbus';
import { contract } from './contract';
const api = createClient(contract, '/api/v1', {
headers: () => ({
Authorization: `Bearer ${getToken()}`,
}),
serializeKey: 'snake',
normalizeKey: 'camel',
});
const { invoices } = await api.invoices.index();
const result = await api.invoices.show(
{ id: '1' },
{ catch: [404] },
);
if (result.ok) {
console.log(result.data);
}