chore: Update the types

This commit is contained in:
BluJ
2023-02-17 15:21:59 -07:00
parent 8958729921
commit 7530f0b2ac
7 changed files with 59 additions and 11 deletions

View File

@@ -23,7 +23,7 @@ export function updateConfig<
type extends "up" | "down",
>(
fn: (
config: ConfigSpec,
config: Record<string, unknown>,
effects: T.Effects,
) => ConfigSpec | Promise<ConfigSpec>,
configured: boolean,

View File

@@ -1,6 +1,6 @@
import { YAML } from "../dependencies.ts";
import { DependsOn, Effects, ExpectedExports, SetResult } from "../types.ts";
import { ConfigSpec } from "../types/config-types.ts";
import { DependsOn, Effects, ExpectedExports } from "../types.ts";
import { okOf } from "../util.ts";
/**
* Will set the config to the default start9/config.yaml
@@ -12,7 +12,7 @@ import { ConfigSpec } from "../types/config-types.ts";
*/
export const setConfig = async (
effects: Effects,
newConfig: ConfigSpec,
newConfig: Record<string, unknown>,
dependsOn: DependsOn = {},
) => {
await effects.createDir({
@@ -25,11 +25,12 @@ export const setConfig = async (
volumeId: "main",
});
const result: SetResult = {
signal: "SIGTERM",
"depends-on": dependsOn,
};
return { result };
return okOf(
{
signal: "SIGTERM",
"depends-on": dependsOn,
} as const,
);
};
const _typeConversionCheck: ExpectedExports.setConfig = setConfig;

View File

@@ -1,5 +1,6 @@
import { matches, TOML, YAML } from "../dependencies.ts";
import * as T from "../types.ts";
import { exists } from "../util.ts";
const previousPath = /(.+?)\/([^/]*)$/;
@@ -73,6 +74,12 @@ export class ConfigFile<A> {
});
}
async read(effects: T.Effects) {
if (
!(await exists(effects, {
path: this.options.path,
volumeId: this.options.volume,
}))
) return null;
return this.options.readData(
await effects.readFile({
path: this.options.path,

View File

@@ -10,4 +10,5 @@ export function nullIfEmpty(s: Record<string, unknown>) {
return Object.keys(s).length === 0 ? null : s;
}
export { setupConfigExports } from "./setup_config_export.ts";
export { ConfigFile };

View File

@@ -0,0 +1,36 @@
import { Config } from "../config_builder/mod.ts";
import { DependsOn, Effects, ExpectedExports } from "../types.ts";
import { ConfigSpec } from "../types/config-types.ts";
import { okOf } from "../util.ts";
import { TypeFromProps } from "../utils/propertiesMatcher.ts";
import { nullIfEmpty } from "./mod.ts";
export function setupConfigExports<A extends ConfigSpec>(options: {
spec: Config<A>;
dependsOn: DependsOn;
write(effects: Effects, config: TypeFromProps<A>): Promise<null>;
read(
effects: Effects,
): Promise<Record<string | number, never> | TypeFromProps<A>>;
}) {
const validator = options.spec.validator();
return {
setConfig: (async (effects: Effects, config: unknown) => {
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 okOf({
signal: "SIGTERM",
"depends-on": options.dependsOn,
});
}) as ExpectedExports.setConfig,
getConfig: (async (effects: Effects) => {
return okOf({
spec: options.spec.build(),
config: nullIfEmpty(await options.read(effects)),
});
}) as ExpectedExports.getConfig,
};
}

View File

@@ -8,7 +8,7 @@ export namespace ExpectedExports {
/** Set configuration is called after we have modified and saved the configuration in the embassy ui. Use this to make a file for the docker to read from for configuration. */
export type setConfig = (
effects: Effects,
input: ConfigSpec,
input: Record<string, unknown>,
) => Promise<ResultType<SetResult>>;
/** Get configuration returns a shape that describes the format that the embassy ui will generate, and later send to the set config */
export type getConfig = (effects: Effects) => Promise<ResultType<ConfigRes>>;
@@ -67,7 +67,7 @@ export namespace ExpectedExports {
export type ConfigRes = {
/** This should be the previous config, that way during set config we start with the previous */
config?: ConfigSpec;
config?: null | Record<string, unknown>;
/** Shape that is describing the form in the ui */
spec: ConfigSpec;
};

View File

@@ -26,6 +26,9 @@ export const errorCode = (code: number, error: string) => ({
"error-code": [code, error] as const,
});
export const error = (error: string) => ({ error });
export const okOf = <A>(result: A) => ({
result,
});
export const ok = { result: null };
export const isKnownError = (e: unknown): e is T.KnownError =>