Query
The query, also called handler, is a callback function that exists on a route and invoked when a request with the query's path is sent from the client.
API
type Arg<TCtx, TInput> = {
ctx: TCtx;
input: TInput;
};
type Query<TCtx, TInput, TReturn> = (arg: Arg) => Promise<TReturn> | TReturn;
Examples
const router = tsdl.router({
fruit: tsdl.router({
addOne: tsdl
.input(z.string().regex(/^[\w\s]+$/))
.query(async ({ input }) => {
await db.addOne(input);
}),
removeOne: tsdl.input(z.number()).query(({ input }) => {
fruit = fruit.filter((v) => v.id !== input);
}),
all: tsdl.query(() => fruit),
}),
});
See middleware page for context examples