Add all REST API endpoints for mobile app consumption: Routes: - GET /api/v1/routes — paginated list with cursor, Zod validation - GET /api/v1/routes/:id — full detail with GPX, versions - POST /api/v1/routes — create with Zod-validated body - PUT /api/v1/routes/:id — update, creates new version - DELETE /api/v1/routes/:id — returns 204 Activities: - GET /api/v1/activities — paginated list with cursor - GET /api/v1/activities/:id — full detail - POST /api/v1/activities — create with GPX stat extraction - DELETE /api/v1/activities/:id — returns 204 Supporting: - POST /api/v1/routes/compute — BRouter proxy - POST /api/v1/uploads — presigned upload URL generation Device management: - GET /api/v1/auth/devices — list connected devices with isCurrent - DELETE /api/v1/auth/devices/:id — revoke device token - Store device_name on token exchange Infrastructure: - requireApiUser() guard — returns 401 with structured error - apiError() helper for consistent error responses - All endpoints use Zod schemas from @trails-cool/api for validation Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
66 lines
2.2 KiB
TypeScript
66 lines
2.2 KiB
TypeScript
import type { Route } from "./+types/api.v1.routes._index";
|
|
import { requireApiUser, apiError } from "~/lib/api-guard.server";
|
|
import { listRoutes, createRoute } from "~/lib/routes.server";
|
|
import {
|
|
PaginationQuerySchema,
|
|
CreateRouteRequestSchema,
|
|
ERROR_CODES,
|
|
} from "@trails-cool/api";
|
|
|
|
/** GET /api/v1/routes — paginated route list */
|
|
export async function loader({ request }: Route.LoaderArgs) {
|
|
const user = await requireApiUser(request);
|
|
const url = new URL(request.url);
|
|
const query = PaginationQuerySchema.safeParse({
|
|
cursor: url.searchParams.get("cursor") ?? undefined,
|
|
limit: url.searchParams.get("limit") ?? undefined,
|
|
});
|
|
if (!query.success) {
|
|
return apiError(400, ERROR_CODES.VALIDATION_ERROR, "Invalid pagination params");
|
|
}
|
|
|
|
const allRoutes = await listRoutes(user.id);
|
|
const { cursor, limit } = query.data;
|
|
|
|
let startIdx = 0;
|
|
if (cursor) {
|
|
const idx = allRoutes.findIndex((r) => r.id === cursor);
|
|
startIdx = idx >= 0 ? idx + 1 : 0;
|
|
}
|
|
|
|
const page = allRoutes.slice(startIdx, startIdx + limit);
|
|
const nextCursor = startIdx + limit < allRoutes.length ? page[page.length - 1]?.id ?? null : null;
|
|
|
|
return Response.json({
|
|
routes: page.map((r) => ({
|
|
id: r.id,
|
|
name: r.name,
|
|
description: r.description,
|
|
distance: r.distance,
|
|
elevationGain: r.elevationGain,
|
|
elevationLoss: r.elevationLoss,
|
|
routingProfile: r.routingProfile,
|
|
dayBreaks: r.dayBreaks ?? [],
|
|
geojson: r.geojson,
|
|
createdAt: r.createdAt.toISOString(),
|
|
updatedAt: r.updatedAt.toISOString(),
|
|
})),
|
|
nextCursor,
|
|
});
|
|
}
|
|
|
|
/** POST /api/v1/routes — create a new route */
|
|
export async function action({ request }: Route.ActionArgs) {
|
|
if (request.method !== "POST") return new Response(null, { status: 405 });
|
|
const user = await requireApiUser(request);
|
|
|
|
const body = await request.json().catch(() => null);
|
|
const parsed = CreateRouteRequestSchema.safeParse(body);
|
|
if (!parsed.success) {
|
|
return apiError(400, ERROR_CODES.VALIDATION_ERROR, "Validation failed",
|
|
parsed.error.issues.map((i) => ({ field: i.path.join("."), message: i.message })));
|
|
}
|
|
|
|
const id = await createRoute(user.id, parsed.data);
|
|
return Response.json({ id }, { status: 201 });
|
|
}
|