@orunelle/theme-sdk 0.0.0-stage → 2.0.0-alpha.14

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.
@@ -0,0 +1,23 @@
1
+ import { type ReactNode } from "react";
2
+ import { QueryClient } from "@tanstack/react-query";
3
+ import type { StorefrontContext } from "@orunelle/theme-contract";
4
+ import { type CommerceClient, type Customer } from "./commerce.js";
5
+ interface Session {
6
+ client: CommerceClient;
7
+ cache: QueryClient;
8
+ ready: boolean;
9
+ authenticated: boolean;
10
+ customerId: string;
11
+ acceptToken: (token: string) => Promise<void>;
12
+ logout: () => Promise<void>;
13
+ }
14
+ export declare function CommerceProvider({ context, children, }: {
15
+ context: StorefrontContext;
16
+ children: ReactNode;
17
+ }): import("react").JSX.Element;
18
+ export declare function useCommerce(): Session;
19
+ /** Never runs on the server, never joins the public SSR QueryClient. */
20
+ export declare function useCustomerQuery<T>(key: readonly unknown[], read: () => Promise<T>, enabled?: boolean): import("@tanstack/react-query").UseQueryResult<T, Error>;
21
+ export declare function customerCacheKey(context: StorefrontContext, resource: string): readonly ["storefront", string, string, "en" | "ar", string, string, string, number, "public" | "preview", string, unknown];
22
+ export type { CommerceClient, Customer };
23
+ //# sourceMappingURL=session.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../src/session.tsx"],"names":[],"mappings":"AAAA,OAAO,EAOL,KAAK,SAAS,EACf,MAAM,OAAO,CAAC;AACf,OAAO,EAAE,WAAW,EAAY,MAAM,uBAAuB,CAAC;AAC9D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAClE,OAAO,EAEL,KAAK,cAAc,EACnB,KAAK,QAAQ,EACd,MAAM,eAAe,CAAC;AAGvB,UAAU,OAAO;IACf,MAAM,EAAE,cAAc,CAAC;IACvB,KAAK,EAAE,WAAW,CAAC;IACnB,KAAK,EAAE,OAAO,CAAC;IACf,aAAa,EAAE,OAAO,CAAC;IACvB,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAC9C,MAAM,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CAC7B;AAED,wBAAgB,gBAAgB,CAAC,EAC/B,OAAO,EACP,QAAQ,GACT,EAAE;IACD,OAAO,EAAE,iBAAiB,CAAC;IAC3B,QAAQ,EAAE,SAAS,CAAC;CACrB,+BA+GA;AACD,wBAAgB,WAAW,IAAI,OAAO,CAIrC;AACD,wEAAwE;AACxE,wBAAgB,gBAAgB,CAAC,CAAC,EAChC,GAAG,EAAE,SAAS,OAAO,EAAE,EACvB,IAAI,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACtB,OAAO,UAAO,4DAaf;AACD,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,iBAAiB,EAAE,QAAQ,EAAE,MAAM,+HAE5E;AACD,YAAY,EAAE,cAAc,EAAE,QAAQ,EAAE,CAAC"}
@@ -0,0 +1,141 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { createContext, useContext, useEffect, useMemo, useRef, useState, } from "react";
3
+ import { QueryClient, useQuery } from "@tanstack/react-query";
4
+ import { createCommerceClient, } from "./commerce.js";
5
+ import { publicQueryKey } from "./query.js";
6
+ const Context = createContext(null);
7
+ export function CommerceProvider({ context, children, }) {
8
+ const token = useRef(undefined);
9
+ const [cache] = useState(() => new QueryClient({
10
+ defaultOptions: {
11
+ queries: { retry: false, gcTime: 0 },
12
+ mutations: { retry: false },
13
+ },
14
+ }));
15
+ const [ready, setReady] = useState(false);
16
+ const [authenticated, setAuthenticated] = useState(false);
17
+ const [customerId, setCustomerId] = useState("");
18
+ // One renewal at a time: requests that fail together share it.
19
+ const renewing = useRef(undefined);
20
+ const client = useMemo(() => {
21
+ const created = createCommerceClient({
22
+ context,
23
+ baseUrl: "/__commerce",
24
+ accessToken: () => token.current,
25
+ // Access tokens are short-lived (15 min) and were only obtained on page
26
+ // load, so a visitor who stayed longer got "unauthorized" errors. Renew
27
+ // the token from the refresh cookie and let the client retry once.
28
+ onUnauthorized: (stale) => {
29
+ if (!token.current)
30
+ return Promise.resolve(false);
31
+ if (token.current !== stale)
32
+ return Promise.resolve(true);
33
+ renewing.current ??= (async () => {
34
+ try {
35
+ const result = await created.customer.refresh();
36
+ if (result.accessToken) {
37
+ token.current = result.accessToken;
38
+ return true;
39
+ }
40
+ }
41
+ catch {
42
+ // The refresh cookie is gone or revoked: signed out below.
43
+ }
44
+ token.current = undefined;
45
+ setAuthenticated(false);
46
+ setCustomerId("");
47
+ return false;
48
+ })().finally(() => {
49
+ renewing.current = undefined;
50
+ });
51
+ return renewing.current;
52
+ },
53
+ });
54
+ return created;
55
+ }, [context.store.id, context.origin, context.locale]);
56
+ const initialRefresh = useRef(undefined);
57
+ useEffect(() => {
58
+ if (!initialRefresh.current)
59
+ initialRefresh.current = (async () => {
60
+ if (context.commerce === "api" && context.mode !== "preview") {
61
+ try {
62
+ const result = await client.customer.refresh();
63
+ if (result.accessToken) {
64
+ token.current = result.accessToken;
65
+ const customer = await client.customer.me();
66
+ if (customer.storeId !== context.store.id)
67
+ throw new Error("Store binding mismatch");
68
+ setCustomerId(customer.id);
69
+ setAuthenticated(true);
70
+ }
71
+ }
72
+ catch {
73
+ token.current = undefined;
74
+ }
75
+ }
76
+ setReady(true);
77
+ })();
78
+ return () => {
79
+ void cache.cancelQueries();
80
+ cache.clear();
81
+ };
82
+ }, [client, cache, context.commerce, context.mode]);
83
+ const value = {
84
+ client,
85
+ cache,
86
+ ready,
87
+ authenticated,
88
+ customerId,
89
+ async acceptToken(value) {
90
+ await cache.cancelQueries();
91
+ cache.clear();
92
+ token.current = value;
93
+ try {
94
+ const customer = await client.customer.me();
95
+ if (customer.storeId !== context.store.id)
96
+ throw new Error("Store binding mismatch");
97
+ setCustomerId(customer.id);
98
+ setAuthenticated(true);
99
+ }
100
+ catch (error) {
101
+ token.current = undefined;
102
+ setCustomerId("");
103
+ setAuthenticated(false);
104
+ throw error;
105
+ }
106
+ },
107
+ async logout() {
108
+ try {
109
+ await client.customer.logout();
110
+ }
111
+ finally {
112
+ token.current = undefined;
113
+ setAuthenticated(false);
114
+ setCustomerId("");
115
+ await cache.cancelQueries();
116
+ cache.clear();
117
+ }
118
+ },
119
+ };
120
+ return _jsx(Context.Provider, { value: value, children: children });
121
+ }
122
+ export function useCommerce() {
123
+ const value = useContext(Context);
124
+ if (!value)
125
+ throw new Error("CommerceProvider is required.");
126
+ return value;
127
+ }
128
+ /** Never runs on the server, never joins the public SSR QueryClient. */
129
+ export function useCustomerQuery(key, read, enabled = true) {
130
+ const session = useCommerce();
131
+ return useQuery({
132
+ queryKey: ["customer", session.customerId || "guest", ...key],
133
+ queryFn: read,
134
+ enabled: session.ready && enabled,
135
+ retry: false,
136
+ meta: { visibility: "private" },
137
+ }, session.cache);
138
+ }
139
+ export function customerCacheKey(context, resource) {
140
+ return publicQueryKey(context, "private:" + resource);
141
+ }
package/package.json CHANGED
@@ -1,6 +1,51 @@
1
1
  {
2
2
  "name": "@orunelle/theme-sdk",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "2.0.0-alpha.14",
4
+ "description": "Orunelle theme SDK: commerce client, sessions and composition helpers for storefront themes",
5
+ "license": "UNLICENSED",
6
+ "homepage": "https://orunelle.com",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/Orunelle/frontend.git",
10
+ "directory": "packages/theme-sdk"
11
+ },
12
+ "publishConfig": {
13
+ "access": "public",
14
+ "tag": "alpha"
15
+ },
16
+ "type": "module",
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "exports": {
21
+ ".": {
22
+ "types": "./dist/index.d.ts",
23
+ "import": "./dist/index.js"
24
+ },
25
+ "./commerce": {
26
+ "types": "./dist/commerce.d.ts",
27
+ "import": "./dist/commerce.js"
28
+ },
29
+ "./query": {
30
+ "types": "./dist/query.d.ts",
31
+ "import": "./dist/query.js"
32
+ }
33
+ },
34
+ "dependencies": {
35
+ "zod": "3.25.76",
36
+ "@tanstack/react-query": "5.102.0",
37
+ "@orunelle/theme-contract": "2.0.0-alpha.14"
38
+ },
39
+ "peerDependencies": {
40
+ "react": "^19.3.1"
41
+ },
42
+ "devDependencies": {
43
+ "@types/react": "19.3.0",
44
+ "react": "19.3.1",
45
+ "typescript": "5.9.3"
46
+ },
47
+ "scripts": {
48
+ "build": "tsc",
49
+ "typecheck": "tsc --noEmit"
50
+ }
6
51
  }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.