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.
36 lines
1.2 KiB
36 lines
1.2 KiB
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]);
|
|
const MAX_SEARCH_LEN = 200;
|
|
|
|
export function parseMeMediaAssetsQuery(q: { page?: string; pageSize?: string; search?: string }): {
|
|
page: number;
|
|
pageSize: number;
|
|
search: string | null;
|
|
} {
|
|
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" });
|
|
}
|
|
let search: string | null = null;
|
|
if (typeof q.search === "string") {
|
|
const s = q.search.trim();
|
|
if (s.length > MAX_SEARCH_LEN) {
|
|
throw createError({ statusCode: 400, statusMessage: `search 最多 ${MAX_SEARCH_LEN} 个字符` });
|
|
}
|
|
search = s.length ? s : null;
|
|
}
|
|
return { page, pageSize: pageSizeRaw, search };
|
|
}
|
|
|