@catalisa/wpp-sdk 0.2.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 +486 -0
- package/dist/client.d.ts +81 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +97 -0
- package/dist/client.js.map +1 -0
- package/dist/errors/index.d.ts +42 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +70 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/http/client.d.ts +48 -0
- package/dist/http/client.d.ts.map +1 -0
- package/dist/http/client.js +125 -0
- package/dist/http/client.js.map +1 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +66 -0
- package/dist/index.js.map +1 -0
- package/dist/resources/apiTokens.d.ts +29 -0
- package/dist/resources/apiTokens.d.ts.map +1 -0
- package/dist/resources/apiTokens.js +38 -0
- package/dist/resources/apiTokens.js.map +1 -0
- package/dist/resources/auth.d.ts +29 -0
- package/dist/resources/auth.d.ts.map +1 -0
- package/dist/resources/auth.js +43 -0
- package/dist/resources/auth.js.map +1 -0
- package/dist/resources/branding.d.ts +27 -0
- package/dist/resources/branding.d.ts.map +1 -0
- package/dist/resources/branding.js +36 -0
- package/dist/resources/branding.js.map +1 -0
- package/dist/resources/health.d.ts +16 -0
- package/dist/resources/health.d.ts.map +1 -0
- package/dist/resources/health.js +19 -0
- package/dist/resources/health.js.map +1 -0
- package/dist/resources/tenants.d.ts +36 -0
- package/dist/resources/tenants.d.ts.map +1 -0
- package/dist/resources/tenants.js +51 -0
- package/dist/resources/tenants.js.map +1 -0
- package/dist/resources/users.d.ts +33 -0
- package/dist/resources/users.d.ts.map +1 -0
- package/dist/resources/users.js +46 -0
- package/dist/resources/users.js.map +1 -0
- package/dist/resources/webhooks.d.ts +113 -0
- package/dist/resources/webhooks.d.ts.map +1 -0
- package/dist/resources/webhooks.js +216 -0
- package/dist/resources/webhooks.js.map +1 -0
- package/dist/resources/whatsapp.d.ts +121 -0
- package/dist/resources/whatsapp.d.ts.map +1 -0
- package/dist/resources/whatsapp.js +183 -0
- package/dist/resources/whatsapp.js.map +1 -0
- package/dist/resources/whatsappAdmin.d.ts +110 -0
- package/dist/resources/whatsappAdmin.d.ts.map +1 -0
- package/dist/resources/whatsappAdmin.js +140 -0
- package/dist/resources/whatsappAdmin.js.map +1 -0
- package/dist/types/admin.d.ts +57 -0
- package/dist/types/admin.d.ts.map +1 -0
- package/dist/types/admin.js +6 -0
- package/dist/types/admin.js.map +1 -0
- package/dist/types/auth.d.ts +31 -0
- package/dist/types/auth.d.ts.map +1 -0
- package/dist/types/auth.js +6 -0
- package/dist/types/auth.js.map +1 -0
- package/dist/types/branding.d.ts +30 -0
- package/dist/types/branding.d.ts.map +1 -0
- package/dist/types/branding.js +6 -0
- package/dist/types/branding.js.map +1 -0
- package/dist/types/common.d.ts +27 -0
- package/dist/types/common.d.ts.map +1 -0
- package/dist/types/common.js +6 -0
- package/dist/types/common.js.map +1 -0
- package/dist/types/index.d.ts +10 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +26 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/webhook.d.ts +126 -0
- package/dist/types/webhook.d.ts.map +1 -0
- package/dist/types/webhook.js +6 -0
- package/dist/types/webhook.js.map +1 -0
- package/dist/types/whatsapp.d.ts +154 -0
- package/dist/types/whatsapp.d.ts.map +1 -0
- package/dist/types/whatsapp.js +6 -0
- package/dist/types/whatsapp.js.map +1 -0
- package/package.json +49 -0
package/README.md
ADDED
|
@@ -0,0 +1,486 @@
|
|
|
1
|
+
# @wpp/sdk
|
|
2
|
+
|
|
3
|
+
TypeScript SDK for WhatsApp Multi-tenant API - Clean, type-safe, and easy to use.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @wpp/sdk
|
|
9
|
+
# or
|
|
10
|
+
yarn add @wpp/sdk
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
### Admin Client (JWT Authentication)
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { WhatsAppAPIClient } from '@wpp/sdk';
|
|
19
|
+
|
|
20
|
+
const admin = new WhatsAppAPIClient({
|
|
21
|
+
baseURL: 'https://api.example.com'
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
// Login
|
|
25
|
+
const { access_token, user } = await admin.login('admin@example.com', 'password');
|
|
26
|
+
console.log('Logged in as:', user.email);
|
|
27
|
+
|
|
28
|
+
// Create tenant
|
|
29
|
+
const tenant = await admin.tenants.create({ name: 'Acme Corp' });
|
|
30
|
+
|
|
31
|
+
// Create API token for tenant
|
|
32
|
+
const token = await admin.apiTokens.create(tenant.id, {
|
|
33
|
+
name: 'Production Token',
|
|
34
|
+
isTest: false
|
|
35
|
+
});
|
|
36
|
+
console.log('API Key:', token.key); // Store this securely!
|
|
37
|
+
|
|
38
|
+
// Connect device for tenant
|
|
39
|
+
await admin.whatsappAdmin.connect(tenant.id);
|
|
40
|
+
|
|
41
|
+
// Get QR code
|
|
42
|
+
const { qr } = await admin.whatsappAdmin.getQR(tenant.id);
|
|
43
|
+
console.log('Scan this QR:', qr);
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### User Client (API Token Authentication)
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
import { WhatsAppAPIClient } from '@wpp/sdk';
|
|
50
|
+
|
|
51
|
+
const client = new WhatsAppAPIClient({
|
|
52
|
+
baseURL: 'https://api.example.com',
|
|
53
|
+
auth: {
|
|
54
|
+
type: 'apiToken',
|
|
55
|
+
token: 'wpp_live_abc123_xyz789'
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// Connect session
|
|
60
|
+
const { correlationId } = await client.whatsapp.connect({
|
|
61
|
+
callbackUrl: 'https://myapp.com/webhook'
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// Send message
|
|
65
|
+
await client.whatsapp.sendMessage({
|
|
66
|
+
jid: '5511999999999@s.whatsapp.net',
|
|
67
|
+
text: 'Hello from SDK!'
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// Send media
|
|
71
|
+
await client.whatsapp.sendMedia({
|
|
72
|
+
jid: '5511999999999@s.whatsapp.net',
|
|
73
|
+
type: 'image',
|
|
74
|
+
url: 'https://example.com/photo.jpg',
|
|
75
|
+
caption: 'Check this out!'
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// List groups
|
|
79
|
+
const groups = await client.whatsapp.listGroups('tenant-id');
|
|
80
|
+
console.log('Groups:', groups);
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Features
|
|
84
|
+
|
|
85
|
+
### ✨ Resource-Based API
|
|
86
|
+
|
|
87
|
+
Clean, intuitive API organized by domain:
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
client.auth // Authentication
|
|
91
|
+
client.users // User management (admin)
|
|
92
|
+
client.tenants // Tenant management (admin)
|
|
93
|
+
client.apiTokens // API token management (admin)
|
|
94
|
+
client.whatsapp // WhatsApp operations (API token auth)
|
|
95
|
+
client.whatsappAdmin // WhatsApp admin operations (JWT auth)
|
|
96
|
+
client.webhooks // Webhook management
|
|
97
|
+
client.branding // Branding customization
|
|
98
|
+
client.health // Health check
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### 🔒 Dual Authentication Support
|
|
102
|
+
|
|
103
|
+
**JWT (Admin endpoints)**
|
|
104
|
+
```typescript
|
|
105
|
+
const client = new WhatsAppAPIClient({
|
|
106
|
+
baseURL: 'https://api.example.com',
|
|
107
|
+
auth: { type: 'jwt', token: 'your-jwt-token' }
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// Or login to get token automatically
|
|
111
|
+
await client.login('admin@example.com', 'password');
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
**API Token (WhatsApp endpoints)**
|
|
115
|
+
```typescript
|
|
116
|
+
const client = new WhatsAppAPIClient({
|
|
117
|
+
baseURL: 'https://api.example.com',
|
|
118
|
+
auth: { type: 'apiToken', token: 'wpp_live_xxx' }
|
|
119
|
+
});
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### 📝 Full TypeScript Support
|
|
123
|
+
|
|
124
|
+
100% typed with IntelliSense support:
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
import { SendMessageRequest, Group, AdvancedWebhook } from '@wpp/sdk';
|
|
128
|
+
|
|
129
|
+
const message: SendMessageRequest = {
|
|
130
|
+
jid: '5511999999999@s.whatsapp.net',
|
|
131
|
+
text: 'Hello!',
|
|
132
|
+
callbackUrl: 'https://myapp.com/callback'
|
|
133
|
+
};
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### ⚠️ Comprehensive Error Handling
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
import { APIError, AuthError, ValidationError, NotFoundError } from '@wpp/sdk';
|
|
140
|
+
|
|
141
|
+
try {
|
|
142
|
+
await client.whatsapp.send({ jid: 'invalid', text: 'test' });
|
|
143
|
+
} catch (error) {
|
|
144
|
+
if (error instanceof ValidationError) {
|
|
145
|
+
console.error('Validation failed:', error.errors);
|
|
146
|
+
} else if (error instanceof AuthError) {
|
|
147
|
+
console.error('Auth failed, status:', error.statusCode);
|
|
148
|
+
} else if (error instanceof APIError) {
|
|
149
|
+
console.error('API error:', error.message);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## Testing
|
|
155
|
+
|
|
156
|
+
The SDK includes comprehensive test suites demonstrating all features:
|
|
157
|
+
|
|
158
|
+
```bash
|
|
159
|
+
# Run all tests
|
|
160
|
+
npm run test:real # Complete integration tests
|
|
161
|
+
npm run test:messaging # Messaging operations only
|
|
162
|
+
npm run test:groups # Group operations only
|
|
163
|
+
npm run test:tenant # Specific tenant tests
|
|
164
|
+
npm run test:admin-crud # Admin CRUD (users, tenants)
|
|
165
|
+
npm run test:branding # Branding configuration
|
|
166
|
+
npm run test:webhooks # Basic webhooks
|
|
167
|
+
|
|
168
|
+
# Or run directly
|
|
169
|
+
npx ts-node -r tsconfig-paths/register examples/test-real.ts
|
|
170
|
+
npx ts-node -r tsconfig-paths/register examples/test-messaging.ts
|
|
171
|
+
npx ts-node -r tsconfig-paths/register examples/test-groups.ts
|
|
172
|
+
npx ts-node -r tsconfig-paths/register examples/test-admin-crud.ts
|
|
173
|
+
npx ts-node -r tsconfig-paths/register examples/test-branding.ts
|
|
174
|
+
npx ts-node -r tsconfig-paths/register examples/test-basic-webhooks.ts
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
**Test Coverage:**
|
|
178
|
+
- ✅ Authentication (JWT + API Token)
|
|
179
|
+
- ✅ **Admin CRUD Operations (users, tenants) - NEW!**
|
|
180
|
+
- ✅ Admin Operations (API tokens)
|
|
181
|
+
- ✅ Messaging (text messages)
|
|
182
|
+
- ✅ Group Operations (create, update, lifecycle testing with polling)
|
|
183
|
+
- ✅ Advanced Webhooks (admin CRUD, filtering, logs, stats)
|
|
184
|
+
- ✅ **Basic Webhooks (CRUD) - NEW!**
|
|
185
|
+
- ✅ Device Management (connect, disconnect, reset)
|
|
186
|
+
- ✅ **Branding Configuration (CRUD) - NEW!**
|
|
187
|
+
- ⏭️ Media Operations (deferred to next phase)
|
|
188
|
+
|
|
189
|
+
**Test Results:**
|
|
190
|
+
|
|
191
|
+
| Test File | Results | Coverage |
|
|
192
|
+
|-----------|---------|----------|
|
|
193
|
+
| `test-real.ts` | 19/20 passing (95%) | Admin ops, webhooks, device mgmt |
|
|
194
|
+
| `test-messaging.ts` | 7/10 passing (70%, 3 skipped) | Text messages, callbacks |
|
|
195
|
+
| `test-groups.ts` | **21/22 passing (95.5%)** | Complete group lifecycle |
|
|
196
|
+
| `test-specific-tenant.ts` | 7/7 passing (100%) | Device operations |
|
|
197
|
+
| **`test-admin-crud.ts`** | **17/17 passing (100%)** 🆕 | User & Tenant CRUD |
|
|
198
|
+
| **`test-branding.ts`** | **6/6 passing (100%)** 🆕 | Branding config |
|
|
199
|
+
| **`test-basic-webhooks.ts`** | **8/8 passing (100%)** 🆕 | Basic webhook CRUD |
|
|
200
|
+
|
|
201
|
+
**Total Coverage:**
|
|
202
|
+
- **Endpoints Tested: 70/108 (64.8%)** ⬆️ from 53.7%
|
|
203
|
+
- **Phase 1 Complete**: +12 endpoints tested
|
|
204
|
+
- **All new tests**: 100% passing 🎉
|
|
205
|
+
|
|
206
|
+
**Test Highlights:**
|
|
207
|
+
- ✅ Complete lifecycle testing with proper async operation polling
|
|
208
|
+
- ✅ Automatic cleanup with try/finally blocks
|
|
209
|
+
- ✅ Comprehensive CRUD coverage for admin resources
|
|
210
|
+
- ✅ Multi-webhook testing
|
|
211
|
+
- ✅ User-tenant integration testing
|
|
212
|
+
|
|
213
|
+
**Improvements in test-groups.ts:**
|
|
214
|
+
- Added `waitForOperation()` helper to poll async operations until completion
|
|
215
|
+
- Added `checkDeviceReady()` helper to verify device status before tests
|
|
216
|
+
- Refactored both user and admin tests with proper polling and group ID extraction
|
|
217
|
+
- Added try/finally cleanup blocks to ensure test groups are deleted
|
|
218
|
+
- Complete lifecycle testing: create → update → verify → cleanup
|
|
219
|
+
|
|
220
|
+
## Examples
|
|
221
|
+
|
|
222
|
+
### Send Message with Callback
|
|
223
|
+
|
|
224
|
+
```typescript
|
|
225
|
+
const { correlationId } = await client.whatsapp.sendMessage({
|
|
226
|
+
jid: '5511999999999@s.whatsapp.net',
|
|
227
|
+
text: 'Hello!',
|
|
228
|
+
callbackUrl: 'https://myapp.com/callback',
|
|
229
|
+
correlationId: 'my-custom-id-123'
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
// Later, check operation status
|
|
233
|
+
const operation = await client.whatsapp.getOperation(correlationId);
|
|
234
|
+
console.log('Status:', operation.status); // 'pending', 'completed', or 'failed'
|
|
235
|
+
console.log('Result:', operation.result);
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
### Group Management
|
|
239
|
+
|
|
240
|
+
```typescript
|
|
241
|
+
// Create group
|
|
242
|
+
const { correlationId } = await client.whatsapp.createGroup({
|
|
243
|
+
subject: 'My Group',
|
|
244
|
+
participants: ['5511111111111@s.whatsapp.net', '5522222222222@s.whatsapp.net']
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
const operation = await client.whatsapp.getOperation(correlationId);
|
|
248
|
+
const groupId = operation.result.gid;
|
|
249
|
+
|
|
250
|
+
// Update group subject
|
|
251
|
+
await client.whatsapp.updateGroupSubject({
|
|
252
|
+
gid: groupId,
|
|
253
|
+
subject: 'New Group Name'
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
// Add participants
|
|
257
|
+
await client.whatsapp.addGroupParticipants({
|
|
258
|
+
gid: groupId,
|
|
259
|
+
participants: ['5533333333333@s.whatsapp.net']
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
// Promote to admin
|
|
263
|
+
await client.whatsapp.promoteGroupParticipants({
|
|
264
|
+
gid: groupId,
|
|
265
|
+
participants: ['5533333333333@s.whatsapp.net']
|
|
266
|
+
});
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
### Advanced Webhooks
|
|
270
|
+
|
|
271
|
+
```typescript
|
|
272
|
+
// Create advanced webhook with filtering
|
|
273
|
+
const webhook = await client.webhooks.create({
|
|
274
|
+
name: 'Group Messages Only',
|
|
275
|
+
urls: ['https://api.example.com/webhook'],
|
|
276
|
+
eventTypes: ['messages.upsert'],
|
|
277
|
+
filters: {
|
|
278
|
+
remoteJid: '120363XXX@g.us', // Specific group
|
|
279
|
+
fromMe: false, // Only incoming messages
|
|
280
|
+
textContains: 'urgent' // Only messages containing 'urgent'
|
|
281
|
+
},
|
|
282
|
+
secret: 'my-webhook-secret',
|
|
283
|
+
retryCount: 3,
|
|
284
|
+
timeoutMs: 10000
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
// Get webhook statistics
|
|
288
|
+
const stats = await client.webhooks.getStats(webhook.id);
|
|
289
|
+
console.log('Trigger count:', stats.triggerCount);
|
|
290
|
+
console.log('Success rate:', stats.successRate);
|
|
291
|
+
console.log('Avg response time:', stats.avgResponseTimeMs);
|
|
292
|
+
|
|
293
|
+
// Get delivery logs
|
|
294
|
+
const logs = await client.webhooks.getLogs(webhook.id, {
|
|
295
|
+
success: false, // Only failed deliveries
|
|
296
|
+
limit: 50
|
|
297
|
+
});
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
### Admin Operations
|
|
301
|
+
|
|
302
|
+
```typescript
|
|
303
|
+
// List all devices
|
|
304
|
+
const devices = await admin.whatsappAdmin.listDevices();
|
|
305
|
+
|
|
306
|
+
// Connect device for specific tenant
|
|
307
|
+
await admin.whatsappAdmin.connect('tenant-123');
|
|
308
|
+
|
|
309
|
+
// Send message as tenant
|
|
310
|
+
await admin.whatsappAdmin.sendMessage('tenant-123', {
|
|
311
|
+
jid: '5511999999999@s.whatsapp.net',
|
|
312
|
+
text: 'Admin sending message as tenant'
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
// Generate QR link for support
|
|
316
|
+
const qrLink = await admin.whatsappAdmin.generateQRLink('tenant-123', {
|
|
317
|
+
expirationHours: 24
|
|
318
|
+
});
|
|
319
|
+
console.log('Share this link:', qrLink.url);
|
|
320
|
+
|
|
321
|
+
// Send QR link to user via WhatsApp
|
|
322
|
+
await admin.whatsappAdmin.sendQRLink('tenant-123', {
|
|
323
|
+
recipientTenantId: 'tenant-456',
|
|
324
|
+
recipientJid: '5511999999999@s.whatsapp.net'
|
|
325
|
+
});
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
### Branding Customization
|
|
329
|
+
|
|
330
|
+
```typescript
|
|
331
|
+
// Update branding for tenant
|
|
332
|
+
await admin.branding.update({
|
|
333
|
+
tenantId: 'tenant-123',
|
|
334
|
+
companyName: 'Acme Corp',
|
|
335
|
+
primaryColor: '#FF5722',
|
|
336
|
+
logoUrl: 'https://example.com/logo.png',
|
|
337
|
+
headerMessage: 'Welcome to Acme Support',
|
|
338
|
+
footerMessage: 'Powered by Acme Corp'
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
// Get public branding (no auth required)
|
|
342
|
+
const branding = await client.branding.get('tenant-123');
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
### User Management
|
|
346
|
+
|
|
347
|
+
```typescript
|
|
348
|
+
// Create user
|
|
349
|
+
const user = await admin.users.create({
|
|
350
|
+
email: 'user@example.com',
|
|
351
|
+
password: 'securepassword',
|
|
352
|
+
role: 'user',
|
|
353
|
+
tenantId: 'tenant-123'
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
// List all users
|
|
357
|
+
const users = await admin.users.list();
|
|
358
|
+
|
|
359
|
+
// Update user role
|
|
360
|
+
await admin.users.update(user.id, {
|
|
361
|
+
role: 'admin'
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
// Delete user
|
|
365
|
+
await admin.users.delete(user.id);
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
### Health Check
|
|
369
|
+
|
|
370
|
+
```typescript
|
|
371
|
+
const health = await client.health.check();
|
|
372
|
+
console.log('API Status:', health.status); // 'ok'
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
## Configuration
|
|
376
|
+
|
|
377
|
+
### Advanced Configuration
|
|
378
|
+
|
|
379
|
+
```typescript
|
|
380
|
+
const client = new WhatsAppAPIClient({
|
|
381
|
+
baseURL: 'https://api.example.com',
|
|
382
|
+
timeout: 60000, // 60 seconds (default: 30000)
|
|
383
|
+
|
|
384
|
+
// Interceptors
|
|
385
|
+
onRequest: async (config) => {
|
|
386
|
+
console.log('Request:', config.method, config.url);
|
|
387
|
+
return config;
|
|
388
|
+
},
|
|
389
|
+
|
|
390
|
+
onResponse: async (response) => {
|
|
391
|
+
console.log('Response:', response.status);
|
|
392
|
+
return response;
|
|
393
|
+
},
|
|
394
|
+
|
|
395
|
+
onError: (error) => {
|
|
396
|
+
console.error('API Error:', error.message);
|
|
397
|
+
}
|
|
398
|
+
});
|
|
399
|
+
```
|
|
400
|
+
|
|
401
|
+
### Dynamic Auth Updates
|
|
402
|
+
|
|
403
|
+
```typescript
|
|
404
|
+
const client = new WhatsAppAPIClient({
|
|
405
|
+
baseURL: 'https://api.example.com'
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
// Login and set JWT
|
|
409
|
+
await client.login('admin@example.com', 'password');
|
|
410
|
+
|
|
411
|
+
// Switch to API token
|
|
412
|
+
client.setAuth('wpp_live_abc123', 'apiToken');
|
|
413
|
+
|
|
414
|
+
// Clear auth
|
|
415
|
+
client.clearAuth();
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
## API Reference
|
|
419
|
+
|
|
420
|
+
### Authentication
|
|
421
|
+
- `auth.login(email, password)` - Login with credentials
|
|
422
|
+
- `auth.forgotPassword(email)` - Request password reset
|
|
423
|
+
- `auth.resetPassword(token, newPassword)` - Reset password
|
|
424
|
+
- `auth.me()` - Get current user
|
|
425
|
+
|
|
426
|
+
### Users (Admin)
|
|
427
|
+
- `users.list()` - List all users
|
|
428
|
+
- `users.get(id)` - Get user by ID
|
|
429
|
+
- `users.create(data)` - Create user
|
|
430
|
+
- `users.update(id, data)` - Update user
|
|
431
|
+
- `users.delete(id)` - Delete user
|
|
432
|
+
|
|
433
|
+
### Tenants (Admin)
|
|
434
|
+
- `tenants.list()` - List all tenants
|
|
435
|
+
- `tenants.get(id)` - Get tenant by ID
|
|
436
|
+
- `tenants.create(data)` - Create tenant
|
|
437
|
+
- `tenants.update(id, data)` - Update tenant
|
|
438
|
+
- `tenants.delete(id)` - Delete tenant
|
|
439
|
+
|
|
440
|
+
### API Tokens (Admin)
|
|
441
|
+
- `apiTokens.list(tenantId)` - List tokens
|
|
442
|
+
- `apiTokens.create(tenantId, data)` - Create token
|
|
443
|
+
- `apiTokens.delete(tenantId, tokenId)` - Delete token
|
|
444
|
+
|
|
445
|
+
### WhatsApp (User)
|
|
446
|
+
- Session: `connect()`, `reconnect()`, `reset()`, `logout()`, `ping()`
|
|
447
|
+
- Messaging: `sendMessage()`, `sendReaction()`, `sendMedia()`
|
|
448
|
+
- Groups: `createGroup()`, `updateGroupSubject()`, `addGroupParticipants()`, etc.
|
|
449
|
+
- Queries: `listGroups()`, `listChats()`, `listContacts()`, `getOperation()`
|
|
450
|
+
|
|
451
|
+
### WhatsApp Admin
|
|
452
|
+
- Device: `listDevices()`, `getDevice()`, `getQR()`
|
|
453
|
+
- Session: `connect()`, `reconnect()`, `disconnect()`, `reset()`, `ping()`
|
|
454
|
+
- Operations: `sendMessage()`, `getOperation()`
|
|
455
|
+
- Groups: `listGroups()`, `createGroup()`, `manageGroupParticipants()`, etc.
|
|
456
|
+
|
|
457
|
+
### Webhooks
|
|
458
|
+
- Basic: `createBasic()`, `listBasic()`, `deleteBasic()`
|
|
459
|
+
- Advanced (Admin): `createAdvanced()`, `listAdvanced()`, `updateAdvanced()`, `deleteAdvanced()`
|
|
460
|
+
- Advanced (User): `create()`, `list()`, `update()`, `delete()`
|
|
461
|
+
- Logs & Stats: `getLogs()`, `getStats()`
|
|
462
|
+
|
|
463
|
+
### Branding
|
|
464
|
+
- `get(tenantId)` - Get branding (public)
|
|
465
|
+
- `update(data)` - Update branding (admin)
|
|
466
|
+
- `delete(tenantId)` - Delete branding (admin)
|
|
467
|
+
|
|
468
|
+
### Health
|
|
469
|
+
- `check()` - Health check
|
|
470
|
+
|
|
471
|
+
## Error Types
|
|
472
|
+
|
|
473
|
+
- `APIError` - Base API error
|
|
474
|
+
- `AuthError` - Authentication/authorization errors (401, 403)
|
|
475
|
+
- `ValidationError` - Validation errors (400, 422)
|
|
476
|
+
- `NotFoundError` - Not found errors (404)
|
|
477
|
+
- `RateLimitError` - Rate limit errors (429)
|
|
478
|
+
- `NetworkError` - Network/connection errors
|
|
479
|
+
|
|
480
|
+
## License
|
|
481
|
+
|
|
482
|
+
MIT
|
|
483
|
+
|
|
484
|
+
## Support
|
|
485
|
+
|
|
486
|
+
For issues and questions, visit the [GitHub repository](https://github.com/your-org/wpp-multitenant).
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { HTTPClientConfig } from './http/client';
|
|
2
|
+
import { AuthResource } from './resources/auth';
|
|
3
|
+
import { UsersResource } from './resources/users';
|
|
4
|
+
import { TenantsResource } from './resources/tenants';
|
|
5
|
+
import { ApiTokensResource } from './resources/apiTokens';
|
|
6
|
+
import { WhatsAppResource } from './resources/whatsapp';
|
|
7
|
+
import { WhatsAppAdminResource } from './resources/whatsappAdmin';
|
|
8
|
+
import { WebhooksResource } from './resources/webhooks';
|
|
9
|
+
import { BrandingResource } from './resources/branding';
|
|
10
|
+
import { HealthResource } from './resources/health';
|
|
11
|
+
export interface WhatsAppAPIClientConfig {
|
|
12
|
+
baseURL: string;
|
|
13
|
+
timeout?: number;
|
|
14
|
+
auth?: {
|
|
15
|
+
type: 'jwt' | 'apiToken';
|
|
16
|
+
token: string;
|
|
17
|
+
};
|
|
18
|
+
onRequest?: HTTPClientConfig['onRequest'];
|
|
19
|
+
onResponse?: HTTPClientConfig['onResponse'];
|
|
20
|
+
onError?: HTTPClientConfig['onError'];
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* WhatsApp Multi-tenant API Client
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* // Admin client (JWT authentication)
|
|
28
|
+
* const admin = new WhatsAppAPIClient({
|
|
29
|
+
* baseURL: 'https://api.example.com',
|
|
30
|
+
* auth: { type: 'jwt', token: 'your-jwt-token' }
|
|
31
|
+
* });
|
|
32
|
+
*
|
|
33
|
+
* await admin.auth.login('admin@example.com', 'password');
|
|
34
|
+
* const tenants = await admin.tenants.list();
|
|
35
|
+
* await admin.whatsappAdmin.connect('tenant-123');
|
|
36
|
+
*
|
|
37
|
+
* // User client (API Token authentication)
|
|
38
|
+
* const user = new WhatsAppAPIClient({
|
|
39
|
+
* baseURL: 'https://api.example.com',
|
|
40
|
+
* auth: { type: 'apiToken', token: 'wpp_live_xxx' }
|
|
41
|
+
* });
|
|
42
|
+
*
|
|
43
|
+
* await user.whatsapp.send({ jid: '5511999999999@s.whatsapp.net', text: 'Hello!' });
|
|
44
|
+
* const groups = await user.whatsapp.listGroups('tenant-id');
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export declare class WhatsAppAPIClient {
|
|
48
|
+
private httpClient;
|
|
49
|
+
readonly auth: AuthResource;
|
|
50
|
+
readonly users: UsersResource;
|
|
51
|
+
readonly tenants: TenantsResource;
|
|
52
|
+
readonly apiTokens: ApiTokensResource;
|
|
53
|
+
readonly whatsapp: WhatsAppResource;
|
|
54
|
+
readonly whatsappAdmin: WhatsAppAdminResource;
|
|
55
|
+
readonly webhooks: WebhooksResource;
|
|
56
|
+
readonly branding: BrandingResource;
|
|
57
|
+
readonly health: HealthResource;
|
|
58
|
+
constructor(config: WhatsAppAPIClientConfig);
|
|
59
|
+
/**
|
|
60
|
+
* Set authentication token
|
|
61
|
+
* @param token JWT or API token
|
|
62
|
+
* @param type Authentication type ('jwt' or 'apiToken')
|
|
63
|
+
*/
|
|
64
|
+
setAuth(token: string, type?: 'jwt' | 'apiToken'): void;
|
|
65
|
+
/**
|
|
66
|
+
* Clear authentication
|
|
67
|
+
*/
|
|
68
|
+
clearAuth(): void;
|
|
69
|
+
/**
|
|
70
|
+
* Login and automatically set JWT auth
|
|
71
|
+
* @param email User email
|
|
72
|
+
* @param password User password
|
|
73
|
+
* @returns Login response with user info and token
|
|
74
|
+
*/
|
|
75
|
+
login(email: string, password: string): Promise<import("./types").LoginResponse>;
|
|
76
|
+
/**
|
|
77
|
+
* Logout and clear authentication
|
|
78
|
+
*/
|
|
79
|
+
logout(): void;
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":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IAC3C,SAAgB,aAAa,EAAE,qBAAqB,CAAC;IACrD,SAAgB,QAAQ,EAAE,gBAAgB,CAAC;IAC3C,SAAgB,QAAQ,EAAE,gBAAgB,CAAC;IAC3C,SAAgB,MAAM,EAAE,cAAc,CAAC;gBAE3B,MAAM,EAAE,uBAAuB;IA2B3C;;;;OAIG;IACH,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,GAAE,KAAK,GAAG,UAAkB,GAAG,IAAI;IAI9D;;OAEG;IACH,SAAS,IAAI,IAAI;IAIjB;;;;;OAKG;IACG,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM;IAM3C;;OAEG;IACH,MAAM,IAAI,IAAI;CAGf"}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WhatsAppAPIClient = void 0;
|
|
4
|
+
const client_1 = require("./http/client");
|
|
5
|
+
const auth_1 = require("./resources/auth");
|
|
6
|
+
const users_1 = require("./resources/users");
|
|
7
|
+
const tenants_1 = require("./resources/tenants");
|
|
8
|
+
const apiTokens_1 = require("./resources/apiTokens");
|
|
9
|
+
const whatsapp_1 = require("./resources/whatsapp");
|
|
10
|
+
const whatsappAdmin_1 = require("./resources/whatsappAdmin");
|
|
11
|
+
const webhooks_1 = require("./resources/webhooks");
|
|
12
|
+
const branding_1 = require("./resources/branding");
|
|
13
|
+
const health_1 = require("./resources/health");
|
|
14
|
+
/**
|
|
15
|
+
* WhatsApp Multi-tenant API Client
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* // Admin client (JWT authentication)
|
|
20
|
+
* const admin = new WhatsAppAPIClient({
|
|
21
|
+
* baseURL: 'https://api.example.com',
|
|
22
|
+
* auth: { type: 'jwt', token: 'your-jwt-token' }
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* await admin.auth.login('admin@example.com', 'password');
|
|
26
|
+
* const tenants = await admin.tenants.list();
|
|
27
|
+
* await admin.whatsappAdmin.connect('tenant-123');
|
|
28
|
+
*
|
|
29
|
+
* // User client (API Token authentication)
|
|
30
|
+
* const user = new WhatsAppAPIClient({
|
|
31
|
+
* baseURL: 'https://api.example.com',
|
|
32
|
+
* auth: { type: 'apiToken', token: 'wpp_live_xxx' }
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* await user.whatsapp.send({ jid: '5511999999999@s.whatsapp.net', text: 'Hello!' });
|
|
36
|
+
* const groups = await user.whatsapp.listGroups('tenant-id');
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
class WhatsAppAPIClient {
|
|
40
|
+
constructor(config) {
|
|
41
|
+
// Initialize HTTP client
|
|
42
|
+
this.httpClient = new client_1.HTTPClient({
|
|
43
|
+
baseURL: config.baseURL,
|
|
44
|
+
timeout: config.timeout,
|
|
45
|
+
onRequest: config.onRequest,
|
|
46
|
+
onResponse: config.onResponse,
|
|
47
|
+
onError: config.onError,
|
|
48
|
+
});
|
|
49
|
+
// Set initial auth if provided
|
|
50
|
+
if (config.auth) {
|
|
51
|
+
this.setAuth(config.auth.token, config.auth.type);
|
|
52
|
+
}
|
|
53
|
+
// Initialize resources
|
|
54
|
+
this.auth = new auth_1.AuthResource(this.httpClient);
|
|
55
|
+
this.users = new users_1.UsersResource(this.httpClient);
|
|
56
|
+
this.tenants = new tenants_1.TenantsResource(this.httpClient);
|
|
57
|
+
this.apiTokens = new apiTokens_1.ApiTokensResource(this.httpClient);
|
|
58
|
+
this.whatsapp = new whatsapp_1.WhatsAppResource(this.httpClient);
|
|
59
|
+
this.whatsappAdmin = new whatsappAdmin_1.WhatsAppAdminResource(this.httpClient);
|
|
60
|
+
this.webhooks = new webhooks_1.WebhooksResource(this.httpClient);
|
|
61
|
+
this.branding = new branding_1.BrandingResource(this.httpClient);
|
|
62
|
+
this.health = new health_1.HealthResource(this.httpClient);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Set authentication token
|
|
66
|
+
* @param token JWT or API token
|
|
67
|
+
* @param type Authentication type ('jwt' or 'apiToken')
|
|
68
|
+
*/
|
|
69
|
+
setAuth(token, type = 'jwt') {
|
|
70
|
+
this.httpClient.setAuth(token, type);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Clear authentication
|
|
74
|
+
*/
|
|
75
|
+
clearAuth() {
|
|
76
|
+
this.httpClient.clearAuth();
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Login and automatically set JWT auth
|
|
80
|
+
* @param email User email
|
|
81
|
+
* @param password User password
|
|
82
|
+
* @returns Login response with user info and token
|
|
83
|
+
*/
|
|
84
|
+
async login(email, password) {
|
|
85
|
+
const response = await this.auth.login(email, password);
|
|
86
|
+
this.setAuth(response.access_token, 'jwt');
|
|
87
|
+
return response;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Logout and clear authentication
|
|
91
|
+
*/
|
|
92
|
+
logout() {
|
|
93
|
+
this.clearAuth();
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
exports.WhatsAppAPIClient = WhatsAppAPIClient;
|
|
97
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":";;;AAAA,0CAA6D;AAC7D,2CAAgD;AAChD,6CAAkD;AAClD,iDAAsD;AACtD,qDAA0D;AAC1D,mDAAwD;AACxD,6DAAkE;AAClE,mDAAwD;AACxD,mDAAwD;AACxD,+CAAoD;AAcpD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAa,iBAAiB;IAc5B,YAAY,MAA+B;QACzC,yBAAyB;QACzB,IAAI,CAAC,UAAU,GAAG,IAAI,mBAAU,CAAC;YAC/B,OAAO,EAAE,MAAM,CAAC,OAAO;YACvB,OAAO,EAAE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