forked from obytes/react-native-template-obytes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.js
92 lines (76 loc) · 2.66 KB
/
env.js
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
const z = require('zod');
const packageJSON = require('./package.json');
/**
* First part: Define your env variables
* Static variable for your app such as: bundle id, package name, app name, etc.
* you can add them to the .env file but we think it's better to keep them here.
*
* We declare a function withEnvSuffix that will add a suffix to the variable name based on the APP_ENV
*
*/
const BUNDLE_ID = 'com.obytes'; // ios bundle id
const PACKAGE = 'com.obytes'; // android package name
const NAME = 'ObytesApp'; // app name
const APP_ENV = process.env.APP_ENV ?? 'development';
/**
* Add a suffix to variable env based on APP_ENV
* @param {string} name
* @returns {string}
*/
const withEnvSuffix = (name) => {
return APP_ENV === 'production' ? name : `${name}.${APP_ENV}`;
};
/**
* 2nd part: Define your env variables schema
* when ever you want to add a new variable you should add it in the schema and the _env object.
* Note : z.string() means that the variable is only exists and can be an empty string but not undefined.
* if you want to make the variable required you should use z.string().min(1) instead.
* Read more about zod here: https://zod.dev/?id=strings
*
*/
const envVars = z.object({
APP_ENV: z.enum(['development', 'staging', 'production']),
NAME: z.string(),
BUNDLE_ID: z.string(),
PACKAGE: z.string(),
VERSION: z.string(),
// ADD YOUR ENV VARS HERE
API_URL: z.string(),
});
/**
* @type {Record<keyof z.infer<typeof envVars> , string | undefined>}
*/
const _env = {
APP_ENV,
NAME: NAME,
BUNDLE_ID: withEnvSuffix(BUNDLE_ID),
PACKAGE: withEnvSuffix(PACKAGE),
VERSION: packageJSON.version,
// ADD YOUR ENV VARS HERE TOO
API_URL: process.env.API_URL,
};
/**
* 3rd part: Validate your env variables
* we use zod to validate our env variables
* if the validation fails we throw an error and log the error to the console
* if the validation passes we export the env variables
* you can access the env variables by importing the Env object from this file
* example: import { Env } from '@env'
**/
const parsed = envVars.safeParse(_env);
if (parsed.success === false) {
console.error(
'❌ Invalid environment variables:',
parsed.error.flatten().fieldErrors,
`\n❌ Missing variables in .env.${APP_ENV} file, Make sure all required variables are defined in the .env.${APP_ENV} file.`,
`\n💡 Tip: If you recently updated the .env.${APP_ENV} file and the error still persists, try restarting the server with the -cc flag to clear the cache.`
);
throw new Error(
'Invalid environment variables, Check terminal for more details '
);
}
const Env = parsed.data;
module.exports = {
Env,
withEnvSuffix,
};