Send JSON and get a typed result back
POST a validated body and read a typed, runtime-validated result — input checked before the request, output checked after.
Task
You are POSTing JSON to an API and want the result back as a typed value — and
you would like bad input rejected before it leaves the process, not returned to
you as a 400 from the server.
Example
One way: validate the body going out and the response coming back, and type the
result with the stitch<T> generic.
import { , } from 'stitchapi';
import { } from 'zod';
const = <{ : number; : string }>({
: 'POST',
: 'https://api.example.com',
: '/users',
: { : (.({ : .() })) },
: (.({ : .(), : .() })),
});
// A bad body throws before the POST; a bad response throws after.
const = await ({ : { : 'Ada' } });user is typed { id: number; name: string }.
How it works
input.body is checked before the request — wrong input fails fast with no
network call. output is checked after the response returns. Either failure
throws STITCH_VALIDATION. The stitch<T>
generic types the resolved value, and toValidator() adapts the Zod schema to
the Validator the config expects — pass toValidator(z.object({...})), not a
raw z.object({...}). Any Standard Schema
validator (Valibot, ArkType) goes through the same toValidator() call.