queuebear 0.1.1 → 0.1.2
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 +72 -58
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,7 +13,6 @@ npm install queuebear
|
|
|
13
13
|
```typescript
|
|
14
14
|
import { QueueBear, serve } from "queuebear";
|
|
15
15
|
|
|
16
|
-
// Create client (defaults to https://api.queuebear.com)
|
|
17
16
|
const qb = new QueueBear({
|
|
18
17
|
apiKey: "qb_live_xxx",
|
|
19
18
|
projectId: "proj_xxx",
|
|
@@ -24,12 +23,12 @@ const qb = new QueueBear({
|
|
|
24
23
|
|
|
25
24
|
The SDK provides access to all QueueBear APIs:
|
|
26
25
|
|
|
27
|
-
| API
|
|
28
|
-
|
|
29
|
-
| `qb.messages`
|
|
30
|
-
| `qb.schedules` | Create and manage cron-based recurring jobs
|
|
31
|
-
| `qb.dlq`
|
|
32
|
-
| `qb.workflows` | Trigger and manage durable workflows
|
|
26
|
+
| API | Description |
|
|
27
|
+
| -------------- | ----------------------------------------------- |
|
|
28
|
+
| `qb.messages` | Publish and manage webhook messages |
|
|
29
|
+
| `qb.schedules` | Create and manage cron-based recurring jobs |
|
|
30
|
+
| `qb.dlq` | Manage failed messages in the dead letter queue |
|
|
31
|
+
| `qb.workflows` | Trigger and manage durable workflows |
|
|
33
32
|
|
|
34
33
|
---
|
|
35
34
|
|
|
@@ -44,13 +43,13 @@ const { messageId } = await qb.messages.publish(
|
|
|
44
43
|
"https://api.example.com/webhook",
|
|
45
44
|
{ event: "user.created", userId: "123" },
|
|
46
45
|
{
|
|
47
|
-
delay: "30s",
|
|
48
|
-
retries: 5,
|
|
49
|
-
method: "POST",
|
|
46
|
+
delay: "30s", // Delay before delivery
|
|
47
|
+
retries: 5, // Number of retry attempts
|
|
48
|
+
method: "POST", // HTTP method
|
|
50
49
|
headers: { "X-API-Key": "secret" }, // Headers to forward
|
|
51
|
-
callbackUrl: "https://...",
|
|
50
|
+
callbackUrl: "https://...", // Success callback
|
|
52
51
|
failureCallbackUrl: "https://...", // Failure callback
|
|
53
|
-
deduplicationId: "unique-id",
|
|
52
|
+
deduplicationId: "unique-id", // Prevent duplicate messages
|
|
54
53
|
}
|
|
55
54
|
);
|
|
56
55
|
```
|
|
@@ -59,7 +58,7 @@ const { messageId } = await qb.messages.publish(
|
|
|
59
58
|
|
|
60
59
|
```typescript
|
|
61
60
|
const message = await qb.messages.get(messageId);
|
|
62
|
-
console.log(message.status);
|
|
61
|
+
console.log(message.status); // "pending" | "completed" | "failed"
|
|
63
62
|
console.log(message.deliveryLogs); // Delivery attempt history
|
|
64
63
|
```
|
|
65
64
|
|
|
@@ -101,7 +100,7 @@ Create cron-based recurring jobs.
|
|
|
101
100
|
```typescript
|
|
102
101
|
const schedule = await qb.schedules.create({
|
|
103
102
|
destination: "https://api.example.com/cron-job",
|
|
104
|
-
cron: "0 9 * * *",
|
|
103
|
+
cron: "0 9 * * *", // Daily at 9 AM
|
|
105
104
|
timezone: "America/New_York",
|
|
106
105
|
method: "POST",
|
|
107
106
|
body: JSON.stringify({ type: "daily-report" }),
|
|
@@ -113,14 +112,14 @@ const schedule = await qb.schedules.create({
|
|
|
113
112
|
|
|
114
113
|
### Common Cron Expressions
|
|
115
114
|
|
|
116
|
-
| Expression
|
|
117
|
-
|
|
118
|
-
| `* * * * *`
|
|
119
|
-
| `0 * * * *`
|
|
120
|
-
| `0 9 * * *`
|
|
121
|
-
| `0 9 * * 1-5` | Weekdays at 9:00 AM
|
|
122
|
-
| `0 0 1 * *`
|
|
123
|
-
| `0 */6 * * *` | Every 6 hours
|
|
115
|
+
| Expression | Description |
|
|
116
|
+
| ------------- | ----------------------- |
|
|
117
|
+
| `* * * * *` | Every minute |
|
|
118
|
+
| `0 * * * *` | Every hour |
|
|
119
|
+
| `0 9 * * *` | Daily at 9:00 AM |
|
|
120
|
+
| `0 9 * * 1-5` | Weekdays at 9:00 AM |
|
|
121
|
+
| `0 0 1 * *` | First day of each month |
|
|
122
|
+
| `0 */6 * * *` | Every 6 hours |
|
|
124
123
|
|
|
125
124
|
### List Schedules
|
|
126
125
|
|
|
@@ -160,15 +159,15 @@ for (const entry of entries) {
|
|
|
160
159
|
|
|
161
160
|
```typescript
|
|
162
161
|
const entry = await qb.dlq.get(dlqId);
|
|
163
|
-
console.log(entry.body);
|
|
164
|
-
console.log(entry.totalAttempts);
|
|
162
|
+
console.log(entry.body); // Original message body
|
|
163
|
+
console.log(entry.totalAttempts); // Number of failed attempts
|
|
165
164
|
```
|
|
166
165
|
|
|
167
166
|
### Retry a Failed Message
|
|
168
167
|
|
|
169
168
|
```typescript
|
|
170
169
|
const result = await qb.dlq.retry(dlqId);
|
|
171
|
-
console.log(result.newMessageId);
|
|
170
|
+
console.log(result.newMessageId); // New message created
|
|
172
171
|
```
|
|
173
172
|
|
|
174
173
|
### Delete Entry / Purge All
|
|
@@ -192,6 +191,7 @@ console.log(`Retried ${results.length} entries`);
|
|
|
192
191
|
Build durable, fault-tolerant workflows with automatic step caching.
|
|
193
192
|
|
|
194
193
|
Workflows consist of two parts:
|
|
194
|
+
|
|
195
195
|
1. **Workflow endpoint** - Created with `serve()`, handles workflow execution
|
|
196
196
|
2. **Client** - Uses `qb.workflows` to trigger and manage workflow runs
|
|
197
197
|
|
|
@@ -212,32 +212,36 @@ export const POST = serve<InputType>(async (context) => {
|
|
|
212
212
|
|
|
213
213
|
**Parameters:**
|
|
214
214
|
|
|
215
|
-
| Parameter | Type
|
|
216
|
-
|
|
215
|
+
| Parameter | Type | Description |
|
|
216
|
+
| --------- | --------------------------------------------- | ---------------------- |
|
|
217
217
|
| `handler` | `(context: WorkflowContext<T>) => Promise<R>` | Your workflow function |
|
|
218
|
-
| `options` | `ServeOptions`
|
|
218
|
+
| `options` | `ServeOptions` | Optional configuration |
|
|
219
219
|
|
|
220
220
|
**Options:**
|
|
221
221
|
|
|
222
|
-
| Option
|
|
223
|
-
|
|
224
|
-
| `signingSecret` | `string` | Secret to verify requests come from QueueBear
|
|
225
|
-
| `baseUrl`
|
|
222
|
+
| Option | Type | Description |
|
|
223
|
+
| --------------- | -------- | ------------------------------------------------------ |
|
|
224
|
+
| `signingSecret` | `string` | Secret to verify requests come from QueueBear |
|
|
225
|
+
| `baseUrl` | `string` | Override QueueBear base URL (auto-detected if not set) |
|
|
226
226
|
|
|
227
227
|
### Framework Integration
|
|
228
228
|
|
|
229
229
|
**Next.js (App Router)**
|
|
230
|
+
|
|
230
231
|
```typescript
|
|
231
232
|
// app/api/workflows/my-workflow/route.ts
|
|
232
233
|
import { serve } from "queuebear";
|
|
233
234
|
|
|
234
235
|
export const POST = serve(async (context) => {
|
|
235
|
-
await context.run("step-1", async () => {
|
|
236
|
+
await context.run("step-1", async () => {
|
|
237
|
+
/* ... */
|
|
238
|
+
});
|
|
236
239
|
return { success: true };
|
|
237
240
|
});
|
|
238
241
|
```
|
|
239
242
|
|
|
240
243
|
**Express**
|
|
244
|
+
|
|
241
245
|
```typescript
|
|
242
246
|
import express from "express";
|
|
243
247
|
import { serve } from "queuebear";
|
|
@@ -246,21 +250,26 @@ const app = express();
|
|
|
246
250
|
app.use(express.json());
|
|
247
251
|
|
|
248
252
|
const handler = serve(async (context) => {
|
|
249
|
-
await context.run("step-1", async () => {
|
|
253
|
+
await context.run("step-1", async () => {
|
|
254
|
+
/* ... */
|
|
255
|
+
});
|
|
250
256
|
return { success: true };
|
|
251
257
|
});
|
|
252
258
|
|
|
253
259
|
app.post("/api/workflows/my-workflow", async (req, res) => {
|
|
254
|
-
const response = await handler(
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
260
|
+
const response = await handler(
|
|
261
|
+
new Request(req.url, {
|
|
262
|
+
method: "POST",
|
|
263
|
+
headers: req.headers as HeadersInit,
|
|
264
|
+
body: JSON.stringify(req.body),
|
|
265
|
+
})
|
|
266
|
+
);
|
|
259
267
|
res.status(response.status).json(await response.json());
|
|
260
268
|
});
|
|
261
269
|
```
|
|
262
270
|
|
|
263
271
|
**Hono**
|
|
272
|
+
|
|
264
273
|
```typescript
|
|
265
274
|
import { Hono } from "hono";
|
|
266
275
|
import { serve } from "queuebear";
|
|
@@ -268,7 +277,9 @@ import { serve } from "queuebear";
|
|
|
268
277
|
const app = new Hono();
|
|
269
278
|
|
|
270
279
|
const handler = serve(async (context) => {
|
|
271
|
-
await context.run("step-1", async () => {
|
|
280
|
+
await context.run("step-1", async () => {
|
|
281
|
+
/* ... */
|
|
282
|
+
});
|
|
272
283
|
return { success: true };
|
|
273
284
|
});
|
|
274
285
|
|
|
@@ -291,26 +302,29 @@ interface OnboardingInput {
|
|
|
291
302
|
email: string;
|
|
292
303
|
}
|
|
293
304
|
|
|
294
|
-
export const POST = serve<OnboardingInput>(
|
|
295
|
-
|
|
305
|
+
export const POST = serve<OnboardingInput>(
|
|
306
|
+
async (context) => {
|
|
307
|
+
const { userId, email } = context.input;
|
|
296
308
|
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
309
|
+
// Step 1: Send welcome email (cached if already done)
|
|
310
|
+
await context.run("send-welcome", async () => {
|
|
311
|
+
await sendEmail(email, "welcome");
|
|
312
|
+
});
|
|
301
313
|
|
|
302
|
-
|
|
303
|
-
|
|
314
|
+
// Step 2: Wait 3 days
|
|
315
|
+
await context.sleep("wait-3-days", 60 * 60 * 24 * 3);
|
|
304
316
|
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
317
|
+
// Step 3: Send tips email
|
|
318
|
+
await context.run("send-tips", async () => {
|
|
319
|
+
await sendEmail(email, "tips");
|
|
320
|
+
});
|
|
309
321
|
|
|
310
|
-
|
|
311
|
-
},
|
|
312
|
-
|
|
313
|
-
|
|
322
|
+
return { completed: true };
|
|
323
|
+
},
|
|
324
|
+
{
|
|
325
|
+
signingSecret: process.env.QUEUEBEAR_SIGNING_SECRET,
|
|
326
|
+
}
|
|
327
|
+
);
|
|
314
328
|
```
|
|
315
329
|
|
|
316
330
|
### Trigger a Workflow
|
|
@@ -332,7 +346,7 @@ const { runId } = await qb.workflows.trigger(
|
|
|
332
346
|
```typescript
|
|
333
347
|
const status = await qb.workflows.getStatus(runId);
|
|
334
348
|
console.log(status.status); // "running" | "sleeping" | "completed"
|
|
335
|
-
console.log(status.steps);
|
|
349
|
+
console.log(status.steps); // Array of step details
|
|
336
350
|
```
|
|
337
351
|
|
|
338
352
|
### Wait for Completion
|
|
@@ -415,7 +429,7 @@ Make an HTTP call as a cached step.
|
|
|
415
429
|
const data = await context.call("fetch-api", {
|
|
416
430
|
url: "https://api.example.com/data",
|
|
417
431
|
method: "POST",
|
|
418
|
-
headers: {
|
|
432
|
+
headers: { Authorization: "Bearer xxx" },
|
|
419
433
|
body: { key: "value" },
|
|
420
434
|
});
|
|
421
435
|
```
|