@zapyapi/sdk 1.0.0-beta.5 → 1.0.0-beta.7
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 +114 -19
- package/index.cjs +865 -139
- package/index.js +865 -130
- package/package.json +1 -1
- package/src/client.d.ts +1 -1
- package/src/index.d.ts +11 -7
- package/src/index.d.ts.map +1 -1
- package/src/types/enums.d.ts +6 -89
- package/src/types/enums.d.ts.map +1 -1
- package/src/types/events/instance-status.d.ts +3 -13
- package/src/types/events/instance-status.d.ts.map +1 -1
- package/src/types/index.d.ts +3 -5
- package/src/types/index.d.ts.map +1 -1
- package/src/version.d.ts +1 -1
- package/src/types/webhook-events.types.d.ts +0 -257
- package/src/types/webhook-events.types.d.ts.map +0 -1
package/README.md
CHANGED
|
@@ -202,12 +202,16 @@ await client.webhooks.deleteConfig();
|
|
|
202
202
|
|
|
203
203
|
## Error Handling
|
|
204
204
|
|
|
205
|
+
The SDK provides detailed error classes for different failure scenarios:
|
|
206
|
+
|
|
205
207
|
```typescript
|
|
206
208
|
import {
|
|
207
209
|
ZapyClient,
|
|
208
210
|
ZapyApiError,
|
|
209
211
|
AuthenticationError,
|
|
210
212
|
RateLimitError,
|
|
213
|
+
NetworkError,
|
|
214
|
+
TimeoutError,
|
|
211
215
|
ValidationError,
|
|
212
216
|
} from '@zapyapi/sdk';
|
|
213
217
|
|
|
@@ -217,17 +221,46 @@ try {
|
|
|
217
221
|
text: 'Hello!',
|
|
218
222
|
});
|
|
219
223
|
} catch (error) {
|
|
220
|
-
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) {
|
|
221
233
|
console.error('Invalid API key');
|
|
222
234
|
} else if (error instanceof RateLimitError) {
|
|
223
235
|
console.error(`Rate limited. Retry after: ${error.retryAfter}ms`);
|
|
236
|
+
console.error(`Retry at: ${error.getRetryAfterDate()}`);
|
|
224
237
|
} else if (error instanceof ZapyApiError) {
|
|
225
238
|
console.error(`API Error [${error.code}]: ${error.message}`);
|
|
239
|
+
console.error(`Status: ${error.statusCode}`);
|
|
226
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));
|
|
227
247
|
}
|
|
228
248
|
}
|
|
229
249
|
```
|
|
230
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
|
+
|
|
231
264
|
## Utilities
|
|
232
265
|
|
|
233
266
|
```typescript
|
|
@@ -251,35 +284,70 @@ extractPhone('5511999999999@c.us'); // '5511999999999'
|
|
|
251
284
|
|
|
252
285
|
## Webhook Event Types
|
|
253
286
|
|
|
254
|
-
The SDK provides
|
|
287
|
+
The SDK provides fully typed webhook events with discriminated unions for each message type:
|
|
255
288
|
|
|
256
289
|
```typescript
|
|
257
|
-
import
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
290
|
+
import {
|
|
291
|
+
ZapyWebhookPayload,
|
|
292
|
+
ZapyEventTypes,
|
|
293
|
+
isTextMessage,
|
|
294
|
+
isImageMessage,
|
|
295
|
+
isVideoMessage,
|
|
296
|
+
isAudioMessage,
|
|
263
297
|
} from '@zapyapi/sdk';
|
|
264
298
|
|
|
265
|
-
function handleWebhook(
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
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
|
+
}
|
|
270
316
|
break;
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
console.log(`Message ${
|
|
317
|
+
|
|
318
|
+
case ZapyEventTypes.MESSAGE_STATUS:
|
|
319
|
+
console.log(`Message ${payload.data.messageId} status: ${payload.data.status}`);
|
|
274
320
|
break;
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
console.log(`
|
|
321
|
+
|
|
322
|
+
case ZapyEventTypes.QR_CODE:
|
|
323
|
+
console.log(`QR Code received, attempt: ${payload.data.attempt}`);
|
|
324
|
+
break;
|
|
325
|
+
|
|
326
|
+
case ZapyEventTypes.INSTANCE_STATUS:
|
|
327
|
+
console.log(`Instance status: ${payload.data.status}`);
|
|
278
328
|
break;
|
|
279
329
|
}
|
|
280
330
|
}
|
|
281
331
|
```
|
|
282
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
|
+
|
|
283
351
|
## Requirements
|
|
284
352
|
|
|
285
353
|
- Node.js 18 or higher
|
|
@@ -295,6 +363,33 @@ const { ZapyClient } = require('@zapyapi/sdk');
|
|
|
295
363
|
const client = new ZapyClient({ apiKey: 'your-api-key' });
|
|
296
364
|
```
|
|
297
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
|
+
|
|
298
393
|
## License
|
|
299
394
|
|
|
300
395
|
MIT
|