mirror of
https://github.com/Start9Labs/start-sdk.git
synced 2026-04-04 14:29:47 +00:00
feat: creating the rest of the sdk
This commit is contained in:
@@ -1,16 +1,16 @@
|
||||
import { ValueSpec } from "../configTypes"
|
||||
import { Utils } from "../../util"
|
||||
import { Utils } from "../../util/utils"
|
||||
import { Value } from "./value"
|
||||
import { _ } from "../../util"
|
||||
import { Effects } from "../../types"
|
||||
import { Parser, object } from "ts-matches"
|
||||
|
||||
export type LazyBuildOptions<WD> = {
|
||||
export type LazyBuildOptions<Store> = {
|
||||
effects: Effects
|
||||
utils: Utils<WD>
|
||||
utils: Utils<Store>
|
||||
}
|
||||
export type LazyBuild<WD, ExpectedOut> = (
|
||||
options: LazyBuildOptions<WD>,
|
||||
export type LazyBuild<Store, ExpectedOut> = (
|
||||
options: LazyBuildOptions<Store>,
|
||||
) => Promise<ExpectedOut> | ExpectedOut
|
||||
|
||||
// prettier-ignore
|
||||
@@ -18,8 +18,8 @@ export type ExtractConfigType<A extends Record<string, any> | Config<Record<stri
|
||||
A extends Config<infer B, any> | Config<infer B, never> ? B :
|
||||
A
|
||||
|
||||
export type ConfigSpecOf<A extends Record<string, any>, WD = never> = {
|
||||
[K in keyof A]: Value<A[K], WD>
|
||||
export type ConfigSpecOf<A extends Record<string, any>, Store = never> = {
|
||||
[K in keyof A]: Value<A[K], Store>
|
||||
}
|
||||
|
||||
export type MaybeLazyValues<A> = LazyBuild<any, A> | A
|
||||
@@ -79,14 +79,14 @@ export const addNodesSpec = Config.of({ hostname: hostname, port: port });
|
||||
|
||||
```
|
||||
*/
|
||||
export class Config<Type extends Record<string, any>, WD> {
|
||||
export class Config<Type extends Record<string, any>, Store> {
|
||||
private constructor(
|
||||
private readonly spec: {
|
||||
[K in keyof Type]: Value<Type[K], WD> | Value<Type[K], never>
|
||||
[K in keyof Type]: Value<Type[K], Store> | Value<Type[K], never>
|
||||
},
|
||||
public validator: Parser<unknown, Type>,
|
||||
) {}
|
||||
async build(options: LazyBuildOptions<WD>) {
|
||||
async build(options: LazyBuildOptions<Store>) {
|
||||
const answer = {} as {
|
||||
[K in keyof Type]: ValueSpec
|
||||
}
|
||||
@@ -96,9 +96,10 @@ export class Config<Type extends Record<string, any>, WD> {
|
||||
return answer
|
||||
}
|
||||
|
||||
static of<Spec extends Record<string, Value<any, any> | Value<any, never>>>(
|
||||
spec: Spec,
|
||||
) {
|
||||
static of<
|
||||
Spec extends Record<string, Value<any, Store> | Value<any, never>>,
|
||||
Store,
|
||||
>(spec: Spec) {
|
||||
const validatorObj = {} as {
|
||||
[K in keyof Spec]: Parser<unknown, any>
|
||||
}
|
||||
@@ -109,14 +110,12 @@ export class Config<Type extends Record<string, any>, WD> {
|
||||
return new Config<
|
||||
{
|
||||
[K in keyof Spec]: Spec[K] extends
|
||||
| Value<infer T, any>
|
||||
| Value<infer T, Store>
|
||||
| Value<infer T, never>
|
||||
? T
|
||||
: never
|
||||
},
|
||||
{
|
||||
[K in keyof Spec]: Spec[K] extends Value<any, infer WD> ? WD : never
|
||||
}[keyof Spec]
|
||||
Store
|
||||
>(spec, validator as any)
|
||||
}
|
||||
|
||||
@@ -129,12 +128,12 @@ export class Config<Type extends Record<string, any>, WD> {
|
||||
required: false,
|
||||
})
|
||||
|
||||
return Config.of<WrapperData>()({
|
||||
myValue: a.withWrapperData(),
|
||||
return Config.of<Store>()({
|
||||
myValue: a.withStore(),
|
||||
})
|
||||
```
|
||||
*/
|
||||
withWrapperData<NewWrapperData extends WD extends never ? any : WD>() {
|
||||
return this as any as Config<Type, NewWrapperData>
|
||||
withStore<NewStore extends Store extends never ? any : Store>() {
|
||||
return this as any as Config<Type, NewStore>
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user