feat: add asset variants endpoint
This commit is contained in:
43
apps/web/app/api/assets/[id]/variants/route.ts
Normal file
43
apps/web/app/api/assets/[id]/variants/route.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { getDb } from "@tline/db";
|
||||
|
||||
import { shapeVariants } from "./shape";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
const paramsSchema = z.object({
|
||||
id: z.string().uuid(),
|
||||
});
|
||||
|
||||
export async function GET(
|
||||
_request: Request,
|
||||
context: { params: Promise<{ id: string }> },
|
||||
): Promise<Response> {
|
||||
const rawParams = await context.params;
|
||||
const paramsParsed = paramsSchema.safeParse(rawParams);
|
||||
if (!paramsParsed.success) {
|
||||
return Response.json(
|
||||
{ error: "invalid_params", issues: paramsParsed.error.issues },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
const db = getDb();
|
||||
const rows = await db<
|
||||
{
|
||||
kind: string;
|
||||
size: number;
|
||||
key: string;
|
||||
mime_type: string;
|
||||
width: number | null;
|
||||
height: number | null;
|
||||
}[]
|
||||
>`
|
||||
select kind, size, key, mime_type, width, height
|
||||
from asset_variants
|
||||
where asset_id = ${paramsParsed.data.id}
|
||||
`;
|
||||
|
||||
return Response.json(shapeVariants(rows));
|
||||
}
|
||||
19
apps/web/app/api/assets/[id]/variants/shape.ts
Normal file
19
apps/web/app/api/assets/[id]/variants/shape.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
type VariantRow = {
|
||||
kind: string;
|
||||
size: number;
|
||||
key: string;
|
||||
};
|
||||
|
||||
type VariantShape = {
|
||||
kind: string;
|
||||
size: number;
|
||||
key: string;
|
||||
};
|
||||
|
||||
export function shapeVariants(rows: VariantRow[]): VariantShape[] {
|
||||
return rows.map((row) => ({
|
||||
kind: row.kind,
|
||||
size: row.size,
|
||||
key: row.key,
|
||||
}));
|
||||
}
|
||||
13
apps/web/src/__tests__/variants-route.test.ts
Normal file
13
apps/web/src/__tests__/variants-route.test.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { test, expect } from "bun:test";
|
||||
|
||||
test("variants route returns only kind/size/key fields", async () => {
|
||||
const { shapeVariants } = await import(
|
||||
"../../app/api/assets/[id]/variants/shape",
|
||||
);
|
||||
const rows = [
|
||||
{ kind: "video_mp4", size: 720, key: "derived/video/a.mp4", mime_type: "video/mp4" },
|
||||
];
|
||||
expect(shapeVariants(rows)).toEqual([
|
||||
{ kind: "video_mp4", size: 720, key: "derived/video/a.mp4" },
|
||||
]);
|
||||
});
|
||||
Reference in New Issue
Block a user