Let AI go to work
This commit is contained in:
@@ -1,20 +1,26 @@
|
||||
import "dotenv/config";
|
||||
import { z } from "zod";
|
||||
|
||||
const configSchema = z.object({
|
||||
engine: z.object({
|
||||
mnemonic: z.string(),
|
||||
mnemonic: z.string().min(1, "ENGINE_MNEMONIC is required"),
|
||||
database: z.object({
|
||||
path: z.string().default("data/engine"),
|
||||
path: z.string().default("./data/xo"),
|
||||
fileName: z.string().default("engine.db"),
|
||||
}),
|
||||
}),
|
||||
syncServer: z.object({
|
||||
url: z.string().default("http://localhost:3000"),
|
||||
url: z.string().default("https://sync.xo.harvmaster.com"),
|
||||
}),
|
||||
// TODO: Remove merchant - eww.
|
||||
merchant: z.object({
|
||||
name: z.string().default("XO Snack Machine"),
|
||||
}),
|
||||
database: z.object({
|
||||
path: z.string().default("data.db"),
|
||||
}),
|
||||
server: z.object({
|
||||
port: z.number().default(3000),
|
||||
port: z.coerce.number().default(3000),
|
||||
host: z.string().default("0.0.0.0"),
|
||||
cors: z
|
||||
.object({
|
||||
@@ -34,28 +40,33 @@ const configSchema = z.object({
|
||||
type ConfigInput = z.input<typeof configSchema>;
|
||||
type ConfigSchema = z.output<typeof configSchema>;
|
||||
|
||||
/**
|
||||
* Converts an object's keys to camelCase.
|
||||
* @param obj - The object to convert to camelCase.
|
||||
* @returns The camelCase object.
|
||||
*/
|
||||
const toCamelCaseObject = (obj: Record<string, string>): Record<string, string> => {
|
||||
return Object.fromEntries(Object.entries(obj).map(([key, value]) => {
|
||||
const camelCaseKey = key.toLowerCase().replace(/([-_][a-z])/g, (group) => group.toUpperCase().replace("-", "").replace("_", ""));
|
||||
return [camelCaseKey, value];
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* The Config class is used to load and parse the configuration for the vending machine.
|
||||
*/
|
||||
export class Config {
|
||||
static fromEnv(): Config {
|
||||
// Parse through process.env, and convert the upperCase keys to camelCase.
|
||||
const envConfig = toCamelCaseObject(Object(process.env));
|
||||
|
||||
// Parse the environment config.
|
||||
return this.from(configSchema.parse(envConfig));
|
||||
return this.from({
|
||||
engine: {
|
||||
mnemonic: process.env.ENGINE_MNEMONIC ?? "",
|
||||
database: {
|
||||
path: process.env.ENGINE_DATABASE_PATH,
|
||||
},
|
||||
},
|
||||
syncServer: {
|
||||
url: process.env.SYNC_SERVER_URL,
|
||||
},
|
||||
// TODO: Remove merchant - eww.
|
||||
merchant: {
|
||||
name: process.env.MERCHANT_NAME,
|
||||
},
|
||||
database: {
|
||||
path: process.env.DATABASE_PATH,
|
||||
},
|
||||
server: {
|
||||
port: process.env.SERVER_PORT,
|
||||
host: process.env.SERVER_HOST,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
static from(config: ConfigInput): Config {
|
||||
@@ -66,6 +77,11 @@ export class Config {
|
||||
return this.config.syncServer;
|
||||
}
|
||||
|
||||
// TODO: Remove merchant - eww.
|
||||
public get merchant() {
|
||||
return this.config.merchant;
|
||||
}
|
||||
|
||||
public get engine() {
|
||||
return this.config.engine;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user