wuzapi 1.3.1 → 1.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/README.md CHANGED
@@ -26,6 +26,8 @@ yarn add wuzapi
26
26
 
27
27
  ## Quick Start
28
28
 
29
+ ### Traditional Usage (Global Token)
30
+
29
31
  ```typescript
30
32
  import WuzapiClient from "wuzapi";
31
33
 
@@ -52,6 +54,48 @@ console.log("Connected:", status.Connected);
52
54
  console.log("Logged In:", status.LoggedIn);
53
55
  ```
54
56
 
57
+ ### Flexible Token Usage
58
+
59
+ ```typescript
60
+ import WuzapiClient from "wuzapi";
61
+
62
+ // Create client without token (or with admin token)
63
+ const client = new WuzapiClient({
64
+ apiUrl: "http://localhost:8080",
65
+ token: "admin-token", // Optional: admin token as default
66
+ });
67
+
68
+ // Use specific user token for operations
69
+ const userToken = "user-specific-token";
70
+
71
+ // Connect with user token
72
+ await client.session.connect(
73
+ {
74
+ Subscribe: ["Message", "ReadReceipt"],
75
+ Immediate: false,
76
+ },
77
+ { token: userToken }
78
+ );
79
+
80
+ // Send message with user token
81
+ await client.chat.sendText(
82
+ {
83
+ Phone: "5491155554444",
84
+ Body: "Hello from user-specific token!",
85
+ },
86
+ { token: userToken }
87
+ );
88
+
89
+ // Admin operations with admin token (uses default)
90
+ const users = await client.admin.listUsers();
91
+
92
+ // Or explicitly use admin token
93
+ const newUser = await client.admin.addUser(
94
+ { name: "John", token: "new-user-token" },
95
+ { token: "admin-token" }
96
+ );
97
+ ```
98
+
55
99
  ## Configuration
56
100
 
57
101
  The client requires a configuration object with the following properties:
@@ -59,8 +103,45 @@ The client requires a configuration object with the following properties:
59
103
  ```typescript
60
104
  interface WuzapiConfig {
61
105
  apiUrl: string; // The WuzAPI server URL
62
- token: string; // Your user authentication token
106
+ token?: string; // Your user authentication token (optional)
63
107
  }
108
+
109
+ interface RequestOptions {
110
+ token?: string; // Token override for specific requests
111
+ }
112
+ ```
113
+
114
+ ### Authentication Options
115
+
116
+ You have two ways to handle authentication:
117
+
118
+ 1. **Global Token**: Set a default token in the client configuration
119
+ 2. **Per-Request Token**: Override the token for specific API calls
120
+
121
+ ```typescript
122
+ // Option 1: Global token (traditional usage)
123
+ const client = new WuzapiClient({
124
+ apiUrl: "http://localhost:8080",
125
+ token: "your-default-token",
126
+ });
127
+
128
+ // Option 2: No global token, specify per request
129
+ const client = new WuzapiClient({
130
+ apiUrl: "http://localhost:8080",
131
+ // token is optional
132
+ });
133
+
134
+ // Option 3: Global token with per-request overrides
135
+ const client = new WuzapiClient({
136
+ apiUrl: "http://localhost:8080",
137
+ token: "admin-token", // Default admin token
138
+ });
139
+
140
+ // Use different token for specific operations
141
+ await client.chat.sendText(
142
+ { Phone: "5491155554444", Body: "Hello!" },
143
+ { token: "user-specific-token" }
144
+ );
64
145
  ```
65
146
 
66
147
  ## API Modules
@@ -109,13 +190,22 @@ await client.session.configureS3({
109
190
  Send messages and manage chat interactions.
110
191
 
111
192
  ```typescript
112
- // Send text message
193
+ // Send text message (using global token)
113
194
  await client.chat.sendText({
114
195
  Phone: "5491155554444",
115
196
  Body: "Hello World!",
116
197
  Id: "optional-message-id",
117
198
  });
118
199
 
200
+ // Send with specific token override
201
+ await client.chat.sendText(
202
+ {
203
+ Phone: "5491155554444",
204
+ Body: "Hello with custom token!",
205
+ },
206
+ { token: "user-specific-token" }
207
+ );
208
+
119
209
  // Reply to a message
120
210
  await client.chat.sendText({
121
211
  Phone: "5491155554444",
@@ -247,7 +337,7 @@ await client.group.removePhoto("120362023605733675@g.us");
247
337
  Manage users (requires admin token).
248
338
 
249
339
  ```typescript
250
- // Create admin client
340
+ // Option 1: Create dedicated admin client
251
341
  const adminClient = new WuzapiClient({
252
342
  apiUrl: "http://localhost:8080",
253
343
  token: "your-admin-token",
@@ -256,31 +346,43 @@ const adminClient = new WuzapiClient({
256
346
  // List all users
257
347
  const users = await adminClient.admin.listUsers();
258
348
 
349
+ // Option 2: Use shared client with token override
350
+ const client = new WuzapiClient({
351
+ apiUrl: "http://localhost:8080",
352
+ // No default token or user token as default
353
+ });
354
+
355
+ // Admin operations with explicit admin token
356
+ const users = await client.admin.listUsers({ token: "admin-token" });
357
+
259
358
  // Add new user
260
- const newUser = await adminClient.admin.addUser({
261
- name: "John Doe",
262
- token: "user-token-123",
263
- webhook: "https://example.com/webhook",
264
- events: "Message,ReadReceipt",
265
- proxyConfig: {
266
- enabled: true,
267
- proxyURL: "socks5://user:pass@proxy:port",
268
- },
269
- s3Config: {
270
- enabled: true,
271
- endpoint: "https://s3.amazonaws.com",
272
- region: "us-east-1",
273
- bucket: "user-media-bucket",
274
- accessKey: "AKIA...",
275
- secretKey: "secret...",
276
- pathStyle: false,
277
- mediaDelivery: "both",
278
- retentionDays: 30,
359
+ const newUser = await client.admin.addUser(
360
+ {
361
+ name: "John Doe",
362
+ token: "user-token-123",
363
+ webhook: "https://example.com/webhook",
364
+ events: "Message,ReadReceipt",
365
+ proxyConfig: {
366
+ enabled: true,
367
+ proxyURL: "socks5://user:pass@proxy:port",
368
+ },
369
+ s3Config: {
370
+ enabled: true,
371
+ endpoint: "https://s3.amazonaws.com",
372
+ region: "us-east-1",
373
+ bucket: "user-media-bucket",
374
+ accessKey: "AKIA...",
375
+ secretKey: "secret...",
376
+ pathStyle: false,
377
+ mediaDelivery: "both",
378
+ retentionDays: 30,
379
+ },
279
380
  },
280
- });
381
+ { token: "admin-token" }
382
+ );
281
383
 
282
384
  // Delete user
283
- await adminClient.admin.deleteUser(2);
385
+ await client.admin.deleteUser(2, { token: "admin-token" });
284
386
  ```
285
387
 
286
388
  ### Webhook Module
@@ -1235,6 +1337,42 @@ try {
1235
1337
  }
1236
1338
  ```
1237
1339
 
1340
+ ### Token Authentication Errors
1341
+
1342
+ When no token is provided (either globally or per-request), the library will throw a specific error:
1343
+
1344
+ ```typescript
1345
+ import { WuzapiError } from "wuzapi";
1346
+
1347
+ // Client without global token
1348
+ const client = new WuzapiClient({
1349
+ apiUrl: "http://localhost:8080",
1350
+ // No token provided
1351
+ });
1352
+
1353
+ try {
1354
+ // This will fail - no token provided
1355
+ await client.chat.sendText({
1356
+ Phone: "5491155554444",
1357
+ Body: "This will fail",
1358
+ });
1359
+ } catch (error) {
1360
+ if (error instanceof WuzapiError && error.code === 401) {
1361
+ console.error("Authentication required:", error.message);
1362
+ // "No authentication token provided. Either set a token in the client config or provide one in the request options."
1363
+ }
1364
+ }
1365
+
1366
+ // Fix by providing token
1367
+ await client.chat.sendText(
1368
+ {
1369
+ Phone: "5491155554444",
1370
+ Body: "Now it works!",
1371
+ },
1372
+ { token: "your-token" }
1373
+ );
1374
+ ```
1375
+
1238
1376
  ### Error Types
1239
1377
 
1240
1378
  - **Network Errors**: Connection issues, timeouts
package/dist/client.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { AxiosInstance } from 'axios';
2
- import { WuzapiConfig } from './types/common.js';
2
+ import { WuzapiConfig, RequestOptions } from './types/common.js';
3
3
  export declare class WuzapiError extends Error {
4
4
  code: number;
5
5
  details?: unknown;
@@ -9,8 +9,13 @@ export declare class BaseClient {
9
9
  protected axios: AxiosInstance;
10
10
  protected config: WuzapiConfig;
11
11
  constructor(config: WuzapiConfig);
12
- protected request<T>(method: "GET" | "POST" | "DELETE", endpoint: string, data?: unknown): Promise<T>;
13
- protected get<T>(endpoint: string): Promise<T>;
14
- protected post<T>(endpoint: string, data?: unknown): Promise<T>;
15
- protected delete<T>(endpoint: string): Promise<T>;
12
+ /**
13
+ * Resolve the token from request options or instance config
14
+ * Throws an error if no token is available
15
+ */
16
+ private resolveToken;
17
+ protected request<T>(method: "GET" | "POST" | "DELETE", endpoint: string, data?: unknown, options?: RequestOptions): Promise<T>;
18
+ protected get<T>(endpoint: string, options?: RequestOptions): Promise<T>;
19
+ protected post<T>(endpoint: string, data?: unknown, options?: RequestOptions): Promise<T>;
20
+ protected delete<T>(endpoint: string, options?: RequestOptions): Promise<T>;
16
21
  }