trails/packages/api/src/routes.ts
Ullrich Schäfer 61a2d0085b
one source of truth for Route/Activity shapes; enforce api contracts
Route and Activity existed three times: hand-written interfaces in
packages/types, Zod contracts in packages/api, and Drizzle columns in
packages/db — each with different fields and nullability. The
hand-written ones had drifted so far they had zero importers; the Zod
contracts were advisory because v1 handlers hand-rolled Response.json
shapes nothing validated.

- packages/types keeps only what both apps actually share (Waypoint,
  WaypointPoiTags) and documents where row types and wire contracts
  live; the dead Route/RouteMetadata/RouteVersion/Activity interfaces
  are gone
- packages/db exports canonical inferred row types (RouteRow,
  ActivityRow, RouteVersionRow, UserRow)
- packages/api contracts are reconciled with the real wire format
  (RouteVersionSchema gains the id and createdBy fields the endpoint
  has always returned) and gain Create*ResponseSchemas
- apiJson(schema, payload) in api-guard parses every v1 response
  through its contract: drift is now a thrown ZodError in tests/CI,
  unknown keys are stripped, and payloads are compile-checked as
  z.input of the schema

Enforcement immediately caught two real drifts: nullable DB
descriptions could ship null where the contract promises string (now
coalesced at the boundary), and GET /api/v1/activities/:id was missing
the routeName and photos fields its contract declares.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-10 07:39:50 +02:00

79 lines
2.4 KiB
TypeScript

import { z } from "zod";
/** Route summary for list views */
export const RouteSummarySchema = z.object({
id: z.uuid(),
name: z.string(),
description: z.string(),
distance: z.number().nullable(),
elevationGain: z.number().nullable(),
elevationLoss: z.number().nullable(),
routingProfile: z.string().nullable(),
dayBreaks: z.array(z.number()),
geojson: z.string().nullable(),
createdAt: z.iso.datetime(),
updatedAt: z.iso.datetime(),
});
/** Route version info */
export const RouteVersionSchema = z.object({
id: z.uuid(),
version: z.number(),
createdBy: z.string().nullable(),
changeDescription: z.string().nullable(),
createdAt: z.iso.datetime(),
});
/** Full route detail */
export const RouteDetailSchema = RouteSummarySchema.extend({
gpx: z.string().nullable(),
versions: z.array(RouteVersionSchema),
});
/** Paginated route list response */
export const RouteListResponseSchema = z.object({
routes: z.array(RouteSummarySchema),
nextCursor: z.string().nullable(),
});
/** Create route request */
export const CreateRouteRequestSchema = z.object({
name: z.string().min(1).max(200),
description: z.string().max(5000).default(""),
gpx: z.string().optional(),
routingProfile: z.string().optional(),
});
/** Update route request */
export const UpdateRouteRequestSchema = z.object({
name: z.string().min(1).max(200).optional(),
description: z.string().max(5000).optional(),
gpx: z.string().optional(),
});
/** Compute route request (BRouter proxy) */
export const ComputeRouteRequestSchema = z.object({
waypoints: z.array(z.object({
lat: z.number(),
lon: z.number(),
})).min(2),
profile: z.string().default("fastbike"),
noGoAreas: z.array(z.object({
points: z.array(z.object({
lat: z.number(),
lon: z.number(),
})),
})).optional(),
});
/** Response to POST /api/v1/routes */
export const CreateRouteResponseSchema = z.object({ id: z.uuid() });
export type RouteSummary = z.infer<typeof RouteSummarySchema>;
export type CreateRouteResponse = z.infer<typeof CreateRouteResponseSchema>;
export type RouteVersion = z.infer<typeof RouteVersionSchema>;
export type RouteDetail = z.infer<typeof RouteDetailSchema>;
export type RouteListResponse = z.infer<typeof RouteListResponseSchema>;
export type CreateRouteRequest = z.infer<typeof CreateRouteRequestSchema>;
export type UpdateRouteRequest = z.infer<typeof UpdateRouteRequestSchema>;
export type ComputeRouteRequest = z.infer<typeof ComputeRouteRequestSchema>;