|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#25467 cut 20] The code-layer read of `ObjectStackProtocolImplementation` |
| 5 | + * through the `metadata` service (`readItemFromMetadataService`): one item, |
| 6 | + * package-scoped (ADR-0048), its diagnosis when the service gives one, the |
| 7 | + * singular/plural spelling as a fallback, and a stored object row's copy not |
| 8 | + * counted as a code layer (#23645). Moved out of `protocol.ts` whole. |
| 9 | + * |
| 10 | + * A MOVE, not a redesign. The function below is the body of the |
| 11 | + * `ObjectStackProtocolImplementation` method of the same name as it stood, |
| 12 | + * re-indented one level and otherwise unedited: the same service lookup, the |
| 13 | + * same two reads in the same order, and the same merged answer. |
| 14 | + * |
| 15 | + * ## How the moved body reaches the class |
| 16 | + * |
| 17 | + * The shape #23050 set for `rest-server.ts` (its `data-action-routes.ts` |
| 18 | + * header carries the full reasoning), as `protocol-batch-data.ts` applies it. |
| 19 | + * The class keeps the method — the published `dist/*.d.ts` lists its name, |
| 20 | + * docblock and declared signature — and its body is one literal of exactly |
| 21 | + * the {@link MetadataServiceReadHost} members it reads, plus a `.call`. The |
| 22 | + * `getServicesRegistry` field is handed over as a getter, as |
| 23 | + * `protocol-package-install.ts` and `protocol-save-door-refusals.ts` hand it, |
| 24 | + * so the body reads the protocol's field at call time. Every other reader of |
| 25 | + * the method keeps calling the class method. |
| 26 | + * |
| 27 | + * What the body reads of `protocol.ts`'s own module-level declarations it |
| 28 | + * imports from it; what it imports from elsewhere, it imports from where |
| 29 | + * `protocol.ts` imported it. Nothing re-exports this module, so the published |
| 30 | + * surface does not move. |
| 31 | + */ |
| 32 | + |
| 33 | +import { isTenantAuthored } from '@objectstack/metadata-core'; |
| 34 | +import { PLURAL_TO_SINGULAR, SINGULAR_TO_PLURAL } from '@objectstack/spec/shared'; |
| 35 | +import type { ObjectStackProtocolImplementation } from './protocol.js'; |
| 36 | +import { canonicalMetaType } from './protocol.js'; |
| 37 | + |
| 38 | +/** |
| 39 | + * The `ObjectStackProtocolImplementation` members the function below reads — |
| 40 | + * every one of them, and no other; the function's `this` is the `Pick` of the |
| 41 | + * members it reads. Each is typed AS the class member (an indexed access on |
| 42 | + * the class), so a change to a member's signature on the class is a change |
| 43 | + * here too, and naming a member the class does not have is a compile error. |
| 44 | + */ |
| 45 | +export interface MetadataServiceReadHost { |
| 46 | + readonly getServicesRegistry: ObjectStackProtocolImplementation['getServicesRegistry']; |
| 47 | +} |
| 48 | + |
| 49 | +/** The body of `ObjectStackProtocolImplementation#readItemFromMetadataService`; the method's docblock there is its contract. */ |
| 50 | +export async function readItemFromMetadataService( |
| 51 | + this: Pick<MetadataServiceReadHost, 'getServicesRegistry'>, |
| 52 | + type: string, |
| 53 | + name: string, |
| 54 | + packageId?: string, |
| 55 | +): Promise<{ data: unknown; degraded: boolean; errors: string[] }> { |
| 56 | + const services = this.getServicesRegistry?.(); |
| 57 | + const metadataService: any = services?.get('metadata'); |
| 58 | + if (!metadataService || typeof metadataService.get !== 'function') { |
| 59 | + return { data: undefined, degraded: false, errors: [] }; |
| 60 | + } |
| 61 | + // ADR-0048 — thread the caller's package id so a single-item fetch is |
| 62 | + // package-scoped. Passed positionally to BOTH reads, so whatever the |
| 63 | + // occupant of the `metadata` slot makes of a third argument today is |
| 64 | + // unchanged by which of the two methods answers. |
| 65 | + const read = async (t: string): Promise<{ data: unknown; degraded: boolean; errors: string[] }> => { |
| 66 | + let answer: { data: unknown; degraded: boolean; errors: string[] }; |
| 67 | + if (typeof metadataService.getDiagnosed === 'function') { |
| 68 | + const diagnosed = await metadataService.getDiagnosed(t, name, packageId); |
| 69 | + answer = { |
| 70 | + data: diagnosed?.data, |
| 71 | + degraded: diagnosed?.degraded === true, |
| 72 | + errors: Array.isArray(diagnosed?.errors) ? diagnosed.errors : [], |
| 73 | + }; |
| 74 | + } else { |
| 75 | + answer = { data: await metadataService.get(t, name, packageId), degraded: false, errors: [] }; |
| 76 | + } |
| 77 | + // [#23645] A stored object row's copy is not a code layer. See the header. |
| 78 | + return canonicalMetaType(t) === 'object' && isTenantAuthored(answer.data) |
| 79 | + ? { ...answer, data: undefined } |
| 80 | + : answer; |
| 81 | + }; |
| 82 | + |
| 83 | + const primary = await read(type); |
| 84 | + if (primary.data !== undefined && primary.data !== null) return primary; |
| 85 | + const alt = PLURAL_TO_SINGULAR[type] ?? SINGULAR_TO_PLURAL[type]; |
| 86 | + if (!alt) return { data: undefined, degraded: primary.degraded, errors: primary.errors }; |
| 87 | + const secondary = await read(alt); |
| 88 | + if (secondary.data !== undefined && secondary.data !== null) return secondary; |
| 89 | + return { |
| 90 | + data: undefined, |
| 91 | + degraded: primary.degraded || secondary.degraded, |
| 92 | + errors: [...primary.errors, ...secondary.errors], |
| 93 | + }; |
| 94 | +} |
0 commit comments