-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.ts
63 lines (54 loc) · 1.73 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { construct } from "radash";
import {
any,
boolean,
coerce,
date,
defaulted,
enums,
mask,
object,
optional,
string,
type Infer,
type Struct,
} from "superstruct";
import { ThemeSchema } from "./theme.ts";
import { generateId } from "./utils.ts";
export type PartialConfig<T> = {
[P in keyof T]?: PartialConfig<T[P]>;
};
export const BaseConfigSchema = defaulted(
object({
id: defaulted(string(), generateId), // TODO: i need to include the id so the database will persist across page reloads, im not sure why its coming through as undefined, run the dev command with --verbose
createdAt: coerce(date(), any(), (d) => (d ? new Date(d) : new Date())),
reset: optional(boolean()),
params: defaulted(enums(["show", "hide"]), "hide"),
theme: ThemeSchema,
}),
{ params: "hide", theme: {} }
);
export type BaseConfig = Infer<typeof BaseConfigSchema>;
export const SafeConfigSchema = coerce(BaseConfigSchema, object(), (c) => {
const config = c as BaseConfig;
return {
...config,
theme: {
...config?.theme,
light: {
fg: config?.theme?.fg ?? config?.theme?.light?.fg ?? undefined,
bg: config?.theme?.bg ?? config?.theme?.light?.bg ?? undefined,
},
dark: {
fg: config?.theme?.fg ?? config?.theme?.dark?.fg ?? undefined,
bg: config?.theme?.bg ?? config?.theme?.dark?.bg ?? undefined,
},
},
};
});
export type SafeConfig = Infer<typeof BaseConfigSchema>;
export const createConfig = <C extends BaseConfig>(
// TODO: not sure if i should add record ie: config: PartialConfig<C> & Record<string, string>,
config: PartialConfig<C>,
schema: Struct<C>
) => mask({ ...config, ...mask(construct(config), SafeConfigSchema) }, schema);