mirror of
https://github.com/Start9Labs/start-sdk.git
synced 2026-03-26 02:11:56 +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>) {
|
||||
return {
|
||||
AutoConfig: <Input, NestedConfigs>(
|
||||
AutoConfig: <Input, NestedConfigs extends Record<string, any>>(
|
||||
configs: AutoConfigFrom<Store, Input, NestedConfigs>,
|
||||
path: keyof AutoConfigFrom<Store, Input, NestedConfigs>,
|
||||
) => new AutoConfig<Store, Input, NestedConfigs>(configs, path),
|
||||
@@ -196,13 +196,18 @@ export class StartSdk<Manifest extends SDKManifest, Store> {
|
||||
setupActions: (...createdActions: CreatedAction<any, any>[]) =>
|
||||
setupActions<Store>(...createdActions),
|
||||
setupAutoConfig: <
|
||||
Input,
|
||||
Input extends Record<string, any>,
|
||||
NestedConfigs extends {
|
||||
[key in keyof Manifest["dependencies"]]: unknown
|
||||
},
|
||||
>(
|
||||
configs: AutoConfigFrom<Store, Input, NestedConfigs>,
|
||||
) => setupAutoConfig<Store, Input, Manifest, NestedConfigs>(configs),
|
||||
config: Config<Input, Store>,
|
||||
autoConfigs: AutoConfigFrom<Store, Input, NestedConfigs>,
|
||||
) =>
|
||||
setupAutoConfig<Store, Input, Manifest, NestedConfigs>(
|
||||
config,
|
||||
autoConfigs,
|
||||
),
|
||||
setupBackups: (...args: SetupBackupsParams<Manifest>) =>
|
||||
setupBackups<Manifest>(...args),
|
||||
setupConfig: <
|
||||
|
||||
@@ -2,16 +2,28 @@ import { AutoConfigure, DeepPartial, Effects, ExpectedExports } from "../types"
|
||||
import { Utils, utils } from "../util/utils"
|
||||
import { deepEqual } from "../util/deepEqual"
|
||||
import { deepMerge } from "../util/deepMerge"
|
||||
import { Config } from "../config/builder/config"
|
||||
|
||||
export type AutoConfigFrom<Store, Input, NestedConfigs> = {
|
||||
[key in keyof NestedConfigs & string]: (options: {
|
||||
effects: Effects
|
||||
localConfig: Input
|
||||
remoteConfig: NestedConfigs[key]
|
||||
utils: Utils<Store>
|
||||
}) => Promise<void | DeepPartial<NestedConfigs[key]>>
|
||||
export type AutoConfigFrom<
|
||||
Store,
|
||||
Input,
|
||||
NestedConfigs extends Record<string, any>,
|
||||
> = {
|
||||
[key in keyof NestedConfigs & string]: {
|
||||
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(
|
||||
readonly configs: AutoConfigFrom<Store, Input, NestedConfigs>,
|
||||
readonly path: keyof AutoConfigFrom<Store, Input, NestedConfigs>,
|
||||
@@ -33,7 +45,7 @@ export class AutoConfig<Store, Input, NestedConfigs> {
|
||||
deepMerge(
|
||||
{},
|
||||
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(
|
||||
{},
|
||||
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 { AutoConfig, AutoConfigFrom } from "./AutoConfig"
|
||||
|
||||
export function setupAutoConfig<
|
||||
Store,
|
||||
Input,
|
||||
Input extends Record<string, any>,
|
||||
Manifest extends SDKManifest,
|
||||
NestedConfigs extends {
|
||||
[key in keyof Manifest["dependencies"]]: unknown
|
||||
},
|
||||
>(configs: AutoConfigFrom<Store, Input, NestedConfigs>) {
|
||||
type C = typeof configs
|
||||
const answer = { ...configs } as unknown as {
|
||||
>(
|
||||
config: Config<Input, Store>,
|
||||
autoConfigs: AutoConfigFrom<Store, Input, NestedConfigs>,
|
||||
) {
|
||||
type C = typeof autoConfigs
|
||||
const answer = { ...autoConfigs } as unknown as {
|
||||
[k in keyof C]: AutoConfig<Store, Input, NestedConfigs>
|
||||
}
|
||||
for (const key in configs) {
|
||||
answer[key as keyof typeof configs] = new AutoConfig<
|
||||
for (const key in autoConfigs) {
|
||||
answer[key as keyof typeof autoConfigs] = new AutoConfig<
|
||||
Store,
|
||||
Input,
|
||||
NestedConfigs
|
||||
>(configs, key as keyof typeof configs)
|
||||
>(autoConfigs, key as keyof typeof autoConfigs)
|
||||
}
|
||||
return answer
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user