@cmssy/react 0.1.6 → 0.1.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.
package/dist/index.cjs CHANGED
@@ -547,6 +547,46 @@ async function resolveForms(config, blocks, locale, defaultLocale, options) {
547
547
  }
548
548
  return forms;
549
549
  }
550
+
551
+ // src/data/site-locales.ts
552
+ var TTL_MS = 6e4;
553
+ var MAX_ENTRIES = 64;
554
+ var cache = /* @__PURE__ */ new Map();
555
+ async function resolveSiteLocales(config, options) {
556
+ const key = `${config.apiUrl}::${config.workspaceSlug}`;
557
+ const cached = cache.get(key);
558
+ if (cached && cached.expires > Date.now()) return cached.value;
559
+ cache.delete(key);
560
+ let value;
561
+ try {
562
+ const data = await graphqlRequest(
563
+ config,
564
+ SITE_CONFIG_QUERY,
565
+ { workspaceSlug: config.workspaceSlug },
566
+ options,
567
+ "site config"
568
+ );
569
+ const siteConfig = data.publicSiteConfig;
570
+ const defaultLocale = siteConfig?.defaultLanguage || "en";
571
+ const enabled = siteConfig?.enabledLanguages ?? [];
572
+ value = {
573
+ defaultLocale,
574
+ locales: enabled.length > 0 ? enabled : [defaultLocale]
575
+ };
576
+ } catch {
577
+ value = { defaultLocale: "en", locales: ["en"] };
578
+ }
579
+ if (cache.size >= MAX_ENTRIES) cache.clear();
580
+ cache.set(key, { value, expires: Date.now() + TTL_MS });
581
+ return value;
582
+ }
583
+ function splitLocaleFromPath(path, siteLocales) {
584
+ const first = path?.[0];
585
+ if (first && first !== siteLocales.defaultLocale && siteLocales.locales.includes(first)) {
586
+ return { locale: first, path: path.slice(1) };
587
+ }
588
+ return { locale: siteLocales.defaultLocale, path };
589
+ }
550
590
  function CmssyBlock({
551
591
  block,
552
592
  locale,
@@ -601,3 +641,5 @@ exports.normalizeSlug = normalizeSlug;
601
641
  exports.parseEditorMessage = parseEditorMessage;
602
642
  exports.postToEditor = postToEditor;
603
643
  exports.resolveForms = resolveForms;
644
+ exports.resolveSiteLocales = resolveSiteLocales;
645
+ exports.splitLocaleFromPath = splitLocaleFromPath;
package/dist/index.d.cts CHANGED
@@ -74,6 +74,16 @@ declare function createCmssyClient(config: CmssyClientConfig): CmssyClient;
74
74
  declare function collectFormIds(blocks: RawBlock[], locale: string, defaultLocale: string): string[];
75
75
  declare function resolveForms(config: CmssyClientConfig, blocks: RawBlock[], locale: string, defaultLocale: string, options?: QueryScopedOptions): Promise<Record<string, CmssyFormDefinition>>;
76
76
 
77
+ interface CmssySiteLocales {
78
+ defaultLocale: string;
79
+ locales: string[];
80
+ }
81
+ declare function resolveSiteLocales(config: CmssyClientConfig, options?: GraphqlRequestOptions): Promise<CmssySiteLocales>;
82
+ declare function splitLocaleFromPath(path: string[] | undefined, siteLocales: CmssySiteLocales): {
83
+ locale: string;
84
+ path: string[] | undefined;
85
+ };
86
+
77
87
  interface CmssyBlockProps {
78
88
  block: RawBlock;
79
89
  locale: string;
@@ -91,4 +101,4 @@ interface UnknownBlockProps {
91
101
  }
92
102
  declare function UnknownBlock({ type }: UnknownBlockProps): react_jsx_runtime.JSX.Element;
93
103
 
94
- export { AppToEditorMessage, BlockDefinition, BlockMap, CmssyBlock, CmssyBlockContext, type CmssyBlockProps, type CmssyClient, CmssyClientConfig, CmssyFormDefinition, CmssyLayoutGroup, CmssyPageData, CmssyServerLayout, type CmssyServerLayoutProps, CmssyServerPage, type CmssyServerPageProps, EditorToAppMessage, FetchLike, type FieldControl, FieldDefinition, type GraphqlRequestOptions, type PostTarget, type QueryScopedOptions, RawBlock, UnknownBlock, type UnknownBlockProps, collectFormIds, createCmssyClient, fields, getBlockContentForLanguage, graphqlRequest, normalizeOrigin, parseEditorMessage, postToEditor, resolveForms };
104
+ export { AppToEditorMessage, BlockDefinition, BlockMap, CmssyBlock, CmssyBlockContext, type CmssyBlockProps, type CmssyClient, CmssyClientConfig, CmssyFormDefinition, CmssyLayoutGroup, CmssyPageData, CmssyServerLayout, type CmssyServerLayoutProps, CmssyServerPage, type CmssyServerPageProps, type CmssySiteLocales, EditorToAppMessage, FetchLike, type FieldControl, FieldDefinition, type GraphqlRequestOptions, type PostTarget, type QueryScopedOptions, RawBlock, UnknownBlock, type UnknownBlockProps, collectFormIds, createCmssyClient, fields, getBlockContentForLanguage, graphqlRequest, normalizeOrigin, parseEditorMessage, postToEditor, resolveForms, resolveSiteLocales, splitLocaleFromPath };
package/dist/index.d.ts CHANGED
@@ -74,6 +74,16 @@ declare function createCmssyClient(config: CmssyClientConfig): CmssyClient;
74
74
  declare function collectFormIds(blocks: RawBlock[], locale: string, defaultLocale: string): string[];
75
75
  declare function resolveForms(config: CmssyClientConfig, blocks: RawBlock[], locale: string, defaultLocale: string, options?: QueryScopedOptions): Promise<Record<string, CmssyFormDefinition>>;
76
76
 
77
+ interface CmssySiteLocales {
78
+ defaultLocale: string;
79
+ locales: string[];
80
+ }
81
+ declare function resolveSiteLocales(config: CmssyClientConfig, options?: GraphqlRequestOptions): Promise<CmssySiteLocales>;
82
+ declare function splitLocaleFromPath(path: string[] | undefined, siteLocales: CmssySiteLocales): {
83
+ locale: string;
84
+ path: string[] | undefined;
85
+ };
86
+
77
87
  interface CmssyBlockProps {
78
88
  block: RawBlock;
79
89
  locale: string;
@@ -91,4 +101,4 @@ interface UnknownBlockProps {
91
101
  }
92
102
  declare function UnknownBlock({ type }: UnknownBlockProps): react_jsx_runtime.JSX.Element;
93
103
 
94
- export { AppToEditorMessage, BlockDefinition, BlockMap, CmssyBlock, CmssyBlockContext, type CmssyBlockProps, type CmssyClient, CmssyClientConfig, CmssyFormDefinition, CmssyLayoutGroup, CmssyPageData, CmssyServerLayout, type CmssyServerLayoutProps, CmssyServerPage, type CmssyServerPageProps, EditorToAppMessage, FetchLike, type FieldControl, FieldDefinition, type GraphqlRequestOptions, type PostTarget, type QueryScopedOptions, RawBlock, UnknownBlock, type UnknownBlockProps, collectFormIds, createCmssyClient, fields, getBlockContentForLanguage, graphqlRequest, normalizeOrigin, parseEditorMessage, postToEditor, resolveForms };
104
+ export { AppToEditorMessage, BlockDefinition, BlockMap, CmssyBlock, CmssyBlockContext, type CmssyBlockProps, type CmssyClient, CmssyClientConfig, CmssyFormDefinition, CmssyLayoutGroup, CmssyPageData, CmssyServerLayout, type CmssyServerLayoutProps, CmssyServerPage, type CmssyServerPageProps, type CmssySiteLocales, EditorToAppMessage, FetchLike, type FieldControl, FieldDefinition, type GraphqlRequestOptions, type PostTarget, type QueryScopedOptions, RawBlock, UnknownBlock, type UnknownBlockProps, collectFormIds, createCmssyClient, fields, getBlockContentForLanguage, graphqlRequest, normalizeOrigin, parseEditorMessage, postToEditor, resolveForms, resolveSiteLocales, splitLocaleFromPath };
package/dist/index.js CHANGED
@@ -545,6 +545,46 @@ async function resolveForms(config, blocks, locale, defaultLocale, options) {
545
545
  }
546
546
  return forms;
547
547
  }
548
+
549
+ // src/data/site-locales.ts
550
+ var TTL_MS = 6e4;
551
+ var MAX_ENTRIES = 64;
552
+ var cache = /* @__PURE__ */ new Map();
553
+ async function resolveSiteLocales(config, options) {
554
+ const key = `${config.apiUrl}::${config.workspaceSlug}`;
555
+ const cached = cache.get(key);
556
+ if (cached && cached.expires > Date.now()) return cached.value;
557
+ cache.delete(key);
558
+ let value;
559
+ try {
560
+ const data = await graphqlRequest(
561
+ config,
562
+ SITE_CONFIG_QUERY,
563
+ { workspaceSlug: config.workspaceSlug },
564
+ options,
565
+ "site config"
566
+ );
567
+ const siteConfig = data.publicSiteConfig;
568
+ const defaultLocale = siteConfig?.defaultLanguage || "en";
569
+ const enabled = siteConfig?.enabledLanguages ?? [];
570
+ value = {
571
+ defaultLocale,
572
+ locales: enabled.length > 0 ? enabled : [defaultLocale]
573
+ };
574
+ } catch {
575
+ value = { defaultLocale: "en", locales: ["en"] };
576
+ }
577
+ if (cache.size >= MAX_ENTRIES) cache.clear();
578
+ cache.set(key, { value, expires: Date.now() + TTL_MS });
579
+ return value;
580
+ }
581
+ function splitLocaleFromPath(path, siteLocales) {
582
+ const first = path?.[0];
583
+ if (first && first !== siteLocales.defaultLocale && siteLocales.locales.includes(first)) {
584
+ return { locale: first, path: path.slice(1) };
585
+ }
586
+ return { locale: siteLocales.defaultLocale, path };
587
+ }
548
588
  function CmssyBlock({
549
589
  block,
550
590
  locale,
@@ -571,4 +611,4 @@ function CmssyBlock({
571
611
  );
572
612
  }
573
613
 
574
- export { CmssyBlock, CmssyServerLayout, CmssyServerPage, FORM_QUERY, MODEL_DEFINITIONS_QUERY, MODEL_RECORDS_QUERY, PROTOCOL_VERSION, SITE_CONFIG_QUERY, SUBMIT_FORM_MUTATION, UnknownBlock, blocksToMeta, blocksToSchemas, buildBlockContext, buildBlockMap, collectFormIds, createCmssyClient, defineBlock, fetchLayouts, fetchPage, fields, getBlockContentForLanguage, graphqlRequest, isProtocolCompatible, normalizeOrigin, normalizeSlug, parseEditorMessage, postToEditor, resolveForms };
614
+ export { CmssyBlock, CmssyServerLayout, CmssyServerPage, FORM_QUERY, MODEL_DEFINITIONS_QUERY, MODEL_RECORDS_QUERY, PROTOCOL_VERSION, SITE_CONFIG_QUERY, SUBMIT_FORM_MUTATION, UnknownBlock, blocksToMeta, blocksToSchemas, buildBlockContext, buildBlockMap, collectFormIds, createCmssyClient, defineBlock, fetchLayouts, fetchPage, fields, getBlockContentForLanguage, graphqlRequest, isProtocolCompatible, normalizeOrigin, normalizeSlug, parseEditorMessage, postToEditor, resolveForms, resolveSiteLocales, splitLocaleFromPath };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cmssy/react",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "description": "React blocks, renderers, data client and editor bridge for cmssy headless sites",
5
5
  "keywords": [
6
6
  "cmssy",