Typed Requests
One of Hedystia's core strengths is that the client knows exactly what every route expects — body shape, query params, path params — and TypeScript enforces these at compile time.
How Typing Works
When you call createClient<typeof app>(), TypeScript extracts the route definitions from your server instance type. The client proxy is then typed according to those definitions.
This means:
- Passing the wrong body shape → TypeScript error
- Accessing a non-existent route → TypeScript error
- Supplying the wrong param type → TypeScript error
No runtime code generation. No schema sync step. The types update the moment you change your server.
Body Typing
import { createClient } from '@hedystia/client'
import type { app } from './server'
const client = createClient<typeof app>('http://localhost:3000')
// Client — TypeScript enforces the exact shape
const { data } = await client.products.post({
body: {
name: 'Widget',
price: 9.99,
tags: ['sale', 'new'], // optional, correctly typed
},
})Param Typing
Path parameters are inferred from the route path and schema:
import { createClient } from '@hedystia/client'
import type { app } from './server'
const client = createClient<typeof app>('http://localhost:3000')
// Client
await client.users.id(42).get() // ✓ TypeScript accepts number
await client.users.id('abc').get() // ✗ TypeScript error: expected numberQuery Typing
import { createClient } from '@hedystia/client'
import type { app } from './server'
const client = createClient<typeof app>('http://localhost:3000')
// Client
await client.search.get({
query: { q: 'hello', limit: 10 },
})Header Typing
import { createClient } from '@hedystia/client'
import type { app } from './server'
const client = createClient<typeof app>('http://localhost:3000')
// Client — TypeScript expects authorization header
await client.secure.get({
headers: { authorization: 'Bearer token' },
})Response Typing
The return type of data is inferred from the response schema:
import { createClient } from '@hedystia/client'
import type { app } from './server'
const client = createClient<typeof app>('http://localhost:3000')
// Client — TypeScript knows the shape of data
const { data } = await client.stats.get()
console.log(data?.users) // typed: number | null
console.log(data?.requests) // typed: number | nullError Typing
import { createClient } from '@hedystia/client'
import type { app } from './server'
const client = createClient<typeof app>('http://localhost:3000')
// Client
const { data, error } = await client.items.id(99).get()
if (error) {
console.log(error.message) // typed: string
console.log(error.code) // typed: number
}Nested Paths
Routes with multi-segment paths map to a chain of properties:
// Server route: GET /api/v1/users/:id/posts
app.group('/api', (app) =>
app.group('/v1', (app) =>
app.group('/users', (app) =>
app.get('/:id/posts', ({ params }) => [], {
params: h.object({ id: h.number().coerce() }),
})
)
)
)
// Client
const client = createClient<typeof app>('')
await client.api.v1.users.id(5).posts.get()Subscription Typing
Subscription callbacks are typed based on the data and error schemas:
import { createClient } from '@hedystia/client'
import type { app } from './server'
const client = createClient<typeof app>('http://localhost:3000')
// Client
client.feed.subscribe(({ data, error }) => {
if (data) {
console.log(data.item) // typed: string
console.log(data.timestamp) // typed: number
}
if (error) {
console.log(error.reason) // typed: string
}
})Cancellation and custom transports
HTTP request options accept a native AbortSignal:
const controller = new AbortController();
const request = client.users.get({ signal: controller.signal });
controller.abort();
const result = await request;Abort and network failures return status: 0, ok: false, data: null and the original error. HTTP errors retain their actual status. Compose timeouts with AbortSignal.timeout(milliseconds) where supported. Cancellation affects HTTP requests; SSE and WebSocket subscriptions retain their own unsubscribe/disconnect APIs.
Pass a Fetch-compatible function in the client configuration to test without a listening server:
const client = createClient<typeof app>("http://localhost", {
fetch: (input, init) => Promise.resolve(app.fetch(new Request(input, init))),
});Dynamic path arguments are encoded as individual URL segments. Pass decoded values: /, ?, #, percent signs and Unicode stay inside the parameter instead of changing the URL structure. Literal . and .. segments still follow the native URL normalizer; do not use them as identifiers.
Compact generated contracts
Generated route declarations now omit schema sections that were not supplied. Use an updated @hedystia/client when regenerating them. Missing request sections do not expose typed body, query or per-request header options. Declare an explicit schema, including h.any() when appropriate, to enable those inputs. Transport-wide headers remain available through the client factory options.
Missing response and error schemas become unknown, so inspect or narrow the returned value before reading its fields. Path placeholders without declared params accept string | number; explicit params and required request schemas keep their types. Subscription fields without schemas also use unknown. Older full declarations and explicit any contracts remain supported.
See compact declarations and measured sizes.
Global schemas in nested groups
Constructor-level schemas remain typed through nested .group() and .use() calls. Configure the global headers and body on the scoped Hedystia instance, then mount that instance under the main application:
import { Hedystia, h } from "hedystia";
import { createClient } from "@hedystia/client";
const scoped = new Hedystia({
headers: h.object({ "x-token": h.string() }),
body: h.object({ tenant: h.string() }),
}).group("/v1", (v1) => v1.group("/users", (users) => users
.post("/create", ({ body }) => ({ tenant: body.tenant }), {
response: h.object({ tenant: h.string() }),
}),
));
const app = new Hedystia().use("/api", scoped);
const client = createClient<typeof app>("https://example.com");
await client.api.v1.users.create.post({
headers: { "x-token": "session-token" },
body: { tenant: "acme" },
});The client requires these fields with their declared types instead of widening omitted local schemas to any. Required global body schemas apply to POST, PUT, PATCH and DELETE; GET keeps its inherited headers without requiring a body. Local path parameter types and declared response types remain intact.
The same inference works for createClient<typeof scoped>, mounting without a prefix, and parent globals applied to separately constructed children. Globals configured on a mounted child stay scoped to its routes and do not affect unrelated routes on the main application. Generated .d.ts contracts also retain the inherited headers and body.
