mirror of
https://github.com/Start9Labs/start-sdk.git
synced 2026-03-30 12:21:57 +00:00
feat: Auto config figure out type
This commit is contained in:
@@ -69,7 +69,7 @@ export class StartSdk<Manifest extends SDKManifest, Store> {
|
|||||||
|
|
||||||
build(isReady: AnyNeverCond<[Manifest, Store], "Build not ready", true>) {
|
build(isReady: AnyNeverCond<[Manifest, Store], "Build not ready", true>) {
|
||||||
return {
|
return {
|
||||||
AutoConfig: <Input, NestedConfigs>(
|
AutoConfig: <Input, NestedConfigs extends Record<string, any>>(
|
||||||
configs: AutoConfigFrom<Store, Input, NestedConfigs>,
|
configs: AutoConfigFrom<Store, Input, NestedConfigs>,
|
||||||
path: keyof AutoConfigFrom<Store, Input, NestedConfigs>,
|
path: keyof AutoConfigFrom<Store, Input, NestedConfigs>,
|
||||||
) => new AutoConfig<Store, Input, NestedConfigs>(configs, path),
|
) => new AutoConfig<Store, Input, NestedConfigs>(configs, path),
|
||||||
@@ -196,13 +196,18 @@ export class StartSdk<Manifest extends SDKManifest, Store> {
|
|||||||
setupActions: (...createdActions: CreatedAction<any, any>[]) =>
|
setupActions: (...createdActions: CreatedAction<any, any>[]) =>
|
||||||
setupActions<Store>(...createdActions),
|
setupActions<Store>(...createdActions),
|
||||||
setupAutoConfig: <
|
setupAutoConfig: <
|
||||||
Input,
|
Input extends Record<string, any>,
|
||||||
NestedConfigs extends {
|
NestedConfigs extends {
|
||||||
[key in keyof Manifest["dependencies"]]: unknown
|
[key in keyof Manifest["dependencies"]]: unknown
|
||||||
},
|
},
|
||||||
>(
|
>(
|
||||||
configs: AutoConfigFrom<Store, Input, NestedConfigs>,
|
config: Config<Input, Store>,
|
||||||
) => setupAutoConfig<Store, Input, Manifest, NestedConfigs>(configs),
|
autoConfigs: AutoConfigFrom<Store, Input, NestedConfigs>,
|
||||||
|
) =>
|
||||||
|
setupAutoConfig<Store, Input, Manifest, NestedConfigs>(
|
||||||
|
config,
|
||||||
|
autoConfigs,
|
||||||
|
),
|
||||||
setupBackups: (...args: SetupBackupsParams<Manifest>) =>
|
setupBackups: (...args: SetupBackupsParams<Manifest>) =>
|
||||||
setupBackups<Manifest>(...args),
|
setupBackups<Manifest>(...args),
|
||||||
setupConfig: <
|
setupConfig: <
|
||||||
|
|||||||
@@ -2,16 +2,28 @@ import { AutoConfigure, DeepPartial, Effects, ExpectedExports } from "../types"
|
|||||||
import { Utils, utils } from "../util/utils"
|
import { Utils, utils } from "../util/utils"
|
||||||
import { deepEqual } from "../util/deepEqual"
|
import { deepEqual } from "../util/deepEqual"
|
||||||
import { deepMerge } from "../util/deepMerge"
|
import { deepMerge } from "../util/deepMerge"
|
||||||
|
import { Config } from "../config/builder/config"
|
||||||
|
|
||||||
export type AutoConfigFrom<Store, Input, NestedConfigs> = {
|
export type AutoConfigFrom<
|
||||||
[key in keyof NestedConfigs & string]: (options: {
|
Store,
|
||||||
effects: Effects
|
Input,
|
||||||
localConfig: Input
|
NestedConfigs extends Record<string, any>,
|
||||||
remoteConfig: NestedConfigs[key]
|
> = {
|
||||||
utils: Utils<Store>
|
[key in keyof NestedConfigs & string]: {
|
||||||
}) => Promise<void | DeepPartial<NestedConfigs[key]>>
|
serviceConfig: Config<NestedConfigs[key], any>
|
||||||
|
autoConfig: (options: {
|
||||||
|
effects: Effects
|
||||||
|
localConfig: Input
|
||||||
|
remoteConfig: NestedConfigs[key]
|
||||||
|
utils: Utils<Store>
|
||||||
|
}) => Promise<void | DeepPartial<NestedConfigs[key]>>
|
||||||
|
}
|
||||||
}
|
}
|
||||||
export class AutoConfig<Store, Input, NestedConfigs> {
|
export class AutoConfig<
|
||||||
|
Store,
|
||||||
|
Input,
|
||||||
|
NestedConfigs extends Record<string, any>,
|
||||||
|
> {
|
||||||
constructor(
|
constructor(
|
||||||
readonly configs: AutoConfigFrom<Store, Input, NestedConfigs>,
|
readonly configs: AutoConfigFrom<Store, Input, NestedConfigs>,
|
||||||
readonly path: keyof AutoConfigFrom<Store, Input, NestedConfigs>,
|
readonly path: keyof AutoConfigFrom<Store, Input, NestedConfigs>,
|
||||||
@@ -33,7 +45,7 @@ export class AutoConfig<Store, Input, NestedConfigs> {
|
|||||||
deepMerge(
|
deepMerge(
|
||||||
{},
|
{},
|
||||||
options.localConfig,
|
options.localConfig,
|
||||||
await this.configs[this.path](newOptions),
|
await this.configs[this.path].autoConfig(newOptions),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@@ -51,7 +63,7 @@ export class AutoConfig<Store, Input, NestedConfigs> {
|
|||||||
return deepMerge(
|
return deepMerge(
|
||||||
{},
|
{},
|
||||||
options.localConfig,
|
options.localConfig,
|
||||||
await this.configs[this.path](newOptions),
|
await this.configs[this.path].autoConfig(newOptions),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,24 +1,28 @@
|
|||||||
|
import { Config } from "../config/builder/config"
|
||||||
import { SDKManifest } from "../manifest/ManifestTypes"
|
import { SDKManifest } from "../manifest/ManifestTypes"
|
||||||
import { AutoConfig, AutoConfigFrom } from "./AutoConfig"
|
import { AutoConfig, AutoConfigFrom } from "./AutoConfig"
|
||||||
|
|
||||||
export function setupAutoConfig<
|
export function setupAutoConfig<
|
||||||
Store,
|
Store,
|
||||||
Input,
|
Input extends Record<string, any>,
|
||||||
Manifest extends SDKManifest,
|
Manifest extends SDKManifest,
|
||||||
NestedConfigs extends {
|
NestedConfigs extends {
|
||||||
[key in keyof Manifest["dependencies"]]: unknown
|
[key in keyof Manifest["dependencies"]]: unknown
|
||||||
},
|
},
|
||||||
>(configs: AutoConfigFrom<Store, Input, NestedConfigs>) {
|
>(
|
||||||
type C = typeof configs
|
config: Config<Input, Store>,
|
||||||
const answer = { ...configs } as unknown as {
|
autoConfigs: AutoConfigFrom<Store, Input, NestedConfigs>,
|
||||||
|
) {
|
||||||
|
type C = typeof autoConfigs
|
||||||
|
const answer = { ...autoConfigs } as unknown as {
|
||||||
[k in keyof C]: AutoConfig<Store, Input, NestedConfigs>
|
[k in keyof C]: AutoConfig<Store, Input, NestedConfigs>
|
||||||
}
|
}
|
||||||
for (const key in configs) {
|
for (const key in autoConfigs) {
|
||||||
answer[key as keyof typeof configs] = new AutoConfig<
|
answer[key as keyof typeof autoConfigs] = new AutoConfig<
|
||||||
Store,
|
Store,
|
||||||
Input,
|
Input,
|
||||||
NestedConfigs
|
NestedConfigs
|
||||||
>(configs, key as keyof typeof configs)
|
>(autoConfigs, key as keyof typeof autoConfigs)
|
||||||
}
|
}
|
||||||
return answer
|
return answer
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user