@docupace/ihub-config 1.1.25 → 1.1.27-edit.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/dist/config-extension.d.ts +72 -1
  2. package/dist/config-extension.d.ts.map +1 -1
  3. package/dist/config-extension.js +2 -0
  4. package/dist/config-extension.js.map +1 -1
  5. package/dist/config-service.d.ts +18 -4
  6. package/dist/config-service.d.ts.map +1 -1
  7. package/dist/config-service.js +438 -24
  8. package/dist/config-service.js.map +1 -1
  9. package/dist/index.d.ts +2 -0
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +1 -0
  12. package/dist/index.js.map +1 -1
  13. package/dist/lib/named-array-merge.d.ts +29 -0
  14. package/dist/lib/named-array-merge.d.ts.map +1 -0
  15. package/dist/lib/named-array-merge.js +158 -0
  16. package/dist/lib/named-array-merge.js.map +1 -0
  17. package/dist/mappers/config-mapper.d.ts.map +1 -1
  18. package/dist/mappers/config-mapper.js +2 -0
  19. package/dist/mappers/config-mapper.js.map +1 -1
  20. package/dist/model/graphs/config/fields.graphql +45 -0
  21. package/dist/model/graphs/config/menu.graphql +6 -0
  22. package/dist/model/graphs/config/page.graphql +8 -1
  23. package/dist/model/graphs/config/panel.graphql +7 -0
  24. package/dist/model/graphs/config/query.graphql +9 -1
  25. package/dist/model/graphs/config/selectOptions.graphql +1 -0
  26. package/dist/model/graphs/config/table.graphql +26 -0
  27. package/dist/model/graphs/config/ui-components.graphql +1 -0
  28. package/dist/providers/index.d.ts +4 -0
  29. package/dist/providers/index.d.ts.map +1 -1
  30. package/dist/providers/index.js +5 -0
  31. package/dist/providers/index.js.map +1 -1
  32. package/dist/providers/screen-layout-patch.d.ts +6 -0
  33. package/dist/providers/screen-layout-patch.d.ts.map +1 -0
  34. package/dist/providers/screen-layout-patch.js +57 -0
  35. package/dist/providers/screen-layout-patch.js.map +1 -0
  36. package/dist/providers/whole-patch.d.ts +6 -0
  37. package/dist/providers/whole-patch.d.ts.map +1 -0
  38. package/dist/providers/whole-patch.js +2 -0
  39. package/dist/providers/whole-patch.js.map +1 -0
  40. package/dist/types/graphql.d.ts +4 -2
  41. package/dist/types/graphql.d.ts.map +1 -1
  42. package/dist/types/graphql.js.map +1 -1
  43. package/model/graphs/config/fields.graphql +45 -0
  44. package/model/graphs/config/menu.graphql +6 -0
  45. package/model/graphs/config/page.graphql +8 -1
  46. package/model/graphs/config/panel.graphql +7 -0
  47. package/model/graphs/config/query.graphql +9 -1
  48. package/model/graphs/config/selectOptions.graphql +1 -0
  49. package/model/graphs/config/table.graphql +26 -0
  50. package/model/graphs/config/ui-components.graphql +1 -0
  51. package/package.json +5 -3
@@ -7,12 +7,259 @@ import { fileExists } from './lib/file-system.js';
7
7
  import replaceDotsOutsideExpressions from './lib/replace-dots-outside-expressions.js';
8
8
  import { createHash } from 'node:crypto';
9
9
  import { injectConfigValue, } from './injector-resolvers/config-injector.js';
10
- import { builtInProviders } from './providers/index.js';
10
+ import { builtInProviders, builtInWholePatchMergers, } from './providers/index.js';
11
11
  import { deepMergeResolvers, } from './resolvers/index.js';
12
12
  import { dump, load } from 'js-yaml';
13
13
  import { buildQuery } from '@docupace/ihub-core/query';
14
+ import { isNamedObjectArray, mergeNamedObjectArray, } from './lib/named-array-merge.js';
15
+ import jp from 'jsonpath';
14
16
  const FULL_OBJECT_EXPRESSION_PATTERN = /^\${(.*?)}$/;
15
17
  const INLINE_OBJECT_EXPRESSION_PATTERN = /\${(.*?)}/g;
18
+ // An override element counts as "self-contained" when it carries enough of
19
+ // its own definition to render on its own — a real attribute path (or paths)
20
+ // or a computed expression — as opposed to a thin `{ name, visible }`-style
21
+ // reference that only makes sense resolved against a base/predefined
22
+ // counterpart of the same name. `paths` (plural) is `Column`'s shape;
23
+ // `path` (singular) is `TableCriteria`'s — both must count, or a resolved
24
+ // filter-criterion `$ref` (which only ever has a singular `path`) is wrongly
25
+ // treated as a dangling reference and silently dropped, even though it just
26
+ // resolved successfully.
27
+ function isSelfContainedOverrideElement(item) {
28
+ const paths = item.paths;
29
+ return ((Array.isArray(paths) && paths.length > 0) ||
30
+ (typeof item.path === 'string' && item.path.length > 0) ||
31
+ (typeof item.expression === 'string' && item.expression.length > 0));
32
+ }
33
+ /**
34
+ * Merge semantics for a config sub-path override against the value already at that
35
+ * JSONPath. Generic — used for any typename/sub-path the configPath/subPath mechanism
36
+ * resolves, not just `Table.columns` — a thin wrapper over the shared
37
+ * `mergeNamedObjectArray` (see `named-array-merge.ts` for the full algorithm), fixed to
38
+ * this call site's own definition of "self-contained" (`isSelfContainedOverrideElement`).
39
+ * Any other shape (scalars, plain objects, arrays without a `name` key) is returned
40
+ * untouched, so the caller's plain whole-value replace applies exactly as it did before
41
+ * this merge existed.
42
+ */
43
+ function mergeSubPathOverrideValue(base, override) {
44
+ if (!isNamedObjectArray(override) || !isNamedObjectArray(base)) {
45
+ return override;
46
+ }
47
+ return mergeNamedObjectArray(base, override, isSelfContainedOverrideElement);
48
+ }
49
+ // Marks each element of a fully-folded named-object array with whether it
50
+ // would still show up — present *and* not hidden — without the layer stored
51
+ // at the identity's own scope tier (`withoutOwnScope`, the same
52
+ // broadest-to-narrowest fold as `mergedValue` minus that one layer; see
53
+ // `applyConfigPathOverride`, where both folds are computed).
54
+ //
55
+ // This drives two decisions in the editing client, both about what its own
56
+ // scope's override has to say explicitly to get the effect the user asked
57
+ // for:
58
+ // - Removing an entry: `inherited` means hiding it (`visible: false`) is
59
+ // required, since deleting it from this scope's override would just have
60
+ // the next resolution fold bring it back from a broader layer. Not
61
+ // inherited means a plain delete suffices.
62
+ // - Adding/re-showing an entry: not inherited means the client must pin
63
+ // `visible: true` on it, since the broader fold either doesn't have it at
64
+ // all or has it hidden — and per-attribute merge semantics would otherwise
65
+ // inherit that `visible: false` straight through this scope's own
66
+ // override, leaving the entry invisible no matter how it's re-added.
67
+ // Hidden-ness is folded into `inherited` (rather than reported as a separate
68
+ // flag) precisely because both decisions only ever care whether the entry
69
+ // would be *there*, and a broader-hidden entry is exactly as absent from the
70
+ // user's view as a missing one.
71
+ //
72
+ // Only stamps when both values are named-object arrays; any other shape
73
+ // (scalar, plain object, unnamed array) is returned unchanged, mirroring
74
+ // `mergeSubPathOverrideValue`'s own shape guard.
75
+ function stampInherited(mergedValue, withoutOwnScope) {
76
+ if (!isNamedObjectArray(mergedValue) ||
77
+ !isNamedObjectArray(withoutOwnScope)) {
78
+ return mergedValue;
79
+ }
80
+ const inheritedByName = new Map(withoutOwnScope.map((item) => [item.name, item]));
81
+ return mergedValue.map((item) => {
82
+ const counterpart = inheritedByName.get(item.name);
83
+ return {
84
+ ...item,
85
+ inherited: !!counterpart && counterpart.visible !== false,
86
+ };
87
+ });
88
+ }
89
+ function isRefElement(item) {
90
+ return (item !== null &&
91
+ typeof item === 'object' &&
92
+ typeof item.$ref === 'string');
93
+ }
94
+ // Resolves a config-path override array element's fragment-only `$ref`
95
+ // (e.g. `"#$.predefinedColumns[?(@.name=='email')]"`) against `refRoot` —
96
+ // the config-path's own sibling fields, not another file. This is
97
+ // deliberately narrower than `template`'s YAML `$ref` (processed by
98
+ // `processReferencedObject`/`getRefConfig`, which loads another config
99
+ // file): an override-array `$ref` only ever supports the `#<jsonpath>`
100
+ // self-reference form, resolved with the already-vendored `jsonpath`
101
+ // package (the same one `applyConfigPathOverride` uses for `subPath`
102
+ // strings). A ref that doesn't start with `#`, resolves to nothing, or
103
+ // resolves to a non-object is treated the same as any other unresolved
104
+ // override reference elsewhere in this module — silently dropped rather
105
+ // than thrown, so a stale reference just doesn't round-trip into the next
106
+ // save. Multiple matches take the first, in JSONPath document order.
107
+ //
108
+ // The resolved object keeps the original fragment string under `ref` (not
109
+ // `$ref` — GraphQL field names can't start with `$`) rather than dropping
110
+ // it once resolved. Without this, a client re-saving the array unchanged
111
+ // (e.g. reordering a different column) would have no way to tell this
112
+ // element apart from a plain inline column and would flatten it back into
113
+ // a frozen copy on that save — exactly the staleness problem `$ref` exists
114
+ // to avoid. `Column.ref` (the GraphQL field) surfaces it back to the client
115
+ // so `docupace-ui-main` can keep re-emitting the same `$ref` indefinitely.
116
+ function resolveOverrideElementRef(item, refRoot) {
117
+ const ref = item.$ref;
118
+ if (!ref.startsWith('#')) {
119
+ return undefined;
120
+ }
121
+ const [match] = jp.query(refRoot, ref.slice(1));
122
+ if (match === null || typeof match !== 'object') {
123
+ return undefined;
124
+ }
125
+ const { $ref: _unusedRef, ...localOverrides } = item;
126
+ return {
127
+ ...match,
128
+ ...localOverrides,
129
+ ref,
130
+ };
131
+ }
132
+ // Expands any `$ref` elements of a config-path override array into the
133
+ // full object they point at (merged with whatever local attributes the
134
+ // element carries alongside `$ref`, e.g. `visible`) before the array is
135
+ // handed to `mergeSubPathOverrideValue`. Runs first so that a `{ $ref,
136
+ // visible }` stub — which has no `name` of its own — always arrives at the
137
+ // name-keyed merge as a proper named object; `mergeSubPathOverrideValue`
138
+ // itself needs no `$ref` awareness.
139
+ function expandRefOverrideElements(value, refRoot) {
140
+ if (!Array.isArray(value)) {
141
+ return value;
142
+ }
143
+ return value
144
+ .map((item) => isRefElement(item) ? resolveOverrideElementRef(item, refRoot) : item)
145
+ .filter((item) => item !== undefined);
146
+ }
147
+ // Builds the object a config-path override's `$ref` fragments resolve
148
+ // against: the config being overridden, overlaid with the *raw* (not yet
149
+ // merged) override value of every other simple top-level subPath in this
150
+ // same patch (e.g. `$.predefinedColumns`). Using the raw override value
151
+ // rather than depending on loop order elsewhere in
152
+ // `applyConfigPathOverride` means a `$.columns` override's `$ref` into
153
+ // `$.predefinedColumns` resolves correctly regardless of which subPath
154
+ // happens to be processed first. Only single-segment paths (`$.foo`) are
155
+ // overlaid — nested subPaths (`$.filter.criteria`) fall back to whatever
156
+ // value is already on `config`, which is fine since nothing currently
157
+ // needs to `$ref` into a nested subPath.
158
+ //
159
+ // `subPathPatch` (the `all` half of `SubPathOverrideLayers`) carries an
160
+ // *ordered* array of layers per jsonPath
161
+ // (broadest to narrowest — see `ConfigurationOverrideResult.subPaths`), not
162
+ // a single already-resolved value. The overlay uses only the narrowest
163
+ // (last) layer, same approximation as before cascading existed: a catalog
164
+ // like `$.predefinedColumns`/`$.predefinedFilters` is always saved as a full
165
+ // replacement at a single scope in practice (never a partial patch spread
166
+ // across tiers), so the narrowest present layer already *is* effectively
167
+ // the whole value — exactly what this overlay used before this function
168
+ // took an array at all.
169
+ function buildRefResolutionRoot(config, subPathPatch) {
170
+ const overlay = {};
171
+ for (const [jsonPath, layers] of Object.entries(subPathPatch)) {
172
+ const topLevelMatch = /^\$\.([^.[]+)$/.exec(jsonPath);
173
+ if (topLevelMatch && layers.length) {
174
+ overlay[topLevelMatch[1]] = layers[layers.length - 1];
175
+ }
176
+ }
177
+ return { ...config, ...overlay };
178
+ }
179
+ // Normalizes a provider's resolve() result into the pair of folds
180
+ // `applyConfigPathOverride` works with. `inherited` stays `null` (rather
181
+ // than defaulting to `{}`) when the provider doesn't report it, so the
182
+ // fallback there stays distinguishable from "reported, but this path has no
183
+ // broader layer".
184
+ function toSubPathOverrideLayers(override) {
185
+ if (!override.subPaths) {
186
+ return null;
187
+ }
188
+ return {
189
+ all: override.subPaths,
190
+ inherited: override.inheritedSubPaths ?? null,
191
+ };
192
+ }
193
+ // Folds one jsonPath's override layers onto the value currently at that
194
+ // location in `target`, and writes the result back in place.
195
+ //
196
+ // `layers` is ordered broadest to narrowest (general, tenant, role, user —
197
+ // see `ConfigurationOverrideResult.subPaths`), so folding them in order onto
198
+ // the current value cascades correctly: each layer only patches what it
199
+ // mentions, inheriting everything else from the fold so far.
200
+ //
201
+ // `inherited` drives the second fold — the same one minus the identity's
202
+ // own-scope layer, i.e. "what this identity would see with no override of
203
+ // its own", which is what `inherited` is stamped against. The provider
204
+ // reports those layers explicitly (`inheritedSubPaths`) because the tier a
205
+ // layer is stored at isn't recoverable from the values alone; dropping the
206
+ // last layer is only a fallback for a provider that doesn't, and is wrong
207
+ // whenever the identity has no row of its own yet (the layer dropped is then
208
+ // a broader one that very much still applies).
209
+ //
210
+ // Throws when `jsonPath` doesn't resolve to a settable location; the caller
211
+ // decides what to do with that.
212
+ function applySubPathOverrideLayers(target, jsonPath, layers, inherited, refRoot) {
213
+ const currentValue = jp.value(target, jsonPath);
214
+ const mergedValue = layers
215
+ .map((layer) => expandRefOverrideElements(layer, refRoot))
216
+ .reduce((base, override) => mergeSubPathOverrideValue(base, override), currentValue);
217
+ const inheritedLayers = inherited
218
+ ? (inherited[jsonPath] ?? [])
219
+ : layers.slice(0, -1);
220
+ const withoutOwnScope = inheritedLayers
221
+ .map((layer) => expandRefOverrideElements(layer, refRoot))
222
+ .reduce((base, override) => mergeSubPathOverrideValue(base, override), currentValue);
223
+ const finalValue = stampInherited(mergedValue, withoutOwnScope);
224
+ const applied = jp.value(target, jsonPath, finalValue);
225
+ if (applied === undefined) {
226
+ throw new Error(`does not resolve to a settable location: "${jsonPath}"`);
227
+ }
228
+ }
229
+ // Deep-sets every stored sub-path override of `subPathPatch` onto a clone of
230
+ // `config`, returning the clone. `config` itself is never mutated.
231
+ function applySubPathOverrides(config, subPathPatch) {
232
+ // Built from the pre-clone config on purpose: `$ref` fragments resolve
233
+ // against the config as it was handed in, not against whatever earlier
234
+ // jsonPaths in this same loop have already written.
235
+ const refRoot = buildRefResolutionRoot(config, subPathPatch.all);
236
+ // Deep-clone once, up front — `jp.value` mutates its target in place, and
237
+ // a shallow `{ ...config }` per jsonPath only protects the top level. A
238
+ // nested subPath (e.g. `$.filter.criteria`) would otherwise still mutate
239
+ // `config.filter`, which is the *same* object reference as the original,
240
+ // potentially-shared config (e.g. reused across tables via
241
+ // `extend`/fragment composition). Config values are always plain JSON
242
+ // (from YAML/domain storage), so a JSON round-trip clone is sufficient and
243
+ // avoids `structuredClone`'s cross-realm `instanceof` pitfalls (its output
244
+ // fails `instanceof Object` checks — e.g. inside the `jsonpath` package —
245
+ // when the calling code runs in a different VM realm/context, as test
246
+ // sandboxes commonly do).
247
+ const result = JSON.parse(JSON.stringify(config));
248
+ for (const [jsonPath, layers] of Object.entries(subPathPatch.all)) {
249
+ // A single malformed override (an unparseable `$ref` JSONPath, or a
250
+ // jsonPath that doesn't resolve to a settable location) must not abort
251
+ // resolution of the rest of this config tree — that would take down the
252
+ // page for every user over one bad admin-authored row. Log and skip just
253
+ // this jsonPath instead.
254
+ try {
255
+ applySubPathOverrideLayers(result, jsonPath, layers, subPathPatch.inherited, refRoot);
256
+ }
257
+ catch (error) {
258
+ log.warn(`Skipping configuration override for "${result.configPath}" at "${jsonPath}": ${error.message}`);
259
+ }
260
+ }
261
+ return result;
262
+ }
16
263
  export class ConfigFileStore {
17
264
  localPath;
18
265
  remotePath;
@@ -154,7 +401,9 @@ class ConfigService {
154
401
  model;
155
402
  configInjectorResolvers;
156
403
  providers;
404
+ wholePatchMergers;
157
405
  resolvers;
406
+ configurationOverrideProvider;
158
407
  constructor(model, configStores, extensionOptions = {}) {
159
408
  this.configStores = configStores;
160
409
  this.model = model;
@@ -164,9 +413,14 @@ class ConfigService {
164
413
  this.providers = extensionOptions.providers
165
414
  ? { ...builtInProviders, ...extensionOptions.providers }
166
415
  : { ...builtInProviders };
416
+ this.wholePatchMergers = extensionOptions.wholePatchMergers
417
+ ? { ...builtInWholePatchMergers, ...extensionOptions.wholePatchMergers }
418
+ : { ...builtInWholePatchMergers };
167
419
  this.resolvers = extensionOptions.resolvers
168
420
  ? { ...extensionOptions.resolvers }
169
421
  : {};
422
+ this.configurationOverrideProvider =
423
+ extensionOptions.configurationOverrideProvider;
170
424
  if (this.model) {
171
425
  this.generateResolvers();
172
426
  }
@@ -232,12 +486,34 @@ class ConfigService {
232
486
  }
233
487
  return processedData;
234
488
  }
489
+ applyReferenceExtensions(processedData, referenceConfig) {
490
+ if (typeof referenceConfig?.extend !== 'object') {
491
+ return processedData;
492
+ }
493
+ const { extend } = referenceConfig;
494
+ for (const key in extend) {
495
+ const extensionValue = extend[key];
496
+ if (!Array.isArray(extensionValue)) {
497
+ throw new GraphQLError(`$ref.extend.${key} must be an array; received ${typeof extensionValue}`);
498
+ }
499
+ const baseValue = processedData[key];
500
+ if (baseValue === undefined) {
501
+ processedData[key] = extensionValue;
502
+ continue;
503
+ }
504
+ if (!Array.isArray(baseValue)) {
505
+ throw new GraphQLError(`$ref.extend.${key} can only extend an array property; "${key}" resolved to ${typeof baseValue}`);
506
+ }
507
+ processedData[key] = [...baseValue, ...extensionValue];
508
+ }
509
+ return processedData;
510
+ }
235
511
  async processReferencedObject(configPath, obj, context, values) {
236
512
  const referenceConfig = obj.$ref;
237
513
  const refConfigPath = typeof referenceConfig === 'object' && referenceConfig.refPath
238
514
  ? referenceConfig.refPath
239
515
  : referenceConfig;
240
- const imageData = await this.processImageReference(refConfigPath);
516
+ const imageData = await this.processImageReference(refConfigPath, context);
241
517
  if (imageData) {
242
518
  return imageData;
243
519
  }
@@ -246,6 +522,7 @@ class ConfigService {
246
522
  ? this.updatePathValues(referencedData, referenceConfig.path)
247
523
  : referencedData;
248
524
  processedData = this.applyReferenceOverrides(processedData, referenceConfig);
525
+ processedData = this.applyReferenceExtensions(processedData, referenceConfig);
249
526
  processedData = await this.processObjectExpressions(processedData, values, context);
250
527
  return this.processObject(configPath, processedData, context, values);
251
528
  }
@@ -262,7 +539,16 @@ class ConfigService {
262
539
  const builtInQueryResolvers = {
263
540
  pages: () => ({
264
541
  getPageConfig: async (obj, context) => {
265
- const pageConfig = await this.getPageConfig(obj.path, context, true, obj.context);
542
+ // `impersonateRoleId`/`impersonateGeneral` are a plain
543
+ // pass-through onto context here — this engine never inspects
544
+ // identity itself; the host's resolveCurrentIdentity is what
545
+ // interprets them and enforces the admin-only restriction.
546
+ const impersonation = obj.impersonateRoleId
547
+ ? { impersonateRoleId: obj.impersonateRoleId }
548
+ : obj.impersonateGeneral
549
+ ? { impersonateGeneral: true }
550
+ : null;
551
+ const pageConfig = await this.getPageConfig(obj.path, impersonation ? { ...context, ...impersonation } : context, true, obj.context);
266
552
  return pageConfig;
267
553
  },
268
554
  getAIConfig: async (obj, context) => {
@@ -346,29 +632,37 @@ class ConfigService {
346
632
  return deepMergeResolvers(builtInMutationResolvers, hostMutationResolvers);
347
633
  };
348
634
  }
349
- async processConfig(config, context, processProviders) {
635
+ // Applies any stored config-path sub-path overrides across the whole
636
+ // config tree before processConfig's own recursive walk runs, so that
637
+ // overridden values (which may introduce new sub-config) still go
638
+ // through provider injection/__typename dispatch below.
639
+ //
640
+ // rootSubPathOverrides lets a caller that already resolved overrides for
641
+ // the root config (e.g. getConfigFromStore, which fetches the whole-object
642
+ // and sub-path overrides together in one round trip) hand them in directly
643
+ // instead of having the root node re-resolved a second time below; nested
644
+ // nodes discovered deeper in the tree are still resolved individually,
645
+ // since their configPaths aren't known until the tree has been loaded.
646
+ async processConfig(config, context, processProviders, rootSubPathOverrides) {
647
+ const overriddenConfig = await this.applyConfigPathOverride(config, context, processProviders, rootSubPathOverrides);
648
+ return this.processConfigTree(overriddenConfig, context, processProviders);
649
+ }
650
+ async processConfigTree(config, context, processProviders) {
350
651
  if (!config) {
351
652
  return config;
352
653
  }
353
654
  // Handle arrays
354
655
  if (Array.isArray(config)) {
355
- return Promise.all(config.map((item) => this.processConfig(item, context, processProviders)));
656
+ return Promise.all(config.map((item) => this.processConfigTree(item, context, processProviders)));
356
657
  }
357
658
  // Handle objects
358
659
  if (typeof config === 'object') {
359
660
  // Recursively process all properties of config
360
661
  let processedConfig = { ...config };
361
662
  for (const key in processedConfig) {
362
- processedConfig[key] = await this.processConfig(processedConfig[key], context, processProviders);
363
- }
364
- // First process the current object if it has __typename
365
- if (processedConfig?.__typename && processProviders) {
366
- const providerClass = this.providers[processedConfig.__typename];
367
- if (providerClass) {
368
- const provider = new providerClass();
369
- processedConfig = await provider.processConfig(processedConfig, this, context);
370
- }
663
+ processedConfig[key] = await this.processConfigTree(processedConfig[key], context, processProviders);
371
664
  }
665
+ processedConfig = await this.applyTypenameProvider(processedConfig, context, processProviders);
372
666
  return processedConfig;
373
667
  }
374
668
  if (processProviders) {
@@ -376,6 +670,59 @@ class ConfigService {
376
670
  }
377
671
  return config;
378
672
  }
673
+ // Walks the whole config tree up front (pre-order) and, for every object
674
+ // that declares a configPath, deep-sets any stored sub-path overrides
675
+ // (JSONPath-addressed, e.g. "$.columns" or "$.filter.criteria").
676
+ //
677
+ // preFetchedSubPaths is only honored for the root node of the walk (the
678
+ // one being called with a defined third argument); nested calls always
679
+ // omit it so their own configPath gets resolved fresh.
680
+ async applyConfigPathOverride(config, context, processProviders, preFetchedSubPaths) {
681
+ if (!config) {
682
+ return config;
683
+ }
684
+ if (Array.isArray(config)) {
685
+ return Promise.all(config.map((item) => this.applyConfigPathOverride(item, context, processProviders)));
686
+ }
687
+ if (typeof config !== 'object') {
688
+ return config;
689
+ }
690
+ let result = config;
691
+ if (processProviders) {
692
+ const subPathPatch = await this.resolveSubPathOverrideLayers(result, context, preFetchedSubPaths);
693
+ if (subPathPatch) {
694
+ result = applySubPathOverrides(result, subPathPatch);
695
+ }
696
+ }
697
+ const overriddenEntries = await Promise.all(Object.entries(result).map(async ([key, value]) => [
698
+ key,
699
+ await this.applyConfigPathOverride(value, context, processProviders),
700
+ ]));
701
+ return Object.fromEntries(overriddenEntries);
702
+ }
703
+ // Resolves the sub-path override layers that apply to `config`, unless the
704
+ // caller already fetched them (`preFetchedSubPaths`, honored for the root
705
+ // node of a walk — including when it is explicitly `null`).
706
+ async resolveSubPathOverrideLayers(config, context, preFetchedSubPaths) {
707
+ if (preFetchedSubPaths !== undefined) {
708
+ return preFetchedSubPaths;
709
+ }
710
+ if (!config.configPath || !this.configurationOverrideProvider) {
711
+ return null;
712
+ }
713
+ return toSubPathOverrideLayers(await this.configurationOverrideProvider.resolve(config.configPath, context));
714
+ }
715
+ async applyTypenameProvider(processedConfig, context, processProviders) {
716
+ if (!processedConfig?.__typename || !processProviders) {
717
+ return processedConfig;
718
+ }
719
+ const providerClass = this.providers[processedConfig.__typename];
720
+ if (!providerClass) {
721
+ return processedConfig;
722
+ }
723
+ const provider = new providerClass();
724
+ return await provider.processConfig(processedConfig, this, context);
725
+ }
379
726
  async getRefConfig(configPath, refConfigPath, context, values) {
380
727
  let fullPath;
381
728
  if (refConfigPath.startsWith('.')) {
@@ -462,13 +809,25 @@ class ConfigService {
462
809
  };
463
810
  return await processValue(object);
464
811
  }
465
- async processImageReference(refConfigPath) {
812
+ async processImageReference(refConfigPath, context) {
466
813
  const SUPPORTED_IMAGE_FORMATS = ['.jpg', '.jpeg', '.png', '.svg'];
467
814
  // Check if the reference is an image file
468
815
  const ext = path.extname(refConfigPath).toLowerCase();
469
816
  if (!SUPPORTED_IMAGE_FORMATS.includes(ext)) {
470
817
  return null;
471
818
  }
819
+ const override = await this.configurationOverrideProvider?.resolve(refConfigPath, context);
820
+ if (override?.whole?.contentType) {
821
+ // A real-MIME-type asset override (image/svg+xml, etc.) is always base64 text, never the
822
+ // JSON-shaped value the CLOB/JSON attribute mismatch affects — see the screen-layout-patch
823
+ // branch below for that case.
824
+ const data = override.whole.value;
825
+ return {
826
+ data,
827
+ mimeType: override.whole.contentType,
828
+ hash: createHash('sha256').update(data).digest('hex'),
829
+ };
830
+ }
472
831
  let standardStore;
473
832
  for (const store of this.configStores) {
474
833
  if (store instanceof ConfigFileStore) {
@@ -520,19 +879,37 @@ class ConfigService {
520
879
  return 'application/octet-stream';
521
880
  }
522
881
  }
523
- async getConfigFromStore(path) {
882
+ // Resolves the whole-object override and any sub-path overrides for
883
+ // configPath together, in one provider round trip, instead of fetching
884
+ // them separately at different points of the processing pipeline. The
885
+ // sub-path overrides are handed back alongside the config so callers can
886
+ // thread them into processConfig rather than re-resolving them later; the
887
+ // whole override is handed back too so a screen-layout-patch (a whole
888
+ // override distinguished by its contentType) can be applied later in the
889
+ // pipeline without a second resolve() call.
890
+ async getConfigFromStore(configPath, context) {
891
+ const override = await this.configurationOverrideProvider?.resolve(configPath, context);
892
+ const subPathOverrides = override
893
+ ? toSubPathOverrideLayers(override)
894
+ : null;
895
+ const whole = override?.whole ?? null;
896
+ if (whole && !whole.contentType) {
897
+ return { config: load(whole.value), subPathOverrides, whole };
898
+ }
524
899
  const exceptions = [];
525
900
  for (const [index, configStore] of this.configStores.entries()) {
526
901
  try {
527
- return await configStore.getConfig(path);
902
+ const config = await configStore.getConfig(configPath);
903
+ return { config, subPathOverrides, whole };
528
904
  }
529
905
  catch (e) {
530
906
  exceptions.push(e);
531
907
  if (index == this.configStores.length - 1) {
532
- throw new GraphQLError(`Config not found in path in any store: ${path}`, { originalError: exceptions });
908
+ throw new GraphQLError(`Config not found in path in any store: ${configPath}`, { originalError: exceptions });
533
909
  }
534
910
  }
535
911
  }
912
+ return { config: undefined, subPathOverrides, whole };
536
913
  }
537
914
  async hasConfig(configPath) {
538
915
  return (await Promise.all(this.configStores.map((configStore) => configStore.hasConfig(configPath)))).some(Boolean);
@@ -542,10 +919,40 @@ class ConfigService {
542
919
  return this.getDirectConfig(globalPath, context, values, processProviders);
543
920
  }
544
921
  async getDirectConfig(configPath, context, values, processProviders) {
545
- const obj = await this.getConfigFromStore(configPath);
922
+ const { config: obj, subPathOverrides, whole, } = await this.getConfigFromStore(configPath, context);
546
923
  const fullObj = await this.processObject(configPath, obj, context, values);
547
- const processedObj = await this.processConfig(fullObj, context, processProviders);
548
- return processedObj;
924
+ const processedObj = await this.processConfig(fullObj, context, processProviders, subPathOverrides);
925
+ // A whole-object patch override needs the fully $ref/rules-resolved config to merge
926
+ // onto (its own document is name-keyed against that resolved shape), so it's applied
927
+ // last — after the whole-file-replacement branch in getConfigFromStore (which this is
928
+ // mutually exclusive with; that branch short-circuits before $ref resolution) and
929
+ // after processConfig, not folded into either.
930
+ return this.applyWholePatchIfPresent(processedObj, whole);
931
+ }
932
+ // Shared by getDirectConfig and getPageConfig — both resolve a fully $ref/rules-processed
933
+ // config that a whole-object patch override (if any exists for this path) needs to merge
934
+ // onto, so both need this as their final step. `whole` is the same whole-override already
935
+ // resolved once by getConfigFromStore — no second resolve() call needed.
936
+ //
937
+ // This has no knowledge of what any particular contentType's patch document looks like —
938
+ // it only knows how to look up a WholePatchMerger by contentType and delegate to it. A
939
+ // `whole` override with no contentType is a full-file replacement, already substituted in
940
+ // by getConfigFromStore, so there's nothing left to merge here; a contentType with no
941
+ // registered merger is left untouched rather than guessed at.
942
+ applyWholePatchIfPresent(processedObj, whole) {
943
+ const merger = whole?.contentType
944
+ ? this.wholePatchMergers[whole.contentType]
945
+ : undefined;
946
+ if (!merger) {
947
+ return processedObj;
948
+ }
949
+ // `Configuration.value` is typed CLOB, but the underlying attribute is JSON — for a
950
+ // JSON-shaped value like a whole patch, it can come back already parsed as an object
951
+ // rather than as the JSON string it was saved as. Handle both shapes.
952
+ const patch = whole.value && typeof whole.value === 'object'
953
+ ? whole.value
954
+ : JSON.parse(whole.value);
955
+ return merger.merge(processedObj, patch);
549
956
  }
550
957
  async listDirectConfigs(configPath) {
551
958
  const configs = await Promise.all(this.configStores.map((configStore) => configStore.listConfigs(configPath)));
@@ -895,21 +1302,28 @@ class ConfigService {
895
1302
  }
896
1303
  async getPageConfig(pagePath, context, processConfig, rulesContext) {
897
1304
  const globalPath = path.join('global', 'pages', pagePath);
898
- const obj = await this.getConfigFromStore(globalPath);
1305
+ const { config: obj, subPathOverrides, whole, } = await this.getConfigFromStore(globalPath, context);
899
1306
  const fullObj = await this.processObject(globalPath, obj, context, null);
900
1307
  const matchingRuleGroups = await this.getRuleGroups(rulesContext, context);
901
1308
  const configToProcess = this.applyFieldRuleOverrides(fullObj, matchingRuleGroups);
902
- return await this.processConfig(configToProcess, context, processConfig);
1309
+ const processedObj = await this.processConfig(configToProcess, context, processConfig, subPathOverrides);
1310
+ // Keyed by globalPath — the same `global/pages/<pagePath>` path a whole-patch save
1311
+ // (e.g. the Screen Designer's, see toPageConfigOverridePath in docupace-ui-main) writes
1312
+ // under, so a patch saved against a route like `contacts/{id}/details` is found here
1313
+ // too, not just in getDirectConfig.
1314
+ return this.applyWholePatchIfPresent(processedObj, whole);
903
1315
  }
904
1316
  async init() {
905
1317
  return Promise.all(this.configStores.map((configStore) => configStore.init()));
906
1318
  }
907
1319
  }
908
- export const defaultConfigService = ({ model, configStores, configInjectorResolvers, providers, resolvers, }) => {
1320
+ export const defaultConfigService = ({ model, configStores, configInjectorResolvers, providers, wholePatchMergers, resolvers, configurationOverrideProvider, }) => {
909
1321
  const extensionOptions = {
910
1322
  configInjectorResolvers,
911
1323
  providers,
1324
+ wholePatchMergers,
912
1325
  resolvers,
1326
+ configurationOverrideProvider,
913
1327
  };
914
1328
  return new ConfigService(model, configStores.map((configStore) => new ConfigFileStore(configStore)), extensionOptions);
915
1329
  };