|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#25467 cut 23] The read-side conversion of a stored metadata item by |
| 5 | + * `ObjectStackProtocolImplementation`, moved out of `protocol.ts` whole: the |
| 6 | + * ADR-0087 conversion chain over a stored body with its notices and TODOs |
| 7 | + * handed back (`convertStoredItemDetailed`, #4327/#17321/#25279), and the |
| 8 | + * read every stored row goes through, which warns once per conversion, row |
| 9 | + * and process and fills a page's declared `type` default |
| 10 | + * (`convertStoredItem`, #20101). |
| 11 | + * |
| 12 | + * A MOVE, not a redesign. Each function below is the body of the |
| 13 | + * `ObjectStackProtocolImplementation` method of the same name as it stood, |
| 14 | + * re-indented one level and otherwise unedited: the same `flow` exemption, |
| 15 | + * the same chain call, the same warn-once key and text, and the same page |
| 16 | + * default. |
| 17 | + * |
| 18 | + * ## How the moved bodies reach the class |
| 19 | + * |
| 20 | + * The shape #23050 set for `rest-server.ts` (its `data-action-routes.ts` |
| 21 | + * header carries the full reasoning), as `protocol-batch-data.ts` applies it. |
| 22 | + * The class keeps both methods — the published `dist/*.d.ts` lists their |
| 23 | + * names, docblocks and declared signatures. `convertStoredItemDetailed` |
| 24 | + * reads no member, so its stub calls the plain function directly. |
| 25 | + * `convertStoredItem`'s stub is one literal of exactly the |
| 26 | + * {@link StoredItemConversionHost} members it reads, plus a `.call`: the |
| 27 | + * `convertStoredItemDetailed` method as a closure, and the |
| 28 | + * `storedConversionWarned` field as a getter, so the warn-once set the body |
| 29 | + * reads and adds to is the protocol's own. Every other reader of either |
| 30 | + * method keeps calling the class method. |
| 31 | + * |
| 32 | + * What the bodies read of `protocol.ts`'s own module-level declarations they |
| 33 | + * import from it; what they import from elsewhere, they import from where |
| 34 | + * `protocol.ts` imported it. Nothing re-exports this module, so the published |
| 35 | + * surface does not move. |
| 36 | + */ |
| 37 | + |
| 38 | +import { applyConversionsToStoredItem, type ConversionNotice, type ConversionTodoNotice } from '@objectstack/spec'; |
| 39 | +import { PLURAL_TO_SINGULAR } from '@objectstack/spec/shared'; |
| 40 | +import type { ObjectStackProtocolImplementation } from './protocol.js'; |
| 41 | +import { withDeclaredPageTypeDefault } from './protocol.js'; |
| 42 | + |
| 43 | +/** |
| 44 | + * The `ObjectStackProtocolImplementation` members the functions below read — |
| 45 | + * every one of them, and no other; a function's `this` is the `Pick` of the |
| 46 | + * members it reads. Each is typed AS the class member (an indexed access on |
| 47 | + * the class), so a change to a member's signature on the class is a change |
| 48 | + * here too, and naming a member the class does not have is a compile error. |
| 49 | + */ |
| 50 | +export interface StoredItemConversionHost { |
| 51 | + readonly convertStoredItemDetailed: ObjectStackProtocolImplementation['convertStoredItemDetailed']; |
| 52 | + readonly storedConversionWarned: ObjectStackProtocolImplementation['storedConversionWarned']; |
| 53 | +} |
| 54 | + |
| 55 | +/** The body of `ObjectStackProtocolImplementation#convertStoredItem`; the method's docblock there is its contract. */ |
| 56 | +export function convertStoredItem( |
| 57 | + this: Pick<StoredItemConversionHost, 'convertStoredItemDetailed' | 'storedConversionWarned'>, |
| 58 | + type: string, |
| 59 | + data: unknown, |
| 60 | +): unknown { |
| 61 | + const singular = PLURAL_TO_SINGULAR[type] ?? type; |
| 62 | + const converted = this.convertStoredItemDetailed(type, data, (n) => { |
| 63 | + const name = (data as { name?: unknown } | null | undefined)?.name; |
| 64 | + const key = `${n.conversionId}|${singular}|${String(name ?? '')}`; |
| 65 | + if (this.storedConversionWarned.has(key)) return; |
| 66 | + this.storedConversionWarned.add(key); |
| 67 | + console.warn( |
| 68 | + `[Protocol] stored ${singular}/${String(name ?? '<unnamed>')} carries a pre-protocol shape; ` + |
| 69 | + `${n.message} The row itself is unchanged — re-save it (Studio edit → save, or run ` + |
| 70 | + `"os migrate meta --stored --apply") to persist the canonical shape.`, |
| 71 | + ); |
| 72 | + }).item; |
| 73 | + return withDeclaredPageTypeDefault(singular, converted); |
| 74 | +} |
| 75 | + |
| 76 | +/** The body of `ObjectStackProtocolImplementation#convertStoredItemDetailed`; the method's docblock there is its contract. */ |
| 77 | +export function convertStoredItemDetailed( |
| 78 | + type: string, |
| 79 | + data: unknown, |
| 80 | + onNotice?: (notice: ConversionNotice) => void, |
| 81 | + knownDatasources?: ReadonlyMap<string, { readonly schemaMode?: unknown }>, |
| 82 | +): { item: unknown; notices: ConversionNotice[]; todos: ConversionTodoNotice[] } { |
| 83 | + const singular = PLURAL_TO_SINGULAR[type] ?? type; |
| 84 | + if (singular === 'flow') return { item: data, notices: [], todos: [] }; |
| 85 | + const notices: ConversionNotice[] = []; |
| 86 | + const todos: ConversionTodoNotice[] = []; |
| 87 | + const item = applyConversionsToStoredItem(singular, data, { |
| 88 | + onNotice: (n) => { |
| 89 | + notices.push(n); |
| 90 | + onNotice?.(n); |
| 91 | + }, |
| 92 | + onTodo: (t) => { |
| 93 | + todos.push(t); |
| 94 | + }, |
| 95 | + ...(knownDatasources ? { knownDatasources } : {}), |
| 96 | + }); |
| 97 | + return { item, notices, todos }; |
| 98 | +} |
0 commit comments