-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.ts
More file actions
39 lines (34 loc) · 1.59 KB
/
Copy pathconfig.ts
File metadata and controls
39 lines (34 loc) · 1.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
/**
* Reads the `PUBLIC_*` env vars Vite bundles into client-side JS (ADR-0030) and fails loudly,
* not silently, if any are missing — a misconfigured deploy should show an error banner, never
* a sign-in button that quietly points at `undefined`.
*/
import type { AuthConfig } from './auth';
/** Only the fields this module reads — deliberately not the full Astro/Vite `ImportMetaEnv`
* (which also carries `BASE_URL`/`MODE`/`DEV`/`PROD`/etc.), so a test can pass a plain object
* with just these five keys instead of satisfying Astro's whole env shape. */
export interface EnvSource {
readonly PUBLIC_COGNITO_DOMAIN?: string;
readonly PUBLIC_COGNITO_CLIENT_ID?: string;
readonly PUBLIC_REDIRECT_URI?: string;
readonly PUBLIC_LOGOUT_URI?: string;
readonly PUBLIC_API_BASE_URL?: string;
}
export class ConfigError extends Error {}
function required(value: string | undefined, name: string): string {
if (!value || !value.trim()) {
throw new ConfigError(`missing required env var ${name} — check web/.env.example`);
}
return value;
}
export function loadAuthConfig(env: EnvSource = import.meta.env): AuthConfig {
return {
domain: required(env.PUBLIC_COGNITO_DOMAIN, 'PUBLIC_COGNITO_DOMAIN'),
clientId: required(env.PUBLIC_COGNITO_CLIENT_ID, 'PUBLIC_COGNITO_CLIENT_ID'),
redirectUri: required(env.PUBLIC_REDIRECT_URI, 'PUBLIC_REDIRECT_URI'),
logoutUri: required(env.PUBLIC_LOGOUT_URI, 'PUBLIC_LOGOUT_URI'),
};
}
export function loadApiBaseUrl(env: EnvSource = import.meta.env): string {
return required(env.PUBLIC_API_BASE_URL, 'PUBLIC_API_BASE_URL').replace(/\/+$/, '');
}