API Reference

new KizunaServer()

Bind a contract to a server handle for defining guards, request context, and the router.

new KizunaServer(contract) is a server handle bound to your contract. It is the server-side counterpart to k: define guards, request context, and the router without passing the contract again.

pnpm add @ts-kizuna/express
bun add @ts-kizuna/express
npm install @ts-kizuna/express
import { KizunaServer } from '@ts-kizuna/express';
pnpm add @ts-kizuna/fastify
bun add @ts-kizuna/fastify
npm install @ts-kizuna/fastify
import { KizunaServer } from '@ts-kizuna/fastify';
pnpm add @ts-kizuna/hono
bun add @ts-kizuna/hono
npm install @ts-kizuna/hono
import { KizunaServer } from '@ts-kizuna/hono';
pnpm add @ts-kizuna/next
bun add @ts-kizuna/next
npm install @ts-kizuna/next
import { KizunaServer } from '@ts-kizuna/next';

Parameters

new KizunaServer(contract: Contract, options?: ServerOptions): Server
ParameterTypeDescription
contractContractThe contract from k.contract
options.jobTransportJobTransportWhere a queued job goes. See Jobs.
options.onJobError(job: string, error: unknown) => voidCalled when a job queued without a transport rejects.
src/server/server.ts
import { KizunaServer } from '@ts-kizuna/express';
import { contract } from './contract';

export const server = new KizunaServer(contract);

Returns

The handle. Keep it as server, the serving surface for this contract:

MethodDescription
server.guardDefine the guard for one identity. See server.guard.
server.requestContextResolve one declared request-context value. See server.requestContext.
server.routerBind typed handlers to the contract or one group. See server.router.
server.jobsBind a handler to each declared job. See server.jobs.
server.apiAssemble it all into the object you mount. See server.api.

With jobs that you queue from your own code, name a transport here. Without one, queue runs the job in this process and it is lost on a crash:

export const server = new KizunaServer(contract, {
    jobTransport: myQueue,
});

Without a transport, a job that fails has already outlived its request and has nowhere to report. onJobError is where it surfaces; without one it is logged.

export const server = new KizunaServer(contract, {
    onJobError: (job, error) => Sentry.captureException(error, { tags: { job } }),
});

On this page