zapind 0.0.0-stage → 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 CHANGED
@@ -1,3 +1,270 @@
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.
1
+ # Zapind
2
+
3
+ **Developer-friendly, type-safe, contextual Zalo SDK for JavaScript & TypeScript** — powered by `zca-js` under the hood.
4
+
5
+ Zapind wraps `zca-js` with a clean domain-oriented API, automatic `threadType` resolution, contextual objects (`user.send()`, `group.send()`, `conversation.send()`, `message.reply()`), built-in session management, unified error hierarchy, caching, request deduplication, and full access to the low-level engine via `client.raw`.
6
+
7
+ ---
8
+
9
+ ## Installation
10
+
11
+ ```bash
12
+ npm install zapind
13
+ ```
14
+
15
+ Requires **Node.js >= 20**.
16
+
17
+ ---
18
+
19
+ ## Quick Start
20
+
21
+ ```ts
22
+ import { Zapind, Reaction } from "zapind";
23
+
24
+ const client = new Zapind({
25
+ session: "./session.json",
26
+ });
27
+
28
+ await client.login({
29
+ onQRCode: ({ imageDataUrl }) => console.log("Scan QR:", imageDataUrl),
30
+ });
31
+
32
+ client.on("message", async (message) => {
33
+ console.log(`${message.sender.name ?? message.sender.id}: ${message.text}`);
34
+
35
+ if (message.text === "!ping") {
36
+ await message.reply("pong!");
37
+ await message.react(Reaction.Heart);
38
+ }
39
+ });
40
+
41
+ client.startListening({ retryOnClose: true });
42
+ ```
43
+
44
+ ---
45
+
46
+ ## Authentication
47
+
48
+ `client.login()` automatically handles the full session lifecycle:
49
+ 1. Restores saved credentials from `session` if present and validates them.
50
+ 2. If no session exists or the session expired, emits `"auth.required"` and falls back to QR login.
51
+ 3. Saves the fresh session automatically.
52
+
53
+ ```ts
54
+ const client = new Zapind({
55
+ session: "./session.json",
56
+ });
57
+
58
+ client.on("auth.required", ({ reason }) => {
59
+ console.log("Authentication required:", reason);
60
+ });
61
+
62
+ const session = await client.login({
63
+ onQRCode: ({ imageDataUrl }) => console.log("QR Data URL:", imageDataUrl),
64
+ onScanned: ({ displayName }) => console.log("Scanned by:", displayName),
65
+ });
66
+
67
+ console.log("Logged in as:", session.accountId, session.displayName);
68
+ ```
69
+
70
+ ---
71
+
72
+ ## Sending Messages
73
+
74
+ No need to manually pass `ThreadType.User` or `ThreadType.Group` — Zapind resolves `threadType` automatically from contextual objects or its cached Group ID registry.
75
+
76
+ ```ts
77
+ // Plain text (auto-detects DM vs Group)
78
+ await client.messages.send(userId, "Hello!");
79
+ await client.messages.send(groupId, "Hello group!");
80
+
81
+ // Unified rich payload
82
+ await client.messages.send(groupId, {
83
+ text: "Check this out",
84
+ replyTo: incomingMessage,
85
+ mentions: [{ userId: "123456", pos: 0, len: 5 }],
86
+ attachment: "./report.pdf",
87
+ });
88
+
89
+ // Send a link, sticker, voice, or video with the same API
90
+ await client.messages.send(groupId, {
91
+ link: { url: "https://github.com/nquangdungg/zapind" },
92
+ });
93
+ ```
94
+
95
+ ---
96
+
97
+ ## Users
98
+
99
+ ```ts
100
+ const user = await client.users.get(userId);
101
+ if (user) {
102
+ await user.send("Hi there!");
103
+ }
104
+
105
+ const byPhone = await client.users.findByPhone("0912345678");
106
+ const byUsername = await client.users.findByUsername("alice");
107
+ const lastOnline = await client.users.getLastOnline(userId);
108
+ ```
109
+
110
+ ---
111
+
112
+ ## Contacts
113
+
114
+ ```ts
115
+ const friends = await client.contacts.list({ count: 50, page: 1 });
116
+ await friends[0]?.send("Hey!");
117
+ await friends[0]?.setAlias("Teammate");
118
+
119
+ const online = await client.contacts.getOnline();
120
+ await client.contacts.sendRequest(userId, "Let's connect!");
121
+ ```
122
+
123
+ ---
124
+
125
+ ## Groups
126
+
127
+ ```ts
128
+ const groupSummaries = await client.groups.list();
129
+ const group = await client.groups.get(groupId);
130
+
131
+ await group.send("Hello everyone!");
132
+ await group.rename("Engineering Team");
133
+ await group.addMembers(["uid-1", "uid-2"]);
134
+
135
+ const history = await group.getMessages(20);
136
+ const members = await client.groups.getMembers(group.memberIds);
137
+ ```
138
+
139
+ ---
140
+
141
+ ## Conversations
142
+
143
+ ```ts
144
+ const conversation = await client.conversations.get(threadId);
145
+ await conversation.send("Hello!");
146
+ await conversation.sendTyping();
147
+ await conversation.pin(true);
148
+ await conversation.mute(3600);
149
+
150
+ const pinned = await client.conversations.getPinned();
151
+ const unread = await client.conversations.getUnread();
152
+ ```
153
+
154
+ ---
155
+
156
+ ## Media
157
+
158
+ ```ts
159
+ // Upload and inspect uploaded media metadata
160
+ const uploaded = await client.media.upload(groupId, "./document.pdf");
161
+
162
+ // Or send media directly with an optional caption
163
+ await client.media.send(groupId, "./document.pdf", "Weekly report");
164
+ ```
165
+
166
+ > **Note:** When uploading local image files by path, provide `imageMetadataGetter` in `new Zapind({ imageMetadataGetter })` or pass a `Buffer` with `{ data, filename, width, height }`.
167
+
168
+ ---
169
+
170
+ ## Events
171
+
172
+ ```ts
173
+ const unsubscribe = client.on("message", (msg) => {
174
+ console.log(msg.text, msg.sender.id, msg.timestamp);
175
+ });
176
+
177
+ client.on("reaction", (evt) => console.log("Reaction:", evt.icon));
178
+ client.on("group.update", (evt) => console.log("Group update:", evt.groupId, evt.type));
179
+ client.on("user.update", (evt) => console.log("User update:", evt.userId, evt.type));
180
+ client.on("connected", () => console.log("Connected"));
181
+ client.on("disconnected", ({ code }) => console.log("Disconnected:", code));
182
+ client.on("error", (err) => console.error("Error:", err.code, err.message));
183
+
184
+ client.startListening({ retryOnClose: true });
185
+
186
+ // Later:
187
+ unsubscribe();
188
+ client.stopListening();
189
+ ```
190
+
191
+ ---
192
+
193
+ ## Sessions
194
+
195
+ Zapind ships with 3 built-in `SessionStore` implementations:
196
+
197
+ - **`MemorySessionStore`** (default) — in-memory only.
198
+ - **`FileSessionStore`** (shorthand: `session: "./session.json"`) — atomic writes with `0o600` permissions on POSIX.
199
+ - **`EncryptedFileSessionStore`** — AES-256-GCM encryption at rest:
200
+
201
+ ```ts
202
+ import { Zapind, EncryptedFileSessionStore } from "zapind";
203
+
204
+ const client = new Zapind({
205
+ session: new EncryptedFileSessionStore("./session.enc.json", process.env.SESSION_KEY!),
206
+ });
207
+ ```
208
+
209
+ To log out and clear local session state:
210
+
211
+ ```ts
212
+ await client.logout({ clearSession: true });
213
+ ```
214
+
215
+ ---
216
+
217
+ ## Error Handling
218
+
219
+ All errors inherit from `ZapindError`:
220
+
221
+ ```ts
222
+ import {
223
+ ZapindError,
224
+ AuthenticationError,
225
+ SessionError,
226
+ NetworkError,
227
+ RateLimitError,
228
+ ValidationError,
229
+ PermissionError,
230
+ NotFoundError,
231
+ APIError,
232
+ } from "zapind";
233
+
234
+ try {
235
+ await client.messages.send(groupId, "Hello");
236
+ } catch (err) {
237
+ if (err instanceof RateLimitError) {
238
+ console.error("Rate limited, retry after:", err.retryAfterMs);
239
+ } else if (err instanceof SessionError) {
240
+ console.error("Session expired, re-login required");
241
+ } else if (err instanceof ZapindError) {
242
+ console.error(err.code, err.message);
243
+ }
244
+ }
245
+ ```
246
+
247
+ ---
248
+
249
+ ## Advanced / Raw API
250
+
251
+ Need a niche or low-level `zca-js` API not yet abstracted? Use `client.raw`:
252
+
253
+ ```ts
254
+ import type { API } from "zca-js";
255
+
256
+ const raw = client.raw as API;
257
+ const polls = await raw.getPollDetail("poll-id");
258
+ ```
259
+
260
+ ---
261
+
262
+ ## License
263
+
264
+ GNU GENERAL PUBLIC LICENSE v3
265
+
266
+ ## More
267
+ This project is based on zca-js,
268
+ originally licensed under the MIT License.
269
+
270
+ Zapind is distributed under the GNU GPL v3.0.