@cms0/cms0 0.2.5 → 0.2.7

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.
@@ -484,12 +484,19 @@ async function normalizeObjectField(descriptor, path, raw, context, trail, isCol
484
484
  }
485
485
  if (isArrayDescriptor(propertyDescriptor)) {
486
486
  const childPath = `${path}/${propertyName}`;
487
- const childRaw = await context.requestJson(childPath, {
488
- query: {
489
- raw: 1,
490
- ...(context.options.locale ? { locale: context.options.locale } : {}),
491
- },
492
- });
487
+ const inlineChildRaw = source[propertyName];
488
+ if (inlineChildRaw === null) {
489
+ output[propertyName] = null;
490
+ continue;
491
+ }
492
+ const childRaw = inlineChildRaw !== undefined
493
+ ? inlineChildRaw
494
+ : await context.requestJson(childPath, {
495
+ query: {
496
+ raw: 1,
497
+ ...(context.options.locale ? { locale: context.options.locale } : {}),
498
+ },
499
+ });
493
500
  const normalizedChild = await normalizeArrayField(propertyDescriptor, childPath, childRaw, context, trail);
494
501
  output[propertyName] = coerceCustomTypeValue(propertyDescriptor, normalizedChild, {
495
502
  locale: context.options.locale,
@@ -500,12 +507,19 @@ async function normalizeObjectField(descriptor, path, raw, context, trail, isCol
500
507
  }
501
508
  if (isObjectDescriptor(propertyDescriptor)) {
502
509
  const childPath = `${path}/${propertyName}`;
503
- const childRaw = await context.requestJson(childPath, {
504
- query: {
505
- raw: 1,
506
- ...(context.options.locale ? { locale: context.options.locale } : {}),
507
- },
508
- });
510
+ const inlineChildRaw = source[propertyName];
511
+ if (inlineChildRaw === null) {
512
+ output[propertyName] = null;
513
+ continue;
514
+ }
515
+ const childRaw = inlineChildRaw !== undefined
516
+ ? inlineChildRaw
517
+ : await context.requestJson(childPath, {
518
+ query: {
519
+ raw: 1,
520
+ ...(context.options.locale ? { locale: context.options.locale } : {}),
521
+ },
522
+ });
509
523
  const normalizedChild = await normalizeObjectField(propertyDescriptor, childPath, childRaw, context, trail, false);
510
524
  output[propertyName] = coerceCustomTypeValue(propertyDescriptor, normalizedChild, {
511
525
  locale: context.options.locale,
@@ -752,13 +766,47 @@ async function runResource(resource, descriptor, baseUrl, apiKey, defaultLocale,
752
766
  if (locale && query.locale === undefined) {
753
767
  query.locale = locale;
754
768
  }
755
- const resourcePath = byId
769
+ const useResolvedEndpoint = shouldNormalize &&
770
+ !byId &&
771
+ resource.kind === "root" &&
772
+ !resource.isCollection &&
773
+ isObjectDescriptor(resource.descriptor) &&
774
+ resolveModelRefs;
775
+ if (useResolvedEndpoint && query.resolveModelRefs === undefined) {
776
+ query.resolveModelRefs = 1;
777
+ }
778
+ const normalizePath = byId
756
779
  ? `${resource.path}/${encodeURIComponent(byId)}`
757
780
  : resource.path;
758
- const rawData = await requestJson(baseUrl, apiKey, resourcePath, {
759
- query,
760
- signal: options?.signal,
761
- });
781
+ const requestPath = useResolvedEndpoint
782
+ ? `_resolved/${encodeURIComponent(resource.key)}`
783
+ : normalizePath;
784
+ let rawData;
785
+ if (useResolvedEndpoint) {
786
+ try {
787
+ rawData = await requestJson(baseUrl, apiKey, requestPath, {
788
+ query,
789
+ signal: options?.signal,
790
+ });
791
+ }
792
+ catch (resolvedError) {
793
+ try {
794
+ rawData = await requestJson(baseUrl, apiKey, normalizePath, {
795
+ query,
796
+ signal: options?.signal,
797
+ });
798
+ }
799
+ catch {
800
+ throw resolvedError;
801
+ }
802
+ }
803
+ }
804
+ else {
805
+ rawData = await requestJson(baseUrl, apiKey, requestPath, {
806
+ query,
807
+ signal: options?.signal,
808
+ });
809
+ }
762
810
  if (!shouldNormalize) {
763
811
  return rawData;
764
812
  }
@@ -804,7 +852,7 @@ async function runResource(resource, descriptor, baseUrl, apiKey, defaultLocale,
804
852
  validateResult(resource, normalizedItems, descriptor, includeIdMode, zodSchemas, modelZodSchemas);
805
853
  return normalizedItems;
806
854
  }
807
- const normalized = await normalizeField(resource.descriptor, resourcePath, rawData, context, new Set(), false);
855
+ const normalized = await normalizeField(resource.descriptor, normalizePath, rawData, context, new Set(), false);
808
856
  validateResult(resource, normalized, descriptor, includeIdMode, zodSchemas, modelZodSchemas);
809
857
  return normalized;
810
858
  }
@@ -837,14 +885,25 @@ function createCmsClient(descriptor, config) {
837
885
  return buildModelAccessor(modelEntry);
838
886
  },
839
887
  });
840
- const rootProxy = new Proxy({}, {
841
- get(_target, property) {
842
- if (typeof property !== "string")
843
- return undefined;
888
+ const clientMeta = {
889
+ locales: config.locales ?? [],
890
+ defaultLocale: config.defaultLocale,
891
+ includeIdDefault: !!config.includeId,
892
+ };
893
+ const rootProxyTarget = {
894
+ models: modelsProxy,
895
+ meta: clientMeta,
896
+ };
897
+ const rootProxy = new Proxy(rootProxyTarget, {
898
+ get(target, property, receiver) {
899
+ if (typeof property !== "string") {
900
+ return Reflect.get(target, property, receiver);
901
+ }
844
902
  if (property === "then")
845
903
  return undefined;
846
- if (property === "models")
847
- return modelsProxy;
904
+ if (Reflect.has(target, property)) {
905
+ return Reflect.get(target, property, receiver);
906
+ }
848
907
  const entry = registry.roots.get(property) ??
849
908
  registry.rootsCaseInsensitive.get(property.toLowerCase());
850
909
  if (!entry) {
@@ -853,13 +912,7 @@ function createCmsClient(descriptor, config) {
853
912
  return async (options) => runResource(entry, descriptor, baseUrl, apiKey, config.defaultLocale, assetUrlBuilder, zodSchemas, modelZodSchemas, globalIncludeId, sharedModelInflightCache, options);
854
913
  },
855
914
  });
856
- const client = Object.assign(rootProxy, {
857
- models: modelsProxy,
858
- locales: config.locales ?? [],
859
- defaultLocale: config.defaultLocale,
860
- includeIdDefault: !!config.includeId,
861
- });
862
- return client;
915
+ return rootProxy;
863
916
  }
864
917
  function cms0(config) {
865
918
  return createCmsClient(schema_descriptors_1.schemaDescriptor, config);