Function: createClient()
Call Signature
createClient<
T>(contract,baseUrl,options?):InferClient<T>
Defined in: src/client.ts:83
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
InferClient<T>
The typed API client.
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);
}Call Signature
createClient<
T>(contract,baseUrl,options?):T
Defined in: src/client.ts:88
Creates a typed API client from a contract.
Type Parameters
| Type Parameter |
|---|
T |
Parameters
| Parameter | Type | Description |
|---|---|---|
contract | Contract | The contract. |
baseUrl | string | The base URL for all requests. |
options? | CreateClientOptions | The client options. |
Returns
T
The typed API client.
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);
}