iterate.com / cloud

git clone https://git.iterate.com/repos/cloud

cloud / internal-packages / dummy-petshop / src / openapi.tsmain at aacbc74

/**
 * The pet shop's typed "pets" API, defined once as oRPC procedures: `GET /openapi.json` is the
 * OpenAPI 3.1 document generated from them, and `GET|POST /api/v2/*` serves them REST-shaped.
 *
 * Every procedure runs behind the same OAuth bearer check as the rest of the
 * shop: the worker resolves the request's access token with `accessGrant`
 * (worker.ts) and hands the resulting owner in as the oRPC context, so an
 * absent or dead token is a 401 before any procedure body runs.
 *
 * This is the shape that lets the pet shop be consumed as a real typed/OpenAPI
 * upstream by the OS's `itx.connectToOpenApi`
 * (test/vitest/os/library-connectors.e2e.test.ts).
 */
import { OpenAPIGenerator } from "@orpc/openapi";
import { OpenAPIHandler } from "@orpc/openapi/fetch";
import { ORPCError, os } from "@orpc/server";
import { ZodToJsonSchemaConverter } from "@orpc/zod/zod4";
import { z } from "zod";
import type { Pet } from "./pets.ts";

/**
 * What every pets procedure is given: the authenticated owner (the `sub` from
 * the resolved access grant) and the request-scoped pet catalogue the shop
 * handler seeds. Injected via oRPC's `.handle(request, { context })`, so the
 * worker's bearer check runs before any procedure body.
 */
export interface PetsContext {
  owner: string;
  pets: Pet[];
}

const Pet = z.object({
  id: z.string().describe("Stable pet identifier, e.g. pet-1."),
  name: z.string().describe("The pet's name."),
  species: z.string().describe("The pet's species, e.g. beagle."),
});

const base = os.$context<PetsContext>();

const listPets = base
  .route({ method: "GET", path: "/pets", summary: "List the account's pets" })
  .output(z.object({ owner: z.string(), pets: z.array(Pet) }))
  .handler(({ context }) => ({ owner: context.owner, pets: context.pets }));

const getPet = base
  .route({ method: "GET", path: "/pets/{id}", summary: "Fetch one pet by id" })
  .input(z.object({ id: z.string().describe("The pet id to fetch.") }))
  .output(Pet)
  .handler(({ input, context }) => {
    const pet = context.pets.find((candidate) => candidate.id === input.id);
    if (!pet) throw new ORPCError("NOT_FOUND", { message: `No pet with id ${input.id}` });
    return pet;
  });

const createPet = base
  .route({ method: "POST", path: "/pets", summary: "Add a pet to the account" })
  .input(z.object({ name: z.string().min(1), species: z.string().min(1) }))
  .output(Pet)
  .handler(({ input, context }) => {
    const pet: Pet = {
      id: `pet-${context.pets.length + 1}`,
      name: input.name,
      species: input.species,
    };
    context.pets.push(pet);
    return pet;
  });

const petsRouter = { listPets, getPet, createPet };

// One generator and handler at module scope: they are pure over the router, and only the
// per-request `context` varies (passed to `.handle`).
const openapiGenerator = new OpenAPIGenerator({
  schemaConverters: [new ZodToJsonSchemaConverter()],
});
const openapiHandler = new OpenAPIHandler(petsRouter);

/** The generated OpenAPI 3.1 document for the pets procedures. */
export async function petshopOpenApiDocument(baseUrl: string) {
  return openapiGenerator.generate(petsRouter, {
    info: { title: "dummy-petshop pets API", version: "2.0.0" },
    servers: [{ url: `${baseUrl}/api/v2` }],
    security: [{ bearer: [] }],
    components: {
      securitySchemes: { bearer: { type: "http", scheme: "bearer" } },
    },
  });
}

/** Serve `/api/v2/*` with the authenticated owner as context; null when no procedure matched. */
export async function handlePetsApiRequest(
  request: Request,
  context: PetsContext,
): Promise<Response | null> {
  const { matched, response } = await openapiHandler.handle(request, {
    prefix: "/api/v2",
    context,
  });
  return matched ? response : null;
}