-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathenvironment.ts
More file actions
88 lines (74 loc) · 2.59 KB
/
Copy pathenvironment.ts
File metadata and controls
88 lines (74 loc) · 2.59 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { config as dotenv } from 'dotenv';
import zod from 'zod';
import type { PostgresConnectionParamaters } from '@hive/postgres';
dotenv({
debug: true,
encoding: 'utf8',
});
const isNumberString = (input: unknown) => zod.string().regex(/^\d+$/).safeParse(input).success;
const numberFromNumberOrNumberString = (input: unknown): number | undefined => {
if (typeof input == 'number') return input;
if (isNumberString(input)) return Number(input);
};
const emptyString = <T extends zod.ZodType>(input: T) => {
return zod.preprocess((value: unknown) => {
if (value === '') return undefined;
return value;
}, input);
};
const NumberFromString = zod.preprocess(numberFromNumberOrNumberString, zod.number().min(1));
const ClickHouseModel = zod.object({
CLICKHOUSE_PROTOCOL: zod.union([zod.literal('http'), zod.literal('https')]),
CLICKHOUSE_HOST: zod.string(),
CLICKHOUSE_PORT: NumberFromString,
CLICKHOUSE_USERNAME: zod.string(),
CLICKHOUSE_PASSWORD: zod.string(),
});
const PostgresModel = zod.object({
POSTGRES_SSL: emptyString(zod.union([zod.literal('1'), zod.literal('0')]).optional()),
POSTGRES_HOST: zod.string(),
POSTGRES_PORT: NumberFromString,
POSTGRES_DB: zod.string(),
POSTGRES_USER: zod.string(),
POSTGRES_PASSWORD: emptyString(zod.string().optional()),
});
const configs = {
clickhouse: ClickHouseModel.safeParse(process.env),
postgres: PostgresModel.safeParse(process.env),
};
const environmentErrors: Array<string> = [];
for (const config of Object.values(configs)) {
if (config.success === false) {
environmentErrors.push(JSON.stringify(config.error.format(), null, 4));
}
}
if (environmentErrors.length) {
const fullError = environmentErrors.join(`\n`);
console.error('❌ Invalid environment variables:', fullError);
process.exit(1);
}
function extractConfig<Output>(config: zod.ZodSafeParseResult<Output>): Output {
if (!config.success) {
throw new Error('Something went wrong.');
}
return config.data;
}
const clickhouse = extractConfig(configs.clickhouse);
const postgres = extractConfig(configs.postgres);
export const env = {
clickhouse: {
protocol: clickhouse.CLICKHOUSE_PROTOCOL,
host: clickhouse.CLICKHOUSE_HOST,
port: clickhouse.CLICKHOUSE_PORT,
username: clickhouse.CLICKHOUSE_USERNAME,
password: clickhouse.CLICKHOUSE_PASSWORD,
},
postgres: {
host: postgres.POSTGRES_HOST,
port: postgres.POSTGRES_PORT,
db: postgres.POSTGRES_DB,
user: postgres.POSTGRES_USER,
password: postgres.POSTGRES_PASSWORD,
ssl: postgres.POSTGRES_SSL === '1',
} satisfies PostgresConnectionParamaters,
} as const;