zapind 0.0.0-stage → 0.1.1
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/LICENSE +56 -0
- package/README.md +270 -3
- package/dist/index.cjs +2551 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1132 -0
- package/dist/index.d.ts +1132 -0
- package/dist/index.js +2520 -0
- package/dist/index.js.map +1 -0
- package/package.json +63 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
|
2
|
+
Version 3, 29 June 2007
|
|
3
|
+
|
|
4
|
+
Copyright © 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
|
5
|
+
|
|
6
|
+
Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
|
|
7
|
+
|
|
8
|
+
This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below.
|
|
9
|
+
|
|
10
|
+
0. Additional Definitions.
|
|
11
|
+
As used herein, “this License” refers to version 3 of the GNU Lesser General Public License, and the “GNU GPL” refers to version 3 of the GNU General Public License.
|
|
12
|
+
|
|
13
|
+
“The Library” refers to a covered work governed by this License, other than an Application or a Combined Work as defined below.
|
|
14
|
+
|
|
15
|
+
An “Application” is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library.
|
|
16
|
+
|
|
17
|
+
A “Combined Work” is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the “Linked Version”.
|
|
18
|
+
|
|
19
|
+
The “Minimal Corresponding Source” for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version.
|
|
20
|
+
|
|
21
|
+
The “Corresponding Application Code” for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work.
|
|
22
|
+
|
|
23
|
+
1. Exception to Section 3 of the GNU GPL.
|
|
24
|
+
You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL.
|
|
25
|
+
|
|
26
|
+
2. Conveying Modified Versions.
|
|
27
|
+
If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version:
|
|
28
|
+
|
|
29
|
+
a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or
|
|
30
|
+
b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy.
|
|
31
|
+
3. Object Code Incorporating Material from Library Header Files.
|
|
32
|
+
The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following:
|
|
33
|
+
|
|
34
|
+
a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License.
|
|
35
|
+
b) Accompany the object code with a copy of the GNU GPL and this license document.
|
|
36
|
+
4. Combined Works.
|
|
37
|
+
You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following:
|
|
38
|
+
|
|
39
|
+
a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License.
|
|
40
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license document.
|
|
41
|
+
c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document.
|
|
42
|
+
d) Do one of the following:
|
|
43
|
+
0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.
|
|
44
|
+
1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version.
|
|
45
|
+
e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.)
|
|
46
|
+
5. Combined Libraries.
|
|
47
|
+
You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following:
|
|
48
|
+
|
|
49
|
+
a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License.
|
|
50
|
+
b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work.
|
|
51
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
|
52
|
+
The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns.
|
|
53
|
+
|
|
54
|
+
Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation.
|
|
55
|
+
|
|
56
|
+
If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library.
|
package/README.md
CHANGED
|
@@ -1,3 +1,270 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
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 LESSER 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 LGPL v3.0.
|