@snapie/chat-client 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/README.md +313 -0
- package/dist/client-Bztyx_3e.d.mts +220 -0
- package/dist/client-Bztyx_3e.d.ts +220 -0
- package/dist/index.d.mts +22 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +472 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +467 -0
- package/dist/index.mjs.map +1 -0
- package/dist/react.d.mts +56 -0
- package/dist/react.d.ts +56 -0
- package/dist/react.js +124 -0
- package/dist/react.js.map +1 -0
- package/dist/react.mjs +118 -0
- package/dist/react.mjs.map +1 -0
- package/package.json +64 -0
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
interface Channel {
|
|
2
|
+
_id: string;
|
|
3
|
+
name: string;
|
|
4
|
+
description?: string;
|
|
5
|
+
type: string;
|
|
6
|
+
conversationKind?: 'channel' | 'group';
|
|
7
|
+
owner?: string;
|
|
8
|
+
members?: string[];
|
|
9
|
+
memberCount: number;
|
|
10
|
+
isPublic: boolean;
|
|
11
|
+
}
|
|
12
|
+
interface Message {
|
|
13
|
+
_id: string;
|
|
14
|
+
sender: string;
|
|
15
|
+
content: string;
|
|
16
|
+
replyTo?: string | null;
|
|
17
|
+
editedAt?: string | null;
|
|
18
|
+
createdAt: string;
|
|
19
|
+
}
|
|
20
|
+
interface Conversation {
|
|
21
|
+
_id: string;
|
|
22
|
+
name: string;
|
|
23
|
+
description?: string;
|
|
24
|
+
type: 'channel' | 'group' | 'dm';
|
|
25
|
+
isPublic: boolean;
|
|
26
|
+
owner?: string;
|
|
27
|
+
members?: string[];
|
|
28
|
+
memberCount?: number;
|
|
29
|
+
peer?: string;
|
|
30
|
+
lastMessage?: Message | null;
|
|
31
|
+
unread?: boolean;
|
|
32
|
+
}
|
|
33
|
+
interface DmDeliveryInfo {
|
|
34
|
+
hasFcm: boolean;
|
|
35
|
+
memoSuggested: boolean;
|
|
36
|
+
cooldownMs: number;
|
|
37
|
+
}
|
|
38
|
+
interface DmStatusInfo {
|
|
39
|
+
meSeenAt: string;
|
|
40
|
+
peerSeenAt: string | null;
|
|
41
|
+
peerLastSeenAt: string | null;
|
|
42
|
+
peerOnline: boolean;
|
|
43
|
+
}
|
|
44
|
+
interface TypingStatusInfo {
|
|
45
|
+
users: string[];
|
|
46
|
+
ttlMs: number;
|
|
47
|
+
}
|
|
48
|
+
interface ChatPreferences {
|
|
49
|
+
mutedUsers: string[];
|
|
50
|
+
blockedUsers: string[];
|
|
51
|
+
}
|
|
52
|
+
interface MessagesResult {
|
|
53
|
+
messages: Message[];
|
|
54
|
+
status?: DmStatusInfo | null;
|
|
55
|
+
}
|
|
56
|
+
interface StorageAdapter {
|
|
57
|
+
getItem(key: string): string | null;
|
|
58
|
+
setItem(key: string, value: string): void;
|
|
59
|
+
removeItem(key: string): void;
|
|
60
|
+
}
|
|
61
|
+
interface ChatClientOptions {
|
|
62
|
+
/** Base URL of the Snapie instance, e.g. "https://snapie.io" */
|
|
63
|
+
baseUrl: string;
|
|
64
|
+
/** Override default localStorage — useful for React Native or Node */
|
|
65
|
+
storage?: StorageAdapter;
|
|
66
|
+
/** How often to poll for new messages when FCM is not available (ms, default 15000) */
|
|
67
|
+
pollInterval?: number;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
declare class ChatService {
|
|
71
|
+
private token;
|
|
72
|
+
private tokenUsername;
|
|
73
|
+
private base;
|
|
74
|
+
private storage;
|
|
75
|
+
constructor(baseUrl: string, storage: StorageAdapter);
|
|
76
|
+
isAuthenticated(): boolean;
|
|
77
|
+
getTokenUsername(): string | null;
|
|
78
|
+
authenticate(username: string, signMessage: (msg: string) => Promise<string>): Promise<void>;
|
|
79
|
+
logout(): void;
|
|
80
|
+
getChannels(): Promise<Channel[]>;
|
|
81
|
+
getConversations(): Promise<Conversation[]>;
|
|
82
|
+
getChannelMessages(channelId: string, opts?: {
|
|
83
|
+
before?: string;
|
|
84
|
+
after?: string;
|
|
85
|
+
limit?: number;
|
|
86
|
+
}): Promise<Message[]>;
|
|
87
|
+
sendChannelMessage(channelId: string, content: string, replyTo?: string): Promise<Message>;
|
|
88
|
+
editChannelMessage(channelId: string, messageId: string, content: string): Promise<Message>;
|
|
89
|
+
joinChannel(channelId: string): Promise<void>;
|
|
90
|
+
leaveChannel(channelId: string): Promise<void>;
|
|
91
|
+
getDmMessages(conversationId: string, opts?: {
|
|
92
|
+
before?: string;
|
|
93
|
+
after?: string;
|
|
94
|
+
limit?: number;
|
|
95
|
+
}): Promise<MessagesResult>;
|
|
96
|
+
sendDmMessage(conversationId: string, content: string, replyTo?: string): Promise<{
|
|
97
|
+
message: Message;
|
|
98
|
+
delivery?: DmDeliveryInfo;
|
|
99
|
+
}>;
|
|
100
|
+
editDmMessage(conversationId: string, messageId: string, content: string): Promise<Message>;
|
|
101
|
+
openDm(targetUser: string): Promise<Conversation>;
|
|
102
|
+
createGroup(payload: {
|
|
103
|
+
name: string;
|
|
104
|
+
description?: string;
|
|
105
|
+
isPublic?: boolean;
|
|
106
|
+
members?: string[];
|
|
107
|
+
}): Promise<Channel>;
|
|
108
|
+
getGroups(): Promise<Channel[]>;
|
|
109
|
+
addGroupMember(groupId: string, member: string): Promise<Channel>;
|
|
110
|
+
removeGroupMember(groupId: string, member: string): Promise<Channel>;
|
|
111
|
+
getUnreadCount(): Promise<number>;
|
|
112
|
+
setTyping(conversationId: string, isTyping: boolean): Promise<void>;
|
|
113
|
+
getTyping(conversationId: string): Promise<TypingStatusInfo>;
|
|
114
|
+
getPreferences(): Promise<ChatPreferences>;
|
|
115
|
+
muteUser(username: string): Promise<void>;
|
|
116
|
+
unmuteUser(username: string): Promise<void>;
|
|
117
|
+
blockUser(username: string): Promise<void>;
|
|
118
|
+
unblockUser(username: string): Promise<void>;
|
|
119
|
+
registerDevice(fcmToken: string): Promise<void>;
|
|
120
|
+
markDmMemoFallbackSent(conversationId: string): Promise<void>;
|
|
121
|
+
private buildQS;
|
|
122
|
+
private get;
|
|
123
|
+
private post;
|
|
124
|
+
request<T>(url: string, opts: RequestInit, auth: boolean): Promise<T>;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
type ConversationsCallback = (conversations: Conversation[]) => void;
|
|
128
|
+
type MessagesCallback = (messages: Message[]) => void;
|
|
129
|
+
type UnreadCallback = (count: number) => void;
|
|
130
|
+
/**
|
|
131
|
+
* High-level Snapie chat client.
|
|
132
|
+
*
|
|
133
|
+
* Usage:
|
|
134
|
+
* const client = new ChatClient({ baseUrl: 'https://snapie.io' });
|
|
135
|
+
* await client.authenticate(username, msg => keychain.sign(msg));
|
|
136
|
+
* const conversations = await client.getConversations();
|
|
137
|
+
* const unsub = client.subscribeToMessages(convId, 'dm', msgs => setMessages(msgs));
|
|
138
|
+
*/
|
|
139
|
+
declare class ChatClient {
|
|
140
|
+
readonly service: ChatService;
|
|
141
|
+
private poller;
|
|
142
|
+
/** Cache of messages per conversationId — avoids re-rendering unchanged data */
|
|
143
|
+
private messageCache;
|
|
144
|
+
constructor(options: ChatClientOptions);
|
|
145
|
+
isAuthenticated(): boolean;
|
|
146
|
+
getUsername(): string | null;
|
|
147
|
+
/**
|
|
148
|
+
* Authenticate with a Hive account.
|
|
149
|
+
* `signMessage` should call Hive Keychain or equivalent with the posting key.
|
|
150
|
+
*/
|
|
151
|
+
authenticate(username: string, signMessage: (challenge: string) => Promise<string>): Promise<void>;
|
|
152
|
+
logout(): void;
|
|
153
|
+
getConversations(): Promise<Conversation[]>;
|
|
154
|
+
openDm(targetUser: string): Promise<Conversation>;
|
|
155
|
+
getChannels(): Promise<Channel[]>;
|
|
156
|
+
getGroups(): Promise<Channel[]>;
|
|
157
|
+
joinChannel(channelId: string): Promise<void>;
|
|
158
|
+
leaveChannel(channelId: string): Promise<void>;
|
|
159
|
+
createGroup(payload: {
|
|
160
|
+
name: string;
|
|
161
|
+
description?: string;
|
|
162
|
+
isPublic?: boolean;
|
|
163
|
+
members?: string[];
|
|
164
|
+
}): Promise<Channel>;
|
|
165
|
+
addGroupMember(groupId: string, member: string): Promise<Channel>;
|
|
166
|
+
removeGroupMember(groupId: string, member: string): Promise<Channel>;
|
|
167
|
+
getMessages(conversationId: string, type: 'channel' | 'dm' | 'group', opts?: {
|
|
168
|
+
before?: string;
|
|
169
|
+
after?: string;
|
|
170
|
+
limit?: number;
|
|
171
|
+
}): Promise<MessagesResult>;
|
|
172
|
+
sendMessage(conversationId: string, type: 'channel' | 'dm' | 'group', content: string, replyTo?: string): Promise<{
|
|
173
|
+
message: Message;
|
|
174
|
+
delivery?: DmDeliveryInfo;
|
|
175
|
+
}>;
|
|
176
|
+
editMessage(conversationId: string, type: 'channel' | 'dm' | 'group', messageId: string, content: string): Promise<Message>;
|
|
177
|
+
/**
|
|
178
|
+
* Subscribe to live updates for a conversation's message list.
|
|
179
|
+
* Calls `callback` immediately with the current messages, then again on every poll tick.
|
|
180
|
+
* Returns an unsubscribe function.
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* const unsub = client.subscribeToMessages(conv._id, conv.type, msgs => setMessages(msgs));
|
|
184
|
+
* // later:
|
|
185
|
+
* unsub();
|
|
186
|
+
*/
|
|
187
|
+
subscribeToMessages(conversationId: string, type: 'channel' | 'dm' | 'group', callback: MessagesCallback): () => void;
|
|
188
|
+
/**
|
|
189
|
+
* Subscribe to the full conversations list, refreshed on every poll tick.
|
|
190
|
+
* Returns an unsubscribe function.
|
|
191
|
+
*/
|
|
192
|
+
subscribeToConversations(callback: ConversationsCallback): () => void;
|
|
193
|
+
/**
|
|
194
|
+
* Subscribe to the unread message count, refreshed on every poll tick.
|
|
195
|
+
* Returns an unsubscribe function.
|
|
196
|
+
*/
|
|
197
|
+
subscribeToUnreadCount(callback: UnreadCallback): () => void;
|
|
198
|
+
/**
|
|
199
|
+
* Notify the client of an incoming FCM foreground message.
|
|
200
|
+
* Call this from your FCM `onMessage` handler to trigger an immediate refresh
|
|
201
|
+
* rather than waiting for the next poll tick.
|
|
202
|
+
*
|
|
203
|
+
* @example
|
|
204
|
+
* onMessage(messaging, () => client.onForegroundPush());
|
|
205
|
+
*/
|
|
206
|
+
onForegroundPush(): void;
|
|
207
|
+
setTyping(conversationId: string, isTyping: boolean): Promise<void>;
|
|
208
|
+
getTyping(conversationId: string): Promise<TypingStatusInfo>;
|
|
209
|
+
getUnreadCount(): Promise<number>;
|
|
210
|
+
getPreferences(): Promise<ChatPreferences>;
|
|
211
|
+
muteUser(username: string): Promise<void>;
|
|
212
|
+
unmuteUser(username: string): Promise<void>;
|
|
213
|
+
blockUser(username: string): Promise<void>;
|
|
214
|
+
unblockUser(username: string): Promise<void>;
|
|
215
|
+
registerDevice(fcmToken: string): Promise<void>;
|
|
216
|
+
markDmMemoFallbackSent(conversationId: string): Promise<void>;
|
|
217
|
+
destroy(): void;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export { ChatClient as C, type DmDeliveryInfo as D, type Message as M, type StorageAdapter as S, type TypingStatusInfo as T, ChatService as a, type ChatClientOptions as b, type Channel as c, type Conversation as d, type MessagesResult as e, type DmStatusInfo as f, type ChatPreferences as g };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { S as StorageAdapter } from './client-Bztyx_3e.mjs';
|
|
2
|
+
export { c as Channel, C as ChatClient, b as ChatClientOptions, g as ChatPreferences, a as ChatService, d as Conversation, D as DmDeliveryInfo, f as DmStatusInfo, M as Message, e as MessagesResult, T as TypingStatusInfo } from './client-Bztyx_3e.mjs';
|
|
3
|
+
|
|
4
|
+
type Handler = () => void | Promise<void>;
|
|
5
|
+
/**
|
|
6
|
+
* Manages a single setInterval that fans out to multiple subscribers.
|
|
7
|
+
* Starting the first subscription starts the timer; removing the last stops it.
|
|
8
|
+
*/
|
|
9
|
+
declare class PollingManager {
|
|
10
|
+
private timer;
|
|
11
|
+
private handlers;
|
|
12
|
+
private interval;
|
|
13
|
+
constructor(intervalMs: number);
|
|
14
|
+
subscribe(handler: Handler): () => void;
|
|
15
|
+
private tick;
|
|
16
|
+
destroy(): void;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Default adapter — uses localStorage when available, falls back to in-memory. */
|
|
20
|
+
declare function createDefaultStorage(): StorageAdapter;
|
|
21
|
+
|
|
22
|
+
export { PollingManager, StorageAdapter, createDefaultStorage };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { S as StorageAdapter } from './client-Bztyx_3e.js';
|
|
2
|
+
export { c as Channel, C as ChatClient, b as ChatClientOptions, g as ChatPreferences, a as ChatService, d as Conversation, D as DmDeliveryInfo, f as DmStatusInfo, M as Message, e as MessagesResult, T as TypingStatusInfo } from './client-Bztyx_3e.js';
|
|
3
|
+
|
|
4
|
+
type Handler = () => void | Promise<void>;
|
|
5
|
+
/**
|
|
6
|
+
* Manages a single setInterval that fans out to multiple subscribers.
|
|
7
|
+
* Starting the first subscription starts the timer; removing the last stops it.
|
|
8
|
+
*/
|
|
9
|
+
declare class PollingManager {
|
|
10
|
+
private timer;
|
|
11
|
+
private handlers;
|
|
12
|
+
private interval;
|
|
13
|
+
constructor(intervalMs: number);
|
|
14
|
+
subscribe(handler: Handler): () => void;
|
|
15
|
+
private tick;
|
|
16
|
+
destroy(): void;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Default adapter — uses localStorage when available, falls back to in-memory. */
|
|
20
|
+
declare function createDefaultStorage(): StorageAdapter;
|
|
21
|
+
|
|
22
|
+
export { PollingManager, StorageAdapter, createDefaultStorage };
|