@softr/sdk
Version:
The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.
41 lines (40 loc) • 1.75 kB
JavaScript
//#region src/codegen/softr-fields.ts
/**
* Renders the app's `src/softr-fields.ts`: every registered datasource's fields by id, with the type
* and options a value's text depends on, registered with the SDK as a side effect of being imported.
* The root route imports it once, and `formatValue(query, alias, value, formatting)` then formats any
* value a query read by its field's own type.
*/
function renderSoftrFields(datasources, sdkVersion) {
const entries = [...datasources].sort((a, b) => a.id.localeCompare(b.id)).map((datasource) => {
const fields = [...datasource.fields].sort((a, b) => a.id.localeCompare(b.id)).map((field) => {
const shape = { type: field.type };
if (field.options !== null && field.options !== void 0 && Object.keys(field.options).length > 0) shape.options = field.options;
if (field.allowMultipleEntries) shape.allowMultipleEntries = true;
return ` ${JSON.stringify(field.id)}: ${JSON.stringify(shape)},`;
});
return [
` // ${datasource.name}`,
` ${JSON.stringify(datasource.id)}: {`,
...fields,
" },"
].join("\n");
});
return [
`// Generated by @softr/sdk ${sdkVersion}. Do not edit: the SDK's codegen rewrites it after a schema change.`,
"//",
"// The type and options of every field, by datasource and field id, for `formatValue` and",
"// `fieldShape` from @softr/sdk. Imported once, in the root route, it registers them with the SDK.",
"import { registerSoftrFields } from \"@softr/sdk\";",
"import type { SoftrFieldRegistry } from \"@softr/sdk\";",
"",
"export const softrFields = {",
...entries,
"} as const satisfies SoftrFieldRegistry;",
"",
"registerSoftrFields(softrFields);",
""
].join("\n");
}
//#endregion
export { renderSoftrFields };