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.

71 lines (69 loc) • 2.9 kB
import { assert } from "../assertions.js"; import { fetch } from "../fetch.js"; import { readSessionToken } from "../auth/session.js"; import { makeChain } from "../datasource/fields.js"; import { describeFailure, dsUrl, listRecords, refererHeader, resolveTarget } from "../datasource/record.js"; import "@tanstack/react-start/server-only"; //#region src/core/options.ts /** * The choices a SELECT field holds right now, read from Softr on every call. A choice the * builder adds or renames later shows up without a new build. The field must be one the * action declares. Any query or mutation over the table works. * * @example * const loadCategories = createServerFn().handler(() => ds.choices(products, 'category')) */ async function listChoices(action, field) { const { actionId, select } = resolveTarget(action, action.resource.operation); const fieldId = select[field]; assert(fieldId, () => `action "${actionId}" declares no field "${field}"`); const res = await fetch(`${dsUrl(actionId)}/metadata`, { headers: { Authorization: `Bearer ${readSessionToken()}`, ...refererHeader() } }); if (!res.ok) throw new Error(`datasource metadata failed for "${actionId}": ${await describeFailure(res)}`); const metadataField = (await res.json()).fields.find(({ id }) => id === fieldId); assert(metadataField?.type === "SELECT", () => `"${field}" on action "${actionId}" is not a SELECT field`); return (metadataField.options?.choices ?? []).map(({ id, label, color }) => ({ id, label, color: color ?? null })); } /** * The rows a linked field can point at, for a picker: each record of the query as its id and the text * of one field, searched on that field when a term is given, a page at a time. * * @example * const loadCompanies = createServerFn() * .validator(z.object({ term: z.string(), offset: z.number().nullable() })) * .handler(({ data }) => ds.options(companiesQuery, { label: 'name', ...data })) */ async function listOptions(query, { label, term = "", offset = null, limit = 20 }) { let list = listRecords(query).limit(limit).offset(offset); const needle = term.trim(); if (needle !== "") list = list.where(() => makeChain(label).contains(needle)); const page = await list; return { options: page.records.map((record) => ({ id: record.id, label: labelOf(record.fields[label]) })), nextOffset: page.nextOffset }; } /** * The text a label field holds: its own text, or the label of the choice or record it names. * A multi-value field, such as a lookup, joins the labels of its values. */ function labelOf(value) { if (value === null || value === void 0) return ""; if (Array.isArray(value)) return value.map(labelOf).filter((text) => text !== "").join(", "); if (typeof value === "object") { const label = value.label; return typeof label === "string" ? label : ""; } return String(value); } //#endregion export { listChoices, listOptions };