Five journal API route handlers had the same inline lambda:
parsed.error.issues.map((i) => ({
field: i.path.join("."),
message: i.message,
}))
Pull it into `zodIssuesToFieldErrors(error)` next to FieldError in
`@trails-cool/api/errors.ts` and re-export it so all five handlers
share the same implementation. The path-flattening rule now lives in
one place, and new routes get it for free.
Public error-response shape is unchanged.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
31 lines
1.3 KiB
TypeScript
31 lines
1.3 KiB
TypeScript
import type { Route } from "./+types/api.v1.uploads";
|
|
import { requireApiUser, apiError } from "~/lib/api-guard.server";
|
|
import { PresignedUploadRequestSchema, ERROR_CODES, zodIssuesToFieldErrors } from "@trails-cool/api";
|
|
import { randomUUID } from "node:crypto";
|
|
|
|
const S3_ENDPOINT = process.env.S3_ENDPOINT ?? "http://localhost:3902";
|
|
const S3_BUCKET = process.env.S3_BUCKET ?? "trails-cool";
|
|
const S3_PUBLIC_URL = process.env.S3_PUBLIC_URL ?? S3_ENDPOINT;
|
|
|
|
/** POST /api/v1/uploads — generate presigned upload URL */
|
|
export async function action({ request }: Route.ActionArgs) {
|
|
if (request.method !== "POST") return new Response(null, { status: 405 });
|
|
await requireApiUser(request);
|
|
|
|
const body = await request.json().catch(() => null);
|
|
const parsed = PresignedUploadRequestSchema.safeParse(body);
|
|
if (!parsed.success) {
|
|
return apiError(400, ERROR_CODES.VALIDATION_ERROR, "Validation failed",
|
|
zodIssuesToFieldErrors(parsed.error));
|
|
}
|
|
|
|
const { filename, resourceType, resourceId } = parsed.data;
|
|
const key = `${resourceType}/${resourceId}/${randomUUID()}-${filename}`;
|
|
|
|
// Return the upload URL and the final public URL
|
|
return Response.json({
|
|
uploadUrl: `${S3_ENDPOINT}/${S3_BUCKET}/${key}`,
|
|
publicUrl: `${S3_PUBLIC_URL}/${S3_BUCKET}/${key}`,
|
|
key,
|
|
});
|
|
}
|