Skip to content

Commit f4060d5

Browse files
committed
Create config.ts
Signed-off-by: rockito10 <[email protected]>
1 parent 81eccb7 commit f4060d5

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

config.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import * as dotenv from 'dotenv';
2+
import validator from 'validator';
3+
4+
dotenv.config();
5+
6+
const {
7+
MODE,
8+
SUPABASE_URL,
9+
SUPABASE_KEY,
10+
SUPABASE_JWT_SECRET,
11+
API_GATEWAY_PROTOCOL,
12+
API_GATEWAY_HOST,
13+
API_GATEWAY_PORT,
14+
API_GATEWAY_PROTOCOL_SECURE
15+
} = process.env;
16+
17+
// console.log({ SUPABASE_KEY, SUPABASE_JWT_SECRET });
18+
19+
// VALIDATION START ---------------------------------------------
20+
21+
export function isValidProtocol(protocol: string): boolean {
22+
return ['http', 'https'].includes(protocol);
23+
}
24+
25+
export function isValidHost(host: string): boolean {
26+
const ipv4Regex = /^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$/g;
27+
28+
return ipv4Regex.test(host) || 'localhost' === host;
29+
}
30+
31+
// VALIDATION END---------------------------------------------
32+
33+
if ('PROD' !== MODE && 'DEV' !== MODE) {
34+
throw new Error('env.MODE must be "PROD" or "DEV".');
35+
}
36+
37+
export { MODE };
38+
39+
if (
40+
!validator.isURL(SUPABASE_URL, {
41+
protocols: ['http', 'https']
42+
})
43+
) {
44+
throw new Error('env SUPABASE_URL is not an URL');
45+
}
46+
47+
if (!SUPABASE_KEY) {
48+
throw new Error('env SUPABASE_KEY value not set".');
49+
}
50+
51+
if (!SUPABASE_JWT_SECRET) {
52+
throw new Error('env SUPABASE_KEY value not set".');
53+
}
54+
55+
export const SUPABASE = {
56+
URL: SUPABASE_URL,
57+
KEY: SUPABASE_KEY,
58+
JWT_SECRET: SUPABASE_JWT_SECRET
59+
};
60+
61+
if (!isValidProtocol(API_GATEWAY_PROTOCOL)) {
62+
throw new Error('env API_GATEWAY_PROTOCOL is not a valid protocol.');
63+
}
64+
65+
if (!isValidHost(API_GATEWAY_HOST)) {
66+
throw new Error('env API_GATEWAY_HOST is not a valid host.');
67+
}
68+
69+
if (!validator.isNumeric(API_GATEWAY_PORT)) {
70+
throw new Error('env API_GATEWAY_PORT is not a valid port.');
71+
}
72+
73+
if (!isValidProtocol(API_GATEWAY_PROTOCOL_SECURE)) {
74+
throw new Error('env API_GATEWAY_PROTOCOL_SECURE is not a valid protocol.');
75+
}
76+
77+
export const API_GATEWAY = {
78+
PROTOCOL: API_GATEWAY_PROTOCOL,
79+
HOST: API_GATEWAY_HOST,
80+
PORT: API_GATEWAY_PORT,
81+
PROTOCOL_SECURE: API_GATEWAY_PROTOCOL_SECURE
82+
};

0 commit comments

Comments
 (0)