Documentation / @zerotal/client / index / ApiClient
Class: ApiClient<Routes>
Defined in: client/src/ApiClient.ts:166
Type-safe HTTP client bound to a route map.
Do not instantiate directly — use createApiClient<Routes>(config).
Type Parameters
Routes
Routes extends ApiRouteMap
Constructors
Constructor
new ApiClient<
Routes>(config?):ApiClient<Routes>
Defined in: client/src/ApiClient.ts:181
Parameters
config?
ApiClientConfig = {}
Returns
ApiClient<Routes>
Methods
setToken()
setToken(
token):void
Defined in: client/src/ApiClient.ts:211
Update (or clear) the bearer token used for subsequent requests.
Parameters
token
TokenSource | null
Returns
void
get()
get<
Path>(path,params?,options?):Promise<ResponseOf<Routes,"GET",Path>>
Defined in: client/src/ApiClient.ts:224
Send a typed GET request.
Type Parameters
Path
Path extends string
Parameters
path
Path
params?
ParamRecord<Path>
options?
GetOptions<QueryOf<Routes, "GET", Path>>
Returns
Promise<ResponseOf<Routes, "GET", Path>>
Example
const user = await api.get('/api/users/{id}', { id: 1 }); // ^? UserResource
const list = await api.get('/api/users', undefined, { query: { page: 2 } });
post()
post<
Path>(path,body?,options?):Promise<ResponseOf<Routes,"POST",Path>>
Defined in: client/src/ApiClient.ts:247
Send a typed POST request. The body may be a plain object (JSON), FormData, Blob,
URLSearchParams, or a string.
Type Parameters
Path
Path extends string
Parameters
path
Path
body?
BodyOf<Routes, "POST", Path>
options?
MutationOptions<QueryOf<Routes, "POST", Path>>
Returns
Promise<ResponseOf<Routes, "POST", Path>>
Example
await api.post('/api/users', { name: 'Alice', email: 'alice@example.com' });
await api.post('/api/avatars', formData); // multipart upload
put()
put<
Path>(path,body?,options?):Promise<ResponseOf<Routes,"PUT",Path>>
Defined in: client/src/ApiClient.ts:257
Type Parameters
Path
Path extends string
Parameters
path
Path
body?
BodyOf<Routes, "PUT", Path>
options?
MutationOptions<QueryOf<Routes, "PUT", Path>>
Returns
Promise<ResponseOf<Routes, "PUT", Path>>
patch()
patch<
Path>(path,body?,options?):Promise<ResponseOf<Routes,"PATCH",Path>>
Defined in: client/src/ApiClient.ts:267
Type Parameters
Path
Path extends string
Parameters
path
Path
body?
BodyOf<Routes, "PATCH", Path>
options?
MutationOptions<QueryOf<Routes, "PATCH", Path>>
Returns
Promise<ResponseOf<Routes, "PATCH", Path>>
delete()
delete<
Path>(path,params?,options?):Promise<ResponseOf<Routes,"DELETE",Path>>
Defined in: client/src/ApiClient.ts:277
Type Parameters
Path
Path extends string
Parameters
path
Path
params?
ParamRecord<Path>
options?
Returns
Promise<ResponseOf<Routes, "DELETE", Path>>