27 lines
992 B
TypeScript
27 lines
992 B
TypeScript
// Query strings and list answers shared by the API modules.
|
|||
|
|
|
||
|
|
/** "?a=1&b=x" from the defined, non-empty values. Empty when nothing is set. */
|
||
|
|
export function qs(params: Record<string, string | number | boolean | null | undefined>): string {
|
||
|
|
const sp = new URLSearchParams();
|
||
|
|
for (const [k, v] of Object.entries(params)) {
|
||
|
|
if (v === undefined || v === null || v === "") continue;
|
||
|
|
sp.set(k, String(v));
|
||
|
|
}
|
||
|
|
const s = sp.toString();
|
||
|
|
return s ? `?${s}` : "";
|
||
|
|
}
|
||
|
|
|
||
|
|
/** A list answer may come bare or wrapped as {key: [...]}. Both give the array. */
|
||
|
|
export function asList<T>(raw: unknown, key: string): T[] {
|
||
|
|
if (Array.isArray(raw)) return raw as T[];
|
||
|
|
if (raw && typeof raw === "object" && Array.isArray((raw as Record<string, unknown>)[key])) {
|
||
|
|
return (raw as Record<string, T[]>)[key] as T[];
|
||
|
|
}
|
||
|
|
return [];
|
||
|
|
}
|
||
|
|
|
||
|
|
/** The If-Match header for a version check. */
|
||
|
|
export function ifMatch(version: number): Record<string, string> {
|
||
|
|
return { "If-Match": String(version) };
|
||
|
|
}
|