25 lines
624 B
TypeScript
25 lines
624 B
TypeScript
/**
|
|
* Makes a damaged row fail its query with a 500 rather than silently vanish
|
|
* from a list. Every service checks the rows of its list queries with it.
|
|
*
|
|
* @module
|
|
*/
|
|
|
|
import { APIException } from "../lib/api-exception.ts";
|
|
|
|
/**
|
|
* Returns `rows` turned by `toApi`, once `isRow` has accepted each.
|
|
*
|
|
* @throws {APIException} 500 when a row doesn't pass `isRow`.
|
|
*/
|
|
export function parseRows<R, T>(
|
|
rows: unknown[],
|
|
isRow: (row: unknown) => row is R,
|
|
toApi: (row: R) => T,
|
|
): T[] {
|
|
if (!rows.every(isRow)) {
|
|
throw new APIException(500, "Malformed data in the database");
|
|
}
|
|
return rows.map(toApi);
|
|
}
|