Skip to content

[M2.1a] API skeleton - #27

Merged
samuelmbabhazi merged 10 commits into
mainfrom
feat/m2.1a-api-skeleton
Nov 3, 2026
Merged

samuelmbabhazi merged 10 commits into
mainfrom
feat/m2.1a-api-skeleton

Conversation

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator

Task

[M2.1a] API skeleton (first half of M2.1, split recorded in docs/PLAN.md). Refs #20

What changed

Domain (packages/domain)

  • Secret<T>: wraps credentials and renders [REDACTED] through String(), template literals, JSON.stringify, util.inspect; the value is reachable only via expose(). Constant-time equals. TypeScript counterpart of Go lib/secret (CLAUDE.md rule 8).
  • DomainError base (kind + stable code + optional details) with InvalidInputError, NotFoundError, ConflictError. Pure, no framework imports.

Control plane (apps/api), NestJS 12

  • platform/config: zod schema for the environment (DATABASE_URL becomes a Secret, ports and URLs validated, every problem reported at once without echoing values), AppConfig typed accessor, .env read in development only.
  • platform/logging: pino JSON request logs through nestjs-pino. One line per request with request id (honours a safe x-request-id), method, URL, status, duration. authorization, cookie, set-cookie, x-api-key, join-token headers and token query parameters are redacted; bodies are never logged; health probes are not logged.
  • platform/http: global ProblemDetailsFilter producing RFC 9457 application/problem+json with type = https://shipkeep.dev/problems/<code>, code, instance, requestId, field errors for validation. Domain error kinds map to 400/401/403/404/409/412; unknown errors become an opaque 500 (internal messages never leak). Validation pipe: whitelist, forbid unknown properties, transform.
  • platform/prisma: Prisma 7 with the pg driver adapter; the connection string is exposed only into the driver. ping() for readiness. Schema and prisma.config.ts in place; the first models arrive with M2.1b. The generated client is not committed; pnpm install regenerates it.
  • platform/health: GET /api/v1/health (liveness) and GET /api/v1/ready (database probe, 503 problem when down).
  • platform/openapi: OpenAPI 3.1 document at /api/v1/openapi.json, bearer and session-cookie security schemes, UI at /api/v1/docs when OPENAPI_UI is on (default outside production). helmet on every response.
  • bootstrap.ts builds the app once for main.ts, the OpenAPI generator and the tests.

Contract and client

  • apps/api/openapi.json generated by pnpm gen:openapi (document-only run of the built API, no port, no database). packages/api-client exposes createShipkeepClient() over openapi-fetch with types generated by openapi-typescript (pnpm gen:api-client), plus isProblemDetails(). pnpm gen runs the whole chain; CI fails on drift.

Tooling

  • NestJS 12 is ESM-only, which Jest (CJS) cannot load: the API now tests with vitest like every other project (swc plugin for decorator metadata). Jest is removed from the workspace. pnpm test:integration runs api:test:integration (vitest, Testcontainers PostgreSQL or DATABASE_URL).
  • @nx/nest removed (generator only, pinned to Nest <12).

Why

Every control plane module (identity, fleet, audit...) needs configuration, logging, error mapping, the database client and the OpenAPI contract in place first. SPEC section 8 fixes the base path, the problem-details format and the OpenAPI location.

How it was tested

  • pnpm nx run-many -t lint typecheck test build: all 7 projects green. Unit tests: 13 domain, 15 api (config schema, filter mapping, HTTP pipeline with security headers, 404 problem, readiness 503, OpenAPI document and UI), 3 api-client, 86 config-schema, 3 proto-ts.
  • pnpm test:integration: the API starts against a real PostgreSQL 17 started by Testcontainers and GET /api/v1/ready returns {"status":"ok","checks":{"database":"ok"}}.
  • pnpm gen twice: no drift. prettier --check: clean.
  • Local pnpm dev:infra stack verified earlier today (PR [M0.5] Configurable dev stack ports #26).

Security considerations

  • Secrets: DATABASE_URL is a Secret; the only expose() is the Prisma adapter constructor. Request logs redact credential headers and never include bodies. Internal errors are not echoed to clients.
  • helmet defaults; x-powered-by removed.
  • Validation rejects unknown properties (forbidNonWhitelisted).
  • No authentication yet (M2.2); the only routes are health, readiness and the OpenAPI document. The security-review label is set because this PR defines the error, logging and configuration foundations every later module inherits.

Checklist

  • Acceptance criteria from docs/PLAN.md are met and M2.1a is ticked
  • Lint, TS tests, Go tests and buf lint pass
  • Tests added, including redaction and unknown-property tests
  • No new shell invocation in product code (tools/gen/openapi.mjs runs the built API with fixed arguments and carries a justified nosemgrep)
  • Audit events: not applicable yet (no state changes; M2.1b adds the audit writer)
  • CLAUDE.md and README updated

@samuelmbabhazi
samuelmbabhazi merged commit 291fa0d into main Nov 3, 2026
10 checks passed
@samuelmbabhazi
samuelmbabhazi deleted the feat/m2.1a-api-skeleton branch November 3, 2026 12:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant