mirror of
https://github.com/Start9Labs/start-sdk.git
synced 2026-03-30 20:24:47 +00:00
feat: Add a builder for variants
This commit is contained in:
33
config/variants.ts
Normal file
33
config/variants.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { ConfigSpec } from "../types.ts";
|
||||
import { BuilderExtract, IBuilder } from "./builder.ts";
|
||||
import { Config } from "./mod.ts";
|
||||
|
||||
export class Variants<A extends { [key: string]: ConfigSpec }> extends IBuilder<A> {
|
||||
static of<
|
||||
A extends {
|
||||
[key: string]: Config<ConfigSpec>;
|
||||
}
|
||||
>(a: A) {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
const variants: { [K in keyof A]: BuilderExtract<A[K]> } = {} as any;
|
||||
for (const key in a) {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
variants[key] = a[key].build() as any;
|
||||
}
|
||||
return new Variants(variants);
|
||||
}
|
||||
|
||||
static empty() {
|
||||
return Variants.of({});
|
||||
}
|
||||
static withVariannt<K extends string, B extends ConfigSpec>(key: K, value: Config<B>) {
|
||||
return Variants.empty().withVariant(key, value);
|
||||
}
|
||||
|
||||
withVariant<K extends string, B extends ConfigSpec>(key: K, value: Config<B>) {
|
||||
return new Variants({
|
||||
...this.a,
|
||||
[key]: value.build(),
|
||||
} as A & { [key in K]: B });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user