Skip to content

Support typed request/response schemas for OpenAPI generation #911

@tom-groves

Description

@tom-groves

Problem

Nitric generates OpenAPI specs during build (as shown in the architecture docs), but the generated specs only include route definitions — no request/response body schemas. This limits the usefulness of the generated spec for:

  1. API documentation — Consumers can't see what payloads are expected
  2. Client generation — Tools like openapi-typescript produce unknown types for bodies
  3. Contract testing — No machine-readable contract to validate against

Currently, developers must validate request bodies manually in handlers, but this knowledge doesn't flow back into the OpenAPI spec.

Suggested Solution

Allow defining schemas using plain TypeScript types:

import { api } from '@nitric/sdk';

interface CreateUserBody {
  name: string;
  email: string;
  age?: number;
}

interface CreateUserResponse {
  id: string;
  name: string;
  email: string;
}

api.post<CreateUserBody, CreateUserResponse>('/users', async (ctx) => {
  const body = ctx.req.json(); // typed as CreateUserBody
  // ...
  return { id: '123', name: body.name, email: body.email };
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions