@cmssy/react 0.1.8 → 0.1.9

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/client.cjs CHANGED
@@ -747,9 +747,105 @@ function CmssyLazyLayout({ load, ...props }) {
747
747
  if (!blocks) return null;
748
748
  return /* @__PURE__ */ jsxRuntime.jsx(CmssyEditableLayout, { ...props, blocks });
749
749
  }
750
+ var CmssyAuthContext = react.createContext(null);
751
+ function CmssyAuthProvider({
752
+ children,
753
+ basePath = "/api/cmssy/auth",
754
+ initialUser
755
+ }) {
756
+ const seeded = initialUser !== void 0;
757
+ const [user, setUser] = react.useState(initialUser ?? null);
758
+ const [loading, setLoading] = react.useState(!seeded);
759
+ const generation = react.useRef(0);
760
+ const base = react.useMemo(() => basePath.replace(/\/+$/, ""), [basePath]);
761
+ const commitUser = react.useCallback((next) => {
762
+ generation.current += 1;
763
+ setUser(next);
764
+ }, []);
765
+ const fetchUser = react.useCallback(async () => {
766
+ try {
767
+ const res = await fetch(`${base}/me`, {
768
+ credentials: "same-origin",
769
+ cache: "no-store"
770
+ });
771
+ const data = await res.json();
772
+ return data.user ?? null;
773
+ } catch {
774
+ return null;
775
+ }
776
+ }, [base]);
777
+ react.useEffect(() => {
778
+ if (seeded) return;
779
+ const gen = generation.current;
780
+ void (async () => {
781
+ const next = await fetchUser();
782
+ if (gen === generation.current) {
783
+ setUser(next);
784
+ setLoading(false);
785
+ }
786
+ })();
787
+ }, [fetchUser, seeded]);
788
+ const postAction = react.useCallback(
789
+ async (action, body) => {
790
+ const res = await fetch(`${base}/${action}`, {
791
+ method: "POST",
792
+ credentials: "same-origin",
793
+ headers: { "content-type": "application/json" },
794
+ body: JSON.stringify(body)
795
+ });
796
+ return await res.json();
797
+ },
798
+ [base]
799
+ );
800
+ const signIn = react.useCallback(
801
+ async (identity, password) => {
802
+ const data = await postAction("sign-in", { identity, password });
803
+ if (data.ok && data.user) commitUser(data.user);
804
+ return { ok: Boolean(data.ok), message: data.message };
805
+ },
806
+ [postAction, commitUser]
807
+ );
808
+ const register = react.useCallback(
809
+ async (identity, password, fields) => {
810
+ const data = await postAction("register", {
811
+ identity,
812
+ password,
813
+ fields: fields ?? {}
814
+ });
815
+ return { ok: Boolean(data.ok), message: data.message };
816
+ },
817
+ [postAction]
818
+ );
819
+ const signOut = react.useCallback(async () => {
820
+ try {
821
+ await postAction("sign-out", {});
822
+ } finally {
823
+ commitUser(null);
824
+ }
825
+ }, [postAction, commitUser]);
826
+ const refresh = react.useCallback(async () => {
827
+ const gen = generation.current;
828
+ const next = await fetchUser();
829
+ if (gen === generation.current) setUser(next);
830
+ }, [fetchUser]);
831
+ const value = react.useMemo(
832
+ () => ({ user, loading, signIn, register, signOut, refresh }),
833
+ [user, loading, signIn, register, signOut, refresh]
834
+ );
835
+ return /* @__PURE__ */ jsxRuntime.jsx(CmssyAuthContext.Provider, { value, children });
836
+ }
837
+ function useCmssyUser() {
838
+ const ctx = react.useContext(CmssyAuthContext);
839
+ if (!ctx) {
840
+ throw new Error("useCmssyUser must be used within <CmssyAuthProvider>");
841
+ }
842
+ return ctx;
843
+ }
750
844
 
845
+ exports.CmssyAuthProvider = CmssyAuthProvider;
751
846
  exports.CmssyEditableLayout = CmssyEditableLayout;
752
847
  exports.CmssyEditablePage = CmssyEditablePage;
753
848
  exports.CmssyLazyEditor = CmssyLazyEditor;
754
849
  exports.CmssyLazyLayout = CmssyLazyLayout;
850
+ exports.useCmssyUser = useCmssyUser;
755
851
  exports.useEditBridge = useEditBridge;
package/dist/client.d.cts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import { B as BlockSchema, a as BlockMeta, C as CmssyPageData, b as BlockDefinition, c as CmssyFormDefinition, d as CmssyLayoutGroup } from './registry-e3O4s_bN.cjs';
3
- import 'react';
3
+ import { ReactNode } from 'react';
4
4
 
5
5
  interface EditBridgeConfig {
6
6
  editorOrigin: string;
@@ -80,4 +80,28 @@ interface CmssyLazyLayoutProps {
80
80
  }
81
81
  declare function CmssyLazyLayout({ load, ...props }: CmssyLazyLayoutProps): react_jsx_runtime.JSX.Element | null;
82
82
 
83
- export { CmssyEditableLayout, type CmssyEditableLayoutProps, CmssyEditablePage, type CmssyEditablePageProps, CmssyLazyEditor, type CmssyLazyEditorProps, CmssyLazyLayout, type CmssyLazyLayoutProps, type EditBridgeConfig, type EditBridgeState, type PatchMap, useEditBridge };
83
+ interface CmssyAuthUser {
84
+ recordId: string;
85
+ email: string;
86
+ }
87
+ interface CmssyAuthActionResult {
88
+ ok: boolean;
89
+ message?: string;
90
+ }
91
+ interface CmssyAuthState {
92
+ user: CmssyAuthUser | null;
93
+ loading: boolean;
94
+ signIn(identity: string, password: string): Promise<CmssyAuthActionResult>;
95
+ register(identity: string, password: string, fields?: Record<string, unknown>): Promise<CmssyAuthActionResult>;
96
+ signOut(): Promise<void>;
97
+ refresh(): Promise<void>;
98
+ }
99
+ interface CmssyAuthProviderProps {
100
+ children: ReactNode;
101
+ basePath?: string;
102
+ initialUser?: CmssyAuthUser | null;
103
+ }
104
+ declare function CmssyAuthProvider({ children, basePath, initialUser, }: CmssyAuthProviderProps): react_jsx_runtime.JSX.Element;
105
+ declare function useCmssyUser(): CmssyAuthState;
106
+
107
+ export { type CmssyAuthActionResult, CmssyAuthProvider, type CmssyAuthProviderProps, type CmssyAuthState, type CmssyAuthUser, CmssyEditableLayout, type CmssyEditableLayoutProps, CmssyEditablePage, type CmssyEditablePageProps, CmssyLazyEditor, type CmssyLazyEditorProps, CmssyLazyLayout, type CmssyLazyLayoutProps, type EditBridgeConfig, type EditBridgeState, type PatchMap, useCmssyUser, useEditBridge };
package/dist/client.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import { B as BlockSchema, a as BlockMeta, C as CmssyPageData, b as BlockDefinition, c as CmssyFormDefinition, d as CmssyLayoutGroup } from './registry-e3O4s_bN.js';
3
- import 'react';
3
+ import { ReactNode } from 'react';
4
4
 
5
5
  interface EditBridgeConfig {
6
6
  editorOrigin: string;
@@ -80,4 +80,28 @@ interface CmssyLazyLayoutProps {
80
80
  }
81
81
  declare function CmssyLazyLayout({ load, ...props }: CmssyLazyLayoutProps): react_jsx_runtime.JSX.Element | null;
82
82
 
83
- export { CmssyEditableLayout, type CmssyEditableLayoutProps, CmssyEditablePage, type CmssyEditablePageProps, CmssyLazyEditor, type CmssyLazyEditorProps, CmssyLazyLayout, type CmssyLazyLayoutProps, type EditBridgeConfig, type EditBridgeState, type PatchMap, useEditBridge };
83
+ interface CmssyAuthUser {
84
+ recordId: string;
85
+ email: string;
86
+ }
87
+ interface CmssyAuthActionResult {
88
+ ok: boolean;
89
+ message?: string;
90
+ }
91
+ interface CmssyAuthState {
92
+ user: CmssyAuthUser | null;
93
+ loading: boolean;
94
+ signIn(identity: string, password: string): Promise<CmssyAuthActionResult>;
95
+ register(identity: string, password: string, fields?: Record<string, unknown>): Promise<CmssyAuthActionResult>;
96
+ signOut(): Promise<void>;
97
+ refresh(): Promise<void>;
98
+ }
99
+ interface CmssyAuthProviderProps {
100
+ children: ReactNode;
101
+ basePath?: string;
102
+ initialUser?: CmssyAuthUser | null;
103
+ }
104
+ declare function CmssyAuthProvider({ children, basePath, initialUser, }: CmssyAuthProviderProps): react_jsx_runtime.JSX.Element;
105
+ declare function useCmssyUser(): CmssyAuthState;
106
+
107
+ export { type CmssyAuthActionResult, CmssyAuthProvider, type CmssyAuthProviderProps, type CmssyAuthState, type CmssyAuthUser, CmssyEditableLayout, type CmssyEditableLayoutProps, CmssyEditablePage, type CmssyEditablePageProps, CmssyLazyEditor, type CmssyLazyEditorProps, CmssyLazyLayout, type CmssyLazyLayoutProps, type EditBridgeConfig, type EditBridgeState, type PatchMap, useCmssyUser, useEditBridge };
package/dist/client.js CHANGED
@@ -1,5 +1,5 @@
1
1
  "use client";
2
- import { useState, useRef, useEffect, useMemo, createElement } from 'react';
2
+ import { createContext, useState, useRef, useEffect, useMemo, useCallback, useContext, createElement } from 'react';
3
3
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
4
4
 
5
5
  // src/registry.ts
@@ -745,5 +745,99 @@ function CmssyLazyLayout({ load, ...props }) {
745
745
  if (!blocks) return null;
746
746
  return /* @__PURE__ */ jsx(CmssyEditableLayout, { ...props, blocks });
747
747
  }
748
+ var CmssyAuthContext = createContext(null);
749
+ function CmssyAuthProvider({
750
+ children,
751
+ basePath = "/api/cmssy/auth",
752
+ initialUser
753
+ }) {
754
+ const seeded = initialUser !== void 0;
755
+ const [user, setUser] = useState(initialUser ?? null);
756
+ const [loading, setLoading] = useState(!seeded);
757
+ const generation = useRef(0);
758
+ const base = useMemo(() => basePath.replace(/\/+$/, ""), [basePath]);
759
+ const commitUser = useCallback((next) => {
760
+ generation.current += 1;
761
+ setUser(next);
762
+ }, []);
763
+ const fetchUser = useCallback(async () => {
764
+ try {
765
+ const res = await fetch(`${base}/me`, {
766
+ credentials: "same-origin",
767
+ cache: "no-store"
768
+ });
769
+ const data = await res.json();
770
+ return data.user ?? null;
771
+ } catch {
772
+ return null;
773
+ }
774
+ }, [base]);
775
+ useEffect(() => {
776
+ if (seeded) return;
777
+ const gen = generation.current;
778
+ void (async () => {
779
+ const next = await fetchUser();
780
+ if (gen === generation.current) {
781
+ setUser(next);
782
+ setLoading(false);
783
+ }
784
+ })();
785
+ }, [fetchUser, seeded]);
786
+ const postAction = useCallback(
787
+ async (action, body) => {
788
+ const res = await fetch(`${base}/${action}`, {
789
+ method: "POST",
790
+ credentials: "same-origin",
791
+ headers: { "content-type": "application/json" },
792
+ body: JSON.stringify(body)
793
+ });
794
+ return await res.json();
795
+ },
796
+ [base]
797
+ );
798
+ const signIn = useCallback(
799
+ async (identity, password) => {
800
+ const data = await postAction("sign-in", { identity, password });
801
+ if (data.ok && data.user) commitUser(data.user);
802
+ return { ok: Boolean(data.ok), message: data.message };
803
+ },
804
+ [postAction, commitUser]
805
+ );
806
+ const register = useCallback(
807
+ async (identity, password, fields) => {
808
+ const data = await postAction("register", {
809
+ identity,
810
+ password,
811
+ fields: fields ?? {}
812
+ });
813
+ return { ok: Boolean(data.ok), message: data.message };
814
+ },
815
+ [postAction]
816
+ );
817
+ const signOut = useCallback(async () => {
818
+ try {
819
+ await postAction("sign-out", {});
820
+ } finally {
821
+ commitUser(null);
822
+ }
823
+ }, [postAction, commitUser]);
824
+ const refresh = useCallback(async () => {
825
+ const gen = generation.current;
826
+ const next = await fetchUser();
827
+ if (gen === generation.current) setUser(next);
828
+ }, [fetchUser]);
829
+ const value = useMemo(
830
+ () => ({ user, loading, signIn, register, signOut, refresh }),
831
+ [user, loading, signIn, register, signOut, refresh]
832
+ );
833
+ return /* @__PURE__ */ jsx(CmssyAuthContext.Provider, { value, children });
834
+ }
835
+ function useCmssyUser() {
836
+ const ctx = useContext(CmssyAuthContext);
837
+ if (!ctx) {
838
+ throw new Error("useCmssyUser must be used within <CmssyAuthProvider>");
839
+ }
840
+ return ctx;
841
+ }
748
842
 
749
- export { CmssyEditableLayout, CmssyEditablePage, CmssyLazyEditor, CmssyLazyLayout, useEditBridge };
843
+ export { CmssyAuthProvider, CmssyEditableLayout, CmssyEditablePage, CmssyLazyEditor, CmssyLazyLayout, useCmssyUser, useEditBridge };
package/dist/index.cjs CHANGED
@@ -685,4 +685,5 @@ exports.parseEditorMessage = parseEditorMessage;
685
685
  exports.postToEditor = postToEditor;
686
686
  exports.resolveForms = resolveForms;
687
687
  exports.resolveSiteLocales = resolveSiteLocales;
688
+ exports.resolveWorkspaceId = resolveWorkspaceId;
688
689
  exports.splitLocaleFromPath = splitLocaleFromPath;
package/dist/index.d.cts CHANGED
@@ -65,6 +65,8 @@ interface GraphqlRequestOptions {
65
65
  }
66
66
  declare function graphqlRequest<T>(config: CmssyClientConfig, query: string, variables: Record<string, unknown>, options?: GraphqlRequestOptions, label?: string): Promise<T>;
67
67
 
68
+ declare function resolveWorkspaceId(config: CmssyClientConfig, options?: GraphqlRequestOptions): Promise<string>;
69
+
68
70
  interface QueryScopedOptions extends GraphqlRequestOptions {
69
71
  workspaceId?: string;
70
72
  }
@@ -106,4 +108,4 @@ interface UnknownBlockProps {
106
108
  }
107
109
  declare function UnknownBlock({ type }: UnknownBlockProps): react_jsx_runtime.JSX.Element;
108
110
 
109
- 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 };
111
+ 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, resolveWorkspaceId, splitLocaleFromPath };
package/dist/index.d.ts CHANGED
@@ -65,6 +65,8 @@ interface GraphqlRequestOptions {
65
65
  }
66
66
  declare function graphqlRequest<T>(config: CmssyClientConfig, query: string, variables: Record<string, unknown>, options?: GraphqlRequestOptions, label?: string): Promise<T>;
67
67
 
68
+ declare function resolveWorkspaceId(config: CmssyClientConfig, options?: GraphqlRequestOptions): Promise<string>;
69
+
68
70
  interface QueryScopedOptions extends GraphqlRequestOptions {
69
71
  workspaceId?: string;
70
72
  }
@@ -106,4 +108,4 @@ interface UnknownBlockProps {
106
108
  }
107
109
  declare function UnknownBlock({ type }: UnknownBlockProps): react_jsx_runtime.JSX.Element;
108
110
 
109
- 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 };
111
+ 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, resolveWorkspaceId, splitLocaleFromPath };
package/dist/index.js CHANGED
@@ -654,4 +654,4 @@ function CmssyBlock({
654
654
  );
655
655
  }
656
656
 
657
- 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 };
657
+ 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, resolveWorkspaceId, splitLocaleFromPath };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cmssy/react",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "description": "React blocks, renderers, data client and editor bridge for cmssy headless sites",
5
5
  "keywords": [
6
6
  "cmssy",