import { AnyParser } from "ts-matches" import { ManifestVersion, SDKManifest } from "./manifest/ManifestTypes" import { RequiredDefault, Value } from "./config/builder/value" import { Config, ExtractConfigType, LazyBuild } from "./config/builder/config" import { DefaultString, ListValueSpecText, Pattern, RandomString, UniqueBy, ValueSpecDatetime, ValueSpecText, } from "./config/configTypes" import { Variants } from "./config/builder/variants" import { CreatedAction, createAction } from "./actions/createAction" import { ActionMetaData, Effects, ActionResult, Metadata, BackupOptions, } from "./types" import { Utils } from "./util/utils" import { AutoConfig, AutoConfigFrom } from "./autoconfig/AutoConfig" import { BackupSet, Backups } from "./backup/Backups" import { smtpConfig } from "./config/configConstants" import { Daemons } from "./mainFn/Daemons" import { healthCheck } from "./health/HealthCheck" import { checkPortListening, containsAddress, } from "./health/checkFns/checkPortListening" import { checkWebUrl, runHealthScript } from "./health/checkFns" import { List } from "./config/builder/list" import { Migration } from "./inits/migrations/Migration" import { Install, InstallFn, setupInstall } from "./inits/setupInstall" import { setupActions } from "./actions/setupActions" import { setupAutoConfig } from "./autoconfig/setupAutoConfig" import { SetupBackupsParams, setupBackups } from "./backup/setupBackups" import { setupInit } from "./inits/setupInit" import { EnsureUniqueId, Migrations, setupMigrations, } from "./inits/migrations/setupMigrations" import { Uninstall, UninstallFn, setupUninstall } from "./inits/setupUninstall" import { setupMain } from "./mainFn" import { defaultTrigger } from "./trigger/defaultTrigger" import { changeOnFirstSuccess, cooldownTrigger } from "./trigger" import setupConfig, { Read, Save } from "./config/setupConfig" export type SdkBuilt = StartSdk["build"] // prettier-ignore type AnyNeverCond = T extends [] ? Else : T extends [never, ...Array] ? Then : T extends [any, ...infer U] ? AnyNeverCond : never export class StartSdk { private constructor() {} private anyOf( a: A, ): AnyNeverCond<[Manifest, Store], "Build not ready", A> { return a as any } static of() { return new StartSdk() } withManifest() { return new StartSdk() } withStore>() { return new StartSdk() } build() { return this.anyOf({ AutoConfig: ( configs: AutoConfigFrom, path: keyof AutoConfigFrom, ) => new AutoConfig(configs, path), Backups: { volumes: (...volumeNames: Array) => Backups.volumes(...volumeNames), addSets: ( ...options: BackupSet[] ) => Backups.addSets(...options), withOptions: (options?: Partial) => Backups.with_options(options), }, Config: { of: < Spec extends Record | Value>, >( spec: Spec, ) => Config.of(spec), }, configConstants: { smtpConfig }, createAction: < Store, ConfigType extends | Record | Config | Config, Type extends Record = ExtractConfigType, >( metaData: Omit & { input: Config | Config }, fn: (options: { effects: Effects utils: Utils input: Type }) => Promise, ) => createAction(metaData, fn), Daemons: { of: Daemons.of }, healthCheck: { checkPortListening, checkWebUrl, of: healthCheck, runHealthScript, }, List: { text: List.text, number: List.number, obj: >( a: { name: string description?: string | null warning?: string | null /** Default [] */ default?: [] minLength?: number | null maxLength?: number | null }, aSpec: { spec: Config displayAs?: null | string uniqueBy?: null | UniqueBy }, ) => List.obj(a, aSpec), dynamicText: ( getA: LazyBuild< Store, { name: string description?: string | null warning?: string | null /** Default = [] */ default?: string[] minLength?: number | null maxLength?: number | null disabled?: false | string generate?: null | RandomString spec: { /** Default = false */ masked?: boolean placeholder?: string | null minLength?: number | null maxLength?: number | null patterns: Pattern[] /** Default = "text" */ inputmode?: ListValueSpecText["inputmode"] } } >, ) => List.dynamicText(getA), dynamicNumber: ( getA: LazyBuild< Store, { name: string description?: string | null warning?: string | null /** Default = [] */ default?: string[] minLength?: number | null maxLength?: number | null disabled?: false | string spec: { integer: boolean min?: number | null max?: number | null step?: string | null units?: string | null placeholder?: string | null } } >, ) => List.dynamicNumber(getA), }, Migration: { of: (options: { version: Version up: (opts: { effects: Effects; utils: Utils }) => Promise down: (opts: { effects: Effects utils: Utils }) => Promise }) => Migration.of(options), }, setupActions: (...createdActions: CreatedAction[]) => setupActions(...createdActions), setupAutoConfig: < Input, NestedConfigs extends { [key in keyof Manifest["dependencies"]]: unknown }, >( configs: AutoConfigFrom, ) => setupAutoConfig(configs), setupBackups: (...args: SetupBackupsParams) => setupBackups(...args), setupConfig: < ConfigType extends | Record | Config | Config, Type extends Record = ExtractConfigType, >( spec: Config | Config, write: Save, read: Read, ) => setupConfig(spec, write, read), setupInit: ( migrations: Migrations, install: Install, uninstall: Uninstall, ) => setupInit(migrations, install, uninstall), setupInstall: (fn: InstallFn) => Install.of(fn), setupMain: ( fn: (o: { effects: Effects started(onTerm: () => void): null utils: Utils }) => Promise>, ) => setupMain(fn), setupMigrations: >>( manifest: SDKManifest, ...migrations: EnsureUniqueId ) => setupMigrations(manifest, ...migrations), setupUninstall: (fn: UninstallFn) => setupUninstall(fn), trigger: { defaultTrigger, cooldownTrigger, changeOnFirstSuccess, }, Value: { toggle: Value.toggle, text: Value.text, textarea: Value.textarea, number: Value.number, color: Value.color, datetime: Value.datetime, select: Value.select, multiselect: Value.multiselect, object: Value.object, union: Value.union, list: Value.list, dynamicToggle: ( a: LazyBuild< Store, { name: string description?: string | null warning?: string | null default: boolean disabled?: false | string } >, ) => Value.dynamicToggle(a), dynamicText: ( getA: LazyBuild< Store, { name: string description?: string | null warning?: string | null required: RequiredDefault /** Default = false */ masked?: boolean placeholder?: string | null minLength?: number | null maxLength?: number | null patterns?: Pattern[] /** Default = 'text' */ inputmode?: ValueSpecText["inputmode"] generate?: null | RandomString } >, ) => Value.dynamicText(getA), dynamicTextarea: ( getA: LazyBuild< Store, { name: string description?: string | null warning?: string | null required: boolean minLength?: number | null maxLength?: number | null placeholder?: string | null disabled?: false | string generate?: null | RandomString } >, ) => Value.dynamicTextarea(getA), dynamicNumber: ( getA: LazyBuild< Store, { name: string description?: string | null warning?: string | null required: RequiredDefault min?: number | null max?: number | null /** Default = '1' */ step?: string | null integer: boolean units?: string | null placeholder?: string | null disabled?: false | string } >, ) => Value.dynamicNumber(getA), dynamicColor: ( getA: LazyBuild< Store, { name: string description?: string | null warning?: string | null required: RequiredDefault disabled?: false | string } >, ) => Value.dynamicColor(getA), dynamicDatetime: ( getA: LazyBuild< Store, { name: string description?: string | null warning?: string | null required: RequiredDefault /** Default = 'datetime-local' */ inputmode?: ValueSpecDatetime["inputmode"] min?: string | null max?: string | null step?: string | null disabled?: false | string } >, ) => Value.dynamicDatetime(getA), dynamicSelect: ( getA: LazyBuild< Store, { name: string description?: string | null warning?: string | null required: RequiredDefault values: Record disabled?: false | string } >, ) => Value.dynamicSelect(getA), dynamicMultiselect: ( getA: LazyBuild< Store, { name: string description?: string | null warning?: string | null default: string[] values: Record minLength?: number | null maxLength?: number | null disabled?: false | string } >, ) => Value.dynamicMultiselect(getA), filteredUnion: < Required extends RequiredDefault, Type extends Record, >( getDisabledFn: LazyBuild, a: { name: string description?: string | null warning?: string | null required: Required }, aVariants: Variants | Variants, ) => Value.filteredUnion( getDisabledFn, a, aVariants, ), }, Variants: { of: < VariantValues extends { [K in string]: { name: string spec: Config } }, >( a: VariantValues, ) => Variants.of(a), }, }) } }