@commercengine/checkout 0.1.0

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/store.cjs ADDED
@@ -0,0 +1,250 @@
1
+ let _commercengine_js = require("@commercengine/js");
2
+ let zustand_middleware = require("zustand/middleware");
3
+ let zustand_vanilla = require("zustand/vanilla");
4
+
5
+ //#region src/store.ts
6
+ /**
7
+ * Commerce Engine Checkout - Universal Store
8
+ *
9
+ * Framework-agnostic state management using Zustand vanilla.
10
+ * This is the core store that all framework bindings use.
11
+ *
12
+ * ## Architecture
13
+ *
14
+ * - **Singleton**: One store instance shared across all components/routes
15
+ * - **Non-blocking**: Checkout iframe loads asynchronously in the background
16
+ * - **SSR-safe**: Initialization is skipped on server, only runs on client
17
+ * - **Zero CWV impact**: Hidden iframe, no layout shifts, no main thread blocking
18
+ *
19
+ * ## Lifecycle
20
+ *
21
+ * 1. `initCheckout()` creates a hidden iframe and returns immediately
22
+ * 2. Iframe loads in background (non-blocking)
23
+ * 3. `onReady` callback fires when checkout is ready
24
+ * 4. `useCheckout()` can be called from any component to access state
25
+ * 5. Same instance persists across route changes (SPA navigation)
26
+ *
27
+ * @commercengine/checkout
28
+ */
29
+ const DEFAULT_STATE = {
30
+ checkout: null,
31
+ isReady: false,
32
+ isOpen: false,
33
+ cartCount: 0,
34
+ cartTotal: 0,
35
+ cartCurrency: "INR"
36
+ };
37
+ /**
38
+ * Vanilla Zustand store for checkout state
39
+ *
40
+ * This is framework-agnostic and can be used directly or via framework bindings.
41
+ *
42
+ * @example Direct usage (vanilla JS)
43
+ * ```ts
44
+ * import { checkoutStore } from "@commercengine/checkout";
45
+ *
46
+ * // Initialize
47
+ * checkoutStore.getState().init({ storeId: "...", apiKey: "..." });
48
+ *
49
+ * // Get current state
50
+ * const { cartCount, isReady } = checkoutStore.getState();
51
+ *
52
+ * // Subscribe to changes
53
+ * checkoutStore.subscribe((state) => {
54
+ * console.log("Cart count:", state.cartCount);
55
+ * });
56
+ *
57
+ * // Call actions
58
+ * checkoutStore.getState().openCart();
59
+ * ```
60
+ */
61
+ const checkoutStore = (0, zustand_vanilla.createStore)()((0, zustand_middleware.subscribeWithSelector)((set, get) => ({
62
+ ...DEFAULT_STATE,
63
+ init: (config) => {
64
+ if (typeof window === "undefined") return;
65
+ const existing = get().checkout;
66
+ if (existing) existing.destroy();
67
+ set({ checkout: new _commercengine_js.Checkout({
68
+ ...config,
69
+ onReady: () => {
70
+ set({ isReady: true });
71
+ config.onReady?.();
72
+ },
73
+ onOpen: () => {
74
+ set({ isOpen: true });
75
+ config.onOpen?.();
76
+ },
77
+ onClose: () => {
78
+ set({ isOpen: false });
79
+ config.onClose?.();
80
+ },
81
+ onCartUpdate: (cart) => {
82
+ set({
83
+ cartCount: cart.count,
84
+ cartTotal: cart.total,
85
+ cartCurrency: cart.currency
86
+ });
87
+ config.onCartUpdate?.(cart);
88
+ },
89
+ onComplete: (order) => {
90
+ config.onComplete?.(order);
91
+ },
92
+ onLogin: (data) => {
93
+ config.onLogin?.(data);
94
+ },
95
+ onLogout: (data) => {
96
+ config.onLogout?.(data);
97
+ },
98
+ onTokenRefresh: (data) => {
99
+ config.onTokenRefresh?.(data);
100
+ },
101
+ onSessionError: () => {
102
+ config.onSessionError?.();
103
+ },
104
+ onError: (error) => {
105
+ set({ isReady: true });
106
+ config.onError?.(error);
107
+ }
108
+ }) });
109
+ },
110
+ destroy: () => {
111
+ const { checkout } = get();
112
+ if (checkout) checkout.destroy();
113
+ set(DEFAULT_STATE);
114
+ },
115
+ openCart: () => {
116
+ get().checkout?.openCart();
117
+ },
118
+ openCheckout: () => {
119
+ get().checkout?.openCheckout();
120
+ },
121
+ close: () => {
122
+ get().checkout?.close();
123
+ },
124
+ addToCart: (productId, variantId, quantity) => {
125
+ get().checkout?.addToCart(productId, variantId, quantity);
126
+ },
127
+ updateTokens: (accessToken, refreshToken) => {
128
+ get().checkout?.updateTokens(accessToken, refreshToken);
129
+ }
130
+ })));
131
+ /**
132
+ * Initialize checkout
133
+ *
134
+ * @example
135
+ * ```ts
136
+ * import { initCheckout } from "@commercengine/checkout";
137
+ *
138
+ * initCheckout({
139
+ * storeId: "store_xxx",
140
+ * apiKey: "ak_xxx",
141
+ * onComplete: (order) => console.log("Order placed:", order.orderNumber),
142
+ * });
143
+ * ```
144
+ */
145
+ function initCheckout(config) {
146
+ checkoutStore.getState().init(config);
147
+ }
148
+ /**
149
+ * Destroy checkout instance
150
+ *
151
+ * @example
152
+ * ```ts
153
+ * import { destroyCheckout } from "@commercengine/checkout";
154
+ *
155
+ * destroyCheckout();
156
+ * ```
157
+ */
158
+ function destroyCheckout() {
159
+ checkoutStore.getState().destroy();
160
+ }
161
+ /**
162
+ * Get checkout state and methods (non-reactive)
163
+ *
164
+ * Use this outside of reactive frameworks or when you don't need reactivity.
165
+ *
166
+ * @example
167
+ * ```ts
168
+ * import { getCheckout } from "@commercengine/checkout";
169
+ *
170
+ * document.getElementById("cart-btn").onclick = () => {
171
+ * getCheckout().openCart();
172
+ * };
173
+ *
174
+ * // Check state
175
+ * if (getCheckout().isReady) {
176
+ * console.log("Cart has", getCheckout().cartCount, "items");
177
+ * }
178
+ * ```
179
+ */
180
+ function getCheckout() {
181
+ const state = checkoutStore.getState();
182
+ return {
183
+ isReady: state.isReady,
184
+ isOpen: state.isOpen,
185
+ cartCount: state.cartCount,
186
+ cartTotal: state.cartTotal,
187
+ cartCurrency: state.cartCurrency,
188
+ openCart: state.openCart,
189
+ openCheckout: state.openCheckout,
190
+ close: state.close,
191
+ addToCart: state.addToCart,
192
+ updateTokens: state.updateTokens
193
+ };
194
+ }
195
+ /**
196
+ * Subscribe to checkout state changes
197
+ *
198
+ * @example
199
+ * ```ts
200
+ * import { subscribeToCheckout } from "@commercengine/checkout";
201
+ *
202
+ * // Subscribe to all changes
203
+ * const unsubscribe = subscribeToCheckout((state) => {
204
+ * console.log("State changed:", state);
205
+ * });
206
+ *
207
+ * // Subscribe to specific state with selector
208
+ * const unsubscribe = subscribeToCheckout(
209
+ * (state) => state.cartCount,
210
+ * (cartCount) => console.log("Cart count:", cartCount)
211
+ * );
212
+ *
213
+ * // Cleanup
214
+ * unsubscribe();
215
+ * ```
216
+ */
217
+ const subscribeToCheckout = checkoutStore.subscribe;
218
+
219
+ //#endregion
220
+ Object.defineProperty(exports, 'checkoutStore', {
221
+ enumerable: true,
222
+ get: function () {
223
+ return checkoutStore;
224
+ }
225
+ });
226
+ Object.defineProperty(exports, 'destroyCheckout', {
227
+ enumerable: true,
228
+ get: function () {
229
+ return destroyCheckout;
230
+ }
231
+ });
232
+ Object.defineProperty(exports, 'getCheckout', {
233
+ enumerable: true,
234
+ get: function () {
235
+ return getCheckout;
236
+ }
237
+ });
238
+ Object.defineProperty(exports, 'initCheckout', {
239
+ enumerable: true,
240
+ get: function () {
241
+ return initCheckout;
242
+ }
243
+ });
244
+ Object.defineProperty(exports, 'subscribeToCheckout', {
245
+ enumerable: true,
246
+ get: function () {
247
+ return subscribeToCheckout;
248
+ }
249
+ });
250
+ //# sourceMappingURL=store.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.cjs","names":["Checkout"],"sources":["../src/store.ts"],"sourcesContent":["/**\n * Commerce Engine Checkout - Universal Store\n *\n * Framework-agnostic state management using Zustand vanilla.\n * This is the core store that all framework bindings use.\n *\n * ## Architecture\n *\n * - **Singleton**: One store instance shared across all components/routes\n * - **Non-blocking**: Checkout iframe loads asynchronously in the background\n * - **SSR-safe**: Initialization is skipped on server, only runs on client\n * - **Zero CWV impact**: Hidden iframe, no layout shifts, no main thread blocking\n *\n * ## Lifecycle\n *\n * 1. `initCheckout()` creates a hidden iframe and returns immediately\n * 2. Iframe loads in background (non-blocking)\n * 3. `onReady` callback fires when checkout is ready\n * 4. `useCheckout()` can be called from any component to access state\n * 5. Same instance persists across route changes (SPA navigation)\n *\n * @commercengine/checkout\n */\n\nimport {\n type AuthLoginData,\n type AuthLogoutData,\n type AuthRefreshData,\n type CartData,\n Checkout,\n type CheckoutConfig,\n type ErrorData,\n type OrderData,\n} from \"@commercengine/js\";\nimport { subscribeWithSelector } from \"zustand/middleware\";\nimport { createStore } from \"zustand/vanilla\";\n\n// =============================================================================\n// TYPES\n// =============================================================================\n\n/**\n * Checkout state\n */\nexport interface CheckoutState {\n /** Internal checkout instance */\n checkout: Checkout | null;\n /** Whether checkout is ready to use */\n isReady: boolean;\n /** Whether checkout overlay is currently open */\n isOpen: boolean;\n /** Number of items in cart */\n cartCount: number;\n /** Cart subtotal amount */\n cartTotal: number;\n /** Cart currency code */\n cartCurrency: string;\n}\n\n/**\n * Checkout actions\n */\nexport interface CheckoutActions {\n /**\n * Initialize checkout with configuration\n * Call this once at app startup\n */\n init: (config: CheckoutConfig) => void;\n\n /**\n * Destroy checkout instance and reset state\n */\n destroy: () => void;\n\n /**\n * Open the cart drawer\n */\n openCart: () => void;\n\n /**\n * Open the checkout drawer directly (for Buy Now flow)\n */\n openCheckout: () => void;\n\n /**\n * Close the checkout overlay\n */\n close: () => void;\n\n /**\n * Add item to cart\n * @param productId - Product ID (required)\n * @param variantId - Variant ID (required, null for non-variant products)\n * @param quantity - Quantity to add (default: 1)\n */\n addToCart: (productId: string, variantId: string | null, quantity?: number) => void;\n\n /**\n * Update authentication tokens\n * Use this when user logs in/out on the parent site\n */\n updateTokens: (accessToken: string, refreshToken?: string) => void;\n}\n\n/**\n * Complete checkout store type\n */\nexport type CheckoutStore = CheckoutState & CheckoutActions;\n\n// =============================================================================\n// DEFAULT STATE\n// =============================================================================\n\nconst DEFAULT_STATE: CheckoutState = {\n checkout: null,\n isReady: false,\n isOpen: false,\n cartCount: 0,\n cartTotal: 0,\n cartCurrency: \"INR\",\n};\n\n// =============================================================================\n// STORE\n// =============================================================================\n\n/**\n * Vanilla Zustand store for checkout state\n *\n * This is framework-agnostic and can be used directly or via framework bindings.\n *\n * @example Direct usage (vanilla JS)\n * ```ts\n * import { checkoutStore } from \"@commercengine/checkout\";\n *\n * // Initialize\n * checkoutStore.getState().init({ storeId: \"...\", apiKey: \"...\" });\n *\n * // Get current state\n * const { cartCount, isReady } = checkoutStore.getState();\n *\n * // Subscribe to changes\n * checkoutStore.subscribe((state) => {\n * console.log(\"Cart count:\", state.cartCount);\n * });\n *\n * // Call actions\n * checkoutStore.getState().openCart();\n * ```\n */\nexport const checkoutStore = createStore<CheckoutStore>()(\n subscribeWithSelector((set, get) => ({\n // State\n ...DEFAULT_STATE,\n\n // Actions\n init: (config) => {\n // Skip on server\n if (typeof window === \"undefined\") return;\n\n // Destroy existing instance if reinitializing\n const existing = get().checkout;\n if (existing) {\n existing.destroy();\n }\n\n // Create new checkout instance with callbacks that update store\n const checkout = new Checkout({\n ...config,\n onReady: () => {\n set({ isReady: true });\n config.onReady?.();\n },\n onOpen: () => {\n set({ isOpen: true });\n config.onOpen?.();\n },\n onClose: () => {\n set({ isOpen: false });\n config.onClose?.();\n },\n onCartUpdate: (cart: CartData) => {\n set({\n cartCount: cart.count,\n cartTotal: cart.total,\n cartCurrency: cart.currency,\n });\n config.onCartUpdate?.(cart);\n },\n onComplete: (order: OrderData) => {\n config.onComplete?.(order);\n },\n onLogin: (data: AuthLoginData) => {\n config.onLogin?.(data);\n },\n onLogout: (data: AuthLogoutData) => {\n config.onLogout?.(data);\n },\n onTokenRefresh: (data: AuthRefreshData) => {\n config.onTokenRefresh?.(data);\n },\n onSessionError: () => {\n config.onSessionError?.();\n },\n onError: (error: ErrorData) => {\n // Still set ready so buttons are enabled (error drawer will show)\n set({ isReady: true });\n config.onError?.(error);\n },\n });\n\n set({ checkout });\n },\n\n destroy: () => {\n const { checkout } = get();\n if (checkout) {\n checkout.destroy();\n }\n set(DEFAULT_STATE);\n },\n\n openCart: () => {\n get().checkout?.openCart();\n },\n\n openCheckout: () => {\n get().checkout?.openCheckout();\n },\n\n close: () => {\n get().checkout?.close();\n },\n\n addToCart: (productId, variantId, quantity) => {\n get().checkout?.addToCart(productId, variantId, quantity);\n },\n\n updateTokens: (accessToken, refreshToken) => {\n get().checkout?.updateTokens(accessToken, refreshToken);\n },\n }))\n);\n\n// =============================================================================\n// CONVENIENCE EXPORTS\n// =============================================================================\n\n/**\n * Initialize checkout\n *\n * @example\n * ```ts\n * import { initCheckout } from \"@commercengine/checkout\";\n *\n * initCheckout({\n * storeId: \"store_xxx\",\n * apiKey: \"ak_xxx\",\n * onComplete: (order) => console.log(\"Order placed:\", order.orderNumber),\n * });\n * ```\n */\nexport function initCheckout(config: CheckoutConfig): void {\n checkoutStore.getState().init(config);\n}\n\n/**\n * Destroy checkout instance\n *\n * @example\n * ```ts\n * import { destroyCheckout } from \"@commercengine/checkout\";\n *\n * destroyCheckout();\n * ```\n */\nexport function destroyCheckout(): void {\n checkoutStore.getState().destroy();\n}\n\n/**\n * Get checkout state and methods (non-reactive)\n *\n * Use this outside of reactive frameworks or when you don't need reactivity.\n *\n * @example\n * ```ts\n * import { getCheckout } from \"@commercengine/checkout\";\n *\n * document.getElementById(\"cart-btn\").onclick = () => {\n * getCheckout().openCart();\n * };\n *\n * // Check state\n * if (getCheckout().isReady) {\n * console.log(\"Cart has\", getCheckout().cartCount, \"items\");\n * }\n * ```\n */\nexport function getCheckout(): Omit<CheckoutStore, \"checkout\" | \"init\" | \"destroy\"> {\n const state = checkoutStore.getState();\n return {\n isReady: state.isReady,\n isOpen: state.isOpen,\n cartCount: state.cartCount,\n cartTotal: state.cartTotal,\n cartCurrency: state.cartCurrency,\n openCart: state.openCart,\n openCheckout: state.openCheckout,\n close: state.close,\n addToCart: state.addToCart,\n updateTokens: state.updateTokens,\n };\n}\n\n/**\n * Subscribe to checkout state changes\n *\n * @example\n * ```ts\n * import { subscribeToCheckout } from \"@commercengine/checkout\";\n *\n * // Subscribe to all changes\n * const unsubscribe = subscribeToCheckout((state) => {\n * console.log(\"State changed:\", state);\n * });\n *\n * // Subscribe to specific state with selector\n * const unsubscribe = subscribeToCheckout(\n * (state) => state.cartCount,\n * (cartCount) => console.log(\"Cart count:\", cartCount)\n * );\n *\n * // Cleanup\n * unsubscribe();\n * ```\n */\nexport const subscribeToCheckout = checkoutStore.subscribe;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiHA,MAAM,gBAA+B;CACnC,UAAU;CACV,SAAS;CACT,QAAQ;CACR,WAAW;CACX,WAAW;CACX,cAAc;CACf;;;;;;;;;;;;;;;;;;;;;;;;;AA8BD,MAAa,kDAA4C,gDAChC,KAAK,SAAS;CAEnC,GAAG;CAGH,OAAO,WAAW;AAEhB,MAAI,OAAO,WAAW,YAAa;EAGnC,MAAM,WAAW,KAAK,CAAC;AACvB,MAAI,SACF,UAAS,SAAS;AAgDpB,MAAI,EAAE,UA5CW,IAAIA,2BAAS;GAC5B,GAAG;GACH,eAAe;AACb,QAAI,EAAE,SAAS,MAAM,CAAC;AACtB,WAAO,WAAW;;GAEpB,cAAc;AACZ,QAAI,EAAE,QAAQ,MAAM,CAAC;AACrB,WAAO,UAAU;;GAEnB,eAAe;AACb,QAAI,EAAE,QAAQ,OAAO,CAAC;AACtB,WAAO,WAAW;;GAEpB,eAAe,SAAmB;AAChC,QAAI;KACF,WAAW,KAAK;KAChB,WAAW,KAAK;KAChB,cAAc,KAAK;KACpB,CAAC;AACF,WAAO,eAAe,KAAK;;GAE7B,aAAa,UAAqB;AAChC,WAAO,aAAa,MAAM;;GAE5B,UAAU,SAAwB;AAChC,WAAO,UAAU,KAAK;;GAExB,WAAW,SAAyB;AAClC,WAAO,WAAW,KAAK;;GAEzB,iBAAiB,SAA0B;AACzC,WAAO,iBAAiB,KAAK;;GAE/B,sBAAsB;AACpB,WAAO,kBAAkB;;GAE3B,UAAU,UAAqB;AAE7B,QAAI,EAAE,SAAS,MAAM,CAAC;AACtB,WAAO,UAAU,MAAM;;GAE1B,CAAC,EAEc,CAAC;;CAGnB,eAAe;EACb,MAAM,EAAE,aAAa,KAAK;AAC1B,MAAI,SACF,UAAS,SAAS;AAEpB,MAAI,cAAc;;CAGpB,gBAAgB;AACd,OAAK,CAAC,UAAU,UAAU;;CAG5B,oBAAoB;AAClB,OAAK,CAAC,UAAU,cAAc;;CAGhC,aAAa;AACX,OAAK,CAAC,UAAU,OAAO;;CAGzB,YAAY,WAAW,WAAW,aAAa;AAC7C,OAAK,CAAC,UAAU,UAAU,WAAW,WAAW,SAAS;;CAG3D,eAAe,aAAa,iBAAiB;AAC3C,OAAK,CAAC,UAAU,aAAa,aAAa,aAAa;;CAE1D,EAAE,CACJ;;;;;;;;;;;;;;;AAoBD,SAAgB,aAAa,QAA8B;AACzD,eAAc,UAAU,CAAC,KAAK,OAAO;;;;;;;;;;;;AAavC,SAAgB,kBAAwB;AACtC,eAAc,UAAU,CAAC,SAAS;;;;;;;;;;;;;;;;;;;;;AAsBpC,SAAgB,cAAoE;CAClF,MAAM,QAAQ,cAAc,UAAU;AACtC,QAAO;EACL,SAAS,MAAM;EACf,QAAQ,MAAM;EACd,WAAW,MAAM;EACjB,WAAW,MAAM;EACjB,cAAc,MAAM;EACpB,UAAU,MAAM;EAChB,cAAc,MAAM;EACpB,OAAO,MAAM;EACb,WAAW,MAAM;EACjB,cAAc,MAAM;EACrB;;;;;;;;;;;;;;;;;;;;;;;;AAyBH,MAAa,sBAAsB,cAAc"}
@@ -0,0 +1,176 @@
1
+ import { Checkout, CheckoutConfig } from "@commercengine/js";
2
+ import * as zustand0 from "zustand";
3
+
4
+ //#region src/store.d.ts
5
+
6
+ /**
7
+ * Checkout state
8
+ */
9
+ interface CheckoutState {
10
+ /** Internal checkout instance */
11
+ checkout: Checkout | null;
12
+ /** Whether checkout is ready to use */
13
+ isReady: boolean;
14
+ /** Whether checkout overlay is currently open */
15
+ isOpen: boolean;
16
+ /** Number of items in cart */
17
+ cartCount: number;
18
+ /** Cart subtotal amount */
19
+ cartTotal: number;
20
+ /** Cart currency code */
21
+ cartCurrency: string;
22
+ }
23
+ /**
24
+ * Checkout actions
25
+ */
26
+ interface CheckoutActions {
27
+ /**
28
+ * Initialize checkout with configuration
29
+ * Call this once at app startup
30
+ */
31
+ init: (config: CheckoutConfig) => void;
32
+ /**
33
+ * Destroy checkout instance and reset state
34
+ */
35
+ destroy: () => void;
36
+ /**
37
+ * Open the cart drawer
38
+ */
39
+ openCart: () => void;
40
+ /**
41
+ * Open the checkout drawer directly (for Buy Now flow)
42
+ */
43
+ openCheckout: () => void;
44
+ /**
45
+ * Close the checkout overlay
46
+ */
47
+ close: () => void;
48
+ /**
49
+ * Add item to cart
50
+ * @param productId - Product ID (required)
51
+ * @param variantId - Variant ID (required, null for non-variant products)
52
+ * @param quantity - Quantity to add (default: 1)
53
+ */
54
+ addToCart: (productId: string, variantId: string | null, quantity?: number) => void;
55
+ /**
56
+ * Update authentication tokens
57
+ * Use this when user logs in/out on the parent site
58
+ */
59
+ updateTokens: (accessToken: string, refreshToken?: string) => void;
60
+ }
61
+ /**
62
+ * Complete checkout store type
63
+ */
64
+ type CheckoutStore = CheckoutState & CheckoutActions;
65
+ /**
66
+ * Vanilla Zustand store for checkout state
67
+ *
68
+ * This is framework-agnostic and can be used directly or via framework bindings.
69
+ *
70
+ * @example Direct usage (vanilla JS)
71
+ * ```ts
72
+ * import { checkoutStore } from "@commercengine/checkout";
73
+ *
74
+ * // Initialize
75
+ * checkoutStore.getState().init({ storeId: "...", apiKey: "..." });
76
+ *
77
+ * // Get current state
78
+ * const { cartCount, isReady } = checkoutStore.getState();
79
+ *
80
+ * // Subscribe to changes
81
+ * checkoutStore.subscribe((state) => {
82
+ * console.log("Cart count:", state.cartCount);
83
+ * });
84
+ *
85
+ * // Call actions
86
+ * checkoutStore.getState().openCart();
87
+ * ```
88
+ */
89
+ declare const checkoutStore: Omit<zustand0.StoreApi<CheckoutStore>, "subscribe"> & {
90
+ subscribe: {
91
+ (listener: (selectedState: CheckoutStore, previousSelectedState: CheckoutStore) => void): () => void;
92
+ <U>(selector: (state: CheckoutStore) => U, listener: (selectedState: U, previousSelectedState: U) => void, options?: {
93
+ equalityFn?: ((a: U, b: U) => boolean) | undefined;
94
+ fireImmediately?: boolean;
95
+ } | undefined): () => void;
96
+ };
97
+ };
98
+ /**
99
+ * Initialize checkout
100
+ *
101
+ * @example
102
+ * ```ts
103
+ * import { initCheckout } from "@commercengine/checkout";
104
+ *
105
+ * initCheckout({
106
+ * storeId: "store_xxx",
107
+ * apiKey: "ak_xxx",
108
+ * onComplete: (order) => console.log("Order placed:", order.orderNumber),
109
+ * });
110
+ * ```
111
+ */
112
+ declare function initCheckout(config: CheckoutConfig): void;
113
+ /**
114
+ * Destroy checkout instance
115
+ *
116
+ * @example
117
+ * ```ts
118
+ * import { destroyCheckout } from "@commercengine/checkout";
119
+ *
120
+ * destroyCheckout();
121
+ * ```
122
+ */
123
+ declare function destroyCheckout(): void;
124
+ /**
125
+ * Get checkout state and methods (non-reactive)
126
+ *
127
+ * Use this outside of reactive frameworks or when you don't need reactivity.
128
+ *
129
+ * @example
130
+ * ```ts
131
+ * import { getCheckout } from "@commercengine/checkout";
132
+ *
133
+ * document.getElementById("cart-btn").onclick = () => {
134
+ * getCheckout().openCart();
135
+ * };
136
+ *
137
+ * // Check state
138
+ * if (getCheckout().isReady) {
139
+ * console.log("Cart has", getCheckout().cartCount, "items");
140
+ * }
141
+ * ```
142
+ */
143
+ declare function getCheckout(): Omit<CheckoutStore, "checkout" | "init" | "destroy">;
144
+ /**
145
+ * Subscribe to checkout state changes
146
+ *
147
+ * @example
148
+ * ```ts
149
+ * import { subscribeToCheckout } from "@commercengine/checkout";
150
+ *
151
+ * // Subscribe to all changes
152
+ * const unsubscribe = subscribeToCheckout((state) => {
153
+ * console.log("State changed:", state);
154
+ * });
155
+ *
156
+ * // Subscribe to specific state with selector
157
+ * const unsubscribe = subscribeToCheckout(
158
+ * (state) => state.cartCount,
159
+ * (cartCount) => console.log("Cart count:", cartCount)
160
+ * );
161
+ *
162
+ * // Cleanup
163
+ * unsubscribe();
164
+ * ```
165
+ */
166
+ declare const subscribeToCheckout: {
167
+ (listener: (selectedState: CheckoutStore, previousSelectedState: CheckoutStore) => void): () => void;
168
+ <U>(selector: (state: CheckoutStore) => U, listener: (selectedState: U, previousSelectedState: U) => void, options?: {
169
+ equalityFn?: ((a: U, b: U) => boolean) | undefined;
170
+ fireImmediately?: boolean;
171
+ } | undefined): () => void;
172
+ };
173
+ //# sourceMappingURL=store.d.ts.map
174
+ //#endregion
175
+ export { destroyCheckout as a, subscribeToCheckout as c, checkoutStore as i, CheckoutState as n, getCheckout as o, CheckoutStore as r, initCheckout as s, CheckoutActions as t };
176
+ //# sourceMappingURL=store.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.d.cts","names":[],"sources":["../src/store.ts"],"sourcesContent":[],"mappings":";;;;;;AAsQA;AAcA;AAuBgB,UA/PC,aAAA,CA+PU;EAAA;UAAS,EA7PxB,QA6PwB,GAAA,IAAA;;EAAD,OAAA,EAAA,OAAA;EAsCtB;EAA6C,MAAA,EAAA,OAAA;;;;;;;;;;;UAnRzC,eAAA;;;;;iBAKA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KAwCL,aAAA,GAAgB,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;cA2C/B,eAAa,KAAA,QAAA,CAAA,SAAA;;;;;;;;;;;;;;;;;;;;;;;iBAgHV,YAAA,SAAqB;;;;;;;;;;;iBAcrB,eAAA,CAAA;;;;;;;;;;;;;;;;;;;;iBAuBA,WAAA,CAAA,GAAe,KAAK;;;;;;;;;;;;;;;;;;;;;;;cAsCvB"}
@@ -0,0 +1,176 @@
1
+ import { Checkout, CheckoutConfig } from "@commercengine/js";
2
+ import * as zustand0 from "zustand";
3
+
4
+ //#region src/store.d.ts
5
+
6
+ /**
7
+ * Checkout state
8
+ */
9
+ interface CheckoutState {
10
+ /** Internal checkout instance */
11
+ checkout: Checkout | null;
12
+ /** Whether checkout is ready to use */
13
+ isReady: boolean;
14
+ /** Whether checkout overlay is currently open */
15
+ isOpen: boolean;
16
+ /** Number of items in cart */
17
+ cartCount: number;
18
+ /** Cart subtotal amount */
19
+ cartTotal: number;
20
+ /** Cart currency code */
21
+ cartCurrency: string;
22
+ }
23
+ /**
24
+ * Checkout actions
25
+ */
26
+ interface CheckoutActions {
27
+ /**
28
+ * Initialize checkout with configuration
29
+ * Call this once at app startup
30
+ */
31
+ init: (config: CheckoutConfig) => void;
32
+ /**
33
+ * Destroy checkout instance and reset state
34
+ */
35
+ destroy: () => void;
36
+ /**
37
+ * Open the cart drawer
38
+ */
39
+ openCart: () => void;
40
+ /**
41
+ * Open the checkout drawer directly (for Buy Now flow)
42
+ */
43
+ openCheckout: () => void;
44
+ /**
45
+ * Close the checkout overlay
46
+ */
47
+ close: () => void;
48
+ /**
49
+ * Add item to cart
50
+ * @param productId - Product ID (required)
51
+ * @param variantId - Variant ID (required, null for non-variant products)
52
+ * @param quantity - Quantity to add (default: 1)
53
+ */
54
+ addToCart: (productId: string, variantId: string | null, quantity?: number) => void;
55
+ /**
56
+ * Update authentication tokens
57
+ * Use this when user logs in/out on the parent site
58
+ */
59
+ updateTokens: (accessToken: string, refreshToken?: string) => void;
60
+ }
61
+ /**
62
+ * Complete checkout store type
63
+ */
64
+ type CheckoutStore = CheckoutState & CheckoutActions;
65
+ /**
66
+ * Vanilla Zustand store for checkout state
67
+ *
68
+ * This is framework-agnostic and can be used directly or via framework bindings.
69
+ *
70
+ * @example Direct usage (vanilla JS)
71
+ * ```ts
72
+ * import { checkoutStore } from "@commercengine/checkout";
73
+ *
74
+ * // Initialize
75
+ * checkoutStore.getState().init({ storeId: "...", apiKey: "..." });
76
+ *
77
+ * // Get current state
78
+ * const { cartCount, isReady } = checkoutStore.getState();
79
+ *
80
+ * // Subscribe to changes
81
+ * checkoutStore.subscribe((state) => {
82
+ * console.log("Cart count:", state.cartCount);
83
+ * });
84
+ *
85
+ * // Call actions
86
+ * checkoutStore.getState().openCart();
87
+ * ```
88
+ */
89
+ declare const checkoutStore: Omit<zustand0.StoreApi<CheckoutStore>, "subscribe"> & {
90
+ subscribe: {
91
+ (listener: (selectedState: CheckoutStore, previousSelectedState: CheckoutStore) => void): () => void;
92
+ <U>(selector: (state: CheckoutStore) => U, listener: (selectedState: U, previousSelectedState: U) => void, options?: {
93
+ equalityFn?: ((a: U, b: U) => boolean) | undefined;
94
+ fireImmediately?: boolean;
95
+ } | undefined): () => void;
96
+ };
97
+ };
98
+ /**
99
+ * Initialize checkout
100
+ *
101
+ * @example
102
+ * ```ts
103
+ * import { initCheckout } from "@commercengine/checkout";
104
+ *
105
+ * initCheckout({
106
+ * storeId: "store_xxx",
107
+ * apiKey: "ak_xxx",
108
+ * onComplete: (order) => console.log("Order placed:", order.orderNumber),
109
+ * });
110
+ * ```
111
+ */
112
+ declare function initCheckout(config: CheckoutConfig): void;
113
+ /**
114
+ * Destroy checkout instance
115
+ *
116
+ * @example
117
+ * ```ts
118
+ * import { destroyCheckout } from "@commercengine/checkout";
119
+ *
120
+ * destroyCheckout();
121
+ * ```
122
+ */
123
+ declare function destroyCheckout(): void;
124
+ /**
125
+ * Get checkout state and methods (non-reactive)
126
+ *
127
+ * Use this outside of reactive frameworks or when you don't need reactivity.
128
+ *
129
+ * @example
130
+ * ```ts
131
+ * import { getCheckout } from "@commercengine/checkout";
132
+ *
133
+ * document.getElementById("cart-btn").onclick = () => {
134
+ * getCheckout().openCart();
135
+ * };
136
+ *
137
+ * // Check state
138
+ * if (getCheckout().isReady) {
139
+ * console.log("Cart has", getCheckout().cartCount, "items");
140
+ * }
141
+ * ```
142
+ */
143
+ declare function getCheckout(): Omit<CheckoutStore, "checkout" | "init" | "destroy">;
144
+ /**
145
+ * Subscribe to checkout state changes
146
+ *
147
+ * @example
148
+ * ```ts
149
+ * import { subscribeToCheckout } from "@commercengine/checkout";
150
+ *
151
+ * // Subscribe to all changes
152
+ * const unsubscribe = subscribeToCheckout((state) => {
153
+ * console.log("State changed:", state);
154
+ * });
155
+ *
156
+ * // Subscribe to specific state with selector
157
+ * const unsubscribe = subscribeToCheckout(
158
+ * (state) => state.cartCount,
159
+ * (cartCount) => console.log("Cart count:", cartCount)
160
+ * );
161
+ *
162
+ * // Cleanup
163
+ * unsubscribe();
164
+ * ```
165
+ */
166
+ declare const subscribeToCheckout: {
167
+ (listener: (selectedState: CheckoutStore, previousSelectedState: CheckoutStore) => void): () => void;
168
+ <U>(selector: (state: CheckoutStore) => U, listener: (selectedState: U, previousSelectedState: U) => void, options?: {
169
+ equalityFn?: ((a: U, b: U) => boolean) | undefined;
170
+ fireImmediately?: boolean;
171
+ } | undefined): () => void;
172
+ };
173
+ //# sourceMappingURL=store.d.ts.map
174
+ //#endregion
175
+ export { destroyCheckout as a, subscribeToCheckout as c, checkoutStore as i, CheckoutState as n, getCheckout as o, CheckoutStore as r, initCheckout as s, CheckoutActions as t };
176
+ //# sourceMappingURL=store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.d.ts","names":[],"sources":["../src/store.ts"],"sourcesContent":[],"mappings":";;;;;;AAsQA;AAcA;AAuBgB,UA/PC,aAAA,CA+PU;EAAA;UAAS,EA7PxB,QA6PwB,GAAA,IAAA;;EAAD,OAAA,EAAA,OAAA;EAsCtB;EAA6C,MAAA,EAAA,OAAA;;;;;;;;;;;UAnRzC,eAAA;;;;;iBAKA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KAwCL,aAAA,GAAgB,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;cA2C/B,eAAa,KAAA,QAAA,CAAA,SAAA;;;;;;;;;;;;;;;;;;;;;;;iBAgHV,YAAA,SAAqB;;;;;;;;;;;iBAcrB,eAAA,CAAA;;;;;;;;;;;;;;;;;;;;iBAuBA,WAAA,CAAA,GAAe,KAAK;;;;;;;;;;;;;;;;;;;;;;;cAsCvB"}