@cshah18/sdk 3.0.4 → 3.0.5

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,126 @@
1
+ import { InternalConfig } from "./types";
2
+ export interface GroupPayload {
3
+ id: string;
4
+ group_name: string;
5
+ participants_count: number;
6
+ max_participants: number;
7
+ status: string;
8
+ expiry_at: Date | string;
9
+ timeLeftSeconds: number;
10
+ }
11
+ export interface MembershipPayload {
12
+ id: string;
13
+ session_id: string;
14
+ created_at: Date | string;
15
+ }
16
+ export interface GroupMemberJoinedPayload {
17
+ group: GroupPayload;
18
+ membership: MembershipPayload;
19
+ product_id?: string;
20
+ isPrimary?: boolean;
21
+ }
22
+ export interface GroupCreatedPayload {
23
+ group: GroupPayload;
24
+ membership: MembershipPayload;
25
+ product_id?: string;
26
+ }
27
+ export interface GroupFulfilledPayload {
28
+ group: GroupPayload;
29
+ frozen_reward: unknown;
30
+ product_id?: string | null;
31
+ }
32
+ export interface SocketClientConfig {
33
+ serverUrl: string;
34
+ merchantKey?: string;
35
+ sdkVersion?: string;
36
+ sessionId?: string;
37
+ debug?: boolean;
38
+ }
39
+ export interface SocketEventHandlers {
40
+ onGroupMemberJoined?: (payload: GroupMemberJoinedPayload) => void;
41
+ onGroupCreated?: (payload: GroupCreatedPayload) => void;
42
+ onGroupFulfilled?: (payload: GroupFulfilledPayload) => void;
43
+ onConnect?: () => void;
44
+ onDisconnect?: (reason: string) => void;
45
+ onError?: (error: Error) => void;
46
+ }
47
+ /**
48
+ * Frontend Socket.IO client for real-time group updates
49
+ * Manages connection to the backend /sdk namespace and handles group subscriptions
50
+ */
51
+ export declare class SocketClient {
52
+ private socket;
53
+ private logger;
54
+ private config;
55
+ private handlers;
56
+ private currentGroupId;
57
+ private currentProductId;
58
+ private maxReconnectAttempts;
59
+ constructor(config: SocketClientConfig);
60
+ /**
61
+ * Connect to the Socket.IO server
62
+ * @param handlers - Event handlers for socket events
63
+ */
64
+ connect(handlers: SocketEventHandlers): Promise<void>;
65
+ /**
66
+ * Register event listeners for group updates
67
+ */
68
+ private registerEventListeners;
69
+ /**
70
+ * Subscribe to a specific group's updates
71
+ * @param groupId - The group ID to subscribe to
72
+ */
73
+ subscribeToGroup(groupId: string): void;
74
+ /**
75
+ * Unsubscribe from a specific group's updates
76
+ * @param groupId - The group ID to unsubscribe from
77
+ */
78
+ unsubscribeFromGroup(groupId: string): void;
79
+ /**
80
+ * Subscribe to product availability updates (groups being created for the product)
81
+ * @param productId - The product ID to watch for
82
+ */
83
+ subscribeToProductAvailable(productId: string): void;
84
+ /**
85
+ * Unsubscribe from product availability updates
86
+ * @param productId - The product ID to stop watching
87
+ */
88
+ unsubscribeFromProductAvailable(productId: string): void;
89
+ /**
90
+ * Check if socket is connected
91
+ */
92
+ isConnected(): boolean;
93
+ /**
94
+ * Get current socket ID
95
+ */
96
+ getSocketId(): string | null;
97
+ /**
98
+ * Get currently subscribed group ID
99
+ */
100
+ getCurrentGroupId(): string | null;
101
+ /**
102
+ * Get currently subscribed product ID
103
+ */
104
+ getCurrentProductId(): string | null;
105
+ /**
106
+ * Disconnect from the server
107
+ */
108
+ disconnect(): void;
109
+ /**
110
+ * Force reconnection to the server
111
+ */
112
+ reconnect(): void;
113
+ /**
114
+ * Update authentication credentials
115
+ * @param merchantKey - New merchant key
116
+ */
117
+ updateAuth(merchantKey: string): void;
118
+ }
119
+ /**
120
+ * Create a socket client from SDK config
121
+ * @param config - Internal SDK configuration
122
+ * @param sdkVersion - SDK version
123
+ * @param sessionId - Session ID from CoBuy instance
124
+ * @returns Configured SocketClient instance
125
+ */
126
+ export declare function createSocketClient(config: InternalConfig, sdkVersion: string, sessionId: string): SocketClient;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cshah18/sdk",
3
- "version": "3.0.4",
3
+ "version": "3.0.5",
4
4
  "description": "CoBuy Embedded SDK for browser JavaScript integration",
5
5
  "type": "module",
6
6
  "main": "dist/cobuy-sdk.umd.js",
@@ -1,74 +0,0 @@
1
- import { InternalConfig, GroupFulfilledEvent, Reward } from "./types";
2
- export interface GroupRealtimeSnapshot {
3
- groupId: string;
4
- productId: string;
5
- participants: number;
6
- maxParticipants?: number;
7
- progressPercent?: number;
8
- expiresAt?: string;
9
- reward?: Reward | null;
10
- status?: string;
11
- timeLeftSeconds?: number;
12
- }
13
- export type GroupChannelEvent = {
14
- type: "update";
15
- snapshot: GroupRealtimeSnapshot;
16
- } | {
17
- type: "fulfilled";
18
- snapshot: GroupRealtimeSnapshot;
19
- };
20
- export type GroupChannelListener = (event: GroupChannelEvent) => void;
21
- /**
22
- * Centralized real-time channel manager for all group-aware UI surfaces.
23
- * Ensures a single socket connection, one room subscription per group, and
24
- * shared event dispatch to interested listeners.
25
- */
26
- export declare class GroupChannelManager {
27
- private readonly config;
28
- private readonly sdkVersion;
29
- private readonly onFulfilledCallback?;
30
- private socket;
31
- private readonly logger;
32
- private readonly listeners;
33
- private readonly groupRefCount;
34
- private readonly groupToProduct;
35
- private readonly latestByGroup;
36
- private readonly latestByProduct;
37
- private isConnecting;
38
- constructor(config: InternalConfig, sdkVersion: string, onFulfilledCallback?: ((event: GroupFulfilledEvent) => void) | undefined);
39
- /**
40
- * Start socket connection if not already connected.
41
- */
42
- connect(): void;
43
- /**
44
- * Subscribe to realtime updates for a group. Multiple callers are reference-counted.
45
- */
46
- subscribeToGroup(groupId: string, productId: string): void;
47
- /**
48
- * Unsubscribe from a group when no listeners remain.
49
- */
50
- unsubscribeFromGroup(groupId: string): void;
51
- /**
52
- * Switch from one group room to another, ensuring only one active subscription.
53
- */
54
- switchGroup(oldGroupId: string | null, newGroupId: string, productId: string): void;
55
- /**
56
- * Register for realtime events. Returns an unsubscribe function.
57
- */
58
- addListener(listener: GroupChannelListener): () => void;
59
- /**
60
- * Retrieve the latest snapshot for a product if one exists.
61
- */
62
- getLatestSnapshotForProduct(productId: string): GroupRealtimeSnapshot | null;
63
- /**
64
- * Disconnect and clear state.
65
- */
66
- disconnect(): void;
67
- private handleGroupUpdate;
68
- private handleGroupFulfilled;
69
- private cacheSnapshot;
70
- private broadcast;
71
- private normalizeSnapshot;
72
- private pickString;
73
- private pickNumber;
74
- }