You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
26 lines
847 B
26 lines
847 B
import { createError } from "h3";
|
|
|
|
function parsePositiveInt(raw: string | undefined, fallback: number, label: string): number {
|
|
if (raw === undefined || raw === "") {
|
|
return fallback;
|
|
}
|
|
const n = Number(raw);
|
|
if (!Number.isInteger(n) || n < 1) {
|
|
throw createError({ statusCode: 400, statusMessage: `${label} 须为正整数` });
|
|
}
|
|
return n;
|
|
}
|
|
|
|
const ALLOWED_PAGE_SIZES = new Set([10, 20, 50]);
|
|
|
|
export function parseMeMediaAssetsQuery(q: { page?: string; pageSize?: string }): {
|
|
page: number;
|
|
pageSize: number;
|
|
} {
|
|
const page = parsePositiveInt(q.page, 1, "page");
|
|
const pageSizeRaw = parsePositiveInt(q.pageSize, 20, "pageSize");
|
|
if (!ALLOWED_PAGE_SIZES.has(pageSizeRaw)) {
|
|
throw createError({ statusCode: 400, statusMessage: "pageSize 须为 10、20 或 50" });
|
|
}
|
|
return { page, pageSize: pageSizeRaw };
|
|
}
|
|
|