Files
start-sdk/lib/config/setupConfigExports.ts
2023-04-18 15:48:55 -06:00

55 lines
2.0 KiB
TypeScript

import { Config } from "./builder";
import { DeepPartial, Dependencies, DependsOn, Effects, ExpectedExports } from "../types";
import { InputSpec } from "./configTypes";
import { nullIfEmpty } from "../util";
import { TypeFromProps } from "../util/propertiesMatcher";
export type Write<A, ConfigType> = (options: { effects: Effects; input: TypeFromProps<A> }) => Promise<ConfigType>;
export type Read<A, ConfigType> = (options: {
effects: Effects;
config: ConfigType;
}) => Promise<null | DeepPartial<TypeFromProps<A>>>;
export type DependenciesFn<A, ConfigType> = (options: {
effects: Effects;
input: TypeFromProps<A>;
config: ConfigType;
}) => Promise<Dependencies | void>;
/**
* 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, ConfigType>(options: {
spec: Config<A>;
write: Write<A, ConfigType>;
read: Read<A, ConfigType>;
dependencies: DependenciesFn<A, ConfigType>;
}) {
const validator = options.spec.validator();
return {
setConfig: (async ({ effects, input }) => {
if (!validator.test(input)) {
await effects.error(String(validator.errorMessage(input)));
return { error: "Set config type error for config" };
}
const config = await options.write({
input: JSON.parse(JSON.stringify(input)),
effects,
});
const dependencies = (await options.dependencies({ effects, input, config })) || [];
await effects.setDependencies(dependencies);
await effects.setWrapperData({ path: "config", value: config || null });
}) as ExpectedExports.setConfig,
getConfig: (async ({ effects, config }) => {
return {
spec: options.spec.build(),
config: nullIfEmpty(await options.read({ effects, config: config as ConfigType })),
};
}) as ExpectedExports.getConfig,
};
}
export default setupConfigExports;