mirror of
https://github.com/Start9Labs/start-sdk.git
synced 2026-03-30 12:21:57 +00:00
chore: Fix the tests and add new types for raw
This commit is contained in:
42
lib/config/setupConfigExports.ts
Normal file
42
lib/config/setupConfigExports.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { Config } from "./builder";
|
||||
import { DeepPartial, DependsOn, Effects, ExpectedExports } from "../types";
|
||||
import { InputSpec } from "./configTypes";
|
||||
import { nullIfEmpty } from "../util";
|
||||
import { TypeFromProps } from "../util/propertiesMatcher";
|
||||
|
||||
/**
|
||||
* We want to setup a config export with a get and set, this
|
||||
* is going to be the default helper to setup config, because it will help
|
||||
* enforce that we have a spec, write, and reading.
|
||||
* @param options
|
||||
* @returns
|
||||
*/
|
||||
export function setupConfigExports<A extends InputSpec>(options: {
|
||||
spec: Config<A>;
|
||||
dependsOn: DependsOn;
|
||||
write(effects: Effects, config: TypeFromProps<A>): Promise<void>;
|
||||
read(effects: Effects): Promise<null | DeepPartial<TypeFromProps<A>>>;
|
||||
}) {
|
||||
const validator = options.spec.validator();
|
||||
return {
|
||||
setConfig: (async ({ effects, input: config }) => {
|
||||
if (!validator.test(config)) {
|
||||
await effects.error(String(validator.errorMessage(config)));
|
||||
return { error: "Set config type error for config" };
|
||||
}
|
||||
await options.write(effects, config);
|
||||
return {
|
||||
signal: "SIGTERM",
|
||||
"depends-on": options.dependsOn,
|
||||
};
|
||||
}) as ExpectedExports.setConfig,
|
||||
getConfig: (async ({ effects }) => {
|
||||
return {
|
||||
spec: options.spec.build(),
|
||||
config: nullIfEmpty(await options.read(effects)),
|
||||
};
|
||||
}) as ExpectedExports.getConfig,
|
||||
};
|
||||
}
|
||||
|
||||
export default setupConfigExports;
|
||||
Reference in New Issue
Block a user