API Development Workflow
Follow this workflow for any API change in this repository.
Core Rules
- Define API contract first in
src/api-serverless/openapi.yaml. - Start every new schema/model name with
Api. - After editing
openapi.yaml, run:cd src/api-serverless && npm run generate - Treat
src/api-serverless/src/generatedas generated-only.- Never edit files in this folder manually.
- Remember generation scope:
- Generates request/response body models.
- Does not generate routes.
- Does not generate query/path param types.
Route Implementation Rules
- Implement routes manually in files ending with
.routes.ts. - Ensure routes align 100% with
openapi.yaml(paths, params, payloads, responses). - If query/path param typing is needed, define those types manually in the route file (or nearby file as appropriate).
- Wire every new route file into
src/api-serverless/src/app.ts. - Validate route input with Joi using
getValidatedByJoiOrThrow(getValidatedByJoi) and a schema (typically defined in the route file). - Never mark routes as cached unless explicitly instructed.
Auth and Request Context Rules
- Use
needsAuthenticatedUser()when authentication is required. - Use
maybeAuthenticatedUser()when authentication is optional. - Use
getAuthenticationContext(req)after auth middleware when auth context is needed. - In routes, always initialize timer:
const timer = Timer.getFromRequest(req); - Pass
timerto downstream service calls and use it to time work as needed.
Route Layer Responsibilities
Keep routes thin:
- Validate input with Joi.
- Do only very light request preparation.
- Call an appropriate service class for business logic.
- Await the service result and return it.
Do not place heavy business logic in routes.
Practical Checklist
- Updated
src/api-serverless/openapi.yamlfirst. - All new schemas/models in OpenAPI start with
Api. - Ran
cd src/api-serverless && npm run generate. - Did not manually edit
src/api-serverless/src/generated/*. - Implemented/updated
.routes.tsfile(s) manually. - Added manual query/path param types where needed.
- Added Joi schema validation via
getValidatedByJoiOrThrow. - Applied auth middleware (
needsAuthenticatedUser/maybeAuthenticatedUser) correctly. - Used
getAuthenticationContext(req)where needed. - Wired
const timer = Timer.getFromRequest(req);and passed timer onward. - Kept route handler logic light and service-driven.
- Wired route file in
src/api-serverless/src/app.ts. - Verified route contract matches
openapi.yamlexactly.
Converted and distributed by TomeVault — claim your Tome and manage your conversions.