|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#25467 cut 30] The object-extender fold, from |
| 5 | + * `ObjectStackProtocolImplementation`, moved out of `protocol.ts` whole: |
| 6 | + * `foldObjectExtendersFromRegistry`, which folds the registry's `extend` |
| 7 | + * contributors onto an OBJECT body that came from the MetadataService, so the |
| 8 | + * by-name read serves the object's resolved schema as the list read does |
| 9 | + * (#7556). |
| 10 | + * |
| 11 | + * A MOVE, not a redesign. The function below is the body of the |
| 12 | + * `ObjectStackProtocolImplementation` method of the same name as it stood, |
| 13 | + * re-indented one level and otherwise unedited. |
| 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 ObjectExtenderFoldHost} member it reads, plus a `.call`. The |
| 22 | + * `engine` field is handed over as a getter, so the read finds it as it |
| 23 | + * stands at call time. Every caller keeps calling the class method. |
| 24 | + * |
| 25 | + * What the body reads from elsewhere, it imports from where `protocol.ts` |
| 26 | + * imports it. Nothing re-exports this module, so the published surface does |
| 27 | + * not move. |
| 28 | + */ |
| 29 | + |
| 30 | +import { PLURAL_TO_SINGULAR } from '@objectstack/spec/shared'; |
| 31 | +import type { ObjectStackProtocolImplementation } from './protocol.js'; |
| 32 | + |
| 33 | +/** |
| 34 | + * The `ObjectStackProtocolImplementation` member the function below reads — |
| 35 | + * the only one; the function's `this` is the `Pick` of it. It is typed AS the |
| 36 | + * class member (an indexed access on the class), so a change to its |
| 37 | + * signature on the class is a change here too. |
| 38 | + */ |
| 39 | +export interface ObjectExtenderFoldHost { |
| 40 | + readonly engine: ObjectStackProtocolImplementation['engine']; |
| 41 | +} |
| 42 | + |
| 43 | +/** The body of `ObjectStackProtocolImplementation#foldObjectExtendersFromRegistry`; the method's docblock there is its contract. */ |
| 44 | +export function foldObjectExtendersFromRegistry( |
| 45 | + this: Pick<ObjectExtenderFoldHost, 'engine'>, |
| 46 | + type: string, |
| 47 | + name: unknown, |
| 48 | + body: unknown, |
| 49 | +): unknown { |
| 50 | + const singular = PLURAL_TO_SINGULAR[type] ?? type; |
| 51 | + if (singular !== 'object') return body; |
| 52 | + if (body === null || typeof body !== 'object') return body; |
| 53 | + // [#8027] The list caller reads the name off the ROW rather than off a |
| 54 | + // request, and a row with no usable `name` has no contributor list to |
| 55 | + // look up — `mergePackageAwareOverlay` skips such rows too. |
| 56 | + if (typeof name !== 'string' || name === '') return body; |
| 57 | + const registry = (this.engine as any)?.registry; |
| 58 | + // Partial registry doubles in tests predate this method; a host that |
| 59 | + // cannot fold answers exactly as it did before. |
| 60 | + if (!registry || typeof registry.foldObjectExtendersOnto !== 'function') return body; |
| 61 | + try { |
| 62 | + return registry.foldObjectExtendersOnto(name, body); |
| 63 | + } catch { |
| 64 | + // The fold is a read over in-memory contributors; a failure here |
| 65 | + // must not turn a served schema into a 5xx. |
| 66 | + return body; |
| 67 | + } |
| 68 | +} |
0 commit comments