updated skills
This commit is contained in:
@@ -54,19 +54,19 @@ bun add @suaveplan/workflow
|
||||
import { createWorkflow } from "@suaveplan/workflow";
|
||||
|
||||
interface OrderData {
|
||||
orderId: string;
|
||||
items: string[];
|
||||
total: number;
|
||||
orderId: string;
|
||||
items: string[];
|
||||
total: number;
|
||||
}
|
||||
|
||||
const result = await createWorkflow<OrderData>("process-order")
|
||||
.addStep("validate", (ctx) => {
|
||||
if (ctx.items.length === 0) throw new Error("No items");
|
||||
})
|
||||
.build()
|
||||
.run({
|
||||
initialData: { orderId: "ord-1", items: ["widget"], total: 29.99 },
|
||||
});
|
||||
.addStep("validate", (ctx) => {
|
||||
if (ctx.items.length === 0) throw new Error("No items");
|
||||
})
|
||||
.build()
|
||||
.run({
|
||||
initialData: { orderId: "ord-1", items: ["widget"], total: 29.99 },
|
||||
});
|
||||
```
|
||||
|
||||
## Modules
|
||||
@@ -103,15 +103,15 @@ builder.onError("continue"); // log + skip the failure, run the rest
|
||||
import { RetryPresets } from "@suaveplan/workflow";
|
||||
|
||||
builder
|
||||
.addStep("flaky-api", async (ctx) => callExternalApi(ctx))
|
||||
.withRetry(RetryPresets.standard());
|
||||
.addStep("flaky-api", async (ctx) => callExternalApi(ctx))
|
||||
.withRetry(RetryPresets.standard());
|
||||
|
||||
builder.addStep("custom", (ctx) => callApi(ctx)).withRetry({
|
||||
maxAttempts: 5,
|
||||
strategy: "exponential",
|
||||
delayMs: 500,
|
||||
maxDelayMs: 10_000,
|
||||
backoffMultiplier: 2,
|
||||
maxAttempts: 5,
|
||||
strategy: "exponential",
|
||||
delayMs: 500,
|
||||
maxDelayMs: 10_000,
|
||||
backoffMultiplier: 2,
|
||||
});
|
||||
```
|
||||
|
||||
@@ -121,13 +121,13 @@ builder.addStep("custom", (ctx) => callApi(ctx)).withRetry({
|
||||
const engine = createWorkflowEngine(config);
|
||||
|
||||
engine.hooks.on("workflow:start", (p) =>
|
||||
console.log(`Workflow ${p.workflowId} started`),
|
||||
console.log(`Workflow ${p.workflowId} started`),
|
||||
);
|
||||
engine.hooks.on("step:complete", (p) =>
|
||||
console.log(`Step ${p.stepId} completed`),
|
||||
console.log(`Step ${p.stepId} completed`),
|
||||
);
|
||||
engine.hooks.on("step:fail", (p) =>
|
||||
console.error(`Step ${p.stepId} failed:`, p.error),
|
||||
console.error(`Step ${p.stepId} failed:`, p.error),
|
||||
);
|
||||
```
|
||||
|
||||
@@ -139,18 +139,18 @@ import { createSaga, createStep } from "@suaveplan/workflow";
|
||||
const saga = createSaga({ id: "booking", name: "Booking Saga" });
|
||||
|
||||
saga
|
||||
.addStep(createStep({
|
||||
id: "reserve",
|
||||
name: "Reserve Seats",
|
||||
execute: (ctx) => reserveSeats(ctx),
|
||||
compensate: (ctx) => releaseSeats(ctx),
|
||||
}))
|
||||
.addStep(createStep({
|
||||
id: "charge",
|
||||
name: "Charge Payment",
|
||||
execute: (ctx) => chargeCard(ctx),
|
||||
compensate: (ctx) => refundCard(ctx),
|
||||
}));
|
||||
.addStep(createStep({
|
||||
id: "reserve",
|
||||
name: "Reserve Seats",
|
||||
execute: (ctx) => reserveSeats(ctx),
|
||||
compensate: (ctx) => releaseSeats(ctx),
|
||||
}))
|
||||
.addStep(createStep({
|
||||
id: "charge",
|
||||
name: "Charge Payment",
|
||||
execute: (ctx) => chargeCard(ctx),
|
||||
compensate: (ctx) => refundCard(ctx),
|
||||
}));
|
||||
|
||||
const result = await saga.execute(context);
|
||||
// If "charge" fails, "reserve" is automatically compensated.
|
||||
@@ -159,4 +159,3 @@ const result = await saga.execute(context);
|
||||
## License
|
||||
|
||||
MIT
|
||||
|
||||
|
||||
Reference in New Issue
Block a user