wip: Creating an sdk builder that has all the generics we need in one place

This commit is contained in:
BluJ
2023-05-08 16:43:47 -06:00
parent e17668da00
commit 2b267c6c60
16 changed files with 275 additions and 51 deletions

248
lib/StartSDK.ts Normal file
View File

@@ -0,0 +1,248 @@
import { AnyParser } from "ts-matches"
import { SDKManifest } from "./manifest/ManifestTypes"
import { RequiredDefault, Value } from "./config/builder/value"
import { Config, ExtractConfigType, LazyBuild } from "./config/builder/config"
import {
DefaultString,
Pattern,
RandomString,
ValueSpecDatetime,
ValueSpecText,
} from "./config/configTypes"
import { Variants } from "./config/builder/variants"
import { createAction } from "./actions/createAction"
import { ActionMetaData, Effects, ActionResult, Metadata } from "./types"
import { Utils } from "./util"
// prettier-ignore
type AnyNeverCond<T extends any[], Then, Else> =
T extends [] ? Else :
T extends [never, ...Array<any>] ? Then :
T extends [any, ...infer U] ? AnyNeverCond<U,Then, Else> :
never
class StartSDK<Manifest extends SDKManifest, Store> {
private constructor() {}
private anyOf<A>(
a: A,
): AnyNeverCond<[Manifest, Store], "Build not ready", A> {
return a as any
}
static of() {
return new StartSDK<never, never>()
}
withManifest<Manifest extends SDKManifest = never>() {
return new StartSDK<Manifest, Store>()
}
withStore<Store extends Record<string, any>>() {
return new StartSDK<Manifest, Store>()
}
build() {
return this.anyOf({
// TODO AutoConfig
// TODO Backup
// TODO Config
// TODO configConstants
// TODO configDependencies
createAction: <
Store,
ConfigType extends
| Record<string, any>
| Config<any, any>
| Config<any, never>,
Type extends Record<string, any> = ExtractConfigType<ConfigType>,
>(
metaData: Omit<ActionMetaData, "input"> & {
input: Config<Type, Store> | Config<Type, never>
},
fn: (options: {
effects: Effects
utils: Utils<Store>
input: Type
}) => Promise<ActionResult>,
) => createAction<Store, ConfigType, Type>(metaData, fn),
// TODO Daemons
// TODO HealthCheck
// TODO healthCheckFns
// TODO List
// TODO mainNetwork
// TODO Migration
// TODO setupActions
// TODO setupAutoConfig
// TODO setupBackup
// TODO setupInit
// TODO setupInstall
// TODO setupMain
// TODO setupManifest
// TODO setupMigrations
// TODO setupUninstall
// TODO trigger changeOnFirstSuccess, cooldown, default
Value: {
toggle: Value.toggle,
text: Value.text,
textarea: Value.textarea,
number: Value.number,
color: Value.color,
datetime: Value.datetime,
select: Value.select,
multiselect: Value.multiselect,
object: Value.object,
union: Value.union,
list: Value.list,
dynamicToggle: (
a: LazyBuild<
Store,
{
name: string
description?: string | null
warning?: string | null
default: boolean
disabled?: false | string
}
>,
) => Value.dynamicToggle<Store>(a),
dynamicText: (
getA: LazyBuild<
Store,
{
name: string
description?: string | null
warning?: string | null
required: RequiredDefault<DefaultString>
/** Default = false */
masked?: boolean
placeholder?: string | null
minLength?: number | null
maxLength?: number | null
patterns?: Pattern[]
/** Default = 'text' */
inputmode?: ValueSpecText["inputmode"]
generate?: null | RandomString
}
>,
) => Value.dynamicText<Store>(getA),
dynamicTextarea: (
getA: LazyBuild<
Store,
{
name: string
description?: string | null
warning?: string | null
required: boolean
minLength?: number | null
maxLength?: number | null
placeholder?: string | null
disabled?: false | string
generate?: null | RandomString
}
>,
) => Value.dynamicTextarea<Store>(getA),
dynamicNumber: (
getA: LazyBuild<
Store,
{
name: string
description?: string | null
warning?: string | null
required: RequiredDefault<number>
min?: number | null
max?: number | null
/** Default = '1' */
step?: string | null
integer: boolean
units?: string | null
placeholder?: string | null
disabled?: false | string
}
>,
) => Value.dynamicNumber<Store>(getA),
dynamicColor: (
getA: LazyBuild<
Store,
{
name: string
description?: string | null
warning?: string | null
required: RequiredDefault<string>
disabled?: false | string
}
>,
) => Value.dynamicColor<Store>(getA),
dynamicDatetime: (
getA: LazyBuild<
Store,
{
name: string
description?: string | null
warning?: string | null
required: RequiredDefault<string>
/** Default = 'datetime-local' */
inputmode?: ValueSpecDatetime["inputmode"]
min?: string | null
max?: string | null
step?: string | null
disabled?: false | string
}
>,
) => Value.dynamicDatetime<Store>(getA),
dynamicSelect: (
getA: LazyBuild<
Store,
{
name: string
description?: string | null
warning?: string | null
required: RequiredDefault<string>
values: Record<string, string>
disabled?: false | string
}
>,
) => Value.dynamicSelect<Store>(getA),
dynamicMultiselect: (
getA: LazyBuild<
Store,
{
name: string
description?: string | null
warning?: string | null
default: string[]
values: Record<string, string>
minLength?: number | null
maxLength?: number | null
disabled?: false | string
}
>,
) => Value.dynamicMultiselect<Store>(getA),
filteredUnion: <
Required extends RequiredDefault<string>,
Type extends Record<string, any>,
>(
getDisabledFn: LazyBuild<Store, string[]>,
a: {
name: string
description?: string | null
warning?: string | null
required: Required
},
aVariants: Variants<Type, Store> | Variants<Type, never>,
) =>
Value.filteredUnion<Required, Type, Store>(
getDisabledFn,
a,
aVariants,
),
},
// TODO Variants
})
}
}
// TODO Test output.ts with sdk
// const test = StartSDK.of()
// .withManifest<any>()
// .withStore<{}>()
// .Value.dynamicToggle({} as any, {} as any)

View File

@@ -1,56 +1,51 @@
import { Config, ExtractConfigType } from "../config/builder/config" import { Config, ExtractConfigType } from "../config/builder/config"
import { ActionMetaData, ActionResult, Effects, ExportedAction } from "../types" import { ActionMetaData, ActionResult, Effects, ExportedAction } from "../types"
import { Utils, createUtils, utils } from "../util" import { Utils, createUtils, utils } from "../util"
import { WrapperDataContract } from "../wrapperData/wrapperDataContract"
export class CreatedAction< export class CreatedAction<
WD, Store,
ConfigType extends Record<string, any> | Config<any, WD> | Config<any, never>, ConfigType extends
| Record<string, any>
| Config<any, Store>
| Config<any, never>,
Type extends Record<string, any> = ExtractConfigType<ConfigType>, Type extends Record<string, any> = ExtractConfigType<ConfigType>,
> { > {
private constructor( private constructor(
readonly wrapperDataContract: WrapperDataContract<WD>,
public readonly myMetaData: ActionMetaData, public readonly myMetaData: ActionMetaData,
readonly fn: (options: { readonly fn: (options: {
effects: Effects effects: Effects
utils: Utils<WD> utils: Utils<Store>
input: Type input: Type
}) => Promise<ActionResult>, }) => Promise<ActionResult>,
readonly input: Config<Type, WD> | Config<Type, never>, readonly input: Config<Type, Store> | Config<Type, never>,
) {} ) {}
public validator = this.input.validator public validator = this.input.validator
static of< static of<
WD, Store,
ConfigType extends ConfigType extends
| Record<string, any> | Record<string, any>
| Config<any, any> | Config<any, any>
| Config<any, never>, | Config<any, never>,
Type extends Record<string, any> = ExtractConfigType<ConfigType>, Type extends Record<string, any> = ExtractConfigType<ConfigType>,
>( >(
wrapperDataContract: WrapperDataContract<WD>,
metaData: Omit<ActionMetaData, "input"> & { metaData: Omit<ActionMetaData, "input"> & {
input: Config<Type, WD> | Config<Type, never> input: Config<Type, Store> | Config<Type, never>
}, },
fn: (options: { fn: (options: {
effects: Effects effects: Effects
utils: Utils<WD> utils: Utils<Store>
input: Type input: Type
}) => Promise<ActionResult>, }) => Promise<ActionResult>,
) { ) {
const { input, ...rest } = metaData const { input, ...rest } = metaData
return new CreatedAction<WD, ConfigType, Type>( return new CreatedAction<Store, ConfigType, Type>(rest, fn, input)
wrapperDataContract,
rest,
fn,
input,
)
} }
exportedAction: ExportedAction = ({ effects, input }) => { exportedAction: ExportedAction = ({ effects, input }) => {
return this.fn({ return this.fn({
effects, effects,
utils: createUtils(this.wrapperDataContract, effects), utils: createUtils(effects),
input: this.validator.unsafeCast(input), input: this.validator.unsafeCast(input),
}) })
} }
@@ -58,7 +53,7 @@ export class CreatedAction<
run = async ({ effects, input }: { effects: Effects; input?: Type }) => { run = async ({ effects, input }: { effects: Effects; input?: Type }) => {
return this.fn({ return this.fn({
effects, effects,
utils: createUtils(this.wrapperDataContract, effects), utils: createUtils(effects),
input: this.validator.unsafeCast(input), input: this.validator.unsafeCast(input),
}) })
} }
@@ -66,7 +61,7 @@ export class CreatedAction<
async getConfig({ effects }: { effects: Effects }) { async getConfig({ effects }: { effects: Effects }) {
return this.input.build({ return this.input.build({
effects, effects,
utils: createUtils(this.wrapperDataContract, effects) as any, utils: createUtils(effects) as any,
}) })
} }
} }

View File

@@ -24,9 +24,8 @@ import {
unknown, unknown,
} from "ts-matches" } from "ts-matches"
import { once } from "../../util/once" import { once } from "../../util/once"
import { WrapperDataContract } from "../../wrapperData/wrapperDataContract"
type RequiredDefault<A> = export type RequiredDefault<A> =
| false | false
| { | {
default: A | null default: A | null
@@ -96,7 +95,7 @@ const username = Value.string({
``` ```
*/ */
export class Value<Type, WD> { export class Value<Type, WD> {
private constructor( protected constructor(
public build: LazyBuild<WD, ValueSpec>, public build: LazyBuild<WD, ValueSpec>,
public validator: Parser<unknown, Type>, public validator: Parser<unknown, Type>,
) {} ) {}
@@ -122,7 +121,6 @@ export class Value<Type, WD> {
) )
} }
static dynamicToggle<WD = never>( static dynamicToggle<WD = never>(
_wrapperDataContract: WrapperDataContract<WD>,
a: LazyBuild< a: LazyBuild<
WD, WD,
{ {
@@ -186,7 +184,6 @@ export class Value<Type, WD> {
) )
} }
static dynamicText<WD = never>( static dynamicText<WD = never>(
_wrapperDataContract: WrapperDataContract<WD>,
getA: LazyBuild< getA: LazyBuild<
WD, WD,
{ {
@@ -258,7 +255,6 @@ export class Value<Type, WD> {
) )
} }
static dynamicTextarea<WD = never>( static dynamicTextarea<WD = never>(
_wrapperDataContract: WrapperDataContract<WD>,
getA: LazyBuild< getA: LazyBuild<
WD, WD,
{ {
@@ -325,7 +321,6 @@ export class Value<Type, WD> {
) )
} }
static dynamicNumber<WD = never>( static dynamicNumber<WD = never>(
_wrapperDataContract: WrapperDataContract<WD>,
getA: LazyBuild< getA: LazyBuild<
WD, WD,
{ {
@@ -387,7 +382,6 @@ export class Value<Type, WD> {
} }
static dynamicColor<WD = never>( static dynamicColor<WD = never>(
_wrapperDataContract: WrapperDataContract<WD>,
getA: LazyBuild< getA: LazyBuild<
WD, WD,
{ {
@@ -445,7 +439,6 @@ export class Value<Type, WD> {
) )
} }
static dynamicDatetime<WD = never>( static dynamicDatetime<WD = never>(
_wrapperDataContract: WrapperDataContract<WD>,
getA: LazyBuild< getA: LazyBuild<
WD, WD,
{ {
@@ -642,7 +635,6 @@ export class Value<Type, WD> {
Type extends Record<string, any>, Type extends Record<string, any>,
WD = never, WD = never,
>( >(
_wrapperDataContract: WrapperDataContract<WD>,
getDisabledFn: LazyBuild<WD, string[]>, getDisabledFn: LazyBuild<WD, string[]>,
a: { a: {
name: string name: string

View File

@@ -1,12 +1,14 @@
import { SDKManifest } from "../manifest/ManifestTypes" import { SDKManifest } from "../manifest/ManifestTypes"
import { Dependency } from "../types" import { Dependency } from "../types"
export type Dependencies<T extends SDKManifest> = { export type ConfigDependencies<T extends SDKManifest> = {
exists(id: keyof T["dependencies"]): Dependency exists(id: keyof T["dependencies"]): Dependency
running(id: keyof T["dependencies"]): Dependency running(id: keyof T["dependencies"]): Dependency
} }
export const dependenciesSet = <T extends SDKManifest>(): Dependencies<T> => ({ export const configDependenciesSet = <
T extends SDKManifest,
>(): ConfigDependencies<T> => ({
exists(id: keyof T["dependencies"]) { exists(id: keyof T["dependencies"]) {
return { return {
id, id,

View File

@@ -1,5 +1,5 @@
import "./builder" import "./builder"
import "./setupConfig" import "./setupConfig"
import "./dependencies" import "./configDependencies"
import "./constants" import "./configConstants"

View File

@@ -1,6 +1,6 @@
import { Effects, ExpectedExports } from "../types" import { Effects, ExpectedExports } from "../types"
import { SDKManifest } from "../manifest/ManifestTypes" import { SDKManifest } from "../manifest/ManifestTypes"
import * as D from "./dependencies" import * as D from "./configDependencies"
import { Config, ExtractConfigType } from "./builder/config" import { Config, ExtractConfigType } from "./builder/config"
import { Utils, utils } from "../util" import { Utils, utils } from "../util"
import nullIfEmpty from "../util/nullIfEmpty" import nullIfEmpty from "../util/nullIfEmpty"
@@ -22,7 +22,7 @@ export type Save<
effects: Effects effects: Effects
input: ExtractConfigType<A> & Record<string, any> input: ExtractConfigType<A> & Record<string, any>
utils: Utils<WD> utils: Utils<WD>
dependencies: D.Dependencies<Manifest> dependencies: D.ConfigDependencies<Manifest>
}) => Promise<{ }) => Promise<{
dependenciesReceipt: DependenciesReceipt dependenciesReceipt: DependenciesReceipt
restart: boolean restart: boolean
@@ -70,7 +70,7 @@ export function setupConfig<
input: JSON.parse(JSON.stringify(input)), input: JSON.parse(JSON.stringify(input)),
effects, effects,
utils: utils(wrapperDataContract, effects), utils: utils(wrapperDataContract, effects),
dependencies: D.dependenciesSet<Manifest>(), dependencies: D.configDependenciesSet<Manifest>(),
}) })
if (restart) { if (restart) {
await effects.restart() await effects.restart()

View File

@@ -19,3 +19,5 @@ import "./inits"
export * as matches from "ts-matches" export * as matches from "ts-matches"
export * as YAML from "yaml" export * as YAML from "yaml"
export * as TOML from "@iarna/toml" export * as TOML from "@iarna/toml"
export class<Manifest extends SDKManifest>

View File

@@ -84,7 +84,6 @@ export type Utils<WD, WrapperOverWrite = { const: never }> = {
nullIfEmpty: typeof nullIfEmpty nullIfEmpty: typeof nullIfEmpty
} }
export const utils = <WD = never, WrapperOverWrite = { const: never }>( export const utils = <WD = never, WrapperOverWrite = { const: never }>(
_wrapperDataContract: WrapperDataContract<WD>,
effects: T.Effects, effects: T.Effects,
): Utils<WD, WrapperOverWrite> => ({ ): Utils<WD, WrapperOverWrite> => ({
createOrUpdateVault: async ({ createOrUpdateVault: async ({

View File

@@ -1,14 +0,0 @@
export declare const wrapperDataContractType: unique symbol
export type WrapperDataContract<A> = {
[wrapperDataContractType]: A
}
export const neverWrapperDataContract: WrapperDataContract<never> = null as any
/**
* Used to indicate the type of the wrapper data. To be used in areas where
* we need to know the wrapper data value
*/
export function createWrapperDataContract<A = never>(): A extends never
? "Wrapper Data Contract must be created with a generic"
: WrapperDataContract<A> {
return null as any
}