@commercengine/js 0.3.4 → 0.4.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/index.d.cts DELETED
@@ -1,456 +0,0 @@
1
- //#region src/types.d.ts
2
- /**
3
- * Commerce Engine Checkout - Type Definitions
4
- *
5
- * @commercengine/js
6
- *
7
- * This is the single source of truth for checkout configuration.
8
- * The checkout app parses these from URL parameters.
9
- */
10
- /**
11
- * Authentication mode
12
- * - 'managed': Checkout manages token lifecycle (refresh, storage)
13
- * - 'provided': Parent manages tokens. Checkout uses in-memory only, syncs via events
14
- */
15
- type AuthMode = "managed" | "provided";
16
- /**
17
- * Item to add to cart
18
- */
19
- interface AddToCartItem {
20
- /**
21
- * Product ID (required)
22
- */
23
- productId: string;
24
- /**
25
- * Variant ID (required, null for non-variant products)
26
- */
27
- variantId: string | null;
28
- /**
29
- * Quantity to add
30
- * @default 1
31
- */
32
- quantity?: number;
33
- }
34
- /**
35
- * Quick buy configuration for initialization
36
- * Adds a product to cart when checkout initializes
37
- */
38
- interface QuickBuyConfig {
39
- /**
40
- * Product ID (required)
41
- */
42
- productId: string;
43
- /**
44
- * Variant ID (required, null for non-variant products)
45
- */
46
- variantId: string | null;
47
- /**
48
- * Quantity to add
49
- * @default 1
50
- */
51
- quantity?: number;
52
- }
53
- /**
54
- * Session behavior mode
55
- * - 'continue-existing': Add quick-buy item to existing cart (default)
56
- * - 'force-new': Clear cart and start fresh
57
- */
58
- type SessionMode = "continue-existing" | "force-new";
59
- /**
60
- * Feature flags for checkout
61
- * All features are enabled by default
62
- */
63
- interface CheckoutFeatures {
64
- /**
65
- * Enable loyalty points redemption
66
- * @default true
67
- */
68
- loyalty?: boolean;
69
- /**
70
- * Enable coupon/promo codes
71
- * @default true
72
- */
73
- coupons?: boolean;
74
- /**
75
- * Enable collect-in-store option
76
- * @default false
77
- */
78
- collectInStore?: boolean;
79
- /**
80
- * Enable free shipping progress tracker in cart
81
- * @default true
82
- */
83
- freeShippingProgress?: boolean;
84
- /**
85
- * Enable product recommendations in cart
86
- * @default true
87
- */
88
- productRecommendations?: boolean;
89
- }
90
- /**
91
- * Environment for checkout
92
- * - 'production': Uses checkout.commercengine.com
93
- * - 'staging': Uses checkout.staging.commercengine.com
94
- */
95
- type Environment = "production" | "staging";
96
- /**
97
- * Configuration for initializing checkout
98
- *
99
- * Either provide `url` directly (for local development) or
100
- * provide `storeId` + `apiKey` (for production).
101
- */
102
- interface CheckoutConfig {
103
- /**
104
- * Environment to use for checkout URL resolution
105
- * - 'production': checkout.commercengine.com (default)
106
- * - 'staging': staging.checkout.commercengine.com
107
- *
108
- * For local development, use the `url` option instead.
109
- */
110
- environment?: Environment;
111
- /**
112
- * Direct checkout URL (overrides environment detection)
113
- * If provided, storeId and apiKey are optional.
114
- * @example "http://localhost:8080"
115
- */
116
- url?: string;
117
- /**
118
- * Your Commerce Engine Store ID
119
- * Required if `url` is not provided.
120
- * @example "store_abc123"
121
- */
122
- storeId?: string;
123
- /**
124
- * Your Commerce Engine API Key
125
- * Required if `url` is not provided.
126
- * @example "ak_test_xyz789"
127
- */
128
- apiKey?: string;
129
- /**
130
- * Theme preference
131
- * @default "system"
132
- */
133
- theme?: "light" | "dark" | "system";
134
- /**
135
- * Appearance customization
136
- */
137
- appearance?: {
138
- /**
139
- * Z-index for the checkout overlay
140
- * @default 99999
141
- */
142
- zIndex?: number;
143
- };
144
- /**
145
- * Authentication mode
146
- * - 'managed': Checkout manages token lifecycle (default)
147
- * - 'provided': Parent manages tokens, checkout uses in-memory only
148
- * @default "managed"
149
- */
150
- authMode?: AuthMode;
151
- /**
152
- * Initial access token (if user already logged in on parent site)
153
- */
154
- accessToken?: string;
155
- /**
156
- * Initial refresh token
157
- */
158
- refreshToken?: string;
159
- /**
160
- * Quick buy configuration for "Buy Now" flows
161
- * When provided, adds the product to cart on initialization
162
- */
163
- quickBuy?: QuickBuyConfig;
164
- /**
165
- * Session behavior when quick buy is used
166
- * @default "continue-existing"
167
- */
168
- sessionMode?: SessionMode;
169
- /**
170
- * Auto-detect quick buy params from parent page URL
171
- * When enabled, SDK parses window.location.search for:
172
- * - product_id, variant_id, qty/quantity, session_mode
173
- * Useful for ad-driven traffic to embedded checkout pages
174
- * @default false
175
- */
176
- autoDetectQuickBuy?: boolean;
177
- /**
178
- * Feature flags to enable/disable checkout features
179
- * All features are enabled by default
180
- */
181
- features?: CheckoutFeatures;
182
- /**
183
- * Called when checkout iframe is loaded and ready
184
- */
185
- onReady?: () => void;
186
- /**
187
- * Called when checkout drawer is opened
188
- */
189
- onOpen?: () => void;
190
- /**
191
- * Called when checkout is closed (all drawers closed)
192
- */
193
- onClose?: () => void;
194
- /**
195
- * Called when order is completed successfully
196
- */
197
- onComplete?: (order: OrderData) => void;
198
- /**
199
- * Called when cart state changes (items added/removed/updated)
200
- * Use this to update cart badge count on parent site
201
- */
202
- onCartUpdate?: (cart: CartData) => void;
203
- /**
204
- * Called when user logs in via checkout
205
- */
206
- onLogin?: (data: AuthLoginData) => void;
207
- /**
208
- * Called when user logs out (receives new anonymous tokens)
209
- */
210
- onLogout?: (data: AuthLogoutData) => void;
211
- /**
212
- * Called when tokens are auto-refreshed by SDK
213
- */
214
- onTokenRefresh?: (data: AuthRefreshData) => void;
215
- /**
216
- * Called when session is corrupted and SDK clears all tokens.
217
- * User needs to re-authenticate.
218
- */
219
- onSessionError?: () => void;
220
- /**
221
- * Called when checkout encounters a configuration error
222
- * This happens when credentials are missing or invalid.
223
- * The error drawer will still be shown, but you can use this
224
- * callback to log the error or show custom UI.
225
- */
226
- onError?: (error: ErrorData) => void;
227
- }
228
- /**
229
- * Channel information from token
230
- */
231
- interface Channel {
232
- id: string;
233
- name: string;
234
- type: string;
235
- }
236
- /**
237
- * User information decoded from JWT token
238
- * Matches @commercengine/storefront-sdk UserInfo 1:1
239
- */
240
- interface UserInfo {
241
- id: string;
242
- email: string | null;
243
- phone: string | null;
244
- username: string;
245
- firstName: string | null;
246
- lastName: string | null;
247
- storeId: string;
248
- isLoggedIn: boolean;
249
- isAnonymous: boolean;
250
- customerId: string | null;
251
- customerGroupId: string | null;
252
- anonymousId: string;
253
- channel: Channel;
254
- tokenExpiry: Date;
255
- tokenIssuedAt: Date;
256
- }
257
- /**
258
- * Login method used for authentication
259
- */
260
- type LoginMethod = "whatsapp" | "phone" | "email";
261
- /**
262
- * Cart state data emitted on cart:updated event
263
- */
264
- interface CartData {
265
- /** Number of items in cart */
266
- count: number;
267
- /** Cart subtotal amount */
268
- total: number;
269
- /** Currency code (e.g., "INR", "USD") */
270
- currency: string;
271
- }
272
- /**
273
- * Order data emitted on checkout:complete event
274
- */
275
- interface OrderData {
276
- /** Order ID */
277
- id: string;
278
- /** Human-readable order number */
279
- orderNumber: string;
280
- }
281
- /**
282
- * Data emitted on login event
283
- */
284
- interface AuthLoginData {
285
- accessToken: string;
286
- refreshToken?: string;
287
- user?: UserInfo;
288
- loginMethod?: LoginMethod;
289
- }
290
- /**
291
- * Data emitted on logout event
292
- */
293
- interface AuthLogoutData {
294
- accessToken: string;
295
- refreshToken?: string;
296
- user?: UserInfo;
297
- }
298
- /**
299
- * Data emitted on token refresh event
300
- */
301
- interface AuthRefreshData {
302
- accessToken: string;
303
- refreshToken?: string;
304
- }
305
- /**
306
- * Error data emitted on checkout:error event
307
- */
308
- interface ErrorData {
309
- /** Error message */
310
- message: string;
311
- }
312
- /**
313
- * Events emitted by checkout
314
- */
315
- type CheckoutEventType = "ready" | "open" | "close" | "complete" | "error" | "cart:updated" | "auth:login" | "auth:logout" | "auth:refresh" | "auth:session-error";
316
- /**
317
- * Event listener function type
318
- */
319
- type EventListener<T = unknown> = (data: T) => void;
320
- //#endregion
321
- //#region src/events.d.ts
322
- declare class EventEmitter {
323
- private listeners;
324
- /**
325
- * Subscribe to an event
326
- */
327
- on<T = unknown>(event: CheckoutEventType, listener: EventListener<T>): void;
328
- /**
329
- * Unsubscribe from an event
330
- */
331
- off<T = unknown>(event: CheckoutEventType, listener: EventListener<T>): void;
332
- /**
333
- * Subscribe to an event (once)
334
- */
335
- once<T = unknown>(event: CheckoutEventType, listener: EventListener<T>): void;
336
- /**
337
- * Emit an event to all subscribers
338
- */
339
- protected emit<T = unknown>(event: CheckoutEventType, data?: T): void;
340
- /**
341
- * Remove all listeners
342
- */
343
- protected removeAllListeners(): void;
344
- }
345
- //#endregion
346
- //#region src/checkout.d.ts
347
- declare class Checkout extends EventEmitter {
348
- private config;
349
- private iframe;
350
- private isReady;
351
- private isOpen;
352
- private cartState;
353
- private boundMessageHandler;
354
- private hasQuickBuyParams;
355
- constructor(config: CheckoutConfig);
356
- private initialize;
357
- /**
358
- * Remove quick buy params from the parent URL to prevent duplicate adds on refresh
359
- * Uses replaceState to avoid adding to browser history
360
- */
361
- private cleanQuickBuyParamsFromUrl;
362
- private handleMessage;
363
- private handleReady;
364
- private handleError;
365
- private handleOpen;
366
- private handleClose;
367
- private handleComplete;
368
- private handleCartUpdate;
369
- private handleLogin;
370
- private handleLogout;
371
- private handleTokenRefresh;
372
- private handleSessionError;
373
- /**
374
- * Open the cart drawer
375
- */
376
- openCart(): void;
377
- /**
378
- * Open the checkout drawer directly (e.g., for Buy Now flow)
379
- */
380
- openCheckout(): void;
381
- /**
382
- * Close the checkout overlay
383
- */
384
- close(): void;
385
- /**
386
- * Update authentication tokens
387
- * Use this when user logs in/out on the parent site
388
- */
389
- updateTokens(accessToken: string, refreshToken?: string): void;
390
- /**
391
- * Add an item to cart and open the cart drawer
392
- *
393
- * @param productId - Product ID (required)
394
- * @param variantId - Variant ID (required, null for non-variant products)
395
- * @param quantity - Quantity to add (default: 1)
396
- */
397
- addToCart(productId: string, variantId: string | null, quantity?: number): void;
398
- /**
399
- * Get current cart state
400
- */
401
- getCart(): CartData;
402
- /**
403
- * Check if checkout is ready
404
- */
405
- get ready(): boolean;
406
- /**
407
- * Check if checkout overlay is currently open
408
- */
409
- get open(): boolean;
410
- /**
411
- * Subscribe to an event
412
- * @override EventEmitter.on with typed overloads
413
- */
414
- on(event: "ready", listener: EventListener<void>): void;
415
- on(event: "open", listener: EventListener<void>): void;
416
- on(event: "close", listener: EventListener<void>): void;
417
- on(event: "complete", listener: EventListener<OrderData>): void;
418
- on(event: "cart:updated", listener: EventListener<CartData>): void;
419
- on(event: "auth:login", listener: EventListener<AuthLoginData>): void;
420
- on(event: "auth:logout", listener: EventListener<AuthLogoutData>): void;
421
- on(event: "auth:refresh", listener: EventListener<AuthRefreshData>): void;
422
- on(event: "auth:session-error", listener: EventListener<void>): void;
423
- /**
424
- * Destroy the checkout instance
425
- * Removes iframe and cleans up event listeners
426
- */
427
- destroy(): void;
428
- }
429
- //#endregion
430
- //#region src/index.d.ts
431
- /**
432
- * Global Commercengine object interface
433
- */
434
- interface CommercengineGlobal {
435
- /**
436
- * Initialize checkout with configuration
437
- * @returns Promise that resolves to Checkout instance
438
- */
439
- init: (config: CheckoutConfig) => Promise<Checkout>;
440
- /**
441
- * Callback invoked when script has loaded
442
- * Set this BEFORE including the script to be notified when ready
443
- */
444
- onLoad?: () => void;
445
- /**
446
- * Current checkout instance (if initialized)
447
- */
448
- instance?: Checkout;
449
- }
450
- /**
451
- * Global Commercengine namespace
452
- */
453
- declare const Commercengine: CommercengineGlobal;
454
- //#endregion
455
- export { type AddToCartItem, type AuthLoginData, type AuthLogoutData, type AuthMode, type AuthRefreshData, type CartData, type Channel, Checkout, type CheckoutConfig, type CheckoutEventType, type CheckoutFeatures, Commercengine, Commercengine as default, type Environment, type ErrorData, type LoginMethod, type OrderData, type QuickBuyConfig, type SessionMode, type UserInfo };
456
- //# sourceMappingURL=index.d.cts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.cts","names":[],"sources":["../src/types.ts","../src/events.ts","../src/checkout.ts","../src/index.ts"],"mappings":";;AAkBA;;;;;AASA;;;;;;;KATY,QAAA;;AA+BZ;;UAtBiB,aAAA;EAsBc;;;EAlB7B,SAAA;EAiCQ;;AAYV;EAxCE,SAAA;;;;AAkDF;EA5CE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