mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
refactor(config): 🔥 Replace config validation with Zod
This commit is contained in:
parent
093337dd4f
commit
fb31375b74
15 changed files with 543 additions and 3491 deletions
File diff suppressed because it is too large
Load diff
|
|
@ -5,22 +5,43 @@
|
|||
* Fuses both and provides a way to retrieve individual values
|
||||
*/
|
||||
|
||||
import { watchConfig } from "c12";
|
||||
import { type Config, defaultConfig } from "./config.type";
|
||||
import { watchConfig, loadConfig } from "c12";
|
||||
import { configValidator, type Config } from "./config.type";
|
||||
import { fromError } from "zod-validation-error";
|
||||
import chalk from "chalk";
|
||||
|
||||
const { config } = await watchConfig<Config>({
|
||||
const { config } = await watchConfig({
|
||||
configFile: "./config/config.toml",
|
||||
defaultConfig: defaultConfig,
|
||||
overrides:
|
||||
(
|
||||
await watchConfig<Config>({
|
||||
await loadConfig<Config>({
|
||||
configFile: "./config/config.internal.toml",
|
||||
defaultConfig: {} as Config,
|
||||
})
|
||||
).config ?? undefined,
|
||||
});
|
||||
|
||||
const exportedConfig = config ?? defaultConfig;
|
||||
const parsed = await configValidator.safeParseAsync(config);
|
||||
|
||||
if (!parsed.success) {
|
||||
console.log(
|
||||
`${chalk.bgRed.white(
|
||||
" CRITICAL ",
|
||||
)} There was an error parsing the config file at ${chalk.bold(
|
||||
"./config/config.toml",
|
||||
)}. Please fix the file and try again.`,
|
||||
);
|
||||
console.log(
|
||||
`${chalk.bgRed.white(
|
||||
" CRITICAL ",
|
||||
)} Follow the installation intructions and get a sample config file from the repository if needed.`,
|
||||
);
|
||||
console.log(
|
||||
`${chalk.bgRed.white(" CRITICAL ")} ${fromError(parsed.error).message}`,
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const exportedConfig = parsed.data;
|
||||
|
||||
export { exportedConfig as config };
|
||||
export type { Config };
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@
|
|||
"main": "index.ts",
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"c12": "^1.10.0"
|
||||
"c12": "^1.10.0",
|
||||
"zod": "^3.23.8",
|
||||
"zod-validation-error": "^3.3.0"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue