/** * Response-envelope unwrappers. * * Apps and mock servers commonly return either a bare value (`T` or * `T[]`) or an envelope `{ data: T }` / `{ data: T[] }`. These helpers * normalize either shape into a predictable `T[]` or `T | null` for * downstream code, eliminating the per-call `'data' in res ? ...` dance. * * Lifted out of the 59 apps that previously carried a local * `lib/unwrap.ts` copy. The two functions cover the response shapes used * by the molecule mock server and the typical resource routes — a bare * value, a `{ data }` envelope, and (for lists) the RICH pagination * envelope `{ data, total, limit, offset }` returned by the pre-built * `@molecule/api-resource-*` list endpoints (comment, review, * activity-feed, bookmark, …), and named list envelopes whose siblings * contain only list metadata, such as `{ items: [...], count: 10 }`. * * @module */ /** * Normalize an unknown response body into a typed array. * * Accepts: * - a bare array → returned as-is (cast) * - `{ data: T[] }` with optional list metadata → the inner array * - a named list such as `{ articles: T[], articlesCount: number }` or * `{ items: T[], count: number }` → the named array * - `HttpResponse` (i.e. `{ data: T[], status, ... }`) → the inner array * - `HttpResponse<{ data: T[] }>` (the response of an envelope-returning * endpoint as it arrives from `@molecule/app-http`'s `HttpClient`) → the * doubly-nested inner array * - `HttpResponse` wrapping any supported list envelope → the inner array * - anything else → `[]` * * Callers commonly pass either the raw JSON body (e.g. from `fetch().then(r => * r.json())`) or the `HttpResponse` returned by `useHttpClient().get(...)`. * Both shapes are handled here so pages don't have to remember to call * `unwrapList(res.data)` vs `unwrapList(res)`. * * An envelope must have exactly one collection array; its other keys may only * be pagination metadata (`count`, `total`, `page`, `limit`, cursors, `meta`, * `links`, etc.) or a numeric count named after the collection (`itemsCount` * or `items_count`). Metadata keys themselves are not collection names. * Resource fields such as `id`, `name` or `subtotal`, and multiple collection * arrays, make the body ambiguous and return `[]`. Select the intended array * explicitly in those cases. No resource fields are discarded, even when a * resource owns a `data` array. Raw bodies and HttpResponse bodies use the same * rule; the generic casts the rows without validating their schema. * * @param res - Raw response body OR an `HttpResponse` envelope from `@molecule/app-http`. * @returns A typed array `T[]`; never `null`/`undefined`. */ export declare function unwrapList(res: unknown): T[]; /** * Normalize a response's transport/data envelopes into a typed object. * Named API fields remain intact: `{ record: value }` stays `{ record: value }`. * Select that field explicitly, e.g. `unwrapSingle<{ record: Item }>(res)?.record`. * The generic asserts the body's type; it does not validate its schema at runtime. * * Accepts: * - a non-empty plain object → returned as-is (cast) * - `{ data: T }` envelope → the inner value * - `{ data: null }`, `{ data: undefined }`, `{ data: [] }`, or * `{ data: {} }` (mock-server's no-match shape) → `null` * - an empty object `{}` → `null` * - arrays, primitives, `null`, `undefined` → `null` * * The "envelope contains an array → null" branch handles the case * where the mock server returns `[]` for unmatched endpoints but the * caller expects a single resource. * * @param res - Raw response body (e.g. `HttpResponse.data` from `@molecule/app-http`). * @returns The typed resource `T`, or `null` when the response shape * indicates "no resource" (including the various empty envelopes * above). */ export declare function unwrapSingle(res: unknown): T | null; //# sourceMappingURL=unwrap.d.ts.map