/** * Checks the rows of a list query, for the services. Every service answers a * row that doesn't have its table's shape with a 500 rather than leaving it * out, so that damage shows instead of making items silently vanish. * * @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( 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); }