@softr/sdk
Version:
The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.
108 lines (95 loc) • 3.31 kB
JavaScript
import { renderDatasourceTypes, renderUserProperties } from "./datasource-types.js";
import { renderUserGroups } from "./user-groups.js";
//#region src/codegen/softr-env.ts
function renderSoftrEnv(input, sdkVersion) {
const { datasourceTypes, interfaces } = renderDatasourceTypes(input.datasources);
return [
header(sdkVersion),
"",
PRELUDE,
"",
datasourceTypes,
"",
"declare module \"@softr/sdk\" {",
indent(interfaces),
"",
indent(renderUserGroups(input.userGroups)),
...input.users === null ? [] : ["", indent(renderUserProperties(input.users.properties, input.datasources))],
"}",
""
].join("\n");
}
function indent(block) {
return block.split("\n").map((line) => line === "" ? line : ` ${line}`).join("\n");
}
/**
* The `export {}` is what makes the generated file a module, and it has to stay. In a file with
* no top-level import or export, `declare module "@softr/sdk"` is not an augmentation, it is an
* ambient declaration that replaces the package, and every export the block does not name
* disappears from the app. Nothing else in the file is exported, so this is the only thing
* holding the file's module-ness up.
*/
function header(sdkVersion) {
return `/**
* THIS FILE IS AUTO GENERATED, DO NOT EDIT THIS MANUALLY
*
* This file exists for type checking. See docs/datasources/ for field reference
* and guide how to use it, and docs/user-groups.md for the app's user groups.
*
* Generated by @softr/sdk@${sdkVersion}
* This file is regenerated automatically when a change is made to the datasource
* or to the app's user groups
*/
export {};`;
}
/**
* The shared value aliases, unexported.
*
* They are the same for every project, so they belong in the SDK, which does not export them
* yet. When it does, they move into the import at the top and this block goes.
*
* The formatted strings are all `string`, and the format is a docstring rather than a template
* literal type. A template literal would reject a value read out of a form input or a
* `useState`, which is where these come from, so every call site would need a cast and the type
* would buy nothing. The data utilities validate the format with zod at the point of the write
* instead, which reports a bad date to the user rather than to the compiler.
*/
const PRELUDE = `/** A date, as \`2026-03-14\`. */
type IsoDate = string;
/** An ISO 8601 instant, as \`2026-03-14T09:30:00.000Z\`. */
type IsoDateTime = string;
/** A time, as \`09:30:00\`. */
type IsoTime = string;
type EmailAddress = string;
type PhoneNumber = string;
type HttpUrl = string;
type LinkedRecord<T = string> = {
id: string;
label: T | null;
};
type LinkedRecordInput = { id: string };
type WorkspaceUser = {
id: string;
name: string | null;
email: EmailAddress | null;
avatarUrl: string | null;
};
type Attachment = {
id: string | null;
filename: string | null;
type: string | null;
url: string;
expiresAt: IsoDateTime | null;
size?: number;
thumbnails?: Array<{ size: "small" | "medium" | "large"; url: string }>;
};
type AttachmentInput = {
url: string;
filename?: string | null;
id?: string | null;
type?: string | null;
};
type SelectChoice = { id: string; label: string | null };
type SelectChoiceInput = { id: string };`;
//#endregion
export { renderSoftrEnv };