From 3f5dbc6a4b626bd4bd9fe565567a0e0da08a87e8 Mon Sep 17 00:00:00 2001 From: Blu-J Date: Tue, 30 May 2023 17:36:30 -0600 Subject: [PATCH] feat: Remove the vault --- lib/StartSdk.ts | 181 +++++------- lib/actions/createAction.ts | 24 +- lib/actions/setupActions.ts | 8 +- lib/config/builder/config.ts | 49 ++-- lib/config/builder/list.ts | 26 +- lib/config/builder/value.ts | 260 ++++++++---------- lib/config/builder/variants.ts | 14 +- lib/config/configConstants.ts | 2 +- lib/config/setupConfig.ts | 27 +- lib/dependencyConfig/DependencyConfig.ts | 7 +- lib/dependencyConfig/setupDependencyConfig.ts | 4 +- lib/inits/migrations/Migration.ts | 30 +- lib/inits/migrations/setupMigrations.ts | 31 +-- lib/inits/setupInit.ts | 10 +- lib/inits/setupInstall.ts | 12 +- lib/inits/setupUninstall.ts | 12 +- lib/interfaces/setupInterfaces.ts | 10 +- lib/mainFn/index.ts | 6 +- lib/test/output.sdk.ts | 1 - lib/test/store.test.ts | 7 +- lib/types.ts | 8 - lib/util/getVault.ts | 44 --- lib/util/index.ts | 4 +- lib/util/utils.ts | 46 +--- 24 files changed, 308 insertions(+), 515 deletions(-) delete mode 100644 lib/util/getVault.ts diff --git a/lib/StartSdk.ts b/lib/StartSdk.ts index b482acb..a013d57 100644 --- a/lib/StartSdk.ts +++ b/lib/StartSdk.ts @@ -52,6 +52,7 @@ import { setupInterfaces, } from "./interfaces/setupInterfaces" import { successFailure } from "./trigger/successFailure" +import { SetupExports } from "./inits/setupExports" // prettier-ignore type AnyNeverCond = @@ -60,42 +61,37 @@ type AnyNeverCond = T extends [any, ...infer U] ? AnyNeverCond : never -export class StartSdk { +export class StartSdk { private constructor(readonly manifest: Manifest) {} static of() { - return new StartSdk(null as never) + return new StartSdk(null as never) } withManifest(manifest: Manifest) { - return new StartSdk(manifest) + return new StartSdk(manifest) } withStore>() { - return new StartSdk(this.manifest) - } - withVault>() { - return new StartSdk(this.manifest) + return new StartSdk(this.manifest) } - build( - isReady: AnyNeverCond<[Manifest, Store, Vault], "Build not ready", true>, - ) { + build(isReady: AnyNeverCond<[Manifest, Store], "Build not ready", true>) { return { configConstants: { smtpConfig }, createAction: < ConfigType extends | Record - | Config - | Config, + | Config + | Config, Type extends Record = ExtractConfigType, >( metaData: Omit & { - input: Config | Config + input: Config | Config }, fn: (options: { effects: Effects - utils: Utils + utils: Utils input: Type }) => Promise, - ) => createAction(metaData, fn), + ) => createAction(metaData, fn), HealthCheck: { of: healthCheck, }, @@ -106,84 +102,78 @@ export class StartSdk { }, patterns, setupActions: (...createdActions: CreatedAction[]) => - setupActions(...createdActions), + setupActions(...createdActions), setupBackups: (...args: SetupBackupsParams) => setupBackups(...args), setupConfig: < - ConfigType extends - | Config - | Config, + ConfigType extends Config | Config, Type extends Record = ExtractConfigType, >( spec: ConfigType, - write: Save, - read: Read, - ) => - setupConfig( - spec, - write, - read, - ), + write: Save, + read: Read, + ) => setupConfig(spec, write, read), setupConfigRead: < ConfigSpec extends - | Config, any, any> - | Config, never, never>, + | Config, any> + | Config, never>, >( _configSpec: ConfigSpec, - fn: Read, + fn: Read, ) => fn, setupConfigSave: < ConfigSpec extends - | Config, any, any> - | Config, never, never>, + | Config, any> + | Config, never>, >( _configSpec: ConfigSpec, - fn: Save, + fn: Save, ) => fn, setupDependencyConfig: >( - config: Config | Config, + config: Config | Config, autoConfigs: { [K in keyof Manifest["dependencies"]]: DependencyConfig< Store, - Vault, Input, any > }, - ) => - setupDependencyConfig( - config, - autoConfigs, - ), + ) => setupDependencyConfig(config, autoConfigs), + setupExports: (fn: SetupExports) => fn, setupDependencyMounts, setupInit: ( - migrations: Migrations, - install: Install, - uninstall: Uninstall, - setInterfaces: SetInterfaces, + migrations: Migrations, + install: Install, + uninstall: Uninstall, + setInterfaces: SetInterfaces, + setupExports: SetupExports, ) => - setupInit(migrations, install, uninstall, setInterfaces), - setupInstall: (fn: InstallFn) => Install.of(fn), + setupInit( + migrations, + install, + uninstall, + setInterfaces, + setupExports, + ), + setupInstall: (fn: InstallFn) => Install.of(fn), setupInterfaces: < ConfigInput extends Record, Output extends InterfacesReceipt, >( - config: Config, - fn: SetInterfaces, + config: Config, + fn: SetInterfaces, ) => setupInterfaces(config, fn), setupMain: ( fn: (o: { effects: Effects started(onTerm: () => void): null - utils: Utils + utils: Utils }) => Promise>, - ) => setupMain(fn), - setupMigrations: >>( + ) => setupMain(fn), + setupMigrations: >>( ...migrations: EnsureUniqueId - ) => - setupMigrations(this.manifest, ...migrations), - setupUninstall: (fn: UninstallFn) => - setupUninstall(fn), + ) => setupMigrations(this.manifest, ...migrations), + setupUninstall: (fn: UninstallFn) => setupUninstall(fn), trigger: { defaultTrigger, cooldownTrigger, @@ -202,13 +192,10 @@ export class StartSdk { }, Config: { of: < - Spec extends Record< - string, - Value | Value - >, + Spec extends Record | Value>, >( spec: Spec, - ) => Config.of(spec), + ) => Config.of(spec), }, Daemons: { of: Daemons.of }, DependencyConfig: { @@ -220,20 +207,16 @@ export class StartSdk { remoteConfig, dependencyConfig, }: { - localConfig: - | Config - | Config - remoteConfig: - | Config - | Config + localConfig: Config | Config + remoteConfig: Config | Config dependencyConfig: (options: { effects: Effects localConfig: LocalConfig remoteConfig: RemoteConfig - utils: Utils + utils: Utils }) => Promise> }) { - return new DependencyConfig( + return new DependencyConfig( dependencyConfig, ) }, @@ -252,15 +235,14 @@ export class StartSdk { maxLength?: number | null }, aSpec: { - spec: Config + spec: Config displayAs?: null | string uniqueBy?: null | UniqueBy }, - ) => List.obj(a, aSpec), + ) => List.obj(a, aSpec), dynamicText: ( getA: LazyBuild< Store, - Vault, { name: string description?: string | null @@ -283,11 +265,10 @@ export class StartSdk { } } >, - ) => List.dynamicText(getA), + ) => List.dynamicText(getA), dynamicNumber: ( getA: LazyBuild< Store, - Vault, { name: string description?: string | null @@ -307,20 +288,17 @@ export class StartSdk { } } >, - ) => List.dynamicNumber(getA), + ) => List.dynamicNumber(getA), }, Migration: { of: (options: { version: Version - up: (opts: { - effects: Effects - utils: Utils - }) => Promise + up: (opts: { effects: Effects; utils: Utils }) => Promise down: (opts: { effects: Effects - utils: Utils + utils: Utils }) => Promise - }) => Migration.of(options), + }) => Migration.of(options), }, Value: { toggle: Value.toggle, @@ -337,7 +315,6 @@ export class StartSdk { dynamicToggle: ( a: LazyBuild< Store, - Vault, { name: string description?: string | null @@ -346,11 +323,10 @@ export class StartSdk { disabled?: false | string } >, - ) => Value.dynamicToggle(a), + ) => Value.dynamicToggle(a), dynamicText: ( getA: LazyBuild< Store, - Vault, { name: string description?: string | null @@ -368,11 +344,10 @@ export class StartSdk { generate?: null | RandomString } >, - ) => Value.dynamicText(getA), + ) => Value.dynamicText(getA), dynamicTextarea: ( getA: LazyBuild< Store, - Vault, { name: string description?: string | null @@ -385,11 +360,10 @@ export class StartSdk { generate?: null | RandomString } >, - ) => Value.dynamicTextarea(getA), + ) => Value.dynamicTextarea(getA), dynamicNumber: ( getA: LazyBuild< Store, - Vault, { name: string description?: string | null @@ -405,11 +379,10 @@ export class StartSdk { disabled?: false | string } >, - ) => Value.dynamicNumber(getA), + ) => Value.dynamicNumber(getA), dynamicColor: ( getA: LazyBuild< Store, - Vault, { name: string description?: string | null @@ -419,11 +392,10 @@ export class StartSdk { disabled?: false | string } >, - ) => Value.dynamicColor(getA), + ) => Value.dynamicColor(getA), dynamicDatetime: ( getA: LazyBuild< Store, - Vault, { name: string description?: string | null @@ -436,11 +408,10 @@ export class StartSdk { disabled?: false | string } >, - ) => Value.dynamicDatetime(getA), + ) => Value.dynamicDatetime(getA), dynamicSelect: ( getA: LazyBuild< Store, - Vault, { name: string description?: string | null @@ -450,11 +421,10 @@ export class StartSdk { disabled?: false | string } >, - ) => Value.dynamicSelect(getA), + ) => Value.dynamicSelect(getA), dynamicMultiselect: ( getA: LazyBuild< Store, - Vault, { name: string description?: string | null @@ -466,23 +436,21 @@ export class StartSdk { disabled?: false | string } >, - ) => Value.dynamicMultiselect(getA), + ) => Value.dynamicMultiselect(getA), filteredUnion: < Required extends RequiredDefault, Type extends Record, >( - getDisabledFn: LazyBuild, + getDisabledFn: LazyBuild, a: { name: string description?: string | null warning?: string | null required: Required }, - aVariants: - | Variants - | Variants, + aVariants: Variants | Variants, ) => - Value.filteredUnion( + Value.filteredUnion( getDisabledFn, a, aVariants, @@ -494,7 +462,6 @@ export class StartSdk { >( getA: LazyBuild< Store, - Vault, { disabled: string[] | false | string name: string @@ -503,22 +470,20 @@ export class StartSdk { required: Required } >, - aVariants: - | Variants - | Variants, - ) => Value.dynamicUnion(getA, aVariants), + aVariants: Variants | Variants, + ) => Value.dynamicUnion(getA, aVariants), }, Variants: { of: < VariantValues extends { [K in string]: { name: string - spec: Config + spec: Config } }, >( a: VariantValues, - ) => Variants.of(a), + ) => Variants.of(a), }, } } diff --git a/lib/actions/createAction.ts b/lib/actions/createAction.ts index 7e0979c..a04302d 100644 --- a/lib/actions/createAction.ts +++ b/lib/actions/createAction.ts @@ -5,47 +5,45 @@ import { Utils, utils } from "../util/utils" export class CreatedAction< Store, - Vault, ConfigType extends | Record - | Config - | Config, + | Config + | Config, Type extends Record = ExtractConfigType, > { private constructor( public readonly myMetaData: Omit, readonly fn: (options: { effects: Effects - utils: Utils + utils: Utils input: Type }) => Promise, - readonly input: Config, + readonly input: Config, ) {} public validator = this.input.validator static of< Store, - Vault, ConfigType extends | Record - | Config - | Config, + | Config + | Config, Type extends Record = ExtractConfigType, >( metaData: Omit & { - input: Config | Config + input: Config | Config }, fn: (options: { effects: Effects - utils: Utils + utils: Utils input: Type }) => Promise, ) { const { input, ...rest } = metaData - return new CreatedAction( + return new CreatedAction( rest, fn, - input as Config, + input as Config, ) } @@ -67,7 +65,7 @@ export class CreatedAction< async ActionMetadata(options: { effects: Effects - utils: Utils + utils: Utils }): Promise { return { ...this.myMetaData, diff --git a/lib/actions/setupActions.ts b/lib/actions/setupActions.ts index b763f9c..735fe8e 100644 --- a/lib/actions/setupActions.ts +++ b/lib/actions/setupActions.ts @@ -3,11 +3,11 @@ import { createUtils } from "../util" import { once } from "../util/once" import { CreatedAction } from "./createAction" -export function setupActions( - ...createdActions: CreatedAction[] +export function setupActions( + ...createdActions: CreatedAction[] ) { const myActions = once(() => { - const actions: Record> = {} + const actions: Record> = {} for (const action of createdActions) { actions[action.myMetaData.id] = action } @@ -21,7 +21,7 @@ export function setupActions( return myActions() }, async actionsMetadata({ effects }: { effects: Effects }) { - const utils = createUtils(effects) + const utils = createUtils(effects) return Promise.all( createdActions.map((x) => x.ActionMetadata({ effects, utils })), ) diff --git a/lib/config/builder/config.ts b/lib/config/builder/config.ts index 0f5c46e..f68b999 100644 --- a/lib/config/builder/config.ts +++ b/lib/config/builder/config.ts @@ -5,28 +5,24 @@ import { _ } from "../../util" import { Effects } from "../../types" import { Parser, object } from "ts-matches" -export type LazyBuildOptions = { +export type LazyBuildOptions = { effects: Effects - utils: Utils + utils: Utils } -export type LazyBuild = ( - options: LazyBuildOptions, +export type LazyBuild = ( + options: LazyBuildOptions, ) => Promise | ExpectedOut // prettier-ignore -export type ExtractConfigType | Config, any, any> | Config, never, never>> = - A extends Config | Config ? B : +export type ExtractConfigType | Config, any> | Config, never>> = + A extends Config | Config ? B : A -export type ConfigSpecOf< - A extends Record, - Store = never, - Vault = never, -> = { - [K in keyof A]: Value +export type ConfigSpecOf, Store = never> = { + [K in keyof A]: Value } -export type MaybeLazyValues = LazyBuild | A +export type MaybeLazyValues = LazyBuild | A /** * Configs are the specs that are used by the os configuration form for this service. * Here is an example of a simple configuration @@ -83,20 +79,14 @@ export const addNodesSpec = Config.of({ hostname: hostname, port: port }); ``` */ -export class Config< - Type extends Record, - Store = never, - Vault = never, -> { +export class Config, Store = never> { private constructor( private readonly spec: { - [K in keyof Type]: - | Value - | Value + [K in keyof Type]: Value | Value }, public validator: Parser, ) {} - async build(options: LazyBuildOptions) { + async build(options: LazyBuildOptions) { const answer = {} as { [K in keyof Type]: ValueSpec } @@ -107,12 +97,8 @@ export class Config< } static of< - Spec extends Record< - string, - Value | Value - >, + Spec extends Record | Value>, Store = never, - Vault = never, >(spec: Spec) { const validatorObj = {} as { [K in keyof Spec]: Parser @@ -124,13 +110,12 @@ export class Config< return new Config< { [K in keyof Spec]: Spec[K] extends - | Value - | Value + | Value + | Value ? T : never }, - Store, - Vault + Store >(spec, validator as any) } @@ -149,6 +134,6 @@ export class Config< ``` */ withStore() { - return this as any as Config + return this as any as Config } } diff --git a/lib/config/builder/list.ts b/lib/config/builder/list.ts index 6198fc6..23de0c4 100644 --- a/lib/config/builder/list.ts +++ b/lib/config/builder/list.ts @@ -22,9 +22,9 @@ export const authorizationList = List.string({ export const auth = Value.list(authorizationList); ``` */ -export class List { +export class List { private constructor( - public build: LazyBuild, + public build: LazyBuild, public validator: Parser, ) {} static text( @@ -49,7 +49,7 @@ export class List { generate?: null | RandomString }, ) { - return new List(() => { + return new List(() => { const spec = { type: "text" as const, placeholder: null, @@ -74,10 +74,9 @@ export class List { return built }, arrayOf(string)) } - static dynamicText( + static dynamicText( getA: LazyBuild< Store, - Vault, { name: string description?: string | null @@ -101,7 +100,7 @@ export class List { } >, ) { - return new List(async (options) => { + return new List(async (options) => { const { spec: aSpec, ...a } = await getA(options) const spec = { type: "text" as const, @@ -146,7 +145,7 @@ export class List { placeholder?: string | null }, ) { - return new List(() => { + return new List(() => { const spec = { type: "number" as const, placeholder: null, @@ -170,10 +169,9 @@ export class List { return built }, arrayOf(number)) } - static dynamicNumber( + static dynamicNumber( getA: LazyBuild< Store, - Vault, { name: string description?: string | null @@ -194,7 +192,7 @@ export class List { } >, ) { - return new List(async (options) => { + return new List(async (options) => { const { spec: aSpec, ...a } = await getA(options) const spec = { type: "number" as const, @@ -218,7 +216,7 @@ export class List { } }, arrayOf(number)) } - static obj, Store, Vault>( + static obj, Store>( a: { name: string description?: string | null @@ -229,12 +227,12 @@ export class List { maxLength?: number | null }, aSpec: { - spec: Config + spec: Config displayAs?: null | string uniqueBy?: null | UniqueBy }, ) { - return new List(async (options) => { + return new List(async (options) => { const { spec: previousSpecSpec, ...restSpec } = aSpec const specSpec = await previousSpecSpec.build(options) const spec = { @@ -276,6 +274,6 @@ export class List { ``` */ withStore() { - return this as any as List + return this as any as List } } diff --git a/lib/config/builder/value.ts b/lib/config/builder/value.ts index 41a3d3b..7092a06 100644 --- a/lib/config/builder/value.ts +++ b/lib/config/builder/value.ts @@ -94,9 +94,9 @@ const username = Value.string({ }); ``` */ -export class Value { +export class Value { protected constructor( - public build: LazyBuild, + public build: LazyBuild, public validator: Parser, ) {} static toggle(a: { @@ -108,7 +108,7 @@ export class Value { Default is false */ immutable?: boolean }) { - return new Value( + return new Value( async () => ({ description: null, warning: null, @@ -120,10 +120,9 @@ export class Value { boolean, ) } - static dynamicToggle( + static dynamicToggle( a: LazyBuild< Store, - Vault, { name: string description?: string | null @@ -133,7 +132,7 @@ export class Value { } >, ) { - return new Value( + return new Value( async (options) => ({ description: null, warning: null, @@ -165,7 +164,7 @@ export class Value { immutable?: boolean generate?: null | RandomString }) { - return new Value, never, never>( + return new Value, never>( async () => ({ type: "text" as const, description: null, @@ -185,10 +184,9 @@ export class Value { asRequiredParser(string, a), ) } - static dynamicText( + static dynamicText( getA: LazyBuild< Store, - Vault, { name: string description?: string | null @@ -211,28 +209,25 @@ export class Value { } >, ) { - return new Value( - async (options) => { - const a = await getA(options) - return { - type: "text" as const, - description: null, - warning: null, - masked: false, - placeholder: null, - minLength: null, - maxLength: null, - patterns: [], - inputmode: "text", - disabled: false, - immutable: false, - generate: a.generate ?? null, - ...a, - ...requiredLikeToAbove(a.required), - } - }, - string.optional(), - ) + return new Value(async (options) => { + const a = await getA(options) + return { + type: "text" as const, + description: null, + warning: null, + masked: false, + placeholder: null, + minLength: null, + maxLength: null, + patterns: [], + inputmode: "text", + disabled: false, + immutable: false, + generate: a.generate ?? null, + ...a, + ...requiredLikeToAbove(a.required), + } + }, string.optional()) } static textarea(a: { name: string @@ -246,7 +241,7 @@ export class Value { Default is false */ immutable?: boolean }) { - return new Value(async () => { + return new Value(async () => { const built: ValueSpecTextarea = { description: null, warning: null, @@ -261,10 +256,9 @@ export class Value { return built }, string) } - static dynamicTextarea( + static dynamicTextarea( getA: LazyBuild< Store, - Vault, { name: string description?: string | null @@ -277,7 +271,7 @@ export class Value { } >, ) { - return new Value(async (options) => { + return new Value(async (options) => { const a = await getA(options) return { description: null, @@ -308,7 +302,7 @@ export class Value { Default is false */ immutable?: boolean }) { - return new Value, never, never>( + return new Value, never>( () => ({ type: "number" as const, description: null, @@ -326,10 +320,9 @@ export class Value { asRequiredParser(number, a), ) } - static dynamicNumber( + static dynamicNumber( getA: LazyBuild< Store, - Vault, { name: string description?: string | null @@ -346,26 +339,23 @@ export class Value { } >, ) { - return new Value( - async (options) => { - const a = await getA(options) - return { - type: "number" as const, - description: null, - warning: null, - min: null, - max: null, - step: null, - units: null, - placeholder: null, - disabled: false, - immutable: false, - ...a, - ...requiredLikeToAbove(a.required), - } - }, - number.optional(), - ) + return new Value(async (options) => { + const a = await getA(options) + return { + type: "number" as const, + description: null, + warning: null, + min: null, + max: null, + step: null, + units: null, + placeholder: null, + disabled: false, + immutable: false, + ...a, + ...requiredLikeToAbove(a.required), + } + }, number.optional()) } static color>(a: { name: string @@ -376,7 +366,7 @@ export class Value { Default is false */ immutable?: boolean }) { - return new Value, never, never>( + return new Value, never>( () => ({ type: "color" as const, description: null, @@ -391,10 +381,9 @@ export class Value { ) } - static dynamicColor( + static dynamicColor( getA: LazyBuild< Store, - Vault, { name: string description?: string | null @@ -404,21 +393,18 @@ export class Value { } >, ) { - return new Value( - async (options) => { - const a = await getA(options) - return { - type: "color" as const, - description: null, - warning: null, - disabled: false, - immutable: false, - ...a, - ...requiredLikeToAbove(a.required), - } - }, - string.optional(), - ) + return new Value(async (options) => { + const a = await getA(options) + return { + type: "color" as const, + description: null, + warning: null, + disabled: false, + immutable: false, + ...a, + ...requiredLikeToAbove(a.required), + } + }, string.optional()) } static datetime>(a: { name: string @@ -433,7 +419,7 @@ export class Value { Default is false */ immutable?: boolean }) { - return new Value, never, never>( + return new Value, never>( () => ({ type: "datetime" as const, description: null, @@ -450,10 +436,9 @@ export class Value { asRequiredParser(string, a), ) } - static dynamicDatetime( + static dynamicDatetime( getA: LazyBuild< Store, - Vault, { name: string description?: string | null @@ -467,24 +452,21 @@ export class Value { } >, ) { - return new Value( - async (options) => { - const a = await getA(options) - return { - type: "datetime" as const, - description: null, - warning: null, - inputmode: "datetime-local", - min: null, - max: null, - disabled: false, - immutable: false, - ...a, - ...requiredLikeToAbove(a.required), - } - }, - string.optional(), - ) + return new Value(async (options) => { + const a = await getA(options) + return { + type: "datetime" as const, + description: null, + warning: null, + inputmode: "datetime-local", + min: null, + max: null, + disabled: false, + immutable: false, + ...a, + ...requiredLikeToAbove(a.required), + } + }, string.optional()) } static select< Required extends RequiredDefault, @@ -505,7 +487,7 @@ export class Value { Default is false */ immutable?: boolean }) { - return new Value, never, never>( + return new Value, never>( () => ({ description: null, warning: null, @@ -523,10 +505,9 @@ export class Value { ) as any, ) } - static dynamicSelect( + static dynamicSelect( getA: LazyBuild< Store, - Vault, { name: string description?: string | null @@ -542,21 +523,18 @@ export class Value { } >, ) { - return new Value( - async (options) => { - const a = await getA(options) - return { - description: null, - warning: null, - type: "select" as const, - disabled: false, - immutable: false, - ...a, - ...requiredLikeToAbove(a.required), - } - }, - string.optional(), - ) + return new Value(async (options) => { + const a = await getA(options) + return { + description: null, + warning: null, + type: "select" as const, + disabled: false, + immutable: false, + ...a, + ...requiredLikeToAbove(a.required), + } + }, string.optional()) } static multiselect>(a: { name: string @@ -576,7 +554,7 @@ export class Value { */ disabled?: false | string | (string & keyof Values)[] }) { - return new Value<(keyof Values)[], never, never>( + return new Value<(keyof Values)[], never>( () => ({ type: "multiselect" as const, minLength: null, @@ -592,10 +570,9 @@ export class Value { ), ) } - static dynamicMultiselect( + static dynamicMultiselect( getA: LazyBuild< Store, - Vault, { name: string description?: string | null @@ -613,7 +590,7 @@ export class Value { } >, ) { - return new Value(async (options) => { + return new Value(async (options) => { const a = await getA(options) return { type: "multiselect" as const, @@ -627,15 +604,15 @@ export class Value { } }, arrayOf(string)) } - static object, Store, Vault>( + static object, Store>( a: { name: string description?: string | null warning?: string | null }, - spec: Config, + spec: Config, ) { - return new Value(async (options) => { + return new Value(async (options) => { const built = await spec.build(options as any) return { type: "object" as const, @@ -646,7 +623,7 @@ export class Value { } }, spec.validator) } - static file(a: { + static file(a: { name: string description?: string | null warning?: string | null @@ -660,17 +637,16 @@ export class Value { ...a, } if (a.required) { - return new Value(() => buildValue, string) + return new Value(() => buildValue, string) } - return new Value( + return new Value( () => buildValue, string.optional(), ) } - static dynamicFile( + static dynamicFile( a: LazyBuild< Store, - Vault, { name: string description?: string | null @@ -680,7 +656,7 @@ export class Value { } >, ) { - return new Value( + return new Value( async (options) => ({ type: "file" as const, description: null, @@ -690,7 +666,7 @@ export class Value { string.optional(), ) } - static union, Type, Store, Vault>( + static union, Type, Store>( a: { name: string description?: string | null @@ -706,9 +682,9 @@ export class Value { */ disabled?: false | string | string[] }, - aVariants: Variants, + aVariants: Variants, ) { - return new Value, Store, Vault>( + return new Value, Store>( async (options) => ({ type: "union" as const, description: null, @@ -726,18 +702,17 @@ export class Value { Required extends RequiredDefault, Type extends Record, Store = never, - Vault = never, >( - getDisabledFn: LazyBuild, + getDisabledFn: LazyBuild, a: { name: string description?: string | null warning?: string | null required: Required }, - aVariants: Variants | Variants, + aVariants: Variants | Variants, ) { - return new Value, Store, Vault>( + return new Value, Store>( async (options) => ({ type: "union" as const, description: null, @@ -755,11 +730,9 @@ export class Value { Required extends RequiredDefault, Type extends Record, Store = never, - Vault = never, >( getA: LazyBuild< Store, - Vault, { disabled: string[] | false | string name: string @@ -768,9 +741,9 @@ export class Value { required: Required } >, - aVariants: Variants | Variants, + aVariants: Variants | Variants, ) { - return new Value(async (options) => { + return new Value(async (options) => { const newValues = await getA(options) return { type: "union" as const, @@ -784,11 +757,8 @@ export class Value { }, aVariants.validator.optional()) } - static list(a: List) { - return new Value( - (options) => a.build(options), - a.validator, - ) + static list(a: List) { + return new Value((options) => a.build(options), a.validator) } /** @@ -806,6 +776,6 @@ export class Value { ``` */ withStore() { - return this as any as Value + return this as any as Value } } diff --git a/lib/config/builder/variants.ts b/lib/config/builder/variants.ts index 8404742..1e7a2a3 100644 --- a/lib/config/builder/variants.ts +++ b/lib/config/builder/variants.ts @@ -51,21 +51,20 @@ export const pruning = Value.union( ); ``` */ -export class Variants { +export class Variants { static text: any private constructor( - public build: LazyBuild, + public build: LazyBuild, public validator: Parser, ) {} static of< VariantValues extends { [K in string]: { name: string - spec: Config | Config + spec: Config | Config } }, Store = never, - Vault = never, >(a: VariantValues) { const validator = anyOf( ...Object.entries(a).map(([name, { spec }]) => @@ -82,12 +81,11 @@ export class Variants { unionSelectKey: K // prettier-ignore unionValueKey: - VariantValues[K]["spec"] extends (Config | Config) ? B : + VariantValues[K]["spec"] extends (Config | Config) ? B : never } }[keyof VariantValues], - Store, - Vault + Store >(async (options) => { const variants = {} as { [K in keyof VariantValues]: { name: string; spec: InputSpec } @@ -117,6 +115,6 @@ export class Variants { ``` */ withStore() { - return this as any as Variants + return this as any as Variants } } diff --git a/lib/config/configConstants.ts b/lib/config/configConstants.ts index 0795dcb..8220ef2 100644 --- a/lib/config/configConstants.ts +++ b/lib/config/configConstants.ts @@ -7,7 +7,7 @@ import { Variants } from "./builder/variants" /** * Base SMTP settings, to be used by StartOS for system wide SMTP */ -export const customSmtp = Config.of, never, never>({ +export const customSmtp = Config.of, never>({ server: Value.text({ name: "SMTP Server", required: { diff --git a/lib/config/setupConfig.ts b/lib/config/setupConfig.ts index 05ba789..1e8c2c4 100644 --- a/lib/config/setupConfig.ts +++ b/lib/config/setupConfig.ts @@ -14,16 +14,15 @@ export type DependenciesReceipt = void & { export type Save< Store, - Vault, A extends | Record - | Config, any, any> - | Config, never, never>, + | Config, any> + | Config, never>, Manifest extends SDKManifest, > = (options: { effects: Effects input: ExtractConfigType & Record - utils: Utils + utils: Utils dependencies: D.ConfigDependencies }) => Promise<{ dependenciesReceipt: DependenciesReceipt @@ -32,14 +31,13 @@ export type Save< }> export type Read< Store, - Vault, A extends | Record - | Config, any, any> - | Config, never, never>, + | Config, any> + | Config, never>, > = (options: { effects: Effects - utils: Utils + utils: Utils }) => Promise & Record)> /** * We want to setup a config export with a get and set, this @@ -50,17 +48,16 @@ export type Read< */ export function setupConfig< Store, - Vault, ConfigType extends | Record - | Config - | Config, + | Config + | Config, Manifest extends SDKManifest, Type extends Record = ExtractConfigType, >( - spec: Config | Config, - write: Save, - read: Read, + spec: Config | Config, + write: Save, + read: Read, ) { const validator = spec.validator return { @@ -82,7 +79,7 @@ export function setupConfig< } }) as ExpectedExports.setConfig, getConfig: (async ({ effects }) => { - const myUtils = utils(effects) + const myUtils = utils(effects) const configValue = nullIfEmpty( (await read({ effects, utils: myUtils })) || null, ) diff --git a/lib/dependencyConfig/DependencyConfig.ts b/lib/dependencyConfig/DependencyConfig.ts index ecde744..42354a9 100644 --- a/lib/dependencyConfig/DependencyConfig.ts +++ b/lib/dependencyConfig/DependencyConfig.ts @@ -9,7 +9,6 @@ import { deepMerge } from "../util/deepMerge" export class DependencyConfig< Store, - Vault, Input extends Record, RemoteConfig extends Record, > { @@ -18,7 +17,7 @@ export class DependencyConfig< effects: Effects localConfig: Input remoteConfig: RemoteConfig - utils: Utils + utils: Utils }) => Promise>, ) {} @@ -28,7 +27,7 @@ export class DependencyConfig< const origConfig = JSON.parse(JSON.stringify(options.localConfig)) const newOptions = { ...options, - utils: utils(options.effects), + utils: utils(options.effects), localConfig: options.localConfig as Input, remoteConfig: options.remoteConfig as RemoteConfig, } @@ -49,7 +48,7 @@ export class DependencyConfig< ): ReturnType { const newOptions = { ...options, - utils: utils(options.effects), + utils: utils(options.effects), localConfig: options.localConfig as Input, remoteConfig: options.remoteConfig as any, } diff --git a/lib/dependencyConfig/setupDependencyConfig.ts b/lib/dependencyConfig/setupDependencyConfig.ts index 71440d3..1039524 100644 --- a/lib/dependencyConfig/setupDependencyConfig.ts +++ b/lib/dependencyConfig/setupDependencyConfig.ts @@ -5,15 +5,13 @@ import { DependencyConfig } from "./DependencyConfig" export function setupDependencyConfig< Store, - Vault, Input extends Record, Manifest extends SDKManifest, >( - _config: Config | Config, + _config: Config | Config, autoConfigs: { [key in keyof Manifest["dependencies"] & string]: DependencyConfig< Store, - Vault, Input, any > diff --git a/lib/inits/migrations/Migration.ts b/lib/inits/migrations/Migration.ts index 2aed622..37928fd 100644 --- a/lib/inits/migrations/Migration.ts +++ b/lib/inits/migrations/Migration.ts @@ -2,39 +2,27 @@ import { ManifestVersion } from "../../manifest/ManifestTypes" import { Effects } from "../../types" import { Utils } from "../../util/utils" -export class Migration { +export class Migration { constructor( readonly options: { version: Version - up: (opts: { - effects: Effects - utils: Utils - }) => Promise - down: (opts: { - effects: Effects - utils: Utils - }) => Promise + up: (opts: { effects: Effects; utils: Utils }) => Promise + down: (opts: { effects: Effects; utils: Utils }) => Promise }, ) {} - static of(options: { + static of(options: { version: Version - up: (opts: { - effects: Effects - utils: Utils - }) => Promise - down: (opts: { - effects: Effects - utils: Utils - }) => Promise + up: (opts: { effects: Effects; utils: Utils }) => Promise + down: (opts: { effects: Effects; utils: Utils }) => Promise }) { - return new Migration(options) + return new Migration(options) } - async up(opts: { effects: Effects; utils: Utils }) { + async up(opts: { effects: Effects; utils: Utils }) { this.up(opts) } - async down(opts: { effects: Effects; utils: Utils }) { + async down(opts: { effects: Effects; utils: Utils }) { this.down(opts) } } diff --git a/lib/inits/migrations/setupMigrations.ts b/lib/inits/migrations/setupMigrations.ts index a7278c7..9d8a6a1 100644 --- a/lib/inits/migrations/setupMigrations.ts +++ b/lib/inits/migrations/setupMigrations.ts @@ -5,34 +5,30 @@ import { createUtils } from "../../util" import { once } from "../../util/once" import { Migration } from "./Migration" -export class Migrations { +export class Migrations { private constructor( readonly manifest: SDKManifest, - readonly migrations: Array>, + readonly migrations: Array>, ) {} private sortedMigrations = once(() => { const migrationsAsVersions = ( - this.migrations as Array> + this.migrations as Array> ).map((x) => [EmVer.parse(x.options.version), x] as const) migrationsAsVersions.sort((a, b) => a[0].compareForSort(b[0])) return migrationsAsVersions }) private currentVersion = once(() => EmVer.parse(this.manifest.version)) - static of< - Store, - Vault, - Migrations extends Array>, - >(manifest: SDKManifest, ...migrations: EnsureUniqueId) { - return new Migrations( - manifest, - migrations as Array>, - ) + static of>>( + manifest: SDKManifest, + ...migrations: EnsureUniqueId + ) { + return new Migrations(manifest, migrations as Array>) } async init({ effects, previousVersion, }: Parameters[0]) { - const utils = createUtils(effects) + const utils = createUtils(effects) if (!!previousVersion) { const previousVersionEmVer = EmVer.parse(previousVersion) for (const [_, migration] of this.sortedMigrations() @@ -46,7 +42,7 @@ export class Migrations { effects, nextVersion, }: Parameters[0]) { - const utils = createUtils(effects) + const utils = createUtils(effects) if (!!nextVersion) { const nextVersionEmVer = EmVer.parse(nextVersion) const reversed = [...this.sortedMigrations()].reverse() @@ -61,16 +57,15 @@ export class Migrations { export function setupMigrations< Store, - Vault, - Migrations extends Array>, + Migrations extends Array>, >(manifest: SDKManifest, ...migrations: EnsureUniqueId) { - return Migrations.of(manifest, ...migrations) + return Migrations.of(manifest, ...migrations) } // prettier-ignore export type EnsureUniqueId = B extends [] ? A : - B extends [Migration, ...infer Rest] ? ( + B extends [Migration, ...infer Rest] ? ( id extends ids ? "One of the ids are not unique"[] : EnsureUniqueId ) : "There exists a migration that is not a Migration"[] diff --git a/lib/inits/setupInit.ts b/lib/inits/setupInit.ts index e3a4b63..bdf3cdc 100644 --- a/lib/inits/setupInit.ts +++ b/lib/inits/setupInit.ts @@ -6,11 +6,11 @@ import { SetupExports } from "./setupExports" import { Install } from "./setupInstall" import { Uninstall } from "./setupUninstall" -export function setupInit( - migrations: Migrations, - install: Install, - uninstall: Uninstall, - setInterfaces: SetInterfaces, +export function setupInit( + migrations: Migrations, + install: Install, + uninstall: Uninstall, + setInterfaces: SetInterfaces, setupExports: SetupExports, ): { init: ExpectedExports.init diff --git a/lib/inits/setupInstall.ts b/lib/inits/setupInstall.ts index a24f0e3..98e0be5 100644 --- a/lib/inits/setupInstall.ts +++ b/lib/inits/setupInstall.ts @@ -1,13 +1,13 @@ import { Effects, ExpectedExports } from "../types" import { Utils, utils } from "../util/utils" -export type InstallFn = (opts: { +export type InstallFn = (opts: { effects: Effects - utils: Utils + utils: Utils }) => Promise -export class Install { - private constructor(readonly fn: InstallFn) {} - static of(fn: InstallFn) { +export class Install { + private constructor(readonly fn: InstallFn) {} + static of(fn: InstallFn) { return new Install(fn) } @@ -23,6 +23,6 @@ export class Install { } } -export function setupInstall(fn: InstallFn) { +export function setupInstall(fn: InstallFn) { return Install.of(fn) } diff --git a/lib/inits/setupUninstall.ts b/lib/inits/setupUninstall.ts index 209ffd9..6a76463 100644 --- a/lib/inits/setupUninstall.ts +++ b/lib/inits/setupUninstall.ts @@ -1,13 +1,13 @@ import { Effects, ExpectedExports } from "../types" import { Utils, utils } from "../util/utils" -export type UninstallFn = (opts: { +export type UninstallFn = (opts: { effects: Effects - utils: Utils + utils: Utils }) => Promise -export class Uninstall { - private constructor(readonly fn: UninstallFn) {} - static of(fn: UninstallFn) { +export class Uninstall { + private constructor(readonly fn: UninstallFn) {} + static of(fn: UninstallFn) { return new Uninstall(fn) } @@ -23,6 +23,6 @@ export class Uninstall { } } -export function setupUninstall(fn: UninstallFn) { +export function setupUninstall(fn: UninstallFn) { return Uninstall.of(fn) } diff --git a/lib/interfaces/setupInterfaces.ts b/lib/interfaces/setupInterfaces.ts index 2ab0c68..3a11d6c 100644 --- a/lib/interfaces/setupInterfaces.ts +++ b/lib/interfaces/setupInterfaces.ts @@ -6,22 +6,20 @@ import { AddressReceipt } from "./AddressReceipt" export type InterfacesReceipt = Array export type SetInterfaces< Store, - Vault, ConfigInput extends Record, Output extends InterfacesReceipt, > = (opts: { effects: Effects input: null | ConfigInput - utils: Utils + utils: Utils }) => Promise export type SetupInterfaces = < Store, - Vault, ConfigInput extends Record, Output extends InterfacesReceipt, >( - config: Config, - fn: SetInterfaces, -) => SetInterfaces + config: Config, + fn: SetInterfaces, +) => SetInterfaces export const NO_INTERFACE_CHANGES = [] as InterfacesReceipt export const setupInterfaces: SetupInterfaces = (_config, fn) => fn diff --git a/lib/mainFn/index.ts b/lib/mainFn/index.ts index 4eefe0f..58e09b7 100644 --- a/lib/mainFn/index.ts +++ b/lib/mainFn/index.ts @@ -17,17 +17,17 @@ import "./Daemons" * @param fn * @returns */ -export const setupMain = ( +export const setupMain = ( fn: (o: { effects: Effects started(onTerm: () => void): null - utils: Utils + utils: Utils }) => Promise>, ): ExpectedExports.main => { return async (options) => { const result = await fn({ ...options, - utils: createMainUtils(options.effects), + utils: createMainUtils(options.effects), }) await result.build().then((x) => x.wait()) } diff --git a/lib/test/output.sdk.ts b/lib/test/output.sdk.ts index 4802317..5cf2374 100644 --- a/lib/test/output.sdk.ts +++ b/lib/test/output.sdk.ts @@ -45,5 +45,4 @@ export const sdk = StartSdk.of() }), ) .withStore<{ storeRoot: { storeLeaf: "value" } }>() - .withVault<{ vaultRoot: "value" }>() .build(true) diff --git a/lib/test/store.test.ts b/lib/test/store.test.ts index e3d7931..0db8757 100644 --- a/lib/test/store.test.ts +++ b/lib/test/store.test.ts @@ -7,9 +7,6 @@ type Store = { someValue: "a" | "b" } } -type Vault = { - hello: string -} const todo = (): A => { throw new Error("not implemented") } @@ -51,10 +48,10 @@ describe("Store", () => { path: "/config/some2Value", value: "a", }) - ;(await createMainUtils(todo()) + ;(await createMainUtils(todo()) .store.getOwn("/config/someValue") .const()) satisfies string - ;(await createMainUtils(todo()) + ;(await createMainUtils(todo()) .store.getOwn("/config") .const()) satisfies Store["config"] await createMainUtils(todo()) diff --git a/lib/types.ts b/lib/types.ts index 85fdb23..33da950 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -462,14 +462,6 @@ export type Effects = { }): Promise stopped(packageId?: string): Promise - - vault: { - list(): Promise - get(opt: { key: string; callback: () => void }): Promise - set(opt: { key: string; value: string }): Promise - move(opt: { fromKey: string; toKey: string }): Promise - delete(opt: { key: string }): Promise - } } // prettier-ignore diff --git a/lib/util/getVault.ts b/lib/util/getVault.ts deleted file mode 100644 index b88a08d..0000000 --- a/lib/util/getVault.ts +++ /dev/null @@ -1,44 +0,0 @@ -import { Effects, EnsureStorePath } from "../types" - -export class GetVault { - constructor(readonly effects: Effects, readonly key: keyof Vault & string) {} - - /** - * Returns the value of Store at the provided path. Restart the service if the value changes - */ - const() { - return this.effects.vault.get({ - key: this.key, - callback: this.effects.restart, - }) - } - /** - * Returns the value of Store at the provided path. Does nothing if the value changes - */ - once() { - return this.effects.vault.get({ - key: this.key, - callback: () => {}, - }) - } - - /** - * Watches the value of Store at the provided path. Takes a custom callback function to run whenever the value changes - */ - async *watch() { - while (true) { - let callback: () => void - const waitForNext = new Promise((resolve) => { - callback = resolve - }) - yield await this.effects.vault.get({ - key: this.key, - callback: () => callback(), - }) - await waitForNext - } - } -} -export function getVault(effects: Effects, key: keyof Vault & string) { - return new GetVault(effects, key) -} diff --git a/lib/util/index.ts b/lib/util/index.ts index 03dac09..ab8ee08 100644 --- a/lib/util/index.ts +++ b/lib/util/index.ts @@ -22,8 +22,8 @@ export const isKnownError = (e: unknown): e is T.KnownError => declare const affine: unique symbol export const createUtils = utils -export const createMainUtils = (effects: T.Effects) => - createUtils(effects) +export const createMainUtils = (effects: T.Effects) => + createUtils(effects) type NeverPossible = { [affine]: string } export type NoAny = NeverPossible extends A diff --git a/lib/util/utils.ts b/lib/util/utils.ts index ce5f403..c0f1cfd 100644 --- a/lib/util/utils.ts +++ b/lib/util/utils.ts @@ -16,7 +16,6 @@ import { GetSystemSmtp } from "./GetSystemSmtp" import { DefaultString } from "../config/configTypes" import { getDefaultString } from "./getDefaultString" import { GetStore, getStore } from "../store/getStore" -import { GetVault, getVault } from "./getVault" import { MountDependenciesOut, mountDependencies, @@ -62,11 +61,6 @@ export type Utils = { path: string search: Record }) => NetworkInterfaceBuilder - createOrUpdateVault: (opts: { - key: keyof Vault & string - value: string | null | undefined - generator: DefaultString - }) => Promise getSystemSmtp: () => GetSystemSmtp & WrapperOverWrite host: { static: (id: string) => StaticHost @@ -108,43 +102,14 @@ export type Utils = { value: ExtractStore, ) => Promise } - vault: { - get: (key: keyof Vault & string) => GetVault & WrapperOverWrite - set: (key: keyof Vault & string, value: string) => Promise - } writeFile: ( fileHelper: FileHelper, data: A, ) => ReturnType["write"]> } -export const utils = < - Store = never, - Vault = never, - WrapperOverWrite = { const: never }, ->( +export const utils = ( effects: Effects, -): Utils => ({ - createOrUpdateVault: async ({ - key, - value, - generator, - }: { - key: keyof Vault & string - value: string | null | undefined - generator: DefaultString - }) => { - if (value) { - await effects.vault.set({ key, value }) - return value - } - const oldValue = await effects.vault.get({ key, callback: noop }) - if (oldValue) { - return oldValue - } - const newValue = getDefaultString(generator) - await effects.vault.set({ key, value: newValue }) - return newValue - }, +): Utils => ({ createInterface: (options: { name: string id: string @@ -198,12 +163,7 @@ export const utils = < }, checkPortListening: checkPortListening.bind(null, effects), checkWebUrl: checkWebUrl.bind(null, effects), - vault: { - get: (key: keyof Vault & string) => - getVault(effects, key) as GetVault & WrapperOverWrite, - set: (key: keyof Vault & string, value: string) => - effects.vault.set({ key, value }), - }, + mountDependencies: < In extends | Record>>