Skip to content

Commit 1d1579e

Browse files
committed
refactor(metadata-protocol): move the stored-item conversion read out of protocol.ts (#25467 cut 23)
Move the bodies of convertStoredItem and convertStoredItemDetailed unedited into protocol-stored-item-conversion.ts. convertStoredItem sits behind a StoredItemConversionHost that hands over convertStoredItemDetailed as a closure and the storedConversionWarned field as a getter; convertStoredItemDetailed reads no member, so its stub calls the plain function directly. withDeclaredPageTypeDefault gains export in place, and applyConversionsToStoredItem, whose last reader moved, leaves protocol.ts's @objectstack/spec import. The class keeps both methods, their docblocks and declared signatures; their callers keep calling the class methods. Claude-Session: https://claude.ai/code/session_016SP1spj613tyGCgxoD1An6 Co-authored-by: Claude <noreply@anthropic.com>
1 parent f986702 commit 1d1579e

2 files changed

Lines changed: 111 additions & 30 deletions

File tree

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
}

‎packages/metadata-protocol/src/protocol.ts‎

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ import { PLURAL_TO_SINGULAR, SINGULAR_TO_PLURAL, canonicalMetaUrlType, metaUrlSp
167167
// live transport in through `attachMetadataMutationPubSub`.
168168
import type { IFieldFileService, IPubSub } from '@objectstack/spec/contracts';
169169
import { FIELD_FILE_SERVICE, RLS_MEMBERSHIP_RESOLVER_SERVICE } from '@objectstack/spec/contracts';
170-
import { applyConversionsToFlow, applyConversionsToStoredItem, type ConversionNotice, type ConversionTodoNotice } from '@objectstack/spec';
170+
import { applyConversionsToFlow, type ConversionNotice, type ConversionTodoNotice } from '@objectstack/spec';
171171
import { type FormView, type I18nLabel, isAggregatedViewContainer, expandViewContainer } from '@objectstack/spec/ui';
172172
// [commit ece4dad31] Emitted-specifier pin. This module's inferred public declarations
173173
// structurally mention `FormFieldInput` (FormView `sections[].fields`), and
@@ -411,6 +411,8 @@ import { readItemFromMetadataService } from './protocol-metadata-service-read.js
411411
import { reportSealedOverlayRows } from './protocol-sealed-overlay-report.js';
412412
// [#25467 cut 22] A row-less view name's expansion for the by-name read, and a container's expansion under its own name.
413413
import { expandUnderOwnName, resolveRowlessExpandedView } from './protocol-view-expansion-names.js';
414+
// [#25467 cut 23] The read-side conversion of a stored item: the ADR-0087 chain with its notices, and the warned-once read with the page `type` default.
415+
import { convertStoredItem, convertStoredItemDetailed } from './protocol-stored-item-conversion.js';
414416

415417
/**
416418
* Canonical Zod schema per metadata type lives in
@@ -2260,7 +2262,7 @@ export function projectStorableViewBody(
22602262
* `null`), and an explicit value is left as it is. Returns the input itself
22612263
* when nothing is filled.
22622264
*/
2263-
function withDeclaredPageTypeDefault(type: string, item: unknown): unknown {
2265+
export function withDeclaredPageTypeDefault(type: string, item: unknown): unknown {
22642266
if ((PLURAL_TO_SINGULAR[type] ?? type) !== 'page') return item;
22652267
if (!item || typeof item !== 'object' || Array.isArray(item)) return item;
22662268
if ((item as Record<string, unknown>).type !== undefined) return item;
@@ -6414,19 +6416,13 @@ export class ObjectStackProtocolImplementation implements
64146416
* its bytes.
64156417
*/
64166418
private convertStoredItem(type: string, data: unknown): unknown {
6417-
const singular = PLURAL_TO_SINGULAR[type] ?? type;
6418-
const converted = this.convertStoredItemDetailed(type, data, (n) => {
6419-
const name = (data as { name?: unknown } | null | undefined)?.name;
6420-
const key = `${n.conversionId}|${singular}|${String(name ?? '')}`;
6421-
if (this.storedConversionWarned.has(key)) return;
6422-
this.storedConversionWarned.add(key);
6423-
console.warn(
6424-
`[Protocol] stored ${singular}/${String(name ?? '<unnamed>')} carries a pre-protocol shape; ` +
6425-
`${n.message} The row itself is unchanged — re-save it (Studio edit → save, or run ` +
6426-
`"os migrate meta --stored --apply") to persist the canonical shape.`,
6427-
);
6428-
}).item;
6429-
return withDeclaredPageTypeDefault(singular, converted);
6419+
// [#25467 cut 23] The body moved to `protocol-stored-item-conversion.ts` (see `StoredItemConversionHost`).
6420+
const readStoredConversionWarned = () => this.storedConversionWarned;
6421+
const host: ThisParameterType<typeof convertStoredItem> = {
6422+
convertStoredItemDetailed: (...a) => this.convertStoredItemDetailed(...a),
6423+
get storedConversionWarned() { return readStoredConversionWarned(); },
6424+
};
6425+
return convertStoredItem.call(host, type, data);
64306426
}
64316427

64326428
/**
@@ -6455,21 +6451,8 @@ export class ObjectStackProtocolImplementation implements
64556451
onNotice?: (notice: ConversionNotice) => void,
64566452
knownDatasources?: ReadonlyMap<string, { readonly schemaMode?: unknown }>,
64576453
): { item: unknown; notices: ConversionNotice[]; todos: ConversionTodoNotice[] } {
6458-
const singular = PLURAL_TO_SINGULAR[type] ?? type;
6459-
if (singular === 'flow') return { item: data, notices: [], todos: [] };
6460-
const notices: ConversionNotice[] = [];
6461-
const todos: ConversionTodoNotice[] = [];
6462-
const item = applyConversionsToStoredItem(singular, data, {
6463-
onNotice: (n) => {
6464-
notices.push(n);
6465-
onNotice?.(n);
6466-
},
6467-
onTodo: (t) => {
6468-
todos.push(t);
6469-
},
6470-
...(knownDatasources ? { knownDatasources } : {}),
6471-
});
6472-
return { item, notices, todos };
6454+
// [#25467 cut 23] The body moved to `protocol-stored-item-conversion.ts`.
6455+
return convertStoredItemDetailed(type, data, onNotice, knownDatasources);
64736456
}
64746457

64756458
/**

0 commit comments

Comments
 (0)