/** * A read's HTTP answer. Decided ONCE per query, at projection, from its `rows:` schema — the rule * `@ultimat3/action`'s `record-wire.ts` already follows: a query whose `rows:` is an entity row * schema ALWAYS answers `{ data, records }` behind `x-ultimate-records: 1` (records may be `{}`), * and every other query answers the bare rows (or `Page`) it always did. * * One body shape per operation — until 21.0.0 a read sent the envelope only when a row came back, * so its OpenAPI was a `oneOf` over "sometimes" and a generated client had to branch on the data. */ import type { RecordRows, Row } from '@ultimat3/core'; import { DATES_HEADER, encodeRecordEnvelope, RECORDS_HEADER } from '@ultimat3/core'; import { hasEntityRows, rowsOf } from '@ultimat3/entity'; import { cappedWireDates, jsonResponse } from '@ultimat3/http'; import type { StandardSchemaV1 } from '@ultimat3/schema'; import type { Page } from './pagination'; /** Turns one read's answer into its response. Built once per route, never per request. */ export type RecordAnswer = (answer: readonly object[] | Page) => Response; /** * `undefined` or an unbranded schema answers the plain `json()` it always did. The rows are * collected one at a time against the ROW schema, since the answer's own shape — an array or a * `Page` — is the route's, not the declaration's. */ export function recordAnswerFor(rows: StandardSchemaV1 | undefined): RecordAnswer { if (!answersRecords(rows)) return (answer) => jsonResponse(answer, datesOf(answer)); return (answer) => { const { records, order } = collect(rows, isPage(answer) ? answer.rows : answer); return jsonResponse(encodeRecordEnvelope(answer, records, undefined, order), { headers: { [RECORDS_HEADER]: '1', ...datesOf(answer).headers }, }); }; } /** * The instants of a read's DATA — the rows, the `Page` or the one row — by path * (`@ultimat3/core`'s `wire-dates.ts`), so the typed client hands back the `Date` the row type * promises. A header, never the body: the body and the OpenAPI document stay what they were. * Capped (`cappedWireDates`, 4 KB — `@ultimat3/action`'s `record-wire.ts` follows the same rule): * an answer needing one path per instant past that sends NO header and is logged once per query. */ function datesOf(data: unknown): { readonly headers: Record } { const dates = cappedWireDates(data); return { headers: dates === undefined ? {} : { [DATES_HEADER]: dates } }; } /** * The same decision for a `single: true` read, whose answer is ONE row: the row itself, or the * envelope with that row as `data` and its records beside it — the shape an action's record wire * answers for a single output, so the transport unwraps both the same way. */ export function recordRowAnswerFor(rows: StandardSchemaV1 | undefined): (row: object) => Response { if (!answersRecords(rows)) return (row) => jsonResponse(row, datesOf(row)); return (row) => { const { records, order } = collect(rows, [row]); return jsonResponse(encodeRecordEnvelope(row, records, undefined, order), { headers: { [RECORDS_HEADER]: '1', ...datesOf(row).headers }, }); }; } /** The one predicate the route and the OpenAPI projection both ask: can this read answer records? */ export function answersRecords(rows: StandardSchemaV1 | undefined): rows is StandardSchemaV1 { return rows !== undefined && hasEntityRows(rows); } /** `Array.isArray` does not narrow a `readonly` array out of a union, so this says it once. */ function isPage(answer: readonly object[] | Page): answer is Page { return !Array.isArray(answer); } /** * Every entity row across the answer's rows: record type -> record key -> row. First sighting wins, * `rowsOf`'s own rule — one record shown twice is one record. Built in `Map`s and handed out as * null-prototype objects: a record type and a record key are data, and `__proto__` must stay a key. * * `order` is each type's keys as the answer met them. An integer-like key is an array index to * every object, so the records object re-sorts it; `encodeRecordEnvelope` sends the order only for * a type whose object lost it, so a uuid-keyed answer's body is what it always was. */ function collect( schema: StandardSchemaV1, list: readonly object[], ): { readonly records: Readonly>; readonly order: Readonly>; } { const out = new Map>(); for (const row of list) { for (const [type, byKey] of Object.entries(rowsOf(schema, row))) { const bucket = out.get(type) ?? new Map(); out.set(type, bucket); for (const [key, found] of Object.entries(byKey)) if (!bucket.has(key)) bucket.set(key, found); } } return { records: nullProto([...out].map(([type, bucket]) => [type, nullProto(bucket)] as const)), order: nullProto([...out].map(([type, bucket]) => [type, [...bucket.keys()]] as const)), }; } function nullProto(entries: Iterable): Readonly> { const target = Object.create(null) as Record; for (const [key, value] of entries) target[key] = value; return target; }