/victorhahn-skills:contract-first-backendfrom victorhahn-skills
Bootstrap and work with contract-first TypeScript/Express backends where openapi.yaml is the source of truth and all types, runtime validation, and consumer clients are generated from the spec. Use when setting up a new service, adding routes, updating schemas, or generating typed clients.
Ships in victorhahn-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
You are an expert at contract-first API development using TypeScript, Express v5, and OpenAPI. The core principle: openapi.yaml is edited by hand; everything else is generated from it.
Determine the user's goal and act accordingly:
Read openapi.yaml, src/types/api.gen.ts, and src/types/helpers.ts before writing any code. The existing spec and generated types are authoritative.
| Concern | Tool |
|---|---|
| Runtime validation | express-openapi-validator (reads spec at startup, enforces every req/res) |
| Type generation | openapi-typescript (generates src/types/api.gen.ts) |
| Type utilities | src/types/helpers.ts (ReqBody / ResBody / PathParams / QueryParams) |
| Framework | Express v5 (async handlers, no try-catch needed) |
| Testing | Vitest + supertest |
| Lint/format | Biome |
| Package manager | pnpm |
When bootstrapping from scratch, create this structure:
my-service/
├── openapi.yaml
├── src/
│ ├── types/
│ │ ├── api.gen.ts ← generated (gitignored)
│ │ └── helpers.ts
│ ├── routes/
│ │ └── <resource>.ts
│ ├── middleware/
│ │ └── error-handler.ts
│ ├── app.ts
│ └── server.ts
├── test/
│ └── <resource>.test.ts
├── package.json
├── tsconfig.json
├── biome.json
└── .gitignore
package.json{
"name": "my-service",
"version": "0.1.0",
"type": "module",
"packageManager": "[email protected]",
"scripts": {
"generate": "openapi-typescript openapi.yaml -o src/types/api.gen.ts",
"predev": "pnpm generate",
"dev": "tsx watch src/server.ts",
"start": "tsx src/server.ts",
"typecheck": "tsc --noEmit",
"lint": "biome check .",
"format": "biome format --write .",
"test": "vitest run",
"test:watch": "vitest"
},
"dependencies": {
"express": "^5.1.0",
"express-openapi-validator": "^5.4.6"
},
"devDependencies": {
"@biomejs/biome": "^1.9.4",
"@types/express": "^5.0.1",
"@types/node": "^22.13.0",
"@types/supertest": "^6.0.2",
"openapi-typescript": "^7.6.1",
"supertest": "^7.0.0",
"tsx": "^4.19.3",
"typescript": "^5.7.3",
"vitest": "^3.0.4"
}
}
tsconfig.json{
"compilerOptions": {
"target": "ES2022",
"module": "Preserve",
"moduleResolution": "Bundler",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"noEmit": true,
"resolveJsonModule": true
},
"include": ["src", "test", "vitest.config.ts"]
}
biome.json{
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
"organizeImports": { "enabled": true },
"linter": { "enabled": true, "rules": { "recommended": true } },
"formatter": { "enabled": true, "indentStyle": "space", "indentWidth": 2, "lineWidth": 100 },
"files": { "ignore": ["src/types/api.gen.ts", "node_modules"] }
}
src/types/helpers.tsimport type { operations } from './api.gen'
export type ReqBody<Op extends keyof operations> =
operations[Op] extends { requestBody: { content: { 'application/json': infer B } } }
? B
: never
export type ResBody<Op extends keyof operations, S extends number = 200> =
operations[Op] extends { responses: { [K in S]: { content: { 'application/json': infer R } } } }
? R
: never
export type PathParams<Op extends keyof operations> =
operations[Op] extends { parameters: { path: infer P } }
? P
: Record<string, string>
export type QueryParams<Op extends keyof operations> =
operations[Op] extends { parameters: { query?: infer Q } }
? NonNullable<Q>
: never
src/middleware/error-handler.tsimport type { ErrorRequestHandler } from 'express'
interface OpenApiValidatorError extends Error {
status?: number
errors?: Array<{ path: string; message: string }>
}
export const errorHandler: ErrorRequestHandler = (err: OpenApiValidatorError, _req, res, _next) => {
const status = err.status ?? 500
res.status(status).json({
message: err.message,
...(err.errors && { errors: err.errors }),
})
}
src/app.tsimport express from 'express'
import { middleware as openApiValidator } from 'express-openapi-validator'
import { fileURLToPath } from 'node:url'
import { dirname, join } from 'node:path'
import { errorHandler } from './middleware/error-handler'
// import { widgetsRouter } from './routes/widgets'
const __dirname = dirname(fileURLToPath(import.meta.url))
export function createApp() {
const app = express()
app.use(express.json())
app.use(
openApiValidator({
apiSpec: join(__dirname, '../openapi.yaml'),
validateRequests: true,
validateResponses: process.env.NODE_ENV !== 'production',
}),
)
app.get('/health', (_req, res) => {
res.json({ status: 'ok' })
})
// app.use('/widgets', widgetsRouter)
app.use(errorHandler)
return app
}
src/server.tsimport { createApp } from './app'
const port = Number(process.env.PORT ?? 3000)
createApp().listen(port, () => console.log(`listening on http://localhost:${port}`))
openapi.yamlAlways include:
operationId (drives the type helpers)/widgets/{id}:
parameters:
- in: path
name: id
required: true
schema:
type: string
get:
operationId: getWidget
summary: Get a widget
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/Widget'
'404':
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
pnpm generate
import { Router } from 'express'
import type { RequestHandler } from 'express'
import type { ResBody, PathParams } from '../types/helpers'
export const widgetStore = new Map<string, ResBody<'getWidget'>>()
const router = Router()
const getWidget: RequestHandler<PathParams<'getWidget'>, ResBody<'getWidget'> | { message: string }> = (req, res) => {
const item = widgetStore.get(req.params.id)
if (!item) {
res.status(404).json({ message: 'Widget not found' })
return
}
res.json(item)
}
router.get('/:id', getWidget)
export { router as widgetsRouter }
src/app.tsimport { widgetsRouter } from './routes/widgets'
app.use('/widgets', widgetsRouter)
import { describe, it, expect, beforeEach } from 'vitest'
import supertest from 'supertest'
import { createApp } from '../src/app'
import { widgetStore } from '../src/routes/widgets'
const req = supertest(createApp())
beforeEach(() => widgetStore.clear())
it('returns 404 for unknown id', async () => {
const res = await req.get('/widgets/nope')
expect(res.status).toBe(404)
})
openapi.yamlpnpm generatepnpm test to confirm runtime validation still passesAdding a required field to a response: handlers returning that resource must include the new field. TypeScript will error if they don't.
Making a field optional: no breaking change to handlers, but tests asserting on that field may need updating.
Renaming a field: pnpm generate causes type errors everywhere the old name is used — TypeScript guides you to every callsite.
All consumers derive from openapi.yaml. Run these from the service root.
pnpm add openapi-fetch
pnpm dlx openapi-typescript openapi.yaml -o client/api.gen.ts
import createClient from 'openapi-fetch'
import type { paths } from './client/api.gen'
const client = createClient<paths>({ baseUrl: 'http://localhost:3000' })
// Fully typed — params, body, and response
const { data, error } = await client.GET('/widgets/{id}', {
params: { path: { id: 'abc' } },
})
pnpm add -D orval
orval.config.ts:
import { defineConfig } from 'orval'
export default defineConfig({
myApi: {
input: { target: 'openapi.yaml' },
output: {
mode: 'single',
target: 'client/api.ts',
client: 'react-query',
},
},
})
sdk.cfg.yaml:
package: mysdk
generate:
models: true
client: true
output: pkg/sdk/client.gen.go
go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen \
--config sdk.cfg.yaml openapi.yaml
pnpm dlx @openapitools/openapi-generator-cli generate \
-i openapi.yaml -g python -o client/python/
OpenAPI operation schemas map directly to MCP tool definitions. Pass openapi.yaml to any MCP codegen tool, or hand-write the mapping using the spec's operationId, parameters, and requestBody as the tool's name, inputSchema.
Run pnpm generate — the validator reads the file, not the generated types. They may have drifted.
Check the error response body — express-openapi-validator returns structured errors:
{
"message": "request/body must have required property 'name'",
"errors": [{ "path": "/body/name", "message": "..." }]
}
The handler returned a response that doesn't match the spec. Check err.message — it will name the field and path. Fix the handler's response shape, not the spec (unless the spec is wrong).
The path or method isn't defined in the spec. The validator rejects unknown routes before they reach your router. Add the route to openapi.yaml first.
src/types/api.gen.ts is always gitignored and regenerated — never hand-edit itapp.ts must have a corresponding path in openapi.yaml or the validator will 404 itoperationId must be globally unique across the spec — the type helpers key off itvalidateResponses stays off in production — it adds overhead and can 500 on edge cases not worth guarding in prodbeforeEach(() => store.clear()) or inject a test doubleWhat SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
Generated from the plugin's indexed repository (victorhahn/skills) — no native marketplace selected
npx claudepluginhub p/victorhahn-victorhahn-skills18 skills in this plugin
/ai-engineering-digestFetch a weekly briefing of what's trending and being discussed in the AI engineering landscape — HN, Reddit, GitHub — and synthesize it into a structured digest covering technical signal, notable projects, and the professional/cultural discourse around AI. Use when the user says "/ai-engineering-digest", "run the digest", "what's the ai meta this week", "weekly ai pulse", "what's trending in ai dev", or wants a broad briefing on the current state of AI engineering. Accepts an optional topic arg for on-demand narrower queries ("/ai-engineering-digest mcp")./aws-solutions-architectExpert AWS solutions architect with deep knowledge of AWS services, architecture patterns, best practices, cost optimization strategies, and Terraform AWS provider implementation. Use this skill whenever the user asks about AWS architecture, AWS service selection, infrastructure design, cost optimization, rightsizing, savings plans, Reserved Instances, AWS debugging, CloudWatch, IAM, networking (VPCs, subnets, security groups, Transit Gateway), ECS/EKS, Lambda, DynamoDB, RDS, S3, SQS/SNS, API Gateway, or any AWS service question. Also trigger for Terraform AWS provider questions, terraform-aws module usage, writing or reviewing .tf files that use AWS resources, and any question that starts with "what's the best way to..." in an AWS or infrastructure context. When in doubt about whether this is an AWS question, use this skill — it's better to over-apply than miss a relevant question.