Files
edgeever/apps/api/src/entity-utils.ts
T
Dariusz Koryto 518fafa86e fix(api): report bad request bodies and fractional limits as client errors (#484)
A malformed JSON body, or a body over the route's size limit, made Hono
throw an HTTPException that the API reported as a 500 internal error. In
the companion API the same requests returned 503 "unavailable, please
retry", so clients retried a request that could never succeed. These
now return 400 bad_request and 413 payload_too_large in the API's usual
error shape.

A fractional limit or offset such as ?limit=1.5 was passed to SQLite as
a float and failed with a datatype mismatch. Limits, offsets and cursors
are now truncated to whole numbers, and the two private copies of the
clamp helper reuse the shared one.
2026-10-06 21:39:22 +08:00

27 lines
874 B
TypeScript

export const createId = (prefix: string) => `${prefix}_${crypto.randomUUID().replace(/-/g, "")}`;
export const isoNow = () => new Date().toISOString();
// Limits, offsets and cursors are bound to SQL integer parameters, which
// reject fractions, so a value such as `?limit=1.5` is truncated first.
export const clampNumber = (value: number, min: number, max: number) =>
Number.isNaN(value) ? min : Math.min(Math.max(Math.trunc(value), min), max);
export const parseJsonArray = (json: string): string[] => {
try {
const value = JSON.parse(json);
return Array.isArray(value) ? value.filter((item) => typeof item === "string") : [];
} catch {
return [];
}
};
export const slugify = (value: string) =>
value
.trim()
.toLowerCase()
.replace(/['"]/g, "")
.replace(/[^\p{L}\p{N}]+/gu, "-")
.replace(/^-+|-+$/g, "")
.slice(0, 80);