Client
The Client interface is returned by createClient. It provides HTTP method helpers, interceptors, and access to default configuration.
interface Client {
defaults: RequestConfig
interceptors: {
request: InterceptorManager<RequestConfig>
response: InterceptorManager<HttpResponse<unknown>>
}
// Each method has two overloads:
// 1. Validating — when config.validate is provided, data is inferred from the validator
// 2. Generic — explicit <T>, or unknown when omitted
request<V extends Validator<unknown>>(config: RequestConfig & { validate: V }): Promise<HttpResponse<ValidatorOutput<V>>>
request<T>(config: RequestConfig): Promise<HttpResponse<T>>
get<V extends Validator<unknown>>(url: string, config: RequestConfig & { validate: V }): Promise<HttpResponse<ValidatorOutput<V>>>
get<T>(url: string, config?: RequestConfig): Promise<HttpResponse<T>>
// delete, head, options — same pattern
post<V extends Validator<unknown>, B = unknown>(url: string, body: B | undefined, config: RequestConfig & { validate: V }): Promise<HttpResponse<ValidatorOutput<V>>>
post<T, B = unknown>(url: string, body?: B, config?: RequestConfig): Promise<HttpResponse<T>>
// put, patch — same pattern
}
Properties
| Property | Type | Description |
|---|---|---|
defaults | RequestConfig | The default configuration. Mutable -- changes affect subsequent requests. |
interceptors.request | InterceptorManager<RequestConfig> | Request interceptor manager. |
interceptors.response | InterceptorManager<HttpResponse<unknown>> | Response interceptor manager. |
Methods
request<T>(config)
Make a request with the given config. The method and url must be specified in the config.
get<T>(url, config?)
Make a GET request. Type parameter T is the expected response data type.
post<T, B>(url, body?, config?)
Make a POST request. T is the response type, B is the body type. The body is passed as the second argument.
put<T, B>(url, body?, config?)
Make a PUT request. Same signature as post.
patch<T, B>(url, body?, config?)
Make a PATCH request. Same signature as post.
delete<T>(url, config?)
Make a DELETE request.
head<T>(url, config?)
Make a HEAD request.
options<T>(url, config?)
Make an OPTIONS request.
Example
import { createClient } from '@parcely/core'
const http = createClient({ baseURL: 'https://api.example.com' })
// GET
const { data } = await http.get<{ name: string }>('/users/me')
// POST with typed body
await http.post<{ id: string }, { name: string }>('/users', { name: 'Mickey' })
// Interceptors
const id = http.interceptors.request.use((config) => ({
...config,
headers: { ...config.headers, 'X-Trace': crypto.randomUUID() },
}))
http.interceptors.request.eject(id)