chore: Remove the stupid config

This commit is contained in:
BluJ
2023-05-02 14:10:21 -06:00
parent 07251c91a6
commit 747fa39c6c
14 changed files with 111 additions and 129 deletions

View File

@@ -5,21 +5,20 @@ import { _ } from "../../util"
import { Effects } from "../../types"
import { Parser, object } from "ts-matches"
export type LazyBuildOptions<WD, ConfigType> = {
export type LazyBuildOptions<WD> = {
effects: Effects
utils: Utils<WD>
config: ConfigType | null
}
export type LazyBuild<WD, ConfigType, ExpectedOut> = (
options: LazyBuildOptions<WD, ConfigType>,
export type LazyBuild<WD, ExpectedOut> = (
options: LazyBuildOptions<WD>,
) => Promise<ExpectedOut> | ExpectedOut
// prettier-ignore
export type ExtractConfigType<A extends Record<string, any> | Config<Record<string, any>, any, any>> =
A extends Config<infer B, any, any> ? B :
export type ExtractConfigType<A extends Record<string, any> | Config<Record<string, any>, any>> =
A extends Config<infer B, any> ? B :
A
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
@@ -76,14 +75,14 @@ export const addNodesSpec = Config.of({ hostname: hostname, port: port });
```
*/
export class Config<Type extends Record<string, any>, WD, ConfigType> {
export class Config<Type extends Record<string, any>, WD> {
private constructor(
private readonly spec: {
[K in keyof Type]: Value<Type[K], WD, ConfigType>
[K in keyof Type]: Value<Type[K], WD>
},
public validator: Parser<unknown, Type>,
) {}
async build(options: LazyBuildOptions<WD, ConfigType>) {
async build(options: LazyBuildOptions<WD>) {
const answer = {} as {
[K in keyof Type]: ValueSpec
}
@@ -93,8 +92,8 @@ export class Config<Type extends Record<string, any>, WD, ConfigType> {
return answer
}
static of<Type extends Record<string, any>, WrapperData, ConfigType>(spec: {
[K in keyof Type]: Value<Type[K], WrapperData, ConfigType>
static of<Type extends Record<string, any>, WrapperData>(spec: {
[K in keyof Type]: Value<Type[K], WrapperData>
}) {
const validatorObj = {} as {
[K in keyof Type]: Parser<unknown, Type[K]>
@@ -103,21 +102,22 @@ export class Config<Type extends Record<string, any>, WD, ConfigType> {
validatorObj[key] = spec[key].validator
}
const validator = object(validatorObj)
return new Config<Type, WrapperData, ConfigType>(spec, validator)
return new Config<Type, WrapperData>(spec, validator)
}
static withWrapperData<WrapperData>() {
return {
of<Type extends Record<string, any>>(spec: {
[K in keyof Type]: Value<Type[K], WrapperData, Type>
[K in keyof Type]: Value<Type[K], WrapperData>
}) {
return Config.of<Type, WrapperData, Type>(spec)
return Config.of<Type, WrapperData>(spec)
},
}
}
}
export function topConfig<WrapperData>() {
return <Type extends Record<string, any>>(spec: {
[K in keyof Type]: Value<Type[K], WrapperData, Type>
}) => Config.of<Type, WrapperData, Type>(spec)
[K in keyof Type]: Value<Type[K], WrapperData>
}) => Config.of<Type, WrapperData>(spec)
}

View File

@@ -19,12 +19,12 @@ export const authorizationList = List.string({
export const auth = Value.list(authorizationList);
```
*/
export class List<Type, WD, ConfigType> {
export class List<Type, WD> {
private constructor(
public build: LazyBuild<WD, ConfigType, ValueSpecList>,
public build: LazyBuild<WD, ValueSpecList>,
public validator: Parser<unknown, Type>,
) {}
static text<WD, CT>(
static text<WD>(
a: {
name: string
description?: string | null
@@ -45,7 +45,7 @@ export class List<Type, WD, ConfigType> {
inputmode?: ListValueSpecText["inputmode"]
},
) {
return new List<string[], WD, CT>(() => {
return new List<string[], WD>(() => {
const spec = {
type: "text" as const,
placeholder: null,
@@ -67,10 +67,9 @@ export class List<Type, WD, ConfigType> {
}
}, arrayOf(string))
}
static dynamicText<WD, CT>(
static dynamicText<WD>(
getA: LazyBuild<
WD,
CT,
{
name: string
description?: string | null
@@ -92,7 +91,7 @@ export class List<Type, WD, ConfigType> {
}
>,
) {
return new List<string[], WD, CT>(async (options) => {
return new List<string[], WD>(async (options) => {
const { spec: aSpec, ...a } = await getA(options)
const spec = {
type: "text" as const,
@@ -115,7 +114,7 @@ export class List<Type, WD, ConfigType> {
}
}, arrayOf(string))
}
static number<WD, CT>(
static number<WD>(
a: {
name: string
description?: string | null
@@ -134,7 +133,7 @@ export class List<Type, WD, ConfigType> {
placeholder?: string | null
},
) {
return new List<number[], WD, CT>(() => {
return new List<number[], WD>(() => {
const spec = {
type: "number" as const,
placeholder: null,
@@ -156,10 +155,9 @@ export class List<Type, WD, ConfigType> {
}
}, arrayOf(number))
}
static dynamicNumber<WD, CT>(
static dynamicNumber<WD>(
getA: LazyBuild<
WD,
CT,
{
name: string
description?: string | null
@@ -179,7 +177,7 @@ export class List<Type, WD, ConfigType> {
}
>,
) {
return new List<number[], WD, CT>(async (options) => {
return new List<number[], WD>(async (options) => {
const { spec: aSpec, ...a } = await getA(options)
const spec = {
type: "number" as const,
@@ -202,7 +200,7 @@ export class List<Type, WD, ConfigType> {
}
}, arrayOf(number))
}
static obj<Type extends Record<string, any>, WrapperData, ConfigType>(
static obj<Type extends Record<string, any>, WrapperData>(
a: {
name: string
description?: string | null
@@ -213,12 +211,12 @@ export class List<Type, WD, ConfigType> {
maxLength?: number | null
},
aSpec: {
spec: Config<Type, WrapperData, ConfigType>
spec: Config<Type, WrapperData>
displayAs?: null | string
uniqueBy?: null | UniqueBy
},
) {
return new List<Type[], WrapperData, ConfigType>(async (options) => {
return new List<Type[], WrapperData>(async (options) => {
const { spec: previousSpecSpec, ...restSpec } = aSpec
const specSpec = await previousSpecSpec.build(options)
const spec = {

View File

@@ -93,18 +93,18 @@ const username = Value.string({
});
```
*/
export class Value<Type, WD, ConfigType> {
export class Value<Type, WD> {
private constructor(
public build: LazyBuild<WD, ConfigType, ValueSpec>,
public build: LazyBuild<WD, ValueSpec>,
public validator: Parser<unknown, Type>,
) {}
static toggle<WD, CT>(a: {
static toggle<WD>(a: {
name: string
description?: string | null
warning?: string | null
default?: boolean | null
}) {
return new Value<boolean, WD, CT>(
return new Value<boolean, WD>(
async () => ({
description: null,
warning: null,
@@ -115,10 +115,9 @@ export class Value<Type, WD, ConfigType> {
boolean,
)
}
static dynamicToggle<WD, CT>(
static dynamicToggle<WD>(
a: LazyBuild<
WD,
CT,
{
name: string
description?: string | null
@@ -127,7 +126,7 @@ export class Value<Type, WD, ConfigType> {
}
>,
) {
return new Value<boolean, WD, CT>(
return new Value<boolean, WD>(
async (options) => ({
description: null,
warning: null,
@@ -138,7 +137,7 @@ export class Value<Type, WD, ConfigType> {
boolean,
)
}
static text<Required extends RequiredDefault<DefaultString>, WD, CT>(a: {
static text<Required extends RequiredDefault<DefaultString>, WD>(a: {
name: string
description?: string | null
warning?: string | null
@@ -153,7 +152,7 @@ export class Value<Type, WD, ConfigType> {
/** Default = 'text' */
inputmode?: ValueSpecText["inputmode"]
}) {
return new Value<AsRequired<string, Required>, WD, CT>(
return new Value<AsRequired<string, Required>, WD>(
async () => ({
type: "text" as const,
description: null,
@@ -170,10 +169,9 @@ export class Value<Type, WD, ConfigType> {
asRequiredParser(string, a),
)
}
static dynamicText<WD, CT>(
static dynamicText<WD>(
getA: LazyBuild<
WD,
CT,
{
name: string
description?: string | null
@@ -191,7 +189,7 @@ export class Value<Type, WD, ConfigType> {
}
>,
) {
return new Value<string | null | undefined, WD, CT>(async (options) => {
return new Value<string | null | undefined, WD>(async (options) => {
const a = await getA(options)
return {
type: "text" as const,
@@ -208,7 +206,7 @@ export class Value<Type, WD, ConfigType> {
}
}, string.optional())
}
static textarea<WD, CT>(a: {
static textarea<WD>(a: {
name: string
description?: string | null
warning?: string | null
@@ -217,7 +215,7 @@ export class Value<Type, WD, ConfigType> {
maxLength?: number | null
placeholder?: string | null
}) {
return new Value<string, WD, CT>(
return new Value<string, WD>(
async () =>
({
description: null,
@@ -231,10 +229,9 @@ export class Value<Type, WD, ConfigType> {
string,
)
}
static dynamicTextarea<WD, CT>(
static dynamicTextarea<WD>(
getA: LazyBuild<
WD,
CT,
{
name: string
description?: string | null
@@ -246,7 +243,7 @@ export class Value<Type, WD, ConfigType> {
}
>,
) {
return new Value<string, WD, CT>(async (options) => {
return new Value<string, WD>(async (options) => {
const a = await getA(options)
return {
description: null,
@@ -259,7 +256,7 @@ export class Value<Type, WD, ConfigType> {
}
}, string)
}
static number<Required extends RequiredDefault<number>, WD, CT>(a: {
static number<Required extends RequiredDefault<number>, WD>(a: {
name: string
description?: string | null
warning?: string | null
@@ -272,7 +269,7 @@ export class Value<Type, WD, ConfigType> {
units?: string | null
placeholder?: string | null
}) {
return new Value<AsRequired<number, Required>, WD, CT>(
return new Value<AsRequired<number, Required>, WD>(
() => ({
type: "number" as const,
description: null,
@@ -288,10 +285,9 @@ export class Value<Type, WD, ConfigType> {
asRequiredParser(number, a),
)
}
static dynamicNumber<WD, CT>(
static dynamicNumber<WD>(
getA: LazyBuild<
WD,
CT,
{
name: string
description?: string | null
@@ -307,7 +303,7 @@ export class Value<Type, WD, ConfigType> {
}
>,
) {
return new Value<number | null | undefined, WD, CT>(async (options) => {
return new Value<number | null | undefined, WD>(async (options) => {
const a = await getA(options)
return {
type: "number" as const,
@@ -323,13 +319,13 @@ export class Value<Type, WD, ConfigType> {
}
}, number.optional())
}
static color<Required extends RequiredDefault<string>, WD, CT>(a: {
static color<Required extends RequiredDefault<string>, WD>(a: {
name: string
description?: string | null
warning?: string | null
required: Required
}) {
return new Value<AsRequired<string, Required>, WD, CT>(
return new Value<AsRequired<string, Required>, WD>(
() => ({
type: "color" as const,
description: null,
@@ -342,10 +338,9 @@ export class Value<Type, WD, ConfigType> {
)
}
static dynamicColor<WD, CT>(
static dynamicColor<WD>(
getA: LazyBuild<
WD,
CT,
{
name: string
description?: string | null
@@ -354,7 +349,7 @@ export class Value<Type, WD, ConfigType> {
}
>,
) {
return new Value<string | null | undefined, WD, CT>(async (options) => {
return new Value<string | null | undefined, WD>(async (options) => {
const a = await getA(options)
return {
type: "color" as const,
@@ -365,7 +360,7 @@ export class Value<Type, WD, ConfigType> {
}
}, string.optional())
}
static datetime<Required extends RequiredDefault<string>, WD, CT>(a: {
static datetime<Required extends RequiredDefault<string>, WD>(a: {
name: string
description?: string | null
warning?: string | null
@@ -376,7 +371,7 @@ export class Value<Type, WD, ConfigType> {
max?: string | null
step?: string | null
}) {
return new Value<AsRequired<string, Required>, WD, CT>(
return new Value<AsRequired<string, Required>, WD>(
() => ({
type: "datetime" as const,
description: null,
@@ -391,10 +386,9 @@ export class Value<Type, WD, ConfigType> {
asRequiredParser(string, a),
)
}
static dynamicDatetime<WD, CT>(
static dynamicDatetime<WD>(
getA: LazyBuild<
WD,
CT,
{
name: string
description?: string | null
@@ -408,7 +402,7 @@ export class Value<Type, WD, ConfigType> {
}
>,
) {
return new Value<string | null | undefined, WD, CT>(async (options) => {
return new Value<string | null | undefined, WD>(async (options) => {
const a = await getA(options)
return {
type: "datetime" as const,
@@ -435,7 +429,7 @@ export class Value<Type, WD, ConfigType> {
required: Required
values: B
}) {
return new Value<AsRequired<keyof B, Required>, WD, CT>(
return new Value<AsRequired<keyof B, Required>, WD>(
() => ({
description: null,
warning: null,
@@ -451,10 +445,9 @@ export class Value<Type, WD, ConfigType> {
) as any,
)
}
static dynamicSelect<WD, CT>(
static dynamicSelect<WD>(
getA: LazyBuild<
WD,
CT,
{
name: string
description?: string | null
@@ -464,7 +457,7 @@ export class Value<Type, WD, ConfigType> {
}
>,
) {
return new Value<string | null | undefined, WD, CT>(async (options) => {
return new Value<string | null | undefined, WD>(async (options) => {
const a = await getA(options)
return {
description: null,
@@ -475,7 +468,7 @@ export class Value<Type, WD, ConfigType> {
}
}, string.optional())
}
static multiselect<Values extends Record<string, string>, WD, CT>(a: {
static multiselect<Values extends Record<string, string>, WD>(a: {
name: string
description?: string | null
warning?: string | null
@@ -484,7 +477,7 @@ export class Value<Type, WD, ConfigType> {
minLength?: number | null
maxLength?: number | null
}) {
return new Value<(keyof Values)[], WD, CT>(
return new Value<(keyof Values)[], WD>(
() => ({
type: "multiselect" as const,
minLength: null,
@@ -498,10 +491,9 @@ export class Value<Type, WD, ConfigType> {
),
)
}
static dynamicMultiselect<WD, CT>(
static dynamicMultiselect<WD>(
getA: LazyBuild<
WD,
CT,
{
name: string
description?: string | null
@@ -513,7 +505,7 @@ export class Value<Type, WD, ConfigType> {
}
>,
) {
return new Value<string[], WD, CT>(async (options) => {
return new Value<string[], WD>(async (options) => {
const a = await getA(options)
return {
type: "multiselect" as const,
@@ -525,15 +517,15 @@ export class Value<Type, WD, ConfigType> {
}
}, arrayOf(string))
}
static object<Type extends Record<string, any>, WrapperData, ConfigType>(
static object<Type extends Record<string, any>, WrapperData>(
a: {
name: string
description?: string | null
warning?: string | null
},
previousSpec: Config<Type, WrapperData, ConfigType>,
previousSpec: Config<Type, WrapperData>,
) {
return new Value<Type, WrapperData, ConfigType>(async (options) => {
return new Value<Type, WrapperData>(async (options) => {
const spec = await previousSpec.build(options as any)
return {
type: "object" as const,
@@ -557,9 +549,9 @@ export class Value<Type, WD, ConfigType> {
required: Required
default?: string | null
},
aVariants: Variants<Type, WrapperData, ConfigType>,
aVariants: Variants<Type, WrapperData>,
) {
return new Value<AsRequired<Type, Required>, WrapperData, ConfigType>(
return new Value<AsRequired<Type, Required>, WrapperData>(
async (options) => ({
type: "union" as const,
description: null,
@@ -584,14 +576,13 @@ export class Value<Type, WD, ConfigType> {
required: Required
default?: string | null
},
aVariants: Variants<Type, WrapperData, ConfigType>,
aVariants: Variants<Type, WrapperData>,
getDisabledFn: LazyBuild<
WrapperData,
ConfigType,
Array<Type extends { unionSelectKey: infer B } ? B & string : never>
>,
) {
return new Value<Type | null | undefined, WrapperData, ConfigType>(
return new Value<Type | null | undefined, WrapperData>(
async (options) => ({
type: "union" as const,
description: null,
@@ -605,11 +596,9 @@ export class Value<Type, WD, ConfigType> {
)
}
static list<Type, WrapperData, ConfigType>(
a: List<Type, WrapperData, ConfigType>,
) {
static list<Type, WrapperData>(a: List<Type, WrapperData>) {
/// TODO
return new Value<Type, WrapperData, ConfigType>(
return new Value<Type, WrapperData>(
(options) => a.build(options),
a.validator,
)

View File

@@ -52,9 +52,9 @@ export const pruning = Value.union(
);
```
*/
export class Variants<Type, WD, ConfigType> {
export class Variants<Type, WD> {
private constructor(
public build: LazyBuild<WD, ConfigType, ValueSpecUnion["variants"]>,
public build: LazyBuild<WD, ValueSpecUnion["variants"]>,
public validator: Parser<unknown, Type>,
) {}
// A extends {
@@ -70,7 +70,7 @@ export class Variants<Type, WD, ConfigType> {
>(a: {
[K in keyof TypeMap]: {
name: string
spec: Config<TypeMap[K], WrapperData, ConfigType>
spec: Config<TypeMap[K], WrapperData>
}
}) {
type TypeOut = {
@@ -89,7 +89,7 @@ export class Variants<Type, WD, ConfigType> {
),
) as Parser<unknown, TypeOut>
return new Variants<TypeOut, WrapperData, ConfigType>(async (options) => {
return new Variants<TypeOut, WrapperData>(async (options) => {
const variants = {} as {
[K in keyof TypeMap]: { name: string; spec: InputSpec }
}

View File

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

View File

@@ -2,7 +2,7 @@ import { Config } from "./builder"
import { DeepPartial, Dependencies, Effects, ExpectedExports } from "../types"
import { InputSpec } from "./configTypes"
import { Utils, nullIfEmpty, once, utils } from "../util"
import { GenericManifest } from "../manifest/ManifestTypes"
import { SDKManifest } from "../manifest/ManifestTypes"
import * as D from "./dependencies"
import { ExtractConfigType } from "./builder/config"
@@ -13,8 +13,8 @@ export type DependenciesReceipt = void & {
export type Save<
WD,
A extends Record<string, any> | Config<Record<string, any>, any, any>,
Manifest extends GenericManifest,
A extends Record<string, any> | Config<Record<string, any>, any>,
Manifest extends SDKManifest,
> = (options: {
effects: Effects
input: ExtractConfigType<A> & Record<string, any>
@@ -23,7 +23,7 @@ export type Save<
}) => Promise<DependenciesReceipt>
export type Read<
WD,
A extends Record<string, any> | Config<Record<string, any>, any, any>,
A extends Record<string, any> | Config<Record<string, any>, any>,
> = (options: {
effects: Effects
utils: Utils<WD>
@@ -37,11 +37,11 @@ export type Read<
*/
export function setupConfig<
WD,
ConfigType extends Record<string, any> | Config<any, any, any>,
Manifest extends GenericManifest,
ConfigType extends Record<string, any> | Config<any, any>,
Manifest extends SDKManifest,
Type extends Record<string, any> = ExtractConfigType<ConfigType>,
>(
spec: Config<Type, WD, Type>,
spec: Config<Type, WD>,
write: Save<WD, Type, Manifest>,
read: Read<WD, Type>,
) {
@@ -68,7 +68,6 @@ export function setupConfig<
spec: await spec.build({
effects,
utils: myUtils,
config: configValue as Type,
}),
config: configValue,
}