Skip to content

Commit 4a2a7a7

Browse files
committed
Simplify postgate config to use static schema like other configs
1 parent d34e48f commit 4a2a7a7

1 file changed

Lines changed: 11 additions & 13 deletions

File tree

src/lib/config/postgate.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
11
import { z } from 'zod';
22
import { getEnv } from './env';
33

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+
});
512

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>;
1714

1815
let cached: PostgateConfig | null = null;
1916

2017
export function getPostgateConfig(): PostgateConfig {
2118
if (cached) return cached;
2219

23-
cached = buildPostgateSchema().parse({
20+
cached = Schema.parse({
2421
url: getEnv('POSTGATE_URL'),
2522
token: getEnv('POSTGATE_TOKEN'),
2623
systemTokenSecret: getEnv('POSTGATE_SYSTEM_TOKEN_SECRET')
2724
});
25+
2826
return cached;
2927
}

0 commit comments

Comments
 (0)