Files
accounted/lib/api/v1/registry.ts
T
Jakob WennbergandClaude Opus 4.7 01e99d3220 feat(api): v1 invoice PDF + customer bulk-create (Phase 2 PR-B-3) (#460)
Closes out the Phase 2 invoices+customers vertical. After this PR, every
write/read the dashboard does on these two resources is reachable via the
public API.

GET /api/v1/companies/{companyId}/invoices/{id}/pdf
  Read-only application/pdf endpoint. Mirrors the dashboard's internal
  /api/invoices/[id]/pdf so a downloaded PDF is byte-equivalent across
  surfaces. Drafts render with the "faktura-utkast-<id-slice>.pdf"
  filename (preview before send is a legitimate workflow); sent invoices
  use "faktura-<number>.pdf"; credit notes use "kreditfaktura-<number>.pdf"
  and embed the original invoice's löpnummer per ML 17 kap 22–23§
  back-reference; proforma + delivery notes get their own prefixes.

  Error codes: INVOICE_PDF_RENDER_FAILED (500, new),
  INVOICE_SEND_COMPANY_SETTINGS_MISSING (404, reused — same condition,
  same remediation).

POST /api/v1/companies/{companyId}/customers/bulk-create
  Mirrors /invoices/bulk-create exactly: same `{ results, summary }`
  shape, same all_or_nothing: true → 501 NOT_IMPLEMENTED contract, same
  50-item cap, same sequential processing. Per-item rollback isn't
  needed (customer insert is a single row), but per-item 23505 →
  CUSTOMER_DUPLICATE_ORG_NUMBER failure surfaces in the results array
  without echoing org_number (GDPR Art.5(1)(c): for sole traders
  org_number IS the personnummer). VIES validation runs per item,
  best-effort — a timeout leaves vat_number_validated=false but does
  NOT fail the item.

Registry: extended EndpointDefinition.response with an optional
  `contentType` field so the OpenAPI generator can emit
  `format: binary` schemas for non-JSON responses. The PDF endpoint is
  the first consumer; future binary endpoints (SIE export, ICS feeds)
  use the same hook.

Scope catalogue: added GET .../pdf → invoices:read,
  POST .../customers/bulk-create → customers:write.

Tests: 14 new integration cases (7 for PDF: sent / draft / credit-note
filename, 404, render-error 500, non-UUID 400, scope rejection; 7 for
customer bulk-create: happy path, dup-org error masking, max-50 cap,
all_or_nothing 501, dry-run preview, empty array, scope rejection).
Suite green: 3232 passing. Build + lint clean.

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-13 10:53:51 +02:00

321 lines
12 KiB
TypeScript

/**
* Single source of truth for the v1 REST surface.
*
* Every endpoint registers its Zod request/response schemas + agent-facing
* metadata (description, use-when, do-not-use-for, pitfalls, example) +
* OpenAPI `x-*` extensions (`x-action-risk`, `x-idempotent`, `x-reversible`,
* `x-dry-run-supported`).
*
* Three artefacts are derived from this registry:
* 1. The OpenAPI 3.1 spec at /api/v1/openapi.json (this file).
* 2. The MCP tool list (future — Phase 5).
* 3. Runtime validators (Zod itself, used by handlers).
*
* Phase 1 ships a minimal Zod→JSON-Schema converter. Phase 2 will swap in
* `@asteasolutions/zod-to-openapi` once the schema surface justifies the
* dependency. The registry shape stays stable across that change.
*/
import type { ZodTypeAny } from 'zod'
import type { ApiKeyScope } from '@/lib/auth/api-keys'
import { API_V1_VERSION } from './version'
export type HttpMethod = 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE'
export type ActionRisk = 'low' | 'medium' | 'high'
export interface EndpointDefinition {
/** HTTP method + path pattern, e.g. 'GET /api/v1/companies'. */
operation: string
method: HttpMethod
path: string
/** One-sentence summary; first sentence of the OpenAPI description. */
summary: string
/** Longer prose for the docs and the registered MCP tool description. */
description: string
/** Positive trigger — when should an agent reach for this endpoint? */
useWhen: string
/** Negative trigger — what looks similar but isn't this. */
doNotUseFor: string
/** Common pitfalls. Bullet-list style; agents see this in tool docs. */
pitfalls: string[]
/** One worked example (used by the contract-test suite). */
example: {
request?: Record<string, unknown>
response: Record<string, unknown>
}
/** Required scope; null for public endpoints. */
scope: ApiKeyScope | null
/** Action risk — informs whether the agent should confirm before calling. */
risk: ActionRisk
/** True for GET requests and well-known idempotent writes. */
idempotent: boolean
/** True for writes that can be undone by a single subsequent call (e.g. credit invoice). */
reversible: boolean
/** True for write endpoints that accept ?dry_run=true. */
dryRunSupported: boolean
/** Optional Zod schemas. */
request?: {
/** Path params (companyId, id, ...). */
params?: ZodTypeAny
/** Query params. */
query?: ZodTypeAny
/** Request body. */
body?: ZodTypeAny
}
response: {
/** Successful response body. */
success: ZodTypeAny
/** Stable error codes this endpoint can emit (cross-referenced with the docs). */
errorCodes?: string[]
/**
* Override the default 'application/json' content type for non-JSON
* responses (e.g. binary downloads). When set to 'application/pdf', the
* OpenAPI generator emits a `{ type: 'string', format: 'binary' }` schema
* instead of deriving from `success`. The `success` schema is still
* required (use `z.unknown()` as a marker) so existing registry consumers
* don't need to handle a missing field.
*/
contentType?: string
}
}
const ENDPOINTS = new Map<string, EndpointDefinition>()
/**
* Register an endpoint. Called from the route file at module load time:
*
* registerEndpoint({
* operation: 'companies.list',
* method: 'GET',
* path: '/api/v1/companies',
* ...
* })
*
* The wrapper does not depend on registration — scope resolution lives in
* `lib/auth/scopes.ts` so a missing register() call only affects docs, not
* runtime auth. CI test asserts every wrapped route appears in the registry.
*/
export function registerEndpoint(def: EndpointDefinition): void {
const key = `${def.method} ${def.path}`
if (ENDPOINTS.has(key)) {
// Duplicate registration is a bug — log loudly. Throwing during a route
// module's top-level eval would break unrelated routes; warn instead.
// eslint-disable-next-line no-console
console.warn(`[api/v1/registry] duplicate endpoint registration: ${key}`)
}
ENDPOINTS.set(key, def)
}
export function listEndpoints(): EndpointDefinition[] {
return Array.from(ENDPOINTS.values())
}
export function getEndpoint(method: HttpMethod, path: string): EndpointDefinition | undefined {
return ENDPOINTS.get(`${method} ${path}`)
}
// ──────────────────────────────────────────────────────────────────
// Minimal Zod → JSON Schema converter
// ──────────────────────────────────────────────────────────────────
// Phase 1 only registers a handful of endpoints with simple schemas. We
// implement just enough to cover them: object, string, number, boolean,
// uuid, array, optional, enum, literal, date-string. When the registry
// surface grows past Phase 2, swap this for @asteasolutions/zod-to-openapi.
interface JsonSchema {
type?: string | string[]
properties?: Record<string, JsonSchema>
required?: string[]
items?: JsonSchema
enum?: unknown[]
const?: unknown
format?: string
description?: string
additionalProperties?: boolean | JsonSchema
}
function zodToJsonSchema(schema: ZodTypeAny): JsonSchema {
const def = (schema as unknown as { _def: { typeName?: string; type?: string } })._def
// Zod 4 uses string discriminators on _def.type ('string', 'object', etc.).
// Fall back to the legacy typeName for cross-version safety.
const discriminator = def.type ?? def.typeName ?? ''
switch (discriminator) {
case 'string':
case 'ZodString':
return { type: 'string' }
case 'number':
case 'ZodNumber':
return { type: 'number' }
case 'boolean':
case 'ZodBoolean':
return { type: 'boolean' }
case 'array':
case 'ZodArray': {
const inner = (def as { element?: ZodTypeAny; type?: ZodTypeAny }).element
?? (def as { type?: ZodTypeAny }).type
return { type: 'array', items: inner ? zodToJsonSchema(inner) : {} }
}
case 'optional':
case 'ZodOptional':
case 'nullable':
case 'ZodNullable': {
const inner = (def as { innerType: ZodTypeAny }).innerType
return zodToJsonSchema(inner)
}
case 'object':
case 'ZodObject': {
const shape = (schema as unknown as { shape: Record<string, ZodTypeAny> }).shape
const properties: Record<string, JsonSchema> = {}
const required: string[] = []
for (const [key, value] of Object.entries(shape)) {
properties[key] = zodToJsonSchema(value)
const valueDef = (value as unknown as { _def: { typeName?: string; type?: string } })._def
const valueDisc = valueDef.type ?? valueDef.typeName ?? ''
if (valueDisc !== 'optional' && valueDisc !== 'ZodOptional') {
required.push(key)
}
}
return {
type: 'object',
properties,
...(required.length > 0 ? { required } : {}),
additionalProperties: false,
}
}
case 'enum':
case 'ZodEnum': {
const enumDef = def as { values?: unknown[]; entries?: Record<string, unknown> }
const values =
enumDef.values ??
(enumDef.entries ? Object.values(enumDef.entries) : [])
return { type: 'string', enum: values }
}
case 'literal':
case 'ZodLiteral': {
const value = (def as { value?: unknown; values?: unknown[] }).value
?? (def as { values?: unknown[] }).values?.[0]
return { const: value }
}
case 'union':
case 'ZodUnion': {
// Best-effort: emit a oneOf with each member converted. No top-level
// `type` constraint — the individual branches carry their own types
// (valid JSON Schema for a union).
const options = (def as { options?: ZodTypeAny[] }).options ?? []
return { oneOf: options.map(zodToJsonSchema) } as unknown as JsonSchema
}
default:
// Unknown construct → empty schema, accept anything.
return {}
}
}
// ──────────────────────────────────────────────────────────────────
// OpenAPI 3.1 spec generation
// ──────────────────────────────────────────────────────────────────
interface OpenApiSpec {
openapi: '3.1.0'
info: { title: string; version: string; description: string }
servers: Array<{ url: string }>
components: { securitySchemes: Record<string, unknown> }
security: Array<Record<string, unknown[]>>
paths: Record<string, Record<string, unknown>>
}
const SCHEME_NAME = 'ApiKey'
export function generateOpenApiSpec(serverUrl: string): OpenApiSpec {
const paths: OpenApiSpec['paths'] = {}
for (const def of ENDPOINTS.values()) {
// OpenAPI path syntax: {param} instead of :param.
const openApiPath = def.path.replace(/:([^/]+)/g, '{$1}')
// Binary responses (e.g. application/pdf) declare a `format: binary`
// schema rather than deriving from the Zod success type.
const successContent = def.response.contentType && def.response.contentType !== 'application/json'
? { [def.response.contentType]: { schema: { type: 'string', format: 'binary' } } }
: { 'application/json': { schema: zodToJsonSchema(def.response.success) } }
const operationDef: Record<string, unknown> = {
operationId: def.operation,
summary: def.summary,
description: [
def.description,
'',
`**Use when:** ${def.useWhen}`,
`**Do not use for:** ${def.doNotUseFor}`,
...(def.pitfalls.length > 0 ? ['', '**Pitfalls:**', ...def.pitfalls.map((p) => `- ${p}`)] : []),
].join('\n'),
'x-action-risk': def.risk,
'x-idempotent': def.idempotent,
'x-reversible': def.reversible,
'x-dry-run-supported': def.dryRunSupported,
...(def.scope ? { 'x-required-scope': def.scope } : {}),
responses: {
'200': {
description: 'Success',
content: successContent,
},
'400': { description: 'Validation error', $ref: '#/components/responses/Error' },
'401': { description: 'Unauthorized', $ref: '#/components/responses/Error' },
'403': { description: 'Insufficient scope', $ref: '#/components/responses/Error' },
'404': { description: 'Not found', $ref: '#/components/responses/Error' },
'429': { description: 'Rate limited', $ref: '#/components/responses/Error' },
'500': { description: 'Internal error', $ref: '#/components/responses/Error' },
},
}
if (!paths[openApiPath]) paths[openApiPath] = {}
paths[openApiPath][def.method.toLowerCase()] = operationDef
}
return {
openapi: '3.1.0',
info: {
title: 'gnubok API',
version: API_V1_VERSION,
description:
'Public REST API for gnubok — Swedish double-entry bookkeeping. ' +
'Every write supports dry-run via `?dry_run=true`. Every request must include ' +
'`Authorization: Bearer gnubok_sk_...`. See /docs/api for the cookbook.',
},
servers: [{ url: serverUrl }],
components: {
securitySchemes: {
[SCHEME_NAME]: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'gnubok_sk_<live|test>_<random>',
},
},
},
security: [{ [SCHEME_NAME]: [] }],
paths,
}
}
/**
* Test-only escape hatch. Clears the registry — used in unit tests so a test
* that registers a fake endpoint doesn't leak into the next test.
*/
export function _resetRegistryForTests(): void {
ENDPOINTS.clear()
}