diff --git a/Makefile b/Makefile index 57d065d..0e2e5c0 100644 --- a/Makefile +++ b/Makefile @@ -5,8 +5,9 @@ test: $(TS_FILES) make clean: rm -rf dist -# utils/test/output.ts: utils/test/config.json scripts/oldSpecToBuilder.ts -# cat utils/test/config.json | deno run scripts/oldSpecToBuilder.ts "../../mod" |deno fmt - > utils/test/output.ts + +lib/util/artifacts/output.ts: lib/util/artifacts/makeOutput.ts lib/scripts/oldSpecToBuilder.ts + npm run buildOutput bundle: fmt $(TS_FILES) .FORCE node_modules npx tsc-multi diff --git a/lib/config/builder/list.ts b/lib/config/builder/list.ts index 42cb685..aaa3280 100644 --- a/lib/config/builder/list.ts +++ b/lib/config/builder/list.ts @@ -35,10 +35,10 @@ import { export class List extends IBuilder { static string< A extends Description & - Default & { - range: string; - spec: StringSpec; - } + Default & { + range: string; + spec: StringSpec; + } >(a: A) { return new List({ type: "list" as const, @@ -48,10 +48,10 @@ export class List extends IBuilder { } static number< A extends Description & - Default & { - range: string; - spec: NumberSpec; - } + Default & { + range: string; + spec: NumberSpec; + } >(a: A) { return new List({ type: "list" as const, @@ -61,14 +61,14 @@ export class List extends IBuilder { } static obj< A extends Description & - Default[]> & { - range: string; - spec: { - spec: Config; - displayAs: null | string; - uniqueBy: null | UniqueBy; - }; - } + Default[]> & { + range: string; + spec: { + spec: Config; + displayAs: null | string; + uniqueBy: null | UniqueBy; + }; + } >(a: A) { const { spec: previousSpec, ...rest } = a; const { spec: previousSpecSpec, ...restSpec } = previousSpec; @@ -91,20 +91,18 @@ export class List extends IBuilder { } static union< A extends Description & - Default & { - range: string; - spec: { - id: B; - name: string; - description: null | string; - warning: null | string; - variants: Variants<{ [key: string]: { name: string, spec: InputSpec } }>; - displayAs: null | string; - uniqueBy: UniqueBy; - default: string; - }; - }, - B extends string + Default & { + range: string; + spec: { + name: string; + description: null | string; + warning: null | string; + variants: Variants<{ [key: string]: { name: string, spec: InputSpec } }>; + displayAs: null | string; + uniqueBy: UniqueBy; + default: string; + }; + } >(a: A) { const { spec: previousSpec, ...rest } = a; const { variants: previousVariants, ...restSpec } = previousSpec; diff --git a/lib/config/builder/variants.ts b/lib/config/builder/variants.ts index d88e226..43dab61 100644 --- a/lib/config/builder/variants.ts +++ b/lib/config/builder/variants.ts @@ -39,16 +39,24 @@ import { Config } from "."; ``` */ export class Variants< - A extends { [key: string]: InputSpec } + A extends { + [key: string]: { + name: string, + spec: InputSpec + } + } > extends IBuilder { static of< A extends { - [key: string]: Config; + [key: string]: { name: string, spec: Config }; } >(a: A) { - const variants: { [K in keyof A]: BuilderExtract } = {} as any; + const variants: { [K in keyof A]: { name: string, spec: BuilderExtract } } = {} as any; for (const key in a) { - variants[key] = a[key].build() as any; + const value = a[key] + variants[key] = { + name: value.name, spec: value.spec.build() as any + } } return new Variants(variants); } diff --git a/lib/types.ts b/lib/types.ts index 635211e..60c36c8 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -171,7 +171,7 @@ export type Effects = { method?: "GET" | "POST" | "PUT" | "DELETE" | "HEAD" | "PATCH"; headers?: Record; body?: string; - }, + } ): Promise<{ method: string; ok: boolean; @@ -281,7 +281,7 @@ export type Effects = { */ getSslCertificate: ( packageId: string, - algorithm?: "ecdsa" | "ed25519", + algorithm?: "ecdsa" | "ed25519" ) => [string, string, string]; /** * @returns PEM encoded ssl key (ecdsa) diff --git a/lib/types/config-types.ts b/lib/types/config-types.ts index 26388b1..6ac74dd 100644 --- a/lib/types/config-types.ts +++ b/lib/types/config-types.ts @@ -107,7 +107,8 @@ export type ListValueSpecOf = T extends "string" /** represents a spec for a list */ export type ValueSpecList = ValueSpecListOf; -export interface ValueSpecListOf extends WithStandalone { +export interface ValueSpecListOf + extends WithStandalone { type: "list"; subtype: T; spec: ListValueSpecOf; @@ -124,7 +125,10 @@ export interface ValueSpecListOf extends WithStanda } // sometimes the type checker needs just a little bit of help -export function isValueSpecListOf(t: ValueSpecList, s: S): t is ValueSpecListOf { +export function isValueSpecListOf( + t: ValueSpecList, + s: S +): t is ValueSpecListOf { return t.subtype === s; } diff --git a/lib/util/propertiesMatcher.ts b/lib/util/propertiesMatcher.ts index 6dd7ee8..3cc3f00 100644 --- a/lib/util/propertiesMatcher.ts +++ b/lib/util/propertiesMatcher.ts @@ -12,55 +12,55 @@ type TypePointer = "pointer"; type TypeUnion = "union"; // prettier-ignore -type GuardDefaultNullable = - A extends { readonly default: unknown} ? Type : - A extends { readonly nullable: true} ? Type : - A extends {readonly nullable: false} ? Type | null | undefined : - Type +type GuardDefaultNullable = + A extends { readonly default: unknown } ? Type : + A extends { readonly nullable: true } ? Type : + A extends { readonly nullable: false } ? Type | null | undefined : + Type // prettier-ignore -type GuardNumber = - A extends {readonly type:TypeNumber} ? GuardDefaultNullable : - unknown +type GuardNumber = + A extends { readonly type: TypeNumber } ? GuardDefaultNullable : + unknown // prettier-ignore -type GuardString = - A extends {readonly type:TypeString} ? GuardDefaultNullable : - unknown +type GuardString = + A extends { readonly type: TypeString } ? GuardDefaultNullable : + unknown // prettier-ignore -type GuardBoolean = - A extends {readonly type:TypeBoolean} ? GuardDefaultNullable : - unknown +type GuardBoolean = + A extends { readonly type: TypeBoolean } ? GuardDefaultNullable : + unknown // prettier-ignore -type GuardObject = - A extends {readonly type: TypeObject, readonly spec: infer B} ? ( - B extends Record ? {readonly [K in keyof B & string]: _>} : - {_error: "Invalid Spec"} - ) : - unknown +type GuardObject = + A extends { readonly type: TypeObject, readonly spec: infer B } ? ( + B extends Record ? { readonly [K in keyof B & string]: _> } : + { _error: "Invalid Spec" } + ) : + unknown // prettier-ignore -export type GuardList = - A extends {readonly type:TypeList, readonly subtype: infer B, spec?: {spec?: infer C }} ? ReadonlyArray & ({type: B, spec: C})>> : - A extends {readonly type:TypeList, readonly subtype: infer B, spec?: {}} ? ReadonlyArray & ({type: B})>> : - unknown +export type GuardList = + A extends { readonly type: TypeList, readonly subtype: infer B, spec?: { spec?: infer C } } ? ReadonlyArray & ({ type: B, spec: C })>> : + A extends { readonly type: TypeList, readonly subtype: infer B, spec?: {} } ? ReadonlyArray & ({ type: B })>> : + unknown // prettier-ignore -type GuardPointer = - A extends {readonly type:TypePointer} ? (string | null) : - unknown +type GuardPointer = + A extends { readonly type: TypePointer } ? (string | null) : + unknown // prettier-ignore -type GuardSelect = - A extends {readonly type:TypeSelect, readonly values: ArrayLike} ? GuardDefaultNullable : - unknown +type GuardSelect = + A extends { readonly type: TypeSelect, readonly values: ArrayLike } ? GuardDefaultNullable : + unknown // prettier-ignore -type GuardMultiselect = - A extends {readonly type:TypeMultiselect, readonly values: ArrayLike} ? GuardDefaultNullable : - unknown +type GuardMultiselect = + A extends { readonly type: TypeMultiselect, readonly values: ArrayLike } ? GuardDefaultNullable : + unknown // prettier-ignore -type GuardUnion = - A extends {readonly type:TypeUnion, readonly tag: {id: infer Id & string}, variants: infer Variants & Record} ? {[K in keyof Variants]: {[keyType in Id & string]: K}&TypeFromProps}[keyof Variants] : - unknown +type GuardUnion = + A extends { readonly type: TypeUnion, readonly tag: { id: infer Id & string }, variants: infer Variants & Record } ? { [K in keyof Variants]: { [keyType in Id & string]: K } & TypeFromProps }[keyof Variants] : + unknown type _ = T; export type GuardAll = GuardNumber & @@ -73,9 +73,9 @@ export type GuardAll = GuardNumber & GuardSelect & GuardMultiselect; // prettier-ignore -export type TypeFromProps = - A extends Record ? {readonly [K in keyof A & string]: _>} : - unknown; +export type TypeFromProps = + A extends Record ? { readonly [K in keyof A & string]: _> } : + unknown; const isType = matches.shape({ type: matches.string }); const recordString = matches.dictionary([matches.string, matches.unknown]); @@ -155,23 +155,23 @@ export function matchNumberWithRange(range: string) { leftValue === "*" ? (_) => true : left === "[" - ? (x) => x >= Number(leftValue) - : (x) => x > Number(leftValue), + ? (x) => x >= Number(leftValue) + : (x) => x > Number(leftValue), leftValue === "*" ? "any" : left === "[" - ? `greaterThanOrEqualTo${leftValue}` - : `greaterThan${leftValue}` + ? `greaterThanOrEqualTo${leftValue}` + : `greaterThan${leftValue}` ) .validate( // prettier-ignore rightValue === "*" ? (_) => true : - right === "]"? (x) => x <= Number(rightValue) : - (x) => x < Number(rightValue), + right === "]" ? (x) => x <= Number(rightValue) : + (x) => x < Number(rightValue), // prettier-ignore rightValue === "*" ? "any" : - right === "]" ? `lessThanOrEqualTo${rightValue}` : - `lessThan${rightValue}` + right === "]" ? `lessThanOrEqualTo${rightValue}` : + `lessThan${rightValue}` ); } function withIntegral(parser: matches.Parser, value: unknown) {