@zapyapi/sdk 1.0.0-beta.1 → 1.0.0-beta.10
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 +131 -30
- package/index.cjs +1507 -140
- package/index.js +1487 -131
- package/package.json +1 -2
- package/src/client.d.ts +1 -1
- package/src/errors.d.ts +101 -5
- package/src/errors.d.ts.map +1 -1
- package/src/generated/Api.d.ts +637 -7
- package/src/generated/Api.d.ts.map +1 -1
- package/src/index.d.ts +13 -8
- package/src/index.d.ts.map +1 -1
- package/src/resources/base.resource.d.ts.map +1 -1
- package/src/resources/instances.resource.d.ts +106 -1
- package/src/resources/instances.resource.d.ts.map +1 -1
- package/src/resources/messages.resource.d.ts +70 -1
- package/src/resources/messages.resource.d.ts.map +1 -1
- package/src/types/enums.d.ts +6 -87
- package/src/types/enums.d.ts.map +1 -1
- package/src/types/events/contact.d.ts +65 -0
- package/src/types/events/contact.d.ts.map +1 -0
- package/src/types/events/index.d.ts +76 -0
- package/src/types/events/index.d.ts.map +1 -0
- package/src/types/events/instance-status.d.ts +10 -0
- package/src/types/events/instance-status.d.ts.map +1 -0
- package/src/types/events/presence.d.ts +18 -0
- package/src/types/events/presence.d.ts.map +1 -0
- package/src/types/events/qr-code.d.ts +9 -0
- package/src/types/events/qr-code.d.ts.map +1 -0
- package/src/types/events/reaction.d.ts +14 -0
- package/src/types/events/reaction.d.ts.map +1 -0
- package/src/types/index.d.ts +8 -5
- package/src/types/index.d.ts.map +1 -1
- package/src/types/instances.types.d.ts +99 -0
- package/src/types/instances.types.d.ts.map +1 -1
- package/src/types/message-events.types.d.ts +245 -0
- package/src/types/message-events.types.d.ts.map +1 -0
- package/src/types/messages.types.d.ts +68 -1
- package/src/types/messages.types.d.ts.map +1 -1
- package/src/utils/phone.d.ts +1 -1
- package/src/version.d.ts +1 -1
- package/src/version.d.ts.map +1 -1
- package/src/types/webhook-events.types.d.ts +0 -232
- package/src/types/webhook-events.types.d.ts.map +0 -1
package/README.md
CHANGED
|
@@ -178,30 +178,40 @@ console.log(media.url);
|
|
|
178
178
|
### Webhooks
|
|
179
179
|
|
|
180
180
|
```typescript
|
|
181
|
-
// Configure
|
|
182
|
-
await client.webhooks.configure(
|
|
181
|
+
// Configure webhook
|
|
182
|
+
await client.webhooks.configure({
|
|
183
183
|
url: 'https://your-server.com/webhook',
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
'X-Custom-Header': 'your-value',
|
|
187
|
-
},
|
|
184
|
+
secret: 'your-secret-min-16-chars', // for HMAC-SHA256 verification
|
|
185
|
+
isActive: true,
|
|
188
186
|
});
|
|
189
187
|
|
|
190
188
|
// Get current configuration
|
|
191
|
-
const config = await client.webhooks.
|
|
189
|
+
const config = await client.webhooks.getConfig();
|
|
190
|
+
|
|
191
|
+
// Check queue status
|
|
192
|
+
const status = await client.webhooks.getQueueStatus();
|
|
193
|
+
console.log(`Pending: ${status.pendingCount}, Failed: ${status.failedCount}`);
|
|
194
|
+
|
|
195
|
+
// Resume paused/failed webhooks
|
|
196
|
+
const result = await client.webhooks.resume();
|
|
197
|
+
console.log(`Resumed ${result.resumedCount} webhooks`);
|
|
192
198
|
|
|
193
199
|
// Delete webhook configuration
|
|
194
|
-
await client.webhooks.
|
|
200
|
+
await client.webhooks.deleteConfig();
|
|
195
201
|
```
|
|
196
202
|
|
|
197
203
|
## Error Handling
|
|
198
204
|
|
|
205
|
+
The SDK provides detailed error classes for different failure scenarios:
|
|
206
|
+
|
|
199
207
|
```typescript
|
|
200
208
|
import {
|
|
201
209
|
ZapyClient,
|
|
202
210
|
ZapyApiError,
|
|
203
211
|
AuthenticationError,
|
|
204
212
|
RateLimitError,
|
|
213
|
+
NetworkError,
|
|
214
|
+
TimeoutError,
|
|
205
215
|
ValidationError,
|
|
206
216
|
} from '@zapyapi/sdk';
|
|
207
217
|
|
|
@@ -211,17 +221,46 @@ try {
|
|
|
211
221
|
text: 'Hello!',
|
|
212
222
|
});
|
|
213
223
|
} catch (error) {
|
|
214
|
-
if (error instanceof
|
|
224
|
+
if (error instanceof TimeoutError) {
|
|
225
|
+
// Request timed out
|
|
226
|
+
console.error(`Request timed out after ${error.timeout}ms`);
|
|
227
|
+
console.error(`Request: ${error.method} ${error.url}`);
|
|
228
|
+
} else if (error instanceof NetworkError) {
|
|
229
|
+
// Network failure (no response received)
|
|
230
|
+
console.error(`Network error: ${error.message}`);
|
|
231
|
+
console.error(`Error code: ${error.code}`); // e.g., ECONNREFUSED
|
|
232
|
+
} else if (error instanceof AuthenticationError) {
|
|
215
233
|
console.error('Invalid API key');
|
|
216
234
|
} else if (error instanceof RateLimitError) {
|
|
217
235
|
console.error(`Rate limited. Retry after: ${error.retryAfter}ms`);
|
|
236
|
+
console.error(`Retry at: ${error.getRetryAfterDate()}`);
|
|
218
237
|
} else if (error instanceof ZapyApiError) {
|
|
219
238
|
console.error(`API Error [${error.code}]: ${error.message}`);
|
|
239
|
+
console.error(`Status: ${error.statusCode}`);
|
|
220
240
|
console.error(`Request ID: ${error.requestId}`);
|
|
241
|
+
console.error(`Request: ${error.method} ${error.url}`);
|
|
242
|
+
|
|
243
|
+
// For debugging/logging
|
|
244
|
+
console.error(error.toDetailedString());
|
|
245
|
+
// Or serialize to JSON
|
|
246
|
+
console.error(JSON.stringify(error.toJSON(), null, 2));
|
|
221
247
|
}
|
|
222
248
|
}
|
|
223
249
|
```
|
|
224
250
|
|
|
251
|
+
### Error Classes
|
|
252
|
+
|
|
253
|
+
| Error Class | Description |
|
|
254
|
+
|-------------|-------------|
|
|
255
|
+
| `ZapyError` | Base error class for all SDK errors |
|
|
256
|
+
| `ZapyApiError` | API returned an error response (4xx, 5xx) |
|
|
257
|
+
| `AuthenticationError` | Invalid or missing API key (401) |
|
|
258
|
+
| `RateLimitError` | Rate limit exceeded (429) |
|
|
259
|
+
| `NetworkError` | Network failure, no response received |
|
|
260
|
+
| `TimeoutError` | Request timed out |
|
|
261
|
+
| `ValidationError` | Input validation failed |
|
|
262
|
+
| `InstanceNotFoundError` | Instance not found (404) |
|
|
263
|
+
|
|
225
264
|
## Utilities
|
|
226
265
|
|
|
227
266
|
```typescript
|
|
@@ -233,47 +272,82 @@ normalizePhone('+5511999999999'); // '5511999999999'
|
|
|
233
272
|
|
|
234
273
|
// Validate phone numbers
|
|
235
274
|
isValidPhone('5511999999999'); // true
|
|
236
|
-
isValidPhone('5511999999999@
|
|
275
|
+
isValidPhone('5511999999999@s.whatsapp.net'); // true
|
|
237
276
|
|
|
238
277
|
// Check if JID is a group
|
|
239
278
|
isGroup('123456789@g.us'); // true
|
|
240
|
-
isGroup('5511999999999@
|
|
279
|
+
isGroup('5511999999999@s.whatsapp.net'); // false
|
|
241
280
|
|
|
242
281
|
// Extract phone from JID
|
|
243
|
-
extractPhone('5511999999999@
|
|
282
|
+
extractPhone('5511999999999@s.whatsapp.net'); // '5511999999999'
|
|
244
283
|
```
|
|
245
284
|
|
|
246
285
|
## Webhook Event Types
|
|
247
286
|
|
|
248
|
-
The SDK provides
|
|
287
|
+
The SDK provides fully typed webhook events with discriminated unions for each message type:
|
|
249
288
|
|
|
250
289
|
```typescript
|
|
251
|
-
import
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
290
|
+
import {
|
|
291
|
+
ZapyWebhookPayload,
|
|
292
|
+
ZapyEventTypes,
|
|
293
|
+
isTextMessage,
|
|
294
|
+
isImageMessage,
|
|
295
|
+
isVideoMessage,
|
|
296
|
+
isAudioMessage,
|
|
257
297
|
} from '@zapyapi/sdk';
|
|
258
298
|
|
|
259
|
-
function handleWebhook(
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
299
|
+
function handleWebhook(payload: ZapyWebhookPayload) {
|
|
300
|
+
console.log(`Event from instance: ${payload.instanceId}`);
|
|
301
|
+
|
|
302
|
+
switch (payload.event) {
|
|
303
|
+
case ZapyEventTypes.MESSAGE:
|
|
304
|
+
// payload.data is ZapyMessage (discriminated union)
|
|
305
|
+
const message = payload.data;
|
|
306
|
+
|
|
307
|
+
if (isTextMessage(message)) {
|
|
308
|
+
console.log(`Text: ${message.text}`);
|
|
309
|
+
} else if (isImageMessage(message)) {
|
|
310
|
+
console.log(`Image: ${message.url}, caption: ${message.caption}`);
|
|
311
|
+
} else if (isVideoMessage(message)) {
|
|
312
|
+
console.log(`Video: ${message.url}`);
|
|
313
|
+
} else if (isAudioMessage(message)) {
|
|
314
|
+
console.log(`Audio: ${message.url}, duration: ${message.duration}s`);
|
|
315
|
+
}
|
|
316
|
+
break;
|
|
317
|
+
|
|
318
|
+
case ZapyEventTypes.MESSAGE_STATUS:
|
|
319
|
+
console.log(`Message ${payload.data.messageId} status: ${payload.data.status}`);
|
|
264
320
|
break;
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
console.log(`
|
|
321
|
+
|
|
322
|
+
case ZapyEventTypes.QR_CODE:
|
|
323
|
+
console.log(`QR Code received, attempt: ${payload.data.attempt}`);
|
|
268
324
|
break;
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
console.log(`
|
|
325
|
+
|
|
326
|
+
case ZapyEventTypes.INSTANCE_STATUS:
|
|
327
|
+
console.log(`Instance status: ${payload.data.status}`);
|
|
272
328
|
break;
|
|
273
329
|
}
|
|
274
330
|
}
|
|
275
331
|
```
|
|
276
332
|
|
|
333
|
+
### Available Message Type Guards
|
|
334
|
+
|
|
335
|
+
```typescript
|
|
336
|
+
import {
|
|
337
|
+
isTextMessage,
|
|
338
|
+
isImageMessage,
|
|
339
|
+
isVideoMessage,
|
|
340
|
+
isAudioMessage,
|
|
341
|
+
isDocumentMessage,
|
|
342
|
+
isStickerMessage,
|
|
343
|
+
isLocationMessage,
|
|
344
|
+
isContactMessage,
|
|
345
|
+
isReactionMessage,
|
|
346
|
+
isPollMessage,
|
|
347
|
+
isMediaMessage, // matches any media type
|
|
348
|
+
} from '@zapyapi/sdk';
|
|
349
|
+
```
|
|
350
|
+
|
|
277
351
|
## Requirements
|
|
278
352
|
|
|
279
353
|
- Node.js 18 or higher
|
|
@@ -289,6 +363,33 @@ const { ZapyClient } = require('@zapyapi/sdk');
|
|
|
289
363
|
const client = new ZapyClient({ apiKey: 'your-api-key' });
|
|
290
364
|
```
|
|
291
365
|
|
|
366
|
+
## Low-Level REST API Client
|
|
367
|
+
|
|
368
|
+
For advanced use cases, the SDK also exports an auto-generated REST client with full type definitions from the OpenAPI spec:
|
|
369
|
+
|
|
370
|
+
```typescript
|
|
371
|
+
import { ZapyRestApi } from '@zapyapi/sdk';
|
|
372
|
+
|
|
373
|
+
const api = new ZapyRestApi({
|
|
374
|
+
baseURL: 'https://api.zapyapi.com',
|
|
375
|
+
headers: {
|
|
376
|
+
'x-api-key': 'your-api-key',
|
|
377
|
+
},
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
// Direct REST calls with full type safety
|
|
381
|
+
const response = await api.instances.managerInstancesControllerListInstances({
|
|
382
|
+
page: 1,
|
|
383
|
+
limit: 10,
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
// Send a message
|
|
387
|
+
await api.sendMessage.sendMessageControllerSendTextMessage(
|
|
388
|
+
'my-instance',
|
|
389
|
+
{ to: '5511999999999', text: 'Hello!' }
|
|
390
|
+
);
|
|
391
|
+
```
|
|
392
|
+
|
|
292
393
|
## License
|
|
293
394
|
|
|
294
395
|
MIT
|