mirror of
https://github.com/Start9Labs/start-sdk.git
synced 2026-03-26 02:11:56 +00:00
feat: Remove the vault
This commit is contained in:
181
lib/StartSdk.ts
181
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<T extends any[], Then, Else> =
|
||||
@@ -60,42 +61,37 @@ type AnyNeverCond<T extends any[], Then, Else> =
|
||||
T extends [any, ...infer U] ? AnyNeverCond<U,Then, Else> :
|
||||
never
|
||||
|
||||
export class StartSdk<Manifest extends SDKManifest, Store, Vault> {
|
||||
export class StartSdk<Manifest extends SDKManifest, Store> {
|
||||
private constructor(readonly manifest: Manifest) {}
|
||||
static of() {
|
||||
return new StartSdk<never, never, never>(null as never)
|
||||
return new StartSdk<never, never>(null as never)
|
||||
}
|
||||
withManifest<Manifest extends SDKManifest = never>(manifest: Manifest) {
|
||||
return new StartSdk<Manifest, Store, Vault>(manifest)
|
||||
return new StartSdk<Manifest, Store>(manifest)
|
||||
}
|
||||
withStore<Store extends Record<string, any>>() {
|
||||
return new StartSdk<Manifest, Store, Vault>(this.manifest)
|
||||
}
|
||||
withVault<Vault extends Record<string, string>>() {
|
||||
return new StartSdk<Manifest, Store, Vault>(this.manifest)
|
||||
return new StartSdk<Manifest, Store>(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<string, any>
|
||||
| Config<any, any, any>
|
||||
| Config<any, never, never>,
|
||||
| Config<any, any>
|
||||
| Config<any, never>,
|
||||
Type extends Record<string, any> = ExtractConfigType<ConfigType>,
|
||||
>(
|
||||
metaData: Omit<ActionMetadata, "input"> & {
|
||||
input: Config<Type, Store, Vault> | Config<Type, never, never>
|
||||
input: Config<Type, Store> | Config<Type, never>
|
||||
},
|
||||
fn: (options: {
|
||||
effects: Effects
|
||||
utils: Utils<Store, Vault>
|
||||
utils: Utils<Store>
|
||||
input: Type
|
||||
}) => Promise<ActionResult>,
|
||||
) => createAction<Store, Vault, ConfigType, Type>(metaData, fn),
|
||||
) => createAction<Store, ConfigType, Type>(metaData, fn),
|
||||
HealthCheck: {
|
||||
of: healthCheck,
|
||||
},
|
||||
@@ -106,84 +102,78 @@ export class StartSdk<Manifest extends SDKManifest, Store, Vault> {
|
||||
},
|
||||
patterns,
|
||||
setupActions: (...createdActions: CreatedAction<any, any, any>[]) =>
|
||||
setupActions<Store, Vault>(...createdActions),
|
||||
setupActions<Store>(...createdActions),
|
||||
setupBackups: (...args: SetupBackupsParams<Manifest>) =>
|
||||
setupBackups<Manifest>(...args),
|
||||
setupConfig: <
|
||||
ConfigType extends
|
||||
| Config<any, Store, Vault>
|
||||
| Config<any, never, never>,
|
||||
ConfigType extends Config<any, Store> | Config<any, never>,
|
||||
Type extends Record<string, any> = ExtractConfigType<ConfigType>,
|
||||
>(
|
||||
spec: ConfigType,
|
||||
write: Save<Store, Vault, Type, Manifest>,
|
||||
read: Read<Store, Vault, Type>,
|
||||
) =>
|
||||
setupConfig<Store, Vault, ConfigType, Manifest, Type>(
|
||||
spec,
|
||||
write,
|
||||
read,
|
||||
),
|
||||
write: Save<Store, Type, Manifest>,
|
||||
read: Read<Store, Type>,
|
||||
) => setupConfig<Store, ConfigType, Manifest, Type>(spec, write, read),
|
||||
setupConfigRead: <
|
||||
ConfigSpec extends
|
||||
| Config<Record<string, any>, any, any>
|
||||
| Config<Record<string, never>, never, never>,
|
||||
| Config<Record<string, any>, any>
|
||||
| Config<Record<string, never>, never>,
|
||||
>(
|
||||
_configSpec: ConfigSpec,
|
||||
fn: Read<Store, Vault, ConfigSpec>,
|
||||
fn: Read<Store, ConfigSpec>,
|
||||
) => fn,
|
||||
setupConfigSave: <
|
||||
ConfigSpec extends
|
||||
| Config<Record<string, any>, any, any>
|
||||
| Config<Record<string, never>, never, never>,
|
||||
| Config<Record<string, any>, any>
|
||||
| Config<Record<string, never>, never>,
|
||||
>(
|
||||
_configSpec: ConfigSpec,
|
||||
fn: Save<Store, Vault, ConfigSpec, Manifest>,
|
||||
fn: Save<Store, ConfigSpec, Manifest>,
|
||||
) => fn,
|
||||
setupDependencyConfig: <Input extends Record<string, any>>(
|
||||
config: Config<Input, Store, Vault> | Config<Input, never, never>,
|
||||
config: Config<Input, Store> | Config<Input, never>,
|
||||
autoConfigs: {
|
||||
[K in keyof Manifest["dependencies"]]: DependencyConfig<
|
||||
Store,
|
||||
Vault,
|
||||
Input,
|
||||
any
|
||||
>
|
||||
},
|
||||
) =>
|
||||
setupDependencyConfig<Store, Vault, Input, Manifest>(
|
||||
config,
|
||||
autoConfigs,
|
||||
),
|
||||
) => setupDependencyConfig<Store, Input, Manifest>(config, autoConfigs),
|
||||
setupExports: (fn: SetupExports<Store>) => fn,
|
||||
setupDependencyMounts,
|
||||
setupInit: (
|
||||
migrations: Migrations<Store, Vault>,
|
||||
install: Install<Store, Vault>,
|
||||
uninstall: Uninstall<Store, Vault>,
|
||||
setInterfaces: SetInterfaces<Store, Vault, any, any>,
|
||||
migrations: Migrations<Store>,
|
||||
install: Install<Store>,
|
||||
uninstall: Uninstall<Store>,
|
||||
setInterfaces: SetInterfaces<Store, any, any>,
|
||||
setupExports: SetupExports<Store>,
|
||||
) =>
|
||||
setupInit<Store, Vault>(migrations, install, uninstall, setInterfaces),
|
||||
setupInstall: (fn: InstallFn<Store, Vault>) => Install.of(fn),
|
||||
setupInit<Store>(
|
||||
migrations,
|
||||
install,
|
||||
uninstall,
|
||||
setInterfaces,
|
||||
setupExports,
|
||||
),
|
||||
setupInstall: (fn: InstallFn<Store>) => Install.of(fn),
|
||||
setupInterfaces: <
|
||||
ConfigInput extends Record<string, any>,
|
||||
Output extends InterfacesReceipt,
|
||||
>(
|
||||
config: Config<ConfigInput, Store, Vault>,
|
||||
fn: SetInterfaces<Store, Vault, ConfigInput, Output>,
|
||||
config: Config<ConfigInput, Store>,
|
||||
fn: SetInterfaces<Store, ConfigInput, Output>,
|
||||
) => setupInterfaces(config, fn),
|
||||
setupMain: (
|
||||
fn: (o: {
|
||||
effects: Effects
|
||||
started(onTerm: () => void): null
|
||||
utils: Utils<Store, Vault, {}>
|
||||
utils: Utils<Store, {}>
|
||||
}) => Promise<Daemons<any>>,
|
||||
) => setupMain<Store, Vault>(fn),
|
||||
setupMigrations: <Migrations extends Array<Migration<Store, Vault, any>>>(
|
||||
) => setupMain<Store>(fn),
|
||||
setupMigrations: <Migrations extends Array<Migration<Store, any>>>(
|
||||
...migrations: EnsureUniqueId<Migrations>
|
||||
) =>
|
||||
setupMigrations<Store, Vault, Migrations>(this.manifest, ...migrations),
|
||||
setupUninstall: (fn: UninstallFn<Store, Vault>) =>
|
||||
setupUninstall<Store, Vault>(fn),
|
||||
) => setupMigrations<Store, Migrations>(this.manifest, ...migrations),
|
||||
setupUninstall: (fn: UninstallFn<Store>) => setupUninstall<Store>(fn),
|
||||
trigger: {
|
||||
defaultTrigger,
|
||||
cooldownTrigger,
|
||||
@@ -202,13 +192,10 @@ export class StartSdk<Manifest extends SDKManifest, Store, Vault> {
|
||||
},
|
||||
Config: {
|
||||
of: <
|
||||
Spec extends Record<
|
||||
string,
|
||||
Value<any, Store, Vault> | Value<any, never, never>
|
||||
>,
|
||||
Spec extends Record<string, Value<any, Store> | Value<any, never>>,
|
||||
>(
|
||||
spec: Spec,
|
||||
) => Config.of<Spec, Store, Vault>(spec),
|
||||
) => Config.of<Spec, Store>(spec),
|
||||
},
|
||||
Daemons: { of: Daemons.of },
|
||||
DependencyConfig: {
|
||||
@@ -220,20 +207,16 @@ export class StartSdk<Manifest extends SDKManifest, Store, Vault> {
|
||||
remoteConfig,
|
||||
dependencyConfig,
|
||||
}: {
|
||||
localConfig:
|
||||
| Config<LocalConfig, Store, Vault>
|
||||
| Config<LocalConfig, never, never>
|
||||
remoteConfig:
|
||||
| Config<RemoteConfig, any, any>
|
||||
| Config<RemoteConfig, never, never>
|
||||
localConfig: Config<LocalConfig, Store> | Config<LocalConfig, never>
|
||||
remoteConfig: Config<RemoteConfig, any> | Config<RemoteConfig, never>
|
||||
dependencyConfig: (options: {
|
||||
effects: Effects
|
||||
localConfig: LocalConfig
|
||||
remoteConfig: RemoteConfig
|
||||
utils: Utils<Store, Vault>
|
||||
utils: Utils<Store>
|
||||
}) => Promise<void | DeepPartial<RemoteConfig>>
|
||||
}) {
|
||||
return new DependencyConfig<Store, Vault, LocalConfig, RemoteConfig>(
|
||||
return new DependencyConfig<Store, LocalConfig, RemoteConfig>(
|
||||
dependencyConfig,
|
||||
)
|
||||
},
|
||||
@@ -252,15 +235,14 @@ export class StartSdk<Manifest extends SDKManifest, Store, Vault> {
|
||||
maxLength?: number | null
|
||||
},
|
||||
aSpec: {
|
||||
spec: Config<Type, Store, Vault>
|
||||
spec: Config<Type, Store>
|
||||
displayAs?: null | string
|
||||
uniqueBy?: null | UniqueBy
|
||||
},
|
||||
) => List.obj<Type, Store, Vault>(a, aSpec),
|
||||
) => List.obj<Type, Store>(a, aSpec),
|
||||
dynamicText: (
|
||||
getA: LazyBuild<
|
||||
Store,
|
||||
Vault,
|
||||
{
|
||||
name: string
|
||||
description?: string | null
|
||||
@@ -283,11 +265,10 @@ export class StartSdk<Manifest extends SDKManifest, Store, Vault> {
|
||||
}
|
||||
}
|
||||
>,
|
||||
) => List.dynamicText<Store, Vault>(getA),
|
||||
) => List.dynamicText<Store>(getA),
|
||||
dynamicNumber: (
|
||||
getA: LazyBuild<
|
||||
Store,
|
||||
Vault,
|
||||
{
|
||||
name: string
|
||||
description?: string | null
|
||||
@@ -307,20 +288,17 @@ export class StartSdk<Manifest extends SDKManifest, Store, Vault> {
|
||||
}
|
||||
}
|
||||
>,
|
||||
) => List.dynamicNumber<Store, Vault>(getA),
|
||||
) => List.dynamicNumber<Store>(getA),
|
||||
},
|
||||
Migration: {
|
||||
of: <Version extends ManifestVersion>(options: {
|
||||
version: Version
|
||||
up: (opts: {
|
||||
effects: Effects
|
||||
utils: Utils<Store, Vault>
|
||||
}) => Promise<void>
|
||||
up: (opts: { effects: Effects; utils: Utils<Store> }) => Promise<void>
|
||||
down: (opts: {
|
||||
effects: Effects
|
||||
utils: Utils<Store, Vault>
|
||||
utils: Utils<Store>
|
||||
}) => Promise<void>
|
||||
}) => Migration.of<Store, Vault, Version>(options),
|
||||
}) => Migration.of<Store, Version>(options),
|
||||
},
|
||||
Value: {
|
||||
toggle: Value.toggle,
|
||||
@@ -337,7 +315,6 @@ export class StartSdk<Manifest extends SDKManifest, Store, Vault> {
|
||||
dynamicToggle: (
|
||||
a: LazyBuild<
|
||||
Store,
|
||||
Vault,
|
||||
{
|
||||
name: string
|
||||
description?: string | null
|
||||
@@ -346,11 +323,10 @@ export class StartSdk<Manifest extends SDKManifest, Store, Vault> {
|
||||
disabled?: false | string
|
||||
}
|
||||
>,
|
||||
) => Value.dynamicToggle<Store, Vault>(a),
|
||||
) => Value.dynamicToggle<Store>(a),
|
||||
dynamicText: (
|
||||
getA: LazyBuild<
|
||||
Store,
|
||||
Vault,
|
||||
{
|
||||
name: string
|
||||
description?: string | null
|
||||
@@ -368,11 +344,10 @@ export class StartSdk<Manifest extends SDKManifest, Store, Vault> {
|
||||
generate?: null | RandomString
|
||||
}
|
||||
>,
|
||||
) => Value.dynamicText<Store, Vault>(getA),
|
||||
) => Value.dynamicText<Store>(getA),
|
||||
dynamicTextarea: (
|
||||
getA: LazyBuild<
|
||||
Store,
|
||||
Vault,
|
||||
{
|
||||
name: string
|
||||
description?: string | null
|
||||
@@ -385,11 +360,10 @@ export class StartSdk<Manifest extends SDKManifest, Store, Vault> {
|
||||
generate?: null | RandomString
|
||||
}
|
||||
>,
|
||||
) => Value.dynamicTextarea<Store, Vault>(getA),
|
||||
) => Value.dynamicTextarea<Store>(getA),
|
||||
dynamicNumber: (
|
||||
getA: LazyBuild<
|
||||
Store,
|
||||
Vault,
|
||||
{
|
||||
name: string
|
||||
description?: string | null
|
||||
@@ -405,11 +379,10 @@ export class StartSdk<Manifest extends SDKManifest, Store, Vault> {
|
||||
disabled?: false | string
|
||||
}
|
||||
>,
|
||||
) => Value.dynamicNumber<Store, Vault>(getA),
|
||||
) => Value.dynamicNumber<Store>(getA),
|
||||
dynamicColor: (
|
||||
getA: LazyBuild<
|
||||
Store,
|
||||
Vault,
|
||||
{
|
||||
name: string
|
||||
description?: string | null
|
||||
@@ -419,11 +392,10 @@ export class StartSdk<Manifest extends SDKManifest, Store, Vault> {
|
||||
disabled?: false | string
|
||||
}
|
||||
>,
|
||||
) => Value.dynamicColor<Store, Vault>(getA),
|
||||
) => Value.dynamicColor<Store>(getA),
|
||||
dynamicDatetime: (
|
||||
getA: LazyBuild<
|
||||
Store,
|
||||
Vault,
|
||||
{
|
||||
name: string
|
||||
description?: string | null
|
||||
@@ -436,11 +408,10 @@ export class StartSdk<Manifest extends SDKManifest, Store, Vault> {
|
||||
disabled?: false | string
|
||||
}
|
||||
>,
|
||||
) => Value.dynamicDatetime<Store, Vault>(getA),
|
||||
) => Value.dynamicDatetime<Store>(getA),
|
||||
dynamicSelect: (
|
||||
getA: LazyBuild<
|
||||
Store,
|
||||
Vault,
|
||||
{
|
||||
name: string
|
||||
description?: string | null
|
||||
@@ -450,11 +421,10 @@ export class StartSdk<Manifest extends SDKManifest, Store, Vault> {
|
||||
disabled?: false | string
|
||||
}
|
||||
>,
|
||||
) => Value.dynamicSelect<Store, Vault>(getA),
|
||||
) => Value.dynamicSelect<Store>(getA),
|
||||
dynamicMultiselect: (
|
||||
getA: LazyBuild<
|
||||
Store,
|
||||
Vault,
|
||||
{
|
||||
name: string
|
||||
description?: string | null
|
||||
@@ -466,23 +436,21 @@ export class StartSdk<Manifest extends SDKManifest, Store, Vault> {
|
||||
disabled?: false | string
|
||||
}
|
||||
>,
|
||||
) => Value.dynamicMultiselect<Store, Vault>(getA),
|
||||
) => Value.dynamicMultiselect<Store>(getA),
|
||||
filteredUnion: <
|
||||
Required extends RequiredDefault<string>,
|
||||
Type extends Record<string, any>,
|
||||
>(
|
||||
getDisabledFn: LazyBuild<Store, Vault, string[]>,
|
||||
getDisabledFn: LazyBuild<Store, string[]>,
|
||||
a: {
|
||||
name: string
|
||||
description?: string | null
|
||||
warning?: string | null
|
||||
required: Required
|
||||
},
|
||||
aVariants:
|
||||
| Variants<Type, Store, Vault>
|
||||
| Variants<Type, never, never>,
|
||||
aVariants: Variants<Type, Store> | Variants<Type, never>,
|
||||
) =>
|
||||
Value.filteredUnion<Required, Type, Store, Vault>(
|
||||
Value.filteredUnion<Required, Type, Store>(
|
||||
getDisabledFn,
|
||||
a,
|
||||
aVariants,
|
||||
@@ -494,7 +462,6 @@ export class StartSdk<Manifest extends SDKManifest, Store, Vault> {
|
||||
>(
|
||||
getA: LazyBuild<
|
||||
Store,
|
||||
Vault,
|
||||
{
|
||||
disabled: string[] | false | string
|
||||
name: string
|
||||
@@ -503,22 +470,20 @@ export class StartSdk<Manifest extends SDKManifest, Store, Vault> {
|
||||
required: Required
|
||||
}
|
||||
>,
|
||||
aVariants:
|
||||
| Variants<Type, Store, Vault>
|
||||
| Variants<Type, never, never>,
|
||||
) => Value.dynamicUnion<Required, Type, Store, Vault>(getA, aVariants),
|
||||
aVariants: Variants<Type, Store> | Variants<Type, never>,
|
||||
) => Value.dynamicUnion<Required, Type, Store>(getA, aVariants),
|
||||
},
|
||||
Variants: {
|
||||
of: <
|
||||
VariantValues extends {
|
||||
[K in string]: {
|
||||
name: string
|
||||
spec: Config<any, Store, Vault>
|
||||
spec: Config<any, Store>
|
||||
}
|
||||
},
|
||||
>(
|
||||
a: VariantValues,
|
||||
) => Variants.of<VariantValues, Store, Vault>(a),
|
||||
) => Variants.of<VariantValues, Store>(a),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,47 +5,45 @@ import { Utils, utils } from "../util/utils"
|
||||
|
||||
export class CreatedAction<
|
||||
Store,
|
||||
Vault,
|
||||
ConfigType extends
|
||||
| Record<string, any>
|
||||
| Config<any, Store, Vault>
|
||||
| Config<any, never, never>,
|
||||
| Config<any, Store>
|
||||
| Config<any, never>,
|
||||
Type extends Record<string, any> = ExtractConfigType<ConfigType>,
|
||||
> {
|
||||
private constructor(
|
||||
public readonly myMetaData: Omit<ActionMetadata, "input">,
|
||||
readonly fn: (options: {
|
||||
effects: Effects
|
||||
utils: Utils<Store, Vault>
|
||||
utils: Utils<Store>
|
||||
input: Type
|
||||
}) => Promise<ActionResult>,
|
||||
readonly input: Config<Type, Store, Vault>,
|
||||
readonly input: Config<Type, Store>,
|
||||
) {}
|
||||
public validator = this.input.validator
|
||||
|
||||
static of<
|
||||
Store,
|
||||
Vault,
|
||||
ConfigType extends
|
||||
| Record<string, any>
|
||||
| Config<any, any, any>
|
||||
| Config<any, never, never>,
|
||||
| Config<any, any>
|
||||
| Config<any, never>,
|
||||
Type extends Record<string, any> = ExtractConfigType<ConfigType>,
|
||||
>(
|
||||
metaData: Omit<ActionMetadata, "input"> & {
|
||||
input: Config<Type, Store, Vault> | Config<Type, never, never>
|
||||
input: Config<Type, Store> | Config<Type, never>
|
||||
},
|
||||
fn: (options: {
|
||||
effects: Effects
|
||||
utils: Utils<Store, Vault>
|
||||
utils: Utils<Store>
|
||||
input: Type
|
||||
}) => Promise<ActionResult>,
|
||||
) {
|
||||
const { input, ...rest } = metaData
|
||||
return new CreatedAction<Store, Vault, ConfigType, Type>(
|
||||
return new CreatedAction<Store, ConfigType, Type>(
|
||||
rest,
|
||||
fn,
|
||||
input as Config<Type, Store, Vault>,
|
||||
input as Config<Type, Store>,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -67,7 +65,7 @@ export class CreatedAction<
|
||||
|
||||
async ActionMetadata(options: {
|
||||
effects: Effects
|
||||
utils: Utils<Store, Vault>
|
||||
utils: Utils<Store>
|
||||
}): Promise<ActionMetadata> {
|
||||
return {
|
||||
...this.myMetaData,
|
||||
|
||||
@@ -3,11 +3,11 @@ import { createUtils } from "../util"
|
||||
import { once } from "../util/once"
|
||||
import { CreatedAction } from "./createAction"
|
||||
|
||||
export function setupActions<Store, Vault>(
|
||||
...createdActions: CreatedAction<Store, Vault, any>[]
|
||||
export function setupActions<Store>(
|
||||
...createdActions: CreatedAction<Store, any>[]
|
||||
) {
|
||||
const myActions = once(() => {
|
||||
const actions: Record<string, CreatedAction<Store, Vault, any>> = {}
|
||||
const actions: Record<string, CreatedAction<Store, any>> = {}
|
||||
for (const action of createdActions) {
|
||||
actions[action.myMetaData.id] = action
|
||||
}
|
||||
@@ -21,7 +21,7 @@ export function setupActions<Store, Vault>(
|
||||
return myActions()
|
||||
},
|
||||
async actionsMetadata({ effects }: { effects: Effects }) {
|
||||
const utils = createUtils<Store, Vault>(effects)
|
||||
const utils = createUtils<Store>(effects)
|
||||
return Promise.all(
|
||||
createdActions.map((x) => x.ActionMetadata({ effects, utils })),
|
||||
)
|
||||
|
||||
@@ -5,28 +5,24 @@ import { _ } from "../../util"
|
||||
import { Effects } from "../../types"
|
||||
import { Parser, object } from "ts-matches"
|
||||
|
||||
export type LazyBuildOptions<Store, Vault> = {
|
||||
export type LazyBuildOptions<Store> = {
|
||||
effects: Effects
|
||||
utils: Utils<Store, Vault>
|
||||
utils: Utils<Store>
|
||||
}
|
||||
export type LazyBuild<Store, Vault, ExpectedOut> = (
|
||||
options: LazyBuildOptions<Store, Vault>,
|
||||
export type LazyBuild<Store, ExpectedOut> = (
|
||||
options: LazyBuildOptions<Store>,
|
||||
) => Promise<ExpectedOut> | ExpectedOut
|
||||
|
||||
// prettier-ignore
|
||||
export type ExtractConfigType<A extends Record<string, any> | Config<Record<string, any>, any, any> | Config<Record<string, any>, never, never>> =
|
||||
A extends Config<infer B, any, any> | Config<infer B, never, never> ? B :
|
||||
export type ExtractConfigType<A extends Record<string, any> | Config<Record<string, any>, any> | Config<Record<string, any>, never>> =
|
||||
A extends Config<infer B, any> | Config<infer B, never> ? B :
|
||||
A
|
||||
|
||||
export type ConfigSpecOf<
|
||||
A extends Record<string, any>,
|
||||
Store = never,
|
||||
Vault = never,
|
||||
> = {
|
||||
[K in keyof A]: Value<A[K], Store, Vault>
|
||||
export type ConfigSpecOf<A extends Record<string, any>, Store = never> = {
|
||||
[K in keyof A]: Value<A[K], Store>
|
||||
}
|
||||
|
||||
export type MaybeLazyValues<A> = LazyBuild<any, any, A> | A
|
||||
export type MaybeLazyValues<A> = LazyBuild<any, A> | 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<string, any>,
|
||||
Store = never,
|
||||
Vault = never,
|
||||
> {
|
||||
export class Config<Type extends Record<string, any>, Store = never> {
|
||||
private constructor(
|
||||
private readonly spec: {
|
||||
[K in keyof Type]:
|
||||
| Value<Type[K], Store, Vault>
|
||||
| Value<Type[K], never, never>
|
||||
[K in keyof Type]: Value<Type[K], Store> | Value<Type[K], never>
|
||||
},
|
||||
public validator: Parser<unknown, Type>,
|
||||
) {}
|
||||
async build(options: LazyBuildOptions<Store, Vault>) {
|
||||
async build(options: LazyBuildOptions<Store>) {
|
||||
const answer = {} as {
|
||||
[K in keyof Type]: ValueSpec
|
||||
}
|
||||
@@ -107,12 +97,8 @@ export class Config<
|
||||
}
|
||||
|
||||
static of<
|
||||
Spec extends Record<
|
||||
string,
|
||||
Value<any, Store, Vault> | Value<any, never, never>
|
||||
>,
|
||||
Spec extends Record<string, Value<any, Store> | Value<any, never>>,
|
||||
Store = never,
|
||||
Vault = never,
|
||||
>(spec: Spec) {
|
||||
const validatorObj = {} as {
|
||||
[K in keyof Spec]: Parser<unknown, any>
|
||||
@@ -124,13 +110,12 @@ export class Config<
|
||||
return new Config<
|
||||
{
|
||||
[K in keyof Spec]: Spec[K] extends
|
||||
| Value<infer T, Store, Vault>
|
||||
| Value<infer T, never, never>
|
||||
| Value<infer T, Store>
|
||||
| Value<infer T, never>
|
||||
? T
|
||||
: never
|
||||
},
|
||||
Store,
|
||||
Vault
|
||||
Store
|
||||
>(spec, validator as any)
|
||||
}
|
||||
|
||||
@@ -149,6 +134,6 @@ export class Config<
|
||||
```
|
||||
*/
|
||||
withStore<NewStore extends Store extends never ? any : Store>() {
|
||||
return this as any as Config<Type, NewStore, Vault>
|
||||
return this as any as Config<Type, NewStore>
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,9 +22,9 @@ export const authorizationList = List.string({
|
||||
export const auth = Value.list(authorizationList);
|
||||
```
|
||||
*/
|
||||
export class List<Type, Store, Vault> {
|
||||
export class List<Type, Store> {
|
||||
private constructor(
|
||||
public build: LazyBuild<Store, Vault, ValueSpecList>,
|
||||
public build: LazyBuild<Store, ValueSpecList>,
|
||||
public validator: Parser<unknown, Type>,
|
||||
) {}
|
||||
static text(
|
||||
@@ -49,7 +49,7 @@ export class List<Type, Store, Vault> {
|
||||
generate?: null | RandomString
|
||||
},
|
||||
) {
|
||||
return new List<string[], never, never>(() => {
|
||||
return new List<string[], never>(() => {
|
||||
const spec = {
|
||||
type: "text" as const,
|
||||
placeholder: null,
|
||||
@@ -74,10 +74,9 @@ export class List<Type, Store, Vault> {
|
||||
return built
|
||||
}, arrayOf(string))
|
||||
}
|
||||
static dynamicText<Store = never, Vault = never>(
|
||||
static dynamicText<Store = never>(
|
||||
getA: LazyBuild<
|
||||
Store,
|
||||
Vault,
|
||||
{
|
||||
name: string
|
||||
description?: string | null
|
||||
@@ -101,7 +100,7 @@ export class List<Type, Store, Vault> {
|
||||
}
|
||||
>,
|
||||
) {
|
||||
return new List<string[], Store, Vault>(async (options) => {
|
||||
return new List<string[], Store>(async (options) => {
|
||||
const { spec: aSpec, ...a } = await getA(options)
|
||||
const spec = {
|
||||
type: "text" as const,
|
||||
@@ -146,7 +145,7 @@ export class List<Type, Store, Vault> {
|
||||
placeholder?: string | null
|
||||
},
|
||||
) {
|
||||
return new List<number[], never, never>(() => {
|
||||
return new List<number[], never>(() => {
|
||||
const spec = {
|
||||
type: "number" as const,
|
||||
placeholder: null,
|
||||
@@ -170,10 +169,9 @@ export class List<Type, Store, Vault> {
|
||||
return built
|
||||
}, arrayOf(number))
|
||||
}
|
||||
static dynamicNumber<Store = never, Vault = never>(
|
||||
static dynamicNumber<Store = never>(
|
||||
getA: LazyBuild<
|
||||
Store,
|
||||
Vault,
|
||||
{
|
||||
name: string
|
||||
description?: string | null
|
||||
@@ -194,7 +192,7 @@ export class List<Type, Store, Vault> {
|
||||
}
|
||||
>,
|
||||
) {
|
||||
return new List<number[], Store, Vault>(async (options) => {
|
||||
return new List<number[], Store>(async (options) => {
|
||||
const { spec: aSpec, ...a } = await getA(options)
|
||||
const spec = {
|
||||
type: "number" as const,
|
||||
@@ -218,7 +216,7 @@ export class List<Type, Store, Vault> {
|
||||
}
|
||||
}, arrayOf(number))
|
||||
}
|
||||
static obj<Type extends Record<string, any>, Store, Vault>(
|
||||
static obj<Type extends Record<string, any>, Store>(
|
||||
a: {
|
||||
name: string
|
||||
description?: string | null
|
||||
@@ -229,12 +227,12 @@ export class List<Type, Store, Vault> {
|
||||
maxLength?: number | null
|
||||
},
|
||||
aSpec: {
|
||||
spec: Config<Type, Store, Vault>
|
||||
spec: Config<Type, Store>
|
||||
displayAs?: null | string
|
||||
uniqueBy?: null | UniqueBy
|
||||
},
|
||||
) {
|
||||
return new List<Type[], Store, Vault>(async (options) => {
|
||||
return new List<Type[], Store>(async (options) => {
|
||||
const { spec: previousSpecSpec, ...restSpec } = aSpec
|
||||
const specSpec = await previousSpecSpec.build(options)
|
||||
const spec = {
|
||||
@@ -276,6 +274,6 @@ export class List<Type, Store, Vault> {
|
||||
```
|
||||
*/
|
||||
withStore<NewStore extends Store extends never ? any : Store>() {
|
||||
return this as any as List<Type, NewStore, Vault>
|
||||
return this as any as List<Type, NewStore>
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,9 +94,9 @@ const username = Value.string({
|
||||
});
|
||||
```
|
||||
*/
|
||||
export class Value<Type, Store, Vault> {
|
||||
export class Value<Type, Store> {
|
||||
protected constructor(
|
||||
public build: LazyBuild<Store, Vault, ValueSpec>,
|
||||
public build: LazyBuild<Store, ValueSpec>,
|
||||
public validator: Parser<unknown, Type>,
|
||||
) {}
|
||||
static toggle(a: {
|
||||
@@ -108,7 +108,7 @@ export class Value<Type, Store, Vault> {
|
||||
Default is false */
|
||||
immutable?: boolean
|
||||
}) {
|
||||
return new Value<boolean, never, never>(
|
||||
return new Value<boolean, never>(
|
||||
async () => ({
|
||||
description: null,
|
||||
warning: null,
|
||||
@@ -120,10 +120,9 @@ export class Value<Type, Store, Vault> {
|
||||
boolean,
|
||||
)
|
||||
}
|
||||
static dynamicToggle<Store = never, Vault = never>(
|
||||
static dynamicToggle<Store = never>(
|
||||
a: LazyBuild<
|
||||
Store,
|
||||
Vault,
|
||||
{
|
||||
name: string
|
||||
description?: string | null
|
||||
@@ -133,7 +132,7 @@ export class Value<Type, Store, Vault> {
|
||||
}
|
||||
>,
|
||||
) {
|
||||
return new Value<boolean, Store, Vault>(
|
||||
return new Value<boolean, Store>(
|
||||
async (options) => ({
|
||||
description: null,
|
||||
warning: null,
|
||||
@@ -165,7 +164,7 @@ export class Value<Type, Store, Vault> {
|
||||
immutable?: boolean
|
||||
generate?: null | RandomString
|
||||
}) {
|
||||
return new Value<AsRequired<string, Required>, never, never>(
|
||||
return new Value<AsRequired<string, Required>, never>(
|
||||
async () => ({
|
||||
type: "text" as const,
|
||||
description: null,
|
||||
@@ -185,10 +184,9 @@ export class Value<Type, Store, Vault> {
|
||||
asRequiredParser(string, a),
|
||||
)
|
||||
}
|
||||
static dynamicText<Store = never, Vault = never>(
|
||||
static dynamicText<Store = never>(
|
||||
getA: LazyBuild<
|
||||
Store,
|
||||
Vault,
|
||||
{
|
||||
name: string
|
||||
description?: string | null
|
||||
@@ -211,28 +209,25 @@ export class Value<Type, Store, Vault> {
|
||||
}
|
||||
>,
|
||||
) {
|
||||
return new Value<string | null | undefined, Store, Vault>(
|
||||
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<string | null | undefined, Store>(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<Type, Store, Vault> {
|
||||
Default is false */
|
||||
immutable?: boolean
|
||||
}) {
|
||||
return new Value<string, never, never>(async () => {
|
||||
return new Value<string, never>(async () => {
|
||||
const built: ValueSpecTextarea = {
|
||||
description: null,
|
||||
warning: null,
|
||||
@@ -261,10 +256,9 @@ export class Value<Type, Store, Vault> {
|
||||
return built
|
||||
}, string)
|
||||
}
|
||||
static dynamicTextarea<Store = never, Vault = never>(
|
||||
static dynamicTextarea<Store = never>(
|
||||
getA: LazyBuild<
|
||||
Store,
|
||||
Vault,
|
||||
{
|
||||
name: string
|
||||
description?: string | null
|
||||
@@ -277,7 +271,7 @@ export class Value<Type, Store, Vault> {
|
||||
}
|
||||
>,
|
||||
) {
|
||||
return new Value<string, Store, Vault>(async (options) => {
|
||||
return new Value<string, Store>(async (options) => {
|
||||
const a = await getA(options)
|
||||
return {
|
||||
description: null,
|
||||
@@ -308,7 +302,7 @@ export class Value<Type, Store, Vault> {
|
||||
Default is false */
|
||||
immutable?: boolean
|
||||
}) {
|
||||
return new Value<AsRequired<number, Required>, never, never>(
|
||||
return new Value<AsRequired<number, Required>, never>(
|
||||
() => ({
|
||||
type: "number" as const,
|
||||
description: null,
|
||||
@@ -326,10 +320,9 @@ export class Value<Type, Store, Vault> {
|
||||
asRequiredParser(number, a),
|
||||
)
|
||||
}
|
||||
static dynamicNumber<Store = never, Vault = never>(
|
||||
static dynamicNumber<Store = never>(
|
||||
getA: LazyBuild<
|
||||
Store,
|
||||
Vault,
|
||||
{
|
||||
name: string
|
||||
description?: string | null
|
||||
@@ -346,26 +339,23 @@ export class Value<Type, Store, Vault> {
|
||||
}
|
||||
>,
|
||||
) {
|
||||
return new Value<number | null | undefined, Store, Vault>(
|
||||
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<number | null | undefined, Store>(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<Required extends RequiredDefault<string>>(a: {
|
||||
name: string
|
||||
@@ -376,7 +366,7 @@ export class Value<Type, Store, Vault> {
|
||||
Default is false */
|
||||
immutable?: boolean
|
||||
}) {
|
||||
return new Value<AsRequired<string, Required>, never, never>(
|
||||
return new Value<AsRequired<string, Required>, never>(
|
||||
() => ({
|
||||
type: "color" as const,
|
||||
description: null,
|
||||
@@ -391,10 +381,9 @@ export class Value<Type, Store, Vault> {
|
||||
)
|
||||
}
|
||||
|
||||
static dynamicColor<Store = never, Vault = never>(
|
||||
static dynamicColor<Store = never>(
|
||||
getA: LazyBuild<
|
||||
Store,
|
||||
Vault,
|
||||
{
|
||||
name: string
|
||||
description?: string | null
|
||||
@@ -404,21 +393,18 @@ export class Value<Type, Store, Vault> {
|
||||
}
|
||||
>,
|
||||
) {
|
||||
return new Value<string | null | undefined, Store, Vault>(
|
||||
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<string | null | undefined, Store>(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<Required extends RequiredDefault<string>>(a: {
|
||||
name: string
|
||||
@@ -433,7 +419,7 @@ export class Value<Type, Store, Vault> {
|
||||
Default is false */
|
||||
immutable?: boolean
|
||||
}) {
|
||||
return new Value<AsRequired<string, Required>, never, never>(
|
||||
return new Value<AsRequired<string, Required>, never>(
|
||||
() => ({
|
||||
type: "datetime" as const,
|
||||
description: null,
|
||||
@@ -450,10 +436,9 @@ export class Value<Type, Store, Vault> {
|
||||
asRequiredParser(string, a),
|
||||
)
|
||||
}
|
||||
static dynamicDatetime<Store = never, Vault = never>(
|
||||
static dynamicDatetime<Store = never>(
|
||||
getA: LazyBuild<
|
||||
Store,
|
||||
Vault,
|
||||
{
|
||||
name: string
|
||||
description?: string | null
|
||||
@@ -467,24 +452,21 @@ export class Value<Type, Store, Vault> {
|
||||
}
|
||||
>,
|
||||
) {
|
||||
return new Value<string | null | undefined, Store, Vault>(
|
||||
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<string | null | undefined, Store>(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<string>,
|
||||
@@ -505,7 +487,7 @@ export class Value<Type, Store, Vault> {
|
||||
Default is false */
|
||||
immutable?: boolean
|
||||
}) {
|
||||
return new Value<AsRequired<keyof B, Required>, never, never>(
|
||||
return new Value<AsRequired<keyof B, Required>, never>(
|
||||
() => ({
|
||||
description: null,
|
||||
warning: null,
|
||||
@@ -523,10 +505,9 @@ export class Value<Type, Store, Vault> {
|
||||
) as any,
|
||||
)
|
||||
}
|
||||
static dynamicSelect<Store = never, Vault = never>(
|
||||
static dynamicSelect<Store = never>(
|
||||
getA: LazyBuild<
|
||||
Store,
|
||||
Vault,
|
||||
{
|
||||
name: string
|
||||
description?: string | null
|
||||
@@ -542,21 +523,18 @@ export class Value<Type, Store, Vault> {
|
||||
}
|
||||
>,
|
||||
) {
|
||||
return new Value<string | null | undefined, Store, Vault>(
|
||||
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<string | null | undefined, Store>(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<Values extends Record<string, string>>(a: {
|
||||
name: string
|
||||
@@ -576,7 +554,7 @@ export class Value<Type, Store, Vault> {
|
||||
*/
|
||||
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<Type, Store, Vault> {
|
||||
),
|
||||
)
|
||||
}
|
||||
static dynamicMultiselect<Store = never, Vault = never>(
|
||||
static dynamicMultiselect<Store = never>(
|
||||
getA: LazyBuild<
|
||||
Store,
|
||||
Vault,
|
||||
{
|
||||
name: string
|
||||
description?: string | null
|
||||
@@ -613,7 +590,7 @@ export class Value<Type, Store, Vault> {
|
||||
}
|
||||
>,
|
||||
) {
|
||||
return new Value<string[], Store, Vault>(async (options) => {
|
||||
return new Value<string[], Store>(async (options) => {
|
||||
const a = await getA(options)
|
||||
return {
|
||||
type: "multiselect" as const,
|
||||
@@ -627,15 +604,15 @@ export class Value<Type, Store, Vault> {
|
||||
}
|
||||
}, arrayOf(string))
|
||||
}
|
||||
static object<Type extends Record<string, any>, Store, Vault>(
|
||||
static object<Type extends Record<string, any>, Store>(
|
||||
a: {
|
||||
name: string
|
||||
description?: string | null
|
||||
warning?: string | null
|
||||
},
|
||||
spec: Config<Type, Store, Vault>,
|
||||
spec: Config<Type, Store>,
|
||||
) {
|
||||
return new Value<Type, Store, Vault>(async (options) => {
|
||||
return new Value<Type, Store>(async (options) => {
|
||||
const built = await spec.build(options as any)
|
||||
return {
|
||||
type: "object" as const,
|
||||
@@ -646,7 +623,7 @@ export class Value<Type, Store, Vault> {
|
||||
}
|
||||
}, spec.validator)
|
||||
}
|
||||
static file<Required extends boolean, Store, Vault>(a: {
|
||||
static file<Required extends boolean, Store>(a: {
|
||||
name: string
|
||||
description?: string | null
|
||||
warning?: string | null
|
||||
@@ -660,17 +637,16 @@ export class Value<Type, Store, Vault> {
|
||||
...a,
|
||||
}
|
||||
if (a.required) {
|
||||
return new Value<string, Store, Vault>(() => buildValue, string)
|
||||
return new Value<string, Store>(() => buildValue, string)
|
||||
}
|
||||
return new Value<string | null | undefined, Store, Vault>(
|
||||
return new Value<string | null | undefined, Store>(
|
||||
() => buildValue,
|
||||
string.optional(),
|
||||
)
|
||||
}
|
||||
static dynamicFile<Required extends boolean, Store, Vault>(
|
||||
static dynamicFile<Required extends boolean, Store>(
|
||||
a: LazyBuild<
|
||||
Store,
|
||||
Vault,
|
||||
{
|
||||
name: string
|
||||
description?: string | null
|
||||
@@ -680,7 +656,7 @@ export class Value<Type, Store, Vault> {
|
||||
}
|
||||
>,
|
||||
) {
|
||||
return new Value<string | null | undefined, Store, Vault>(
|
||||
return new Value<string | null | undefined, Store>(
|
||||
async (options) => ({
|
||||
type: "file" as const,
|
||||
description: null,
|
||||
@@ -690,7 +666,7 @@ export class Value<Type, Store, Vault> {
|
||||
string.optional(),
|
||||
)
|
||||
}
|
||||
static union<Required extends RequiredDefault<string>, Type, Store, Vault>(
|
||||
static union<Required extends RequiredDefault<string>, Type, Store>(
|
||||
a: {
|
||||
name: string
|
||||
description?: string | null
|
||||
@@ -706,9 +682,9 @@ export class Value<Type, Store, Vault> {
|
||||
*/
|
||||
disabled?: false | string | string[]
|
||||
},
|
||||
aVariants: Variants<Type, Store, Vault>,
|
||||
aVariants: Variants<Type, Store>,
|
||||
) {
|
||||
return new Value<AsRequired<Type, Required>, Store, Vault>(
|
||||
return new Value<AsRequired<Type, Required>, Store>(
|
||||
async (options) => ({
|
||||
type: "union" as const,
|
||||
description: null,
|
||||
@@ -726,18 +702,17 @@ export class Value<Type, Store, Vault> {
|
||||
Required extends RequiredDefault<string>,
|
||||
Type extends Record<string, any>,
|
||||
Store = never,
|
||||
Vault = never,
|
||||
>(
|
||||
getDisabledFn: LazyBuild<Store, Vault, string[] | false | string>,
|
||||
getDisabledFn: LazyBuild<Store, string[] | false | string>,
|
||||
a: {
|
||||
name: string
|
||||
description?: string | null
|
||||
warning?: string | null
|
||||
required: Required
|
||||
},
|
||||
aVariants: Variants<Type, Store, Vault> | Variants<Type, never, never>,
|
||||
aVariants: Variants<Type, Store> | Variants<Type, never>,
|
||||
) {
|
||||
return new Value<AsRequired<Type, Required>, Store, Vault>(
|
||||
return new Value<AsRequired<Type, Required>, Store>(
|
||||
async (options) => ({
|
||||
type: "union" as const,
|
||||
description: null,
|
||||
@@ -755,11 +730,9 @@ export class Value<Type, Store, Vault> {
|
||||
Required extends RequiredDefault<string>,
|
||||
Type extends Record<string, any>,
|
||||
Store = never,
|
||||
Vault = never,
|
||||
>(
|
||||
getA: LazyBuild<
|
||||
Store,
|
||||
Vault,
|
||||
{
|
||||
disabled: string[] | false | string
|
||||
name: string
|
||||
@@ -768,9 +741,9 @@ export class Value<Type, Store, Vault> {
|
||||
required: Required
|
||||
}
|
||||
>,
|
||||
aVariants: Variants<Type, Store, Vault> | Variants<Type, never, never>,
|
||||
aVariants: Variants<Type, Store> | Variants<Type, never>,
|
||||
) {
|
||||
return new Value<Type | null | undefined, Store, Vault>(async (options) => {
|
||||
return new Value<Type | null | undefined, Store>(async (options) => {
|
||||
const newValues = await getA(options)
|
||||
return {
|
||||
type: "union" as const,
|
||||
@@ -784,11 +757,8 @@ export class Value<Type, Store, Vault> {
|
||||
}, aVariants.validator.optional())
|
||||
}
|
||||
|
||||
static list<Type, Store, Vault>(a: List<Type, Store, Vault>) {
|
||||
return new Value<Type, Store, Vault>(
|
||||
(options) => a.build(options),
|
||||
a.validator,
|
||||
)
|
||||
static list<Type, Store>(a: List<Type, Store>) {
|
||||
return new Value<Type, Store>((options) => a.build(options), a.validator)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -806,6 +776,6 @@ export class Value<Type, Store, Vault> {
|
||||
```
|
||||
*/
|
||||
withStore<NewStore extends Store extends never ? any : Store>() {
|
||||
return this as any as Value<Type, NewStore, Vault>
|
||||
return this as any as Value<Type, NewStore>
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,21 +51,20 @@ export const pruning = Value.union(
|
||||
);
|
||||
```
|
||||
*/
|
||||
export class Variants<Type, Store, Vault> {
|
||||
export class Variants<Type, Store> {
|
||||
static text: any
|
||||
private constructor(
|
||||
public build: LazyBuild<Store, Vault, ValueSpecUnion["variants"]>,
|
||||
public build: LazyBuild<Store, ValueSpecUnion["variants"]>,
|
||||
public validator: Parser<unknown, Type>,
|
||||
) {}
|
||||
static of<
|
||||
VariantValues extends {
|
||||
[K in string]: {
|
||||
name: string
|
||||
spec: Config<any, Store, Vault> | Config<any, never, never>
|
||||
spec: Config<any, Store> | Config<any, never>
|
||||
}
|
||||
},
|
||||
Store = never,
|
||||
Vault = never,
|
||||
>(a: VariantValues) {
|
||||
const validator = anyOf(
|
||||
...Object.entries(a).map(([name, { spec }]) =>
|
||||
@@ -82,12 +81,11 @@ export class Variants<Type, Store, Vault> {
|
||||
unionSelectKey: K
|
||||
// prettier-ignore
|
||||
unionValueKey:
|
||||
VariantValues[K]["spec"] extends (Config<infer B, Store,Vault> | Config<infer B, never, never>) ? B :
|
||||
VariantValues[K]["spec"] extends (Config<infer B, Store> | Config<infer B, never>) ? 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<Type, Store, Vault> {
|
||||
```
|
||||
*/
|
||||
withStore<NewStore extends Store extends never ? any : Store>() {
|
||||
return this as any as Variants<Type, NewStore, Vault>
|
||||
return this as any as Variants<Type, NewStore>
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<ConfigSpecOf<SmtpValue>, never, never>({
|
||||
export const customSmtp = Config.of<ConfigSpecOf<SmtpValue>, never>({
|
||||
server: Value.text({
|
||||
name: "SMTP Server",
|
||||
required: {
|
||||
|
||||
@@ -14,16 +14,15 @@ export type DependenciesReceipt = void & {
|
||||
|
||||
export type Save<
|
||||
Store,
|
||||
Vault,
|
||||
A extends
|
||||
| Record<string, any>
|
||||
| Config<Record<string, any>, any, any>
|
||||
| Config<Record<string, never>, never, never>,
|
||||
| Config<Record<string, any>, any>
|
||||
| Config<Record<string, never>, never>,
|
||||
Manifest extends SDKManifest,
|
||||
> = (options: {
|
||||
effects: Effects
|
||||
input: ExtractConfigType<A> & Record<string, any>
|
||||
utils: Utils<Store, Vault>
|
||||
utils: Utils<Store>
|
||||
dependencies: D.ConfigDependencies<Manifest>
|
||||
}) => Promise<{
|
||||
dependenciesReceipt: DependenciesReceipt
|
||||
@@ -32,14 +31,13 @@ export type Save<
|
||||
}>
|
||||
export type Read<
|
||||
Store,
|
||||
Vault,
|
||||
A extends
|
||||
| Record<string, any>
|
||||
| Config<Record<string, any>, any, any>
|
||||
| Config<Record<string, any>, never, never>,
|
||||
| Config<Record<string, any>, any>
|
||||
| Config<Record<string, any>, never>,
|
||||
> = (options: {
|
||||
effects: Effects
|
||||
utils: Utils<Store, Vault>
|
||||
utils: Utils<Store>
|
||||
}) => Promise<void | (ExtractConfigType<A> & Record<string, any>)>
|
||||
/**
|
||||
* 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<string, any>
|
||||
| Config<any, any, any>
|
||||
| Config<any, never, never>,
|
||||
| Config<any, any>
|
||||
| Config<any, never>,
|
||||
Manifest extends SDKManifest,
|
||||
Type extends Record<string, any> = ExtractConfigType<ConfigType>,
|
||||
>(
|
||||
spec: Config<Type, Store, Vault> | Config<Type, never, never>,
|
||||
write: Save<Store, Vault, Type, Manifest>,
|
||||
read: Read<Store, Vault, Type>,
|
||||
spec: Config<Type, Store> | Config<Type, never>,
|
||||
write: Save<Store, Type, Manifest>,
|
||||
read: Read<Store, Type>,
|
||||
) {
|
||||
const validator = spec.validator
|
||||
return {
|
||||
@@ -82,7 +79,7 @@ export function setupConfig<
|
||||
}
|
||||
}) as ExpectedExports.setConfig,
|
||||
getConfig: (async ({ effects }) => {
|
||||
const myUtils = utils<Store, Vault>(effects)
|
||||
const myUtils = utils<Store>(effects)
|
||||
const configValue = nullIfEmpty(
|
||||
(await read({ effects, utils: myUtils })) || null,
|
||||
)
|
||||
|
||||
@@ -9,7 +9,6 @@ import { deepMerge } from "../util/deepMerge"
|
||||
|
||||
export class DependencyConfig<
|
||||
Store,
|
||||
Vault,
|
||||
Input extends Record<string, any>,
|
||||
RemoteConfig extends Record<string, any>,
|
||||
> {
|
||||
@@ -18,7 +17,7 @@ export class DependencyConfig<
|
||||
effects: Effects
|
||||
localConfig: Input
|
||||
remoteConfig: RemoteConfig
|
||||
utils: Utils<Store, Vault>
|
||||
utils: Utils<Store>
|
||||
}) => Promise<void | DeepPartial<RemoteConfig>>,
|
||||
) {}
|
||||
|
||||
@@ -28,7 +27,7 @@ export class DependencyConfig<
|
||||
const origConfig = JSON.parse(JSON.stringify(options.localConfig))
|
||||
const newOptions = {
|
||||
...options,
|
||||
utils: utils<Store, Vault>(options.effects),
|
||||
utils: utils<Store>(options.effects),
|
||||
localConfig: options.localConfig as Input,
|
||||
remoteConfig: options.remoteConfig as RemoteConfig,
|
||||
}
|
||||
@@ -49,7 +48,7 @@ export class DependencyConfig<
|
||||
): ReturnType<DependencyConfigType["autoConfigure"]> {
|
||||
const newOptions = {
|
||||
...options,
|
||||
utils: utils<Store, Vault>(options.effects),
|
||||
utils: utils<Store>(options.effects),
|
||||
localConfig: options.localConfig as Input,
|
||||
remoteConfig: options.remoteConfig as any,
|
||||
}
|
||||
|
||||
@@ -5,15 +5,13 @@ import { DependencyConfig } from "./DependencyConfig"
|
||||
|
||||
export function setupDependencyConfig<
|
||||
Store,
|
||||
Vault,
|
||||
Input extends Record<string, any>,
|
||||
Manifest extends SDKManifest,
|
||||
>(
|
||||
_config: Config<Input, Store, Vault> | Config<Input, never, never>,
|
||||
_config: Config<Input, Store> | Config<Input, never>,
|
||||
autoConfigs: {
|
||||
[key in keyof Manifest["dependencies"] & string]: DependencyConfig<
|
||||
Store,
|
||||
Vault,
|
||||
Input,
|
||||
any
|
||||
>
|
||||
|
||||
@@ -2,39 +2,27 @@ import { ManifestVersion } from "../../manifest/ManifestTypes"
|
||||
import { Effects } from "../../types"
|
||||
import { Utils } from "../../util/utils"
|
||||
|
||||
export class Migration<Store, Vault, Version extends ManifestVersion> {
|
||||
export class Migration<Stor, Version extends ManifestVersion> {
|
||||
constructor(
|
||||
readonly options: {
|
||||
version: Version
|
||||
up: (opts: {
|
||||
effects: Effects
|
||||
utils: Utils<Store, Vault>
|
||||
}) => Promise<void>
|
||||
down: (opts: {
|
||||
effects: Effects
|
||||
utils: Utils<Store, Vault>
|
||||
}) => Promise<void>
|
||||
up: (opts: { effects: Effects; utils: Utils<Stor> }) => Promise<void>
|
||||
down: (opts: { effects: Effects; utils: Utils<Stor> }) => Promise<void>
|
||||
},
|
||||
) {}
|
||||
static of<Store, Vault, Version extends ManifestVersion>(options: {
|
||||
static of<Stor, Version extends ManifestVersion>(options: {
|
||||
version: Version
|
||||
up: (opts: {
|
||||
effects: Effects
|
||||
utils: Utils<Store, Vault>
|
||||
}) => Promise<void>
|
||||
down: (opts: {
|
||||
effects: Effects
|
||||
utils: Utils<Store, Vault>
|
||||
}) => Promise<void>
|
||||
up: (opts: { effects: Effects; utils: Utils<Stor> }) => Promise<void>
|
||||
down: (opts: { effects: Effects; utils: Utils<Stor> }) => Promise<void>
|
||||
}) {
|
||||
return new Migration<Store, Vault, Version>(options)
|
||||
return new Migration<Stor, Version>(options)
|
||||
}
|
||||
|
||||
async up(opts: { effects: Effects; utils: Utils<Store, Vault> }) {
|
||||
async up(opts: { effects: Effects; utils: Utils<Stor> }) {
|
||||
this.up(opts)
|
||||
}
|
||||
|
||||
async down(opts: { effects: Effects; utils: Utils<Store, Vault> }) {
|
||||
async down(opts: { effects: Effects; utils: Utils<Stor> }) {
|
||||
this.down(opts)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,34 +5,30 @@ import { createUtils } from "../../util"
|
||||
import { once } from "../../util/once"
|
||||
import { Migration } from "./Migration"
|
||||
|
||||
export class Migrations<Store, Vault> {
|
||||
export class Migrations<Store> {
|
||||
private constructor(
|
||||
readonly manifest: SDKManifest,
|
||||
readonly migrations: Array<Migration<Store, Vault, any>>,
|
||||
readonly migrations: Array<Migration<Store, any>>,
|
||||
) {}
|
||||
private sortedMigrations = once(() => {
|
||||
const migrationsAsVersions = (
|
||||
this.migrations as Array<Migration<Store, Vault, any>>
|
||||
this.migrations as Array<Migration<Store, any>>
|
||||
).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<Migration<Store, Vault, any>>,
|
||||
>(manifest: SDKManifest, ...migrations: EnsureUniqueId<Migrations>) {
|
||||
return new Migrations(
|
||||
manifest,
|
||||
migrations as Array<Migration<Store, Vault, any>>,
|
||||
)
|
||||
static of<Store, Migrations extends Array<Migration<Store, any>>>(
|
||||
manifest: SDKManifest,
|
||||
...migrations: EnsureUniqueId<Migrations>
|
||||
) {
|
||||
return new Migrations(manifest, migrations as Array<Migration<Store, any>>)
|
||||
}
|
||||
async init({
|
||||
effects,
|
||||
previousVersion,
|
||||
}: Parameters<ExpectedExports.init>[0]) {
|
||||
const utils = createUtils<Store, Vault>(effects)
|
||||
const utils = createUtils<Store>(effects)
|
||||
if (!!previousVersion) {
|
||||
const previousVersionEmVer = EmVer.parse(previousVersion)
|
||||
for (const [_, migration] of this.sortedMigrations()
|
||||
@@ -46,7 +42,7 @@ export class Migrations<Store, Vault> {
|
||||
effects,
|
||||
nextVersion,
|
||||
}: Parameters<ExpectedExports.uninit>[0]) {
|
||||
const utils = createUtils<Store, Vault>(effects)
|
||||
const utils = createUtils<Store>(effects)
|
||||
if (!!nextVersion) {
|
||||
const nextVersionEmVer = EmVer.parse(nextVersion)
|
||||
const reversed = [...this.sortedMigrations()].reverse()
|
||||
@@ -61,16 +57,15 @@ export class Migrations<Store, Vault> {
|
||||
|
||||
export function setupMigrations<
|
||||
Store,
|
||||
Vault,
|
||||
Migrations extends Array<Migration<Store, Vault, any>>,
|
||||
Migrations extends Array<Migration<Store, any>>,
|
||||
>(manifest: SDKManifest, ...migrations: EnsureUniqueId<Migrations>) {
|
||||
return Migrations.of<Store, Vault, Migrations>(manifest, ...migrations)
|
||||
return Migrations.of<Store, Migrations>(manifest, ...migrations)
|
||||
}
|
||||
|
||||
// prettier-ignore
|
||||
export type EnsureUniqueId<A, B = A, ids = never> =
|
||||
B extends [] ? A :
|
||||
B extends [Migration<any,any, infer id>, ...infer Rest] ? (
|
||||
B extends [Migration<any, infer id>, ...infer Rest] ? (
|
||||
id extends ids ? "One of the ids are not unique"[] :
|
||||
EnsureUniqueId<A, Rest, id | ids>
|
||||
) : "There exists a migration that is not a Migration"[]
|
||||
|
||||
@@ -6,11 +6,11 @@ import { SetupExports } from "./setupExports"
|
||||
import { Install } from "./setupInstall"
|
||||
import { Uninstall } from "./setupUninstall"
|
||||
|
||||
export function setupInit<Store, Vault>(
|
||||
migrations: Migrations<Store, Vault>,
|
||||
install: Install<Store, Vault>,
|
||||
uninstall: Uninstall<Store, Vault>,
|
||||
setInterfaces: SetInterfaces<Store, Vault, any, any>,
|
||||
export function setupInit<Store>(
|
||||
migrations: Migrations<Store>,
|
||||
install: Install<Store>,
|
||||
uninstall: Uninstall<Store>,
|
||||
setInterfaces: SetInterfaces<Store, any, any>,
|
||||
setupExports: SetupExports<Store>,
|
||||
): {
|
||||
init: ExpectedExports.init
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import { Effects, ExpectedExports } from "../types"
|
||||
import { Utils, utils } from "../util/utils"
|
||||
|
||||
export type InstallFn<Store, Vault> = (opts: {
|
||||
export type InstallFn<Store> = (opts: {
|
||||
effects: Effects
|
||||
utils: Utils<Store, Vault>
|
||||
utils: Utils<Store>
|
||||
}) => Promise<void>
|
||||
export class Install<Store, Vault> {
|
||||
private constructor(readonly fn: InstallFn<Store, Vault>) {}
|
||||
static of<Store, Vault>(fn: InstallFn<Store, Vault>) {
|
||||
export class Install<Store> {
|
||||
private constructor(readonly fn: InstallFn<Store>) {}
|
||||
static of<Store>(fn: InstallFn<Store>) {
|
||||
return new Install(fn)
|
||||
}
|
||||
|
||||
@@ -23,6 +23,6 @@ export class Install<Store, Vault> {
|
||||
}
|
||||
}
|
||||
|
||||
export function setupInstall<Store, Vault>(fn: InstallFn<Store, Vault>) {
|
||||
export function setupInstall<Store>(fn: InstallFn<Store>) {
|
||||
return Install.of(fn)
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import { Effects, ExpectedExports } from "../types"
|
||||
import { Utils, utils } from "../util/utils"
|
||||
|
||||
export type UninstallFn<Store, Vault> = (opts: {
|
||||
export type UninstallFn<Store> = (opts: {
|
||||
effects: Effects
|
||||
utils: Utils<Store, Vault>
|
||||
utils: Utils<Store>
|
||||
}) => Promise<void>
|
||||
export class Uninstall<Store, Vault> {
|
||||
private constructor(readonly fn: UninstallFn<Store, Vault>) {}
|
||||
static of<Store, Vault>(fn: UninstallFn<Store, Vault>) {
|
||||
export class Uninstall<Store> {
|
||||
private constructor(readonly fn: UninstallFn<Store>) {}
|
||||
static of<Store>(fn: UninstallFn<Store>) {
|
||||
return new Uninstall(fn)
|
||||
}
|
||||
|
||||
@@ -23,6 +23,6 @@ export class Uninstall<Store, Vault> {
|
||||
}
|
||||
}
|
||||
|
||||
export function setupUninstall<Store, Vault>(fn: UninstallFn<Store, Vault>) {
|
||||
export function setupUninstall<Store>(fn: UninstallFn<Store>) {
|
||||
return Uninstall.of(fn)
|
||||
}
|
||||
|
||||
@@ -6,22 +6,20 @@ import { AddressReceipt } from "./AddressReceipt"
|
||||
export type InterfacesReceipt = Array<Address[] & AddressReceipt>
|
||||
export type SetInterfaces<
|
||||
Store,
|
||||
Vault,
|
||||
ConfigInput extends Record<string, any>,
|
||||
Output extends InterfacesReceipt,
|
||||
> = (opts: {
|
||||
effects: Effects
|
||||
input: null | ConfigInput
|
||||
utils: Utils<Store, Vault>
|
||||
utils: Utils<Store>
|
||||
}) => Promise<Output>
|
||||
export type SetupInterfaces = <
|
||||
Store,
|
||||
Vault,
|
||||
ConfigInput extends Record<string, any>,
|
||||
Output extends InterfacesReceipt,
|
||||
>(
|
||||
config: Config<ConfigInput, Store, Vault>,
|
||||
fn: SetInterfaces<Store, Vault, ConfigInput, Output>,
|
||||
) => SetInterfaces<Store, Vault, ConfigInput, Output>
|
||||
config: Config<ConfigInput, Store>,
|
||||
fn: SetInterfaces<Store, ConfigInput, Output>,
|
||||
) => SetInterfaces<Store, ConfigInput, Output>
|
||||
export const NO_INTERFACE_CHANGES = [] as InterfacesReceipt
|
||||
export const setupInterfaces: SetupInterfaces = (_config, fn) => fn
|
||||
|
||||
@@ -17,17 +17,17 @@ import "./Daemons"
|
||||
* @param fn
|
||||
* @returns
|
||||
*/
|
||||
export const setupMain = <Store, Vault>(
|
||||
export const setupMain = <Store>(
|
||||
fn: (o: {
|
||||
effects: Effects
|
||||
started(onTerm: () => void): null
|
||||
utils: Utils<Store, Vault, {}>
|
||||
utils: Utils<Store, {}>
|
||||
}) => Promise<Daemons<any>>,
|
||||
): ExpectedExports.main => {
|
||||
return async (options) => {
|
||||
const result = await fn({
|
||||
...options,
|
||||
utils: createMainUtils<Store, Vault>(options.effects),
|
||||
utils: createMainUtils<Store>(options.effects),
|
||||
})
|
||||
await result.build().then((x) => x.wait())
|
||||
}
|
||||
|
||||
@@ -45,5 +45,4 @@ export const sdk = StartSdk.of()
|
||||
}),
|
||||
)
|
||||
.withStore<{ storeRoot: { storeLeaf: "value" } }>()
|
||||
.withVault<{ vaultRoot: "value" }>()
|
||||
.build(true)
|
||||
|
||||
@@ -7,9 +7,6 @@ type Store = {
|
||||
someValue: "a" | "b"
|
||||
}
|
||||
}
|
||||
type Vault = {
|
||||
hello: string
|
||||
}
|
||||
const todo = <A>(): A => {
|
||||
throw new Error("not implemented")
|
||||
}
|
||||
@@ -51,10 +48,10 @@ describe("Store", () => {
|
||||
path: "/config/some2Value",
|
||||
value: "a",
|
||||
})
|
||||
;(await createMainUtils<Store, Vault>(todo<Effects>())
|
||||
;(await createMainUtils<Store>(todo<Effects>())
|
||||
.store.getOwn("/config/someValue")
|
||||
.const()) satisfies string
|
||||
;(await createMainUtils<Store, Vault>(todo<Effects>())
|
||||
;(await createMainUtils<Store>(todo<Effects>())
|
||||
.store.getOwn("/config")
|
||||
.const()) satisfies Store["config"]
|
||||
await createMainUtils(todo<Effects>())
|
||||
|
||||
@@ -462,14 +462,6 @@ export type Effects = {
|
||||
}): Promise<string>
|
||||
|
||||
stopped(packageId?: string): Promise<boolean>
|
||||
|
||||
vault: {
|
||||
list(): Promise<string[]>
|
||||
get(opt: { key: string; callback: () => void }): Promise<string>
|
||||
set(opt: { key: string; value: string }): Promise<void>
|
||||
move(opt: { fromKey: string; toKey: string }): Promise<void>
|
||||
delete(opt: { key: string }): Promise<void>
|
||||
}
|
||||
}
|
||||
|
||||
// prettier-ignore
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
import { Effects, EnsureStorePath } from "../types"
|
||||
|
||||
export class GetVault<Vault> {
|
||||
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<void>((resolve) => {
|
||||
callback = resolve
|
||||
})
|
||||
yield await this.effects.vault.get({
|
||||
key: this.key,
|
||||
callback: () => callback(),
|
||||
})
|
||||
await waitForNext
|
||||
}
|
||||
}
|
||||
}
|
||||
export function getVault<Vault>(effects: Effects, key: keyof Vault & string) {
|
||||
return new GetVault<Vault>(effects, key)
|
||||
}
|
||||
@@ -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 = <Store, Vault>(effects: T.Effects) =>
|
||||
createUtils<Store, Vault, {}>(effects)
|
||||
export const createMainUtils = <Store>(effects: T.Effects) =>
|
||||
createUtils<Store, {}>(effects)
|
||||
|
||||
type NeverPossible = { [affine]: string }
|
||||
export type NoAny<A> = NeverPossible extends A
|
||||
|
||||
@@ -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<Store, WrapperOverWrite = { const: never }> = {
|
||||
path: string
|
||||
search: Record<string, string>
|
||||
}) => NetworkInterfaceBuilder
|
||||
createOrUpdateVault: (opts: {
|
||||
key: keyof Vault & string
|
||||
value: string | null | undefined
|
||||
generator: DefaultString
|
||||
}) => Promise<string>
|
||||
getSystemSmtp: () => GetSystemSmtp & WrapperOverWrite
|
||||
host: {
|
||||
static: (id: string) => StaticHost
|
||||
@@ -108,43 +102,14 @@ export type Utils<Store, WrapperOverWrite = { const: never }> = {
|
||||
value: ExtractStore<Store, Path>,
|
||||
) => Promise<void>
|
||||
}
|
||||
vault: {
|
||||
get: (key: keyof Vault & string) => GetVault<Vault> & WrapperOverWrite
|
||||
set: (key: keyof Vault & string, value: string) => Promise<void>
|
||||
}
|
||||
writeFile: <A>(
|
||||
fileHelper: FileHelper<A>,
|
||||
data: A,
|
||||
) => ReturnType<FileHelper<A>["write"]>
|
||||
}
|
||||
export const utils = <
|
||||
Store = never,
|
||||
Vault = never,
|
||||
WrapperOverWrite = { const: never },
|
||||
>(
|
||||
export const utils = <Store = never, WrapperOverWrite = { const: never }>(
|
||||
effects: Effects,
|
||||
): Utils<Store, Vault, WrapperOverWrite> => ({
|
||||
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<Store, WrapperOverWrite> => ({
|
||||
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<Vault>(effects, key) as GetVault<Vault> & WrapperOverWrite,
|
||||
set: (key: keyof Vault & string, value: string) =>
|
||||
effects.vault.set({ key, value }),
|
||||
},
|
||||
|
||||
mountDependencies: <
|
||||
In extends
|
||||
| Record<ManifestId, Record<VolumeName, Record<NamedPath, Path>>>
|
||||
|
||||
Reference in New Issue
Block a user