@bship/sdk
v0.2.3
Published
Write BuildShip workflows as plain TypeScript, run them locally, and call them from servers and browsers.
Readme
@bship/sdk
Write BuildShip workflows as plain TypeScript. Run them locally with a live map, deploy them to a BuildShip project, and call them from your servers and browsers.
Get started
In your app's folder (Vite, Next.js or plain Node):
npx @bship/cli@latest setupThis installs @bship/sdk and @bship/cli, adds an example workflow in flows/hello.ts, the
dev and deploy scripts, the project variable in .env.local, and a skill that briefs coding
agents on all of this. Your framework's config files stay untouched.
The skill alone, for any agent skills supports:
npx skills add https://cdn.jsdelivr.net/npm/@bship/sdk/skill/SKILL.mdWrite a workflow
Workflows live in flows/. Mark each step once, where it's defined. Call sites stay plain
function calls, with ordinary if, loops and try. Split a long workflow into groups.
// flows/refund.ts
import { workflow, step, group, sleep } from "@bship/sdk";
const loadOrder = step("loadOrder", async (id: string) => db.orders.find(id));
const issueRefund = step("issueRefund", async (order: Order) => stripe.refunds.create(…), {
retries: 2, // 1s, then 2s apart
timeout: "10s",
});
const notifyAll = group("notifyAll", async (order: Order) => {
await Promise.all([emailCustomer(order), notifyTeam(order)]);
});
export const processRefund = workflow("processRefund", async (input: { orderId: string }) => {
const order = await loadOrder(input.orderId);
if (!order.paid) {
await step("log unpaid", async () => console.warn("unpaid", order.id)); // a one-off step, in place
return { refunded: false };
}
await issueRefund(order);
await notifyAll(order);
await sleep("1d");
return { refunded: true };
});
processRefund.trigger("api", "/refunds"); // only workflows with an "api" trigger are callable over HTTPRun it
npm run dev # your app, plus workflows on http://localhost:3939
npx buildship run processRefund '{"orderId":"o1"}' # one workflow: each step, then the path it tooknpm run dev runs buildship dev -- <your dev command>. Edits to flows/ apply on the next call.
Call it
From a browser, call it by name through @bship/sdk/client, which has no Node imports. The
type-only import is erased at build, so no workflow code reaches the browser:
import { callWorkflow } from "@bship/sdk/client";
import type { greetUser } from "../flows/greet";
const { message } = await callWorkflow<typeof greetUser>("greetUser", { name: "Ada" });From server code, call the handle:
import { callWorkflow } from "@bship/sdk";
import { greetUser } from "./flows/greet";
const { message } = await callWorkflow(greetUser, { name: "Ada" });In npm run dev both run against your machine, so an edit shows on the next call and
console.log prints in your terminal: the browser calls buildship dev, and server code runs the
workflow in-process. That holds even with a project set.
Deploy it
Set your project in .env.local, under the name your framework hands to the browser:
| App | Variable |
| -------- | ----------------------------------------------- |
| Vite | VITE_BUILDSHIP_PROJECT=buildship-123abc |
| Next.js | NEXT_PUBLIC_BUILDSHIP_PROJECT=buildship-123abc |
| Anything | BUILDSHIP_PROJECT=buildship-123abc |
npm run deployWorkflows then run at https://123abc.buildship.run/v1/function/<name>/run, and every
callWorkflow in your production build, browser or server, goes there. npm run dev keeps
running your local workflows; npm run dev:remote runs your app against the deployed ones.
API
| Export | What it does |
| -------------------------- | -------------------------------------------------------------------------------- |
| step(name, impl, opts?) | A step. impl may be sync or async; calling the step always returns a Promise. opts: retries (1s, 2s, 4s… apart) and timeout ("30s"). A step with no arguments runs when awaited, so await step("x", async () => …) defines and runs one in place. |
| group(name, impl) | A named part of a workflow that calls steps. The map draws it as a box holding its steps. |
| workflow(name, fn) | A workflow: a plain async (input) => output. Calling the handle runs it. |
| .trigger(type, value) | "api" + route, or "cron" + schedule. Chainable. |
| sleep(duration) | "500ms", "30s", "5m", "2h", "1d". |
| callWorkflow(wf, input) | Runs another workflow: in-process, or on the project's runtime once one is set. |
| onEvent(fn) | Every run event as it happens: started, completed and failed, for workflows, groups and steps. |
| onRun(flows, fn) | Every finished run, replayed on its map: the node each call ran at and the edges it took. |
| isDeployed() | true inside workflows running on BuildShip, false locally. Works at file load too. |
@bship/sdk/client exports callWorkflow(name, input), fetchGraph() and fetchRuns() (the map
and the latest runs buildship dev serves), currentProject() and projectUrls(project). In app
code, currentProject() tells you where calls go: undefined under npm run dev, the project id
in a production build.
The map is read from the workflow body, so the body sticks to shapes it can draw: await every
step (and only steps: wrap other async work in one), branch with if/switch, loop with
for/while, and run steps in parallel with await Promise.all([...]). npx buildship graph
names the line and the fix for anything else.
