@every-app/sdk 0.0.6 → 0.0.8

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 (37) hide show
  1. package/dist/client/EmbeddedAppProvider.d.ts +5 -8
  2. package/dist/client/EmbeddedAppProvider.d.ts.map +1 -1
  3. package/dist/client/EmbeddedAppProvider.js +28 -27
  4. package/dist/client/GatewayRequiredError.d.ts +20 -0
  5. package/dist/client/GatewayRequiredError.d.ts.map +1 -0
  6. package/dist/client/GatewayRequiredError.js +92 -0
  7. package/dist/client/_internal/useEveryAppRouter.d.ts +3 -5
  8. package/dist/client/_internal/useEveryAppRouter.js +53 -56
  9. package/dist/client/_internal/useEveryAppSession.d.ts +8 -10
  10. package/dist/client/_internal/useEveryAppSession.d.ts.map +1 -1
  11. package/dist/client/_internal/useEveryAppSession.js +32 -27
  12. package/dist/client/authenticatedFetch.d.ts +2 -5
  13. package/dist/client/authenticatedFetch.js +17 -17
  14. package/dist/client/index.d.ts +3 -2
  15. package/dist/client/index.d.ts.map +1 -1
  16. package/dist/client/index.js +2 -1
  17. package/dist/client/lazyInitForWorkers.d.ts +2 -4
  18. package/dist/client/lazyInitForWorkers.js +42 -45
  19. package/dist/client/session-manager.d.ts +29 -23
  20. package/dist/client/session-manager.d.ts.map +1 -1
  21. package/dist/client/session-manager.js +143 -126
  22. package/dist/client/useSessionTokenClientMiddleware.d.ts +2 -8
  23. package/dist/client/useSessionTokenClientMiddleware.js +17 -20
  24. package/dist/server/auth-config.d.ts +1 -1
  25. package/dist/server/auth-config.js +4 -4
  26. package/dist/server/authenticateRequest.d.ts +11 -16
  27. package/dist/server/authenticateRequest.js +53 -57
  28. package/dist/server/getLocalD1Url.d.ts +1 -1
  29. package/dist/server/getLocalD1Url.js +29 -42
  30. package/dist/server/index.d.ts +1 -1
  31. package/dist/server/types.d.ts +3 -3
  32. package/package.json +2 -2
  33. package/src/client/EmbeddedAppProvider.tsx +11 -0
  34. package/src/client/GatewayRequiredError.tsx +162 -0
  35. package/src/client/_internal/useEveryAppSession.tsx +3 -0
  36. package/src/client/index.ts +2 -1
  37. package/src/client/session-manager.ts +16 -0
@@ -1,12 +1,9 @@
1
1
  import React from "react";
2
2
  import { SessionManagerConfig } from "./session-manager";
3
3
  interface EmbeddedProviderConfig extends SessionManagerConfig {
4
- children: React.ReactNode;
4
+ children: React.ReactNode;
5
5
  }
6
- export declare function EmbeddedAppProvider({
7
- children,
8
- ...config
9
- }: EmbeddedProviderConfig): import("react/jsx-runtime").JSX.Element | null;
6
+ export declare function EmbeddedAppProvider({ children, ...config }: EmbeddedProviderConfig): import("react/jsx-runtime").JSX.Element | null;
10
7
  /**
11
8
  * Hook to get the current authenticated user.
12
9
  * Returns the user's ID and email extracted from the JWT token,
@@ -26,8 +23,8 @@ export declare function EmbeddedAppProvider({
26
23
  * ```
27
24
  */
28
25
  export declare function useCurrentUser(): {
29
- userId: string;
30
- email: string;
26
+ userId: string;
27
+ email: string;
31
28
  } | null;
32
29
  export {};
33
- //# sourceMappingURL=EmbeddedAppProvider.d.ts.map
30
+ //# sourceMappingURL=EmbeddedAppProvider.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"EmbeddedAppProvider.d.ts","sourceRoot":"","sources":["../../src/client/EmbeddedAppProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA6C,MAAM,OAAO,CAAC;AAClE,OAAO,EAAkB,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAIzE,UAAU,sBAAuB,SAAQ,oBAAoB;IAC3D,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;CAC3B;AAUD,wBAAgB,mBAAmB,CAAC,EAClC,QAAQ,EACR,GAAG,MAAM,EACV,EAAE,sBAAsB,kDAmBxB;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,cAAc,IAAI;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAmBzE"}
1
+ {"version":3,"file":"EmbeddedAppProvider.d.ts","sourceRoot":"","sources":["../../src/client/EmbeddedAppProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA6C,MAAM,OAAO,CAAC;AAClE,OAAO,EAAkB,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAKzE,UAAU,sBAAuB,SAAQ,oBAAoB;IAC3D,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;CAC3B;AAUD,wBAAgB,mBAAmB,CAAC,EAClC,QAAQ,EACR,GAAG,MAAM,EACV,EAAE,sBAAsB,kDA6BxB;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,cAAc,IAAI;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAmBzE"}
@@ -2,22 +2,25 @@ import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { createContext, useContext, useMemo } from "react";
3
3
  import { useEveryAppSession } from "./_internal/useEveryAppSession";
4
4
  import { useEveryAppRouter } from "./_internal/useEveryAppRouter";
5
+ import { GatewayRequiredError } from "./GatewayRequiredError";
5
6
  const EmbeddedAppContext = createContext(null);
6
7
  export function EmbeddedAppProvider({ children, ...config }) {
7
- const { sessionManager, sessionTokenState } = useEveryAppSession({
8
- sessionManagerConfig: config,
9
- });
10
- useEveryAppRouter({ sessionManager });
11
- if (!sessionManager) return null;
12
- const value = {
13
- sessionManager,
14
- isAuthenticated: sessionTokenState.status === "VALID",
15
- sessionTokenState,
16
- };
17
- return _jsx(EmbeddedAppContext.Provider, {
18
- value: value,
19
- children: children,
20
- });
8
+ const { sessionManager, sessionTokenState } = useEveryAppSession({
9
+ sessionManagerConfig: config,
10
+ });
11
+ useEveryAppRouter({ sessionManager });
12
+ if (!sessionManager)
13
+ return null;
14
+ // Check if the app is running outside of the Gateway iframe
15
+ if (!sessionManager.isInIframe) {
16
+ return (_jsx(GatewayRequiredError, { gatewayOrigin: sessionManager.parentOrigin, appId: config.appId }));
17
+ }
18
+ const value = {
19
+ sessionManager,
20
+ isAuthenticated: sessionTokenState.status === "VALID",
21
+ sessionTokenState,
22
+ };
23
+ return (_jsx(EmbeddedAppContext.Provider, { value: value, children: children }));
21
24
  }
22
25
  /**
23
26
  * Hook to get the current authenticated user.
@@ -38,18 +41,16 @@ export function EmbeddedAppProvider({ children, ...config }) {
38
41
  * ```
39
42
  */
40
43
  export function useCurrentUser() {
41
- const context = useContext(EmbeddedAppContext);
42
- if (!context) {
43
- throw new Error(
44
- "useCurrentUser must be used within an EmbeddedAppProvider",
45
- );
46
- }
47
- const { sessionManager, sessionTokenState } = context;
48
- return useMemo(() => {
49
- // Only return user if we have a valid token
50
- if (sessionTokenState.status !== "VALID") {
51
- return null;
44
+ const context = useContext(EmbeddedAppContext);
45
+ if (!context) {
46
+ throw new Error("useCurrentUser must be used within an EmbeddedAppProvider");
52
47
  }
53
- return sessionManager.getUser();
54
- }, [sessionManager, sessionTokenState]);
48
+ const { sessionManager, sessionTokenState } = context;
49
+ return useMemo(() => {
50
+ // Only return user if we have a valid token
51
+ if (sessionTokenState.status !== "VALID") {
52
+ return null;
53
+ }
54
+ return sessionManager.getUser();
55
+ }, [sessionManager, sessionTokenState]);
55
56
  }
@@ -0,0 +1,20 @@
1
+ interface GatewayRequiredErrorProps {
2
+ /**
3
+ * The origin of the Gateway (e.g., "https://gateway.example.com").
4
+ */
5
+ gatewayOrigin: string;
6
+ /**
7
+ * The app ID used in the Gateway URL path.
8
+ */
9
+ appId: string;
10
+ }
11
+ /**
12
+ * Error component displayed when an embedded app is accessed directly
13
+ * instead of through the Every App Gateway.
14
+ *
15
+ * This component informs users that authentication requires accessing
16
+ * the app through the Gateway and provides a link to do so.
17
+ */
18
+ export declare function GatewayRequiredError({ gatewayOrigin, appId, }: GatewayRequiredErrorProps): import("react/jsx-runtime").JSX.Element;
19
+ export {};
20
+ //# sourceMappingURL=GatewayRequiredError.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GatewayRequiredError.d.ts","sourceRoot":"","sources":["../../src/client/GatewayRequiredError.tsx"],"names":[],"mappings":"AAEA,UAAU,yBAAyB;IACjC;;OAEG;IACH,aAAa,EAAE,MAAM,CAAC;IACtB;;OAEG;IACH,KAAK,EAAE,MAAM,CAAC;CACf;AAuFD;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAC,EACnC,aAAa,EACb,KAAK,GACN,EAAE,yBAAyB,2CAsD3B"}
@@ -0,0 +1,92 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ // CSS custom properties for theming
3
+ const CSS_VARIABLES = `
4
+ @media (prefers-color-scheme: light) {
5
+ :root {
6
+ --gateway-bg: oklch(100% 0 0);
7
+ --gateway-text: oklch(0% 0 0);
8
+ --gateway-text-muted: oklch(40% 0 0);
9
+ --gateway-icon-bg: oklch(94% 0 0);
10
+ --gateway-icon-stroke: oklch(80% 0.18 90);
11
+ --gateway-border: oklch(94% 0 0);
12
+ }
13
+ }
14
+ @media (prefers-color-scheme: dark) {
15
+ :root {
16
+ --gateway-bg: #0a0f0d;
17
+ --gateway-text: oklch(92% 0 0);
18
+ --gateway-text-muted: oklch(60% 0 0);
19
+ --gateway-icon-bg: oklch(22% 0 0);
20
+ --gateway-icon-stroke: oklch(85% 0.18 90);
21
+ --gateway-border: oklch(30% 0 0);
22
+ }
23
+ }
24
+ `;
25
+ const styles = {
26
+ container: {
27
+ display: "flex",
28
+ flexDirection: "column",
29
+ alignItems: "center",
30
+ justifyContent: "center",
31
+ minHeight: "100vh",
32
+ padding: "24px",
33
+ fontFamily: '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif',
34
+ backgroundColor: "var(--gateway-bg, oklch(100% 0 0))",
35
+ color: "var(--gateway-text, oklch(0% 0 0))",
36
+ colorScheme: "light dark",
37
+ },
38
+ content: {
39
+ maxWidth: "380px",
40
+ width: "100%",
41
+ textAlign: "left",
42
+ },
43
+ iconContainer: {
44
+ width: "44px",
45
+ height: "44px",
46
+ marginBottom: "16px",
47
+ borderRadius: "0.25rem",
48
+ backgroundColor: "var(--gateway-icon-bg, oklch(94% 0 0))",
49
+ display: "flex",
50
+ alignItems: "center",
51
+ justifyContent: "center",
52
+ border: "1px solid var(--gateway-border, oklch(94% 0 0))",
53
+ },
54
+ title: {
55
+ fontSize: "18px",
56
+ fontWeight: 600,
57
+ marginBottom: "8px",
58
+ color: "var(--gateway-text, oklch(0% 0 0))",
59
+ letterSpacing: "-0.01em",
60
+ },
61
+ description: {
62
+ fontSize: "14px",
63
+ lineHeight: 1.5,
64
+ color: "var(--gateway-text-muted, oklch(40% 0 0))",
65
+ marginBottom: "20px",
66
+ },
67
+ link: {
68
+ display: "inline-flex",
69
+ alignItems: "center",
70
+ gap: "8px",
71
+ fontSize: "14px",
72
+ fontWeight: 500,
73
+ lineHeight: "24px",
74
+ color: "rgb(168, 162, 158)",
75
+ textDecoration: "underline",
76
+ textDecorationColor: "rgb(87, 83, 78)",
77
+ textUnderlineOffset: "4px",
78
+ cursor: "pointer",
79
+ transition: "color, background-color, border-color, text-decoration-color, fill, stroke 0.15s cubic-bezier(0.4, 0, 0.2, 1)",
80
+ },
81
+ };
82
+ /**
83
+ * Error component displayed when an embedded app is accessed directly
84
+ * instead of through the Every App Gateway.
85
+ *
86
+ * This component informs users that authentication requires accessing
87
+ * the app through the Gateway and provides a link to do so.
88
+ */
89
+ export function GatewayRequiredError({ gatewayOrigin, appId, }) {
90
+ const gatewayUrl = `${gatewayOrigin}/apps/${appId}${window.location.pathname}`;
91
+ return (_jsxs("div", { style: styles.container, children: [_jsx("style", { children: CSS_VARIABLES }), _jsxs("div", { style: styles.content, children: [_jsx("div", { style: styles.iconContainer, children: _jsxs("svg", { width: "22", height: "22", viewBox: "0 0 24 24", fill: "none", stroke: "var(--gateway-icon-stroke, oklch(55% 0.22 25))", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round", children: [_jsx("path", { d: "M10.29 3.86L1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z" }), _jsx("line", { x1: "12", y1: "9", x2: "12", y2: "13" }), _jsx("line", { x1: "12", y1: "17", x2: "12.01", y2: "17" })] }) }), _jsx("h1", { style: styles.title, children: "Open in Gateway" }), _jsx("p", { style: styles.description, children: "The Gateway handles sign in and user auth. Access your app from there so that this works properly." }), _jsxs("a", { href: gatewayUrl, style: styles.link, children: ["Go to Gateway", _jsxs("svg", { width: "16", height: "16", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round", children: [_jsx("line", { x1: "5", y1: "12", x2: "19", y2: "12" }), _jsx("polyline", { points: "12 5 19 12 12 19" })] })] })] })] }));
92
+ }
@@ -1,9 +1,7 @@
1
1
  import { SessionManager } from "../session-manager";
2
2
  interface UseEveryAppRouterParams {
3
- sessionManager: SessionManager | null;
3
+ sessionManager: SessionManager | null;
4
4
  }
5
- export declare function useEveryAppRouter({
6
- sessionManager,
7
- }: UseEveryAppRouterParams): void;
5
+ export declare function useEveryAppRouter({ sessionManager }: UseEveryAppRouterParams): void;
8
6
  export {};
9
- //# sourceMappingURL=useEveryAppRouter.d.ts.map
7
+ //# sourceMappingURL=useEveryAppRouter.d.ts.map
@@ -1,60 +1,57 @@
1
1
  import { useEffect } from "react";
2
2
  import { useRouter } from "@tanstack/react-router";
3
3
  export function useEveryAppRouter({ sessionManager }) {
4
- const router = useRouter();
5
- // Route synchronization effect
6
- useEffect(() => {
7
- if (!sessionManager) return;
8
- // Listen for route sync messages from parent
9
- const handleMessage = (event) => {
10
- if (event.origin !== sessionManager.parentOrigin) return;
11
- if (
12
- event.data.type === "ROUTE_CHANGE" &&
13
- event.data.direction === "parent-to-child"
14
- ) {
15
- const targetRoute = event.data.route;
16
- const currentRoute = window.location.pathname;
17
- // Only navigate if the route is different from current location
18
- if (targetRoute && targetRoute !== currentRoute) {
19
- router.navigate({ to: targetRoute });
20
- }
21
- }
22
- };
23
- window.addEventListener("message", handleMessage);
24
- // Simplified route change detection with 2 reliable methods
25
- let lastReportedPath = window.location.pathname;
26
- const handleRouteChange = () => {
27
- const currentPath = window.location.pathname;
28
- // Only report if the path actually changed
29
- if (currentPath === lastReportedPath) {
30
- return;
31
- }
32
- lastReportedPath = currentPath;
33
- if (window.parent !== window) {
34
- window.parent.postMessage(
35
- {
36
- type: "ROUTE_CHANGE",
37
- route: currentPath,
38
- appId: sessionManager.appId,
39
- direction: "child-to-parent",
40
- },
41
- sessionManager.parentOrigin,
42
- );
43
- }
44
- };
45
- // Listen to popstate for browser back/forward
46
- window.addEventListener("popstate", handleRouteChange);
47
- // Polling to detect route changes (catches router navigation)
48
- const pollInterval = setInterval(() => {
49
- const currentPath = window.location.pathname;
50
- if (currentPath !== lastReportedPath) {
51
- handleRouteChange();
52
- }
53
- }, 100);
54
- return () => {
55
- window.removeEventListener("message", handleMessage);
56
- window.removeEventListener("popstate", handleRouteChange);
57
- clearInterval(pollInterval);
58
- };
59
- }, [sessionManager]);
4
+ const router = useRouter();
5
+ // Route synchronization effect
6
+ useEffect(() => {
7
+ if (!sessionManager)
8
+ return;
9
+ // Listen for route sync messages from parent
10
+ const handleMessage = (event) => {
11
+ if (event.origin !== sessionManager.parentOrigin)
12
+ return;
13
+ if (event.data.type === "ROUTE_CHANGE" &&
14
+ event.data.direction === "parent-to-child") {
15
+ const targetRoute = event.data.route;
16
+ const currentRoute = window.location.pathname;
17
+ // Only navigate if the route is different from current location
18
+ if (targetRoute && targetRoute !== currentRoute) {
19
+ router.navigate({ to: targetRoute });
20
+ }
21
+ }
22
+ };
23
+ window.addEventListener("message", handleMessage);
24
+ // Simplified route change detection with 2 reliable methods
25
+ let lastReportedPath = window.location.pathname;
26
+ const handleRouteChange = () => {
27
+ const currentPath = window.location.pathname;
28
+ // Only report if the path actually changed
29
+ if (currentPath === lastReportedPath) {
30
+ return;
31
+ }
32
+ lastReportedPath = currentPath;
33
+ if (window.parent !== window) {
34
+ window.parent.postMessage({
35
+ type: "ROUTE_CHANGE",
36
+ route: currentPath,
37
+ appId: sessionManager.appId,
38
+ direction: "child-to-parent",
39
+ }, sessionManager.parentOrigin);
40
+ }
41
+ };
42
+ // Listen to popstate for browser back/forward
43
+ window.addEventListener("popstate", handleRouteChange);
44
+ // Polling to detect route changes (catches router navigation)
45
+ const pollInterval = setInterval(() => {
46
+ const currentPath = window.location.pathname;
47
+ if (currentPath !== lastReportedPath) {
48
+ handleRouteChange();
49
+ }
50
+ }, 100);
51
+ return () => {
52
+ window.removeEventListener("message", handleMessage);
53
+ window.removeEventListener("popstate", handleRouteChange);
54
+ clearInterval(pollInterval);
55
+ };
56
+ }, [sessionManager]);
60
57
  }
@@ -1,15 +1,13 @@
1
1
  import { SessionManager, SessionManagerConfig } from "../session-manager";
2
2
  interface UseEveryAppSessionParams {
3
- sessionManagerConfig: SessionManagerConfig;
3
+ sessionManagerConfig: SessionManagerConfig;
4
4
  }
5
- export declare function useEveryAppSession({
6
- sessionManagerConfig,
7
- }: UseEveryAppSessionParams): {
8
- sessionManager: SessionManager | null;
9
- sessionTokenState: {
10
- status: "NO_TOKEN" | "VALID" | "EXPIRED" | "REFRESHING";
11
- token: string | null;
12
- };
5
+ export declare function useEveryAppSession({ sessionManagerConfig, }: UseEveryAppSessionParams): {
6
+ sessionManager: SessionManager | null;
7
+ sessionTokenState: {
8
+ status: "NO_TOKEN" | "VALID" | "EXPIRED" | "REFRESHING";
9
+ token: string | null;
10
+ };
13
11
  };
14
12
  export {};
15
- //# sourceMappingURL=useEveryAppSession.d.ts.map
13
+ //# sourceMappingURL=useEveryAppSession.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"useEveryAppSession.d.ts","sourceRoot":"","sources":["../../../src/client/_internal/useEveryAppSession.tsx"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAE1E,UAAU,wBAAwB;IAChC,oBAAoB,EAAE,oBAAoB,CAAC;CAC5C;AAED,wBAAgB,kBAAkB,CAAC,EACjC,oBAAoB,GACrB,EAAE,wBAAwB;;;;;;EAsC1B"}
1
+ {"version":3,"file":"useEveryAppSession.d.ts","sourceRoot":"","sources":["../../../src/client/_internal/useEveryAppSession.tsx"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAE1E,UAAU,wBAAwB;IAChC,oBAAoB,EAAE,oBAAoB,CAAC;CAC5C;AAED,wBAAgB,kBAAkB,CAAC,EACjC,oBAAoB,GACrB,EAAE,wBAAwB;;;;;;EAyC1B"}
@@ -1,31 +1,36 @@
1
1
  import { useEffect, useRef, useState } from "react";
2
2
  import { SessionManager } from "../session-manager";
3
- export function useEveryAppSession({ sessionManagerConfig }) {
4
- const sessionManagerRef = useRef(null);
5
- const [sessionTokenState, setSessionTokenState] = useState({
6
- status: "NO_TOKEN",
7
- token: null,
8
- });
9
- if (!sessionManagerRef.current && typeof document !== "undefined") {
10
- sessionManagerRef.current = new SessionManager(sessionManagerConfig);
11
- }
12
- const sessionManager = sessionManagerRef.current;
13
- useEffect(() => {
14
- if (!sessionManager) return;
15
- const interval = setInterval(() => {
16
- setSessionTokenState(sessionManager.getTokenState());
17
- }, 5000);
18
- sessionManager.getToken().catch((err) => {
19
- console.error("[EmbeddedProvider] Initial token request failed:", err);
3
+ export function useEveryAppSession({ sessionManagerConfig, }) {
4
+ const sessionManagerRef = useRef(null);
5
+ const [sessionTokenState, setSessionTokenState] = useState({
6
+ status: "NO_TOKEN",
7
+ token: null,
20
8
  });
21
- return () => {
22
- clearInterval(interval);
23
- };
24
- }, [sessionManager]);
25
- useEffect(() => {
26
- if (!sessionManager) return;
27
- // Make sessionManager globally accessible for middleware
28
- window.__embeddedSessionManager = sessionManager;
29
- }, [sessionManager]);
30
- return { sessionManager, sessionTokenState };
9
+ if (!sessionManagerRef.current && typeof document !== "undefined") {
10
+ sessionManagerRef.current = new SessionManager(sessionManagerConfig);
11
+ }
12
+ const sessionManager = sessionManagerRef.current;
13
+ useEffect(() => {
14
+ if (!sessionManager)
15
+ return;
16
+ // Skip token requests when not in iframe - the app will show GatewayRequiredError instead
17
+ if (!sessionManager.isInIframe)
18
+ return;
19
+ const interval = setInterval(() => {
20
+ setSessionTokenState(sessionManager.getTokenState());
21
+ }, 5000);
22
+ sessionManager.getToken().catch((err) => {
23
+ console.error("[EmbeddedProvider] Initial token request failed:", err);
24
+ });
25
+ return () => {
26
+ clearInterval(interval);
27
+ };
28
+ }, [sessionManager]);
29
+ useEffect(() => {
30
+ if (!sessionManager)
31
+ return;
32
+ // Make sessionManager globally accessible for middleware
33
+ window.__embeddedSessionManager = sessionManager;
34
+ }, [sessionManager]);
35
+ return { sessionManager, sessionTokenState };
31
36
  }
@@ -5,8 +5,5 @@ export declare function getSessionToken(): Promise<string>;
5
5
  /**
6
6
  * Performs a fetch request with the authorization header automatically added
7
7
  */
8
- export declare function authenticatedFetch(
9
- input: RequestInfo | URL,
10
- init?: RequestInit,
11
- ): Promise<Response>;
12
- //# sourceMappingURL=authenticatedFetch.d.ts.map
8
+ export declare function authenticatedFetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response>;
9
+ //# sourceMappingURL=authenticatedFetch.d.ts.map
@@ -2,26 +2,26 @@
2
2
  * Gets the current session token from the embedded session manager
3
3
  */
4
4
  export async function getSessionToken() {
5
- const windowWithSession = window;
6
- const sessionManager = windowWithSession.__embeddedSessionManager;
7
- if (!sessionManager) {
8
- throw new Error("Session manager not available");
9
- }
10
- const token = await sessionManager.getToken();
11
- if (!token) {
12
- throw new Error("No token available");
13
- }
14
- return token;
5
+ const windowWithSession = window;
6
+ const sessionManager = windowWithSession.__embeddedSessionManager;
7
+ if (!sessionManager) {
8
+ throw new Error("Session manager not available");
9
+ }
10
+ const token = await sessionManager.getToken();
11
+ if (!token) {
12
+ throw new Error("No token available");
13
+ }
14
+ return token;
15
15
  }
16
16
  /**
17
17
  * Performs a fetch request with the authorization header automatically added
18
18
  */
19
19
  export async function authenticatedFetch(input, init) {
20
- const token = await getSessionToken();
21
- const headers = new Headers(init?.headers);
22
- headers.set("Authorization", `Bearer ${token}`);
23
- return fetch(input, {
24
- ...init,
25
- headers,
26
- });
20
+ const token = await getSessionToken();
21
+ const headers = new Headers(init?.headers);
22
+ headers.set("Authorization", `Bearer ${token}`);
23
+ return fetch(input, {
24
+ ...init,
25
+ headers,
26
+ });
27
27
  }
@@ -1,7 +1,8 @@
1
- export { SessionManager } from "./session-manager";
1
+ export { SessionManager, isRunningInIframe } from "./session-manager";
2
2
  export type { SessionManagerConfig } from "./session-manager";
3
3
  export { useSessionTokenClientMiddleware } from "./useSessionTokenClientMiddleware";
4
4
  export { EmbeddedAppProvider, useCurrentUser } from "./EmbeddedAppProvider";
5
+ export { GatewayRequiredError } from "./GatewayRequiredError";
5
6
  export { lazyInitForWorkers } from "./lazyInitForWorkers";
6
7
  export { authenticatedFetch, getSessionToken } from "./authenticatedFetch";
7
- //# sourceMappingURL=index.d.ts.map
8
+ //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACnD,YAAY,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAC9D,OAAO,EAAE,+BAA+B,EAAE,MAAM,mCAAmC,CAAC;AAEpF,OAAO,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAE5E,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAE1D,OAAO,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AACtE,YAAY,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAC9D,OAAO,EAAE,+BAA+B,EAAE,MAAM,mCAAmC,CAAC;AAEpF,OAAO,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAC5E,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAE9D,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAE1D,OAAO,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC"}
@@ -1,5 +1,6 @@
1
- export { SessionManager } from "./session-manager";
1
+ export { SessionManager, isRunningInIframe } from "./session-manager";
2
2
  export { useSessionTokenClientMiddleware } from "./useSessionTokenClientMiddleware";
3
3
  export { EmbeddedAppProvider, useCurrentUser } from "./EmbeddedAppProvider";
4
+ export { GatewayRequiredError } from "./GatewayRequiredError";
4
5
  export { lazyInitForWorkers } from "./lazyInitForWorkers";
5
6
  export { authenticatedFetch, getSessionToken } from "./authenticatedFetch";
@@ -18,7 +18,5 @@
18
18
  * );
19
19
  * ```
20
20
  */
21
- export declare function lazyInitForWorkers<T extends object>(
22
- factory: () => T,
23
- ): T;
24
- //# sourceMappingURL=lazyInitForWorkers.d.ts.map
21
+ export declare function lazyInitForWorkers<T extends object>(factory: () => T): T;
22
+ //# sourceMappingURL=lazyInitForWorkers.d.ts.map
@@ -19,50 +19,47 @@
19
19
  * ```
20
20
  */
21
21
  export function lazyInitForWorkers(factory) {
22
- // Closure: This variable is captured by getInstance() and the Proxy traps below.
23
- // It remains in memory as long as the returned Proxy is referenced, enabling singleton behavior.
24
- let instance = null;
25
- function getInstance() {
26
- if (!instance) {
27
- instance = factory();
22
+ // Closure: This variable is captured by getInstance() and the Proxy traps below.
23
+ // It remains in memory as long as the returned Proxy is referenced, enabling singleton behavior.
24
+ let instance = null;
25
+ function getInstance() {
26
+ if (!instance) {
27
+ instance = factory();
28
+ }
29
+ return instance;
28
30
  }
29
- return instance;
30
- }
31
- return new Proxy(
32
- {},
33
- {
34
- get(_, prop) {
35
- const inst = getInstance();
36
- const value = inst[prop];
37
- // Bind methods to the instance to preserve `this` context
38
- return typeof value === "function" ? value.bind(inst) : value;
39
- },
40
- set(_, prop, value) {
41
- const inst = getInstance();
42
- inst[prop] = value;
43
- return true;
44
- },
45
- deleteProperty(_, prop) {
46
- const inst = getInstance();
47
- delete inst[prop];
48
- return true;
49
- },
50
- has(_, prop) {
51
- const inst = getInstance();
52
- return prop in inst;
53
- },
54
- ownKeys(_) {
55
- const inst = getInstance();
56
- return Reflect.ownKeys(inst);
57
- },
58
- getOwnPropertyDescriptor(_, prop) {
59
- const inst = getInstance();
60
- return Reflect.getOwnPropertyDescriptor(inst, prop);
61
- },
62
- getPrototypeOf(_) {
63
- const inst = getInstance();
64
- return Reflect.getPrototypeOf(inst);
65
- },
66
- },
67
- );
31
+ return new Proxy({}, {
32
+ get(_, prop) {
33
+ const inst = getInstance();
34
+ const value = inst[prop];
35
+ // Bind methods to the instance to preserve `this` context
36
+ return typeof value === "function" ? value.bind(inst) : value;
37
+ },
38
+ set(_, prop, value) {
39
+ const inst = getInstance();
40
+ inst[prop] = value;
41
+ return true;
42
+ },
43
+ deleteProperty(_, prop) {
44
+ const inst = getInstance();
45
+ delete inst[prop];
46
+ return true;
47
+ },
48
+ has(_, prop) {
49
+ const inst = getInstance();
50
+ return prop in inst;
51
+ },
52
+ ownKeys(_) {
53
+ const inst = getInstance();
54
+ return Reflect.ownKeys(inst);
55
+ },
56
+ getOwnPropertyDescriptor(_, prop) {
57
+ const inst = getInstance();
58
+ return Reflect.getOwnPropertyDescriptor(inst, prop);
59
+ },
60
+ getPrototypeOf(_) {
61
+ const inst = getInstance();
62
+ return Reflect.getPrototypeOf(inst);
63
+ },
64
+ });
68
65
  }