@softr/sdk
Version:
The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.
39 lines (37 loc) • 1.87 kB
JavaScript
import { softrApiUrl } from "../softr-api.js";
import { fetch } from "../fetch.js";
import { sessionConfig } from "../auth/config.js";
import { assertCanChange, readSessionToken } from "../auth/session.js";
import { describeFailure } from "../datasource/record.js";
//#region src/core/workflow.ts
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
async function triggerWorkflow(action, payload) {
assertCanChange(`Workflow "${action.id}"`);
const triggerRes = await fetch(`${softrApiUrl("SOFTR_PLATFORM_API_URL")}/v1/vibe/applications/${sessionConfig().applicationId}/actions/${action.id}/workflow`, {
method: "POST",
headers: {
Authorization: `Bearer ${readSessionToken()}`,
"Content-Type": "application/json"
},
body: JSON.stringify({ input: payload ?? {} })
});
if (!triggerRes.ok) throw new Error(`Workflow trigger failed: ${await describeFailure(triggerRes)}`);
let details = await triggerRes.json();
while (details.status !== "FINISHED") {
if (details.status === "PENDING_INTERACTION") for (const interaction of details.nextInteractions) {
const interactionRes = await fetch(`${softrApiUrl("SOFTR_WORKFLOWS_API_URL")}/v1/interactive-actions/executions/${details.id}/actions/${interaction.id}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: "{}"
});
if (!interactionRes.ok) throw new Error(`Workflow interaction failed: ${await describeFailure(interactionRes)}`);
}
await sleep(1e3);
const detailsRes = await fetch(`${softrApiUrl("SOFTR_WORKFLOWS_API_URL")}/v1/workflows/${action.resource.workflowId}/executions/${details.id}/details`);
if (!detailsRes.ok) throw new Error(`Workflow status check failed: ${await describeFailure(detailsRes)}`);
details = await detailsRes.json();
}
}
const workflow = { trigger: triggerWorkflow };
//#endregion
export { workflow };