UNPKG

@softr/sdk

Version:

The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.

126 lines (124 loc) • 4.7 kB
import { assert } from "./assertions.js"; import { aliasesComparedWithUser, buildEnforcedFilter } from "./datasource/enforced-filter.js"; //#region src/action.ts function defineQuery(definition) { const { id, filterFields, filter } = definition; assert(filter === void 0 === (filterFields === void 0), () => `defineQuery "${id}": declare filter and filterFields together. The filter names aliases of filterFields.`); return { id, resource: { type: "DATASOURCE", datasourceId: definition.datasourceId, operation: "read", fields: definition.fields, filterFields, filter: filter === void 0 ? void 0 : buildEnforcedFilter(filter, Object.keys(filterFields ?? {})) }, permissions: definition.permissions }; } function defineMutation(definition) { const { id, datasourceId, operation, fields, filterFields, filter, permissions } = definition; assert(filter === void 0 === (filterFields === void 0), () => `defineMutation "${id}": declare filter and filterFields together. The filter names aliases of filterFields.`); assert(filter === void 0 || operation === "update" || operation === "delete", () => `defineMutation "${id}": a ${operation} mutation takes no filter. Only an update or a delete has one.`); const enforced = filter === void 0 ? void 0 : buildEnforcedFilter(filter, Object.keys(filterFields ?? {})); if (operation === "update" && enforced !== void 0) for (const [alias, token] of aliasesComparedWithUser(enforced.filterConditionGroup)) { const fieldId = filterFields?.[alias]; const written = Object.entries(fields ?? {}).find(([, writtenId]) => writtenId === fieldId); assert(written === void 0, () => `defineMutation "${id}": "${written?.[0]}" in fields writes ${fieldId}, and the filter compares that field with "${token}". A user could then give the record to another user. Remove "${written?.[0]}" from fields. If some users must reassign records, declare a separate mutation for them, with its own permissions and without this filter.`); } return { id, resource: { type: "DATASOURCE", datasourceId, operation, fields, filterFields, filter: enforced }, permissions }; } function defineWorkflow(definition) { const { id, workflowId, triggerId, body, permissions } = definition; return { id, resource: { type: "WORKFLOW", workflowId, triggerId, body }, permissions }; } function defineIntegration(definition) { const { id, integrationId, method, url, permissions } = definition; return { id, resource: { type: "INTEGRATION", integrationId, method, url }, permissions }; } /** * Declares an email action: who may send it, and the fields every email it sends keeps whatever the * call says. Softr sends it and bills the workspace's workflow credits. * * - `to`, `cc`, `bcc`: addresses. An element may be a whole `{LOGGED_IN_USER:EMAIL}` or * `{LOGGED_IN_USER:<field>}` placeholder, filled in for the signed-in sender. * - `replyTo`: an address or one such placeholder. * - `senderId`: one of the workspace's email senders, for an email that must come from that address. * Left out, the app's sender is used, else Softr's testing address. The call can never choose the sender. * * A field declared here wins over the call's, and declaring `to` also ignores the call's `cc` and * `bcc`. Fix the recipients whenever the use case allows. An action that leaves `to` out lets the * caller choose the recipients, so Softr stores it only for custom user groups: * `{ userGroupTypes: ["LOGGED_IN_USERS"], customGroups: [...] }`. * * Pass the returned object to `email.send()` at runtime. * * @example * const notifyOwner = defineEmailAction({ * id: "notifyOwner", * to: ["owner@acme.com"], * permissions: { userGroupTypes: ["NON_LOGGED_IN_USERS", "LOGGED_IN_USERS"] }, * }) * * await email.send(notifyOwner, { subject: "New submission", body: html, replyTo: data.email }) */ function defineEmailAction(definition) { const { id, permissions,...fields } = definition; return { id, resource: { type: "SOFTR_EMAIL", ...fields }, permissions }; } function isDatasourceAction(value) { if (typeof value !== "object" || value === null) return false; const candidate = value; const resource = candidate.resource; return typeof candidate.id === "string" && typeof resource === "object" && resource !== null && resource.type === "DATASOURCE"; } function defineAiAction(definition) { const { id, model, permissions } = definition; return { id, resource: { type: "SOFTR_AI", model }, permissions }; } //#endregion export { defineAiAction, defineEmailAction, defineIntegration, defineMutation, defineQuery, defineWorkflow, isDatasourceAction };