Examples

Copy-paste patterns that combine multiple Orveth packages. Each snippet uses only symbols exported from public entrypoints—see the package index for import lists.

HTTP API with config and logging

Parse PORT at startup so misconfiguration fails before listening. Log from a route handler without blocking the response.

src/main.ts
import { parseConfig, createProcessEnvSource, requiredString } from "@orveth/config";
import { ConsoleLogger } from "@orveth/logger";
import { Orveth } from "orveth";

const env = parseConfig(createProcessEnvSource(), {
  PORT: requiredString(),
});
const log = new ConsoleLogger({ name: "api" });
const app = new Orveth();

app.get("/health", (ctx) => {
  log.info("health_check");
  return ctx.ok({ ok: true });
});

await app.listen(Number(env.PORT));

Request logging middleware

Middleware must call next() unless it ends the request. This pattern records duration after the downstream handler finishes.

src/middleware/log.ts
import type { Middleware } from "@orveth/server";

export const logRequests: Middleware = async (ctx, next) => {
  const started = performance.now();
  await next();
  const ms = performance.now() - started;
  console.info(`[${ctx.method} ${ctx.path}] ${ms.toFixed(1)}ms`);
};

TLS + JWT-protected routes

Issue tokens on login, verify the Authorization bearer on protected routes, and bind HTTPS with PEM files. See JWT and HTTPS for API details.

src/secure.ts
import { signJwt, verifyBearerJwt } from "@orveth/jwt";
import { Orveth } from "orveth";
import { listenHttps, readTlsFilePair } from "@orveth/https";

const secret = process.env.JWT_SECRET!;
const app = new Orveth();

app.post("/login", async (ctx) => {
  const token = await signJwt({ sub: "demo" }, secret, { expiresIn: "1h" });
  return ctx.ok({ token });
});

app.get("/me", async (ctx) => {
  const payload = await verifyBearerJwt<{ sub: string }>(ctx.request.headers.authorization, secret);
  return ctx.ok({ sub: payload.sub });
});

const tls = await readTlsFilePair({
  certPath: process.env.TLS_CERT_PATH!,
  keyPath: process.env.TLS_KEY_PATH!,
});
await listenHttps(app, 443, tls);

Prisma readiness and shutdown

Your application owns the Prisma schema and client. Orveth adds a health route factory and signal handlers for clean disconnect.

src/db-health.ts
import { PrismaClient } from "@prisma/client";
import { Orveth } from "orveth";
import { createDatabaseHealthRoute, registerPrismaShutdown } from "@orveth/prisma";

const prisma = new PrismaClient();
registerPrismaShutdown(prisma);

const app = new Orveth();
app.get("/health/db", createDatabaseHealthRoute({ client: prisma }));
await app.listen(3000);