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", type extends "up" | "down",
>( >(
fn: ( fn: (
config: ConfigSpec, config: Record<string, unknown>,
effects: T.Effects, effects: T.Effects,
) => ConfigSpec | Promise<ConfigSpec>, ) => ConfigSpec | Promise<ConfigSpec>,
configured: boolean, configured: boolean,

View File

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

View File

@@ -1,5 +1,6 @@
import { matches, TOML, YAML } from "../dependencies.ts"; import { matches, TOML, YAML } from "../dependencies.ts";
import * as T from "../types.ts"; import * as T from "../types.ts";
import { exists } from "../util.ts";
const previousPath = /(.+?)\/([^/]*)$/; const previousPath = /(.+?)\/([^/]*)$/;
@@ -73,6 +74,12 @@ export class ConfigFile<A> {
}); });
} }
async read(effects: T.Effects) { async read(effects: T.Effects) {
if (
!(await exists(effects, {
path: this.options.path,
volumeId: this.options.volume,
}))
) return null;
return this.options.readData( return this.options.readData(
await effects.readFile({ await effects.readFile({
path: this.options.path, 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; return Object.keys(s).length === 0 ? null : s;
} }
export { setupConfigExports } from "./setup_config_export.ts";
export { ConfigFile }; 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. */ /** 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 = ( export type setConfig = (
effects: Effects, effects: Effects,
input: ConfigSpec, input: Record<string, unknown>,
) => Promise<ResultType<SetResult>>; ) => 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 */ /** 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>>; export type getConfig = (effects: Effects) => Promise<ResultType<ConfigRes>>;
@@ -67,7 +67,7 @@ export namespace ExpectedExports {
export type ConfigRes = { export type ConfigRes = {
/** This should be the previous config, that way during set config we start with the previous */ /** 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 */ /** Shape that is describing the form in the ui */
spec: ConfigSpec; spec: ConfigSpec;
}; };

View File

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