forked from veridatum-labs/earnproof-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.ts
More file actions
87 lines (77 loc) · 3.64 KB
/
Copy pathenv.ts
File metadata and controls
87 lines (77 loc) · 3.64 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
import { z } from "zod";
export const DEPLOYMENT_PROFILES = ["local", "preview", "production"] as const;
export type DeploymentProfile = (typeof DEPLOYMENT_PROFILES)[number];
export const publicEnvSchema = z.object({
NEXT_PUBLIC_APP_URL: z.string().url(),
NEXT_PUBLIC_API_URL: z.string().url(),
NEXT_PUBLIC_STELLAR_NETWORK: z.literal("testnet"),
NEXT_PUBLIC_STELLAR_NETWORK_PASSPHRASE: z.string().min(1),
NEXT_PUBLIC_STELLAR_HORIZON_URL: z.string().url(),
NEXT_PUBLIC_HELP_URL: z.string().url().optional(),
NEXT_PUBLIC_STELLAR_EXPLORER_URL: z.string().url().optional(),
NEXT_PUBLIC_WEB_VITALS_ENDPOINT: z.string().url().optional(),
});
export type PublicEnv = z.infer<typeof publicEnvSchema>;
const LOCAL_DEFAULTS = {
NEXT_PUBLIC_APP_URL: "http://localhost:3000",
NEXT_PUBLIC_API_URL: "http://localhost:4000/api/v1",
NEXT_PUBLIC_STELLAR_NETWORK: "testnet",
NEXT_PUBLIC_STELLAR_NETWORK_PASSPHRASE: "Test SDF Network ; September 2015",
NEXT_PUBLIC_STELLAR_HORIZON_URL: "https://horizon-testnet.stellar.org",
NEXT_PUBLIC_HELP_URL: "https://help.earnproof.com",
} as const;
export type EnvLike = Record<string, string | undefined>;
export function resolveDeploymentProfile(env: EnvLike = process.env): DeploymentProfile {
const vercelEnv = env.VERCEL_ENV;
if (vercelEnv === "production") return "production";
if (vercelEnv === "preview") return "preview";
if (env.NODE_ENV === "production" && env.EARNPROOF_REQUIRE_SECURITY_ORIGINS === "true") {
return "production";
}
return "local";
}
export function originsAreRequired(env: EnvLike = process.env): boolean {
const profile = resolveDeploymentProfile(env);
return (
profile === "preview" ||
profile === "production" ||
env.EARNPROOF_REQUIRE_SECURITY_ORIGINS === "true"
);
}
/**
* Load and validate public environment. Preview/production (or an explicit
* `EARNPROOF_REQUIRE_SECURITY_ORIGINS=true` flag) fail closed when required
* origins are missing. Local and CI `next build` keep documented defaults so
* the app can compile without a Vercel/runtime env file.
*/
export function loadPublicEnv(env: EnvLike = process.env): PublicEnv {
const required = originsAreRequired(env);
const candidate = {
NEXT_PUBLIC_APP_URL: env.NEXT_PUBLIC_APP_URL ?? (required ? undefined : LOCAL_DEFAULTS.NEXT_PUBLIC_APP_URL),
NEXT_PUBLIC_API_URL: env.NEXT_PUBLIC_API_URL ?? (required ? undefined : LOCAL_DEFAULTS.NEXT_PUBLIC_API_URL),
NEXT_PUBLIC_STELLAR_NETWORK:
env.NEXT_PUBLIC_STELLAR_NETWORK ?? (required ? undefined : LOCAL_DEFAULTS.NEXT_PUBLIC_STELLAR_NETWORK),
NEXT_PUBLIC_STELLAR_NETWORK_PASSPHRASE:
env.NEXT_PUBLIC_STELLAR_NETWORK_PASSPHRASE ??
(required ? undefined : LOCAL_DEFAULTS.NEXT_PUBLIC_STELLAR_NETWORK_PASSPHRASE),
NEXT_PUBLIC_STELLAR_HORIZON_URL:
env.NEXT_PUBLIC_STELLAR_HORIZON_URL ??
(required ? undefined : LOCAL_DEFAULTS.NEXT_PUBLIC_STELLAR_HORIZON_URL),
NEXT_PUBLIC_HELP_URL: env.NEXT_PUBLIC_HELP_URL ?? (required ? undefined : LOCAL_DEFAULTS.NEXT_PUBLIC_HELP_URL),
NEXT_PUBLIC_STELLAR_EXPLORER_URL: env.NEXT_PUBLIC_STELLAR_EXPLORER_URL,
NEXT_PUBLIC_WEB_VITALS_ENDPOINT: env.NEXT_PUBLIC_WEB_VITALS_ENDPOINT,
};
const parsed = publicEnvSchema.safeParse(candidate);
if (!parsed.success) {
const missing = parsed.error.issues
.map((issue) => issue.path.join("."))
.filter(Boolean);
const profile = resolveDeploymentProfile(env);
throw new Error(
`EarnProof public origins are missing or invalid for the ${profile} policy. ` +
`Set ${missing.join(", ") || "required NEXT_PUBLIC_* origin variables"} ` +
`before serving wallet, API, or QR traffic.`,
);
}
return parsed.data;
}