queuebear 0.1.1 → 0.1.3
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 +70 -57
- package/package.json +2 -2
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,35 @@ 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
|
-
|
|
222
|
+
| Option | Type | Description |
|
|
223
|
+
| --------------- | -------- | --------------------------------------------- |
|
|
224
224
|
| `signingSecret` | `string` | Secret to verify requests come from QueueBear |
|
|
225
|
-
| `baseUrl` | `string` | Override QueueBear base URL (auto-detected if not set) |
|
|
226
225
|
|
|
227
226
|
### Framework Integration
|
|
228
227
|
|
|
229
228
|
**Next.js (App Router)**
|
|
229
|
+
|
|
230
230
|
```typescript
|
|
231
231
|
// app/api/workflows/my-workflow/route.ts
|
|
232
232
|
import { serve } from "queuebear";
|
|
233
233
|
|
|
234
234
|
export const POST = serve(async (context) => {
|
|
235
|
-
await context.run("step-1", async () => {
|
|
235
|
+
await context.run("step-1", async () => {
|
|
236
|
+
/* ... */
|
|
237
|
+
});
|
|
236
238
|
return { success: true };
|
|
237
239
|
});
|
|
238
240
|
```
|
|
239
241
|
|
|
240
242
|
**Express**
|
|
243
|
+
|
|
241
244
|
```typescript
|
|
242
245
|
import express from "express";
|
|
243
246
|
import { serve } from "queuebear";
|
|
@@ -246,21 +249,26 @@ const app = express();
|
|
|
246
249
|
app.use(express.json());
|
|
247
250
|
|
|
248
251
|
const handler = serve(async (context) => {
|
|
249
|
-
await context.run("step-1", async () => {
|
|
252
|
+
await context.run("step-1", async () => {
|
|
253
|
+
/* ... */
|
|
254
|
+
});
|
|
250
255
|
return { success: true };
|
|
251
256
|
});
|
|
252
257
|
|
|
253
258
|
app.post("/api/workflows/my-workflow", async (req, res) => {
|
|
254
|
-
const response = await handler(
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
+
const response = await handler(
|
|
260
|
+
new Request(req.url, {
|
|
261
|
+
method: "POST",
|
|
262
|
+
headers: req.headers as HeadersInit,
|
|
263
|
+
body: JSON.stringify(req.body),
|
|
264
|
+
})
|
|
265
|
+
);
|
|
259
266
|
res.status(response.status).json(await response.json());
|
|
260
267
|
});
|
|
261
268
|
```
|
|
262
269
|
|
|
263
270
|
**Hono**
|
|
271
|
+
|
|
264
272
|
```typescript
|
|
265
273
|
import { Hono } from "hono";
|
|
266
274
|
import { serve } from "queuebear";
|
|
@@ -268,7 +276,9 @@ import { serve } from "queuebear";
|
|
|
268
276
|
const app = new Hono();
|
|
269
277
|
|
|
270
278
|
const handler = serve(async (context) => {
|
|
271
|
-
await context.run("step-1", async () => {
|
|
279
|
+
await context.run("step-1", async () => {
|
|
280
|
+
/* ... */
|
|
281
|
+
});
|
|
272
282
|
return { success: true };
|
|
273
283
|
});
|
|
274
284
|
|
|
@@ -291,26 +301,29 @@ interface OnboardingInput {
|
|
|
291
301
|
email: string;
|
|
292
302
|
}
|
|
293
303
|
|
|
294
|
-
export const POST = serve<OnboardingInput>(
|
|
295
|
-
|
|
304
|
+
export const POST = serve<OnboardingInput>(
|
|
305
|
+
async (context) => {
|
|
306
|
+
const { userId, email } = context.input;
|
|
296
307
|
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
308
|
+
// Step 1: Send welcome email (cached if already done)
|
|
309
|
+
await context.run("send-welcome", async () => {
|
|
310
|
+
await sendEmail(email, "welcome");
|
|
311
|
+
});
|
|
301
312
|
|
|
302
|
-
|
|
303
|
-
|
|
313
|
+
// Step 2: Wait 3 days
|
|
314
|
+
await context.sleep("wait-3-days", 60 * 60 * 24 * 3);
|
|
304
315
|
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
316
|
+
// Step 3: Send tips email
|
|
317
|
+
await context.run("send-tips", async () => {
|
|
318
|
+
await sendEmail(email, "tips");
|
|
319
|
+
});
|
|
309
320
|
|
|
310
|
-
|
|
311
|
-
},
|
|
312
|
-
|
|
313
|
-
|
|
321
|
+
return { completed: true };
|
|
322
|
+
},
|
|
323
|
+
{
|
|
324
|
+
signingSecret: process.env.QUEUEBEAR_SIGNING_SECRET,
|
|
325
|
+
}
|
|
326
|
+
);
|
|
314
327
|
```
|
|
315
328
|
|
|
316
329
|
### Trigger a Workflow
|
|
@@ -332,7 +345,7 @@ const { runId } = await qb.workflows.trigger(
|
|
|
332
345
|
```typescript
|
|
333
346
|
const status = await qb.workflows.getStatus(runId);
|
|
334
347
|
console.log(status.status); // "running" | "sleeping" | "completed"
|
|
335
|
-
console.log(status.steps);
|
|
348
|
+
console.log(status.steps); // Array of step details
|
|
336
349
|
```
|
|
337
350
|
|
|
338
351
|
### Wait for Completion
|
|
@@ -415,7 +428,7 @@ Make an HTTP call as a cached step.
|
|
|
415
428
|
const data = await context.call("fetch-api", {
|
|
416
429
|
url: "https://api.example.com/data",
|
|
417
430
|
method: "POST",
|
|
418
|
-
headers: {
|
|
431
|
+
headers: { Authorization: "Bearer xxx" },
|
|
419
432
|
body: { key: "value" },
|
|
420
433
|
});
|
|
421
434
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "queuebear",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "QueueBear SDK for message queues, scheduled jobs, and durable workflows",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
],
|
|
43
43
|
"author": {
|
|
44
44
|
"name": "Robert Marshall",
|
|
45
|
-
"email": "
|
|
45
|
+
"email": "hello@robertmarshall.dev",
|
|
46
46
|
"url": "https://robertmarshall.dev"
|
|
47
47
|
},
|
|
48
48
|
"license": "MIT",
|