|
1 | 1 | import { z } from 'zod'; |
2 | 2 | import { getEnv } from './env'; |
3 | 3 |
|
4 | | -const pgToken = z.string().regex(/^pg_[a-f0-9]{64}$/, 'POSTGATE_TOKEN must be a valid pg_xxx token'); |
| 4 | +const Schema = z.object({ |
| 5 | + url: z.url().default('http://localhost:6080'), |
| 6 | + token: z |
| 7 | + .string() |
| 8 | + .regex(/^pg_[a-f0-9]{64}$/, 'POSTGATE_TOKEN must be a valid pg_xxx token') |
| 9 | + .optional(), |
| 10 | + systemTokenSecret: z.string().min(32, 'POSTGATE_SYSTEM_TOKEN_SECRET must be at least 32 characters') |
| 11 | +}); |
5 | 12 |
|
6 | | -function buildPostgateSchema() { |
7 | | - const hasDatabaseBinding = !!(globalThis as any).env?.DATABASE?.query; |
8 | | - |
9 | | - return z.object({ |
10 | | - url: z.url().default('http://localhost:6080'), |
11 | | - token: hasDatabaseBinding ? pgToken.optional() : pgToken, |
12 | | - systemTokenSecret: z.string().min(32, 'POSTGATE_SYSTEM_TOKEN_SECRET must be at least 32 characters') |
13 | | - }); |
14 | | -} |
15 | | - |
16 | | -export type PostgateConfig = z.infer<ReturnType<typeof buildPostgateSchema>>; |
| 13 | +export type PostgateConfig = z.infer<typeof Schema>; |
17 | 14 |
|
18 | 15 | let cached: PostgateConfig | null = null; |
19 | 16 |
|
20 | 17 | export function getPostgateConfig(): PostgateConfig { |
21 | 18 | if (cached) return cached; |
22 | 19 |
|
23 | | - cached = buildPostgateSchema().parse({ |
| 20 | + cached = Schema.parse({ |
24 | 21 | url: getEnv('POSTGATE_URL'), |
25 | 22 | token: getEnv('POSTGATE_TOKEN'), |
26 | 23 | systemTokenSecret: getEnv('POSTGATE_SYSTEM_TOKEN_SECRET') |
27 | 24 | }); |
| 25 | + |
28 | 26 | return cached; |
29 | 27 | } |
0 commit comments