-
Notifications
You must be signed in to change notification settings - Fork 67
/
config.ts
93 lines (72 loc) · 2.08 KB
/
config.ts
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
89
90
91
92
93
function isProduction() {
if (process.env.VERCEL && process.env.CI) {
return true;
}
return process.env.NODE_ENV === "production";
}
function resolveEnvReference(name: string) {
const env = process.env[name];
if (env?.startsWith("@")) {
const referencedVar = process.env[env.slice(1)];
return referencedVar;
}
return env;
}
export function withoutTrailingSlash(url: string): string {
if (url.endsWith("/")) {
return url.slice(0, url.length - 1);
}
return url;
}
export function prefixWithProtocol(string: string): string {
if (string.startsWith("http://") || string.startsWith("https://")) {
return string;
}
if (isProduction()) {
return "https://" + string;
} else {
return "http://" + string;
}
}
export function getQuirrelBaseUrl(): string | undefined {
const fromEnvironment =
process.env.QUIRREL_API_URL ?? process.env.QUIRREL_URL;
if (fromEnvironment) {
return prefixWithProtocol(withoutTrailingSlash(fromEnvironment));
}
return isProduction() ? "https://api.quirrel.dev" : "http://localhost:9181";
}
export function getQuirrelToken(): string | undefined {
return process.env.QUIRREL_TOKEN;
}
export function getEncryptionSecret(): string | undefined {
return process.env.QUIRREL_ENCRYPTION_SECRET;
}
export function getSignaturePublicKey(): string | undefined {
return process.env.QUIRREL_SIGNATURE_PUBLIC_KEY;
}
export function getOldEncryptionSecrets(): string[] | null {
return JSON.parse(process.env.QUIRREL_OLD_SECRETS ?? "null");
}
let developmentApplicationBaseUrl: string | undefined;
export function getApplicationBaseUrl(): string {
const baseUrl =
resolveEnvReference("QUIRREL_BASE_URL") || developmentApplicationBaseUrl;
if (!baseUrl) {
throw new Error("Please specify QUIRREL_BASE_URL.");
}
return prefixWithProtocol(baseUrl);
}
export function registerDevelopmentDefaults({
applicationBaseUrl,
}: {
applicationBaseUrl: string;
}) {
if (isProduction()) {
return;
}
if (developmentApplicationBaseUrl) {
return;
}
developmentApplicationBaseUrl = applicationBaseUrl;
}