@discover-cloud/shared 1.2.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/utils/env.utils.d.ts +46 -0
- package/dist/utils/env.utils.js +61 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ENVIRONMENT HELPERS (@discover-cloud/shared)
|
|
3
|
+
* ───────────────────────────────────────────────
|
|
4
|
+
* Typed accessors for process.env values.
|
|
5
|
+
*
|
|
6
|
+
* Why not read process.env directly?
|
|
7
|
+
* - process.env values are always string | undefined. Reading them inline
|
|
8
|
+
* forces every callsite to handle undefined or cast — this pushes that
|
|
9
|
+
* contract to one place.
|
|
10
|
+
* - getEnv() fails fast at startup (before serving any traffic) if a
|
|
11
|
+
* required variable is absent, surfacing misconfiguration immediately
|
|
12
|
+
* rather than at runtime inside a request handler.
|
|
13
|
+
* - Centralised access makes it straightforward to add validation, type
|
|
14
|
+
* coercion, or secret-redaction logic later without touching callsites.
|
|
15
|
+
*
|
|
16
|
+
* Usage:
|
|
17
|
+
* // Required — throws at startup if missing
|
|
18
|
+
* const dbUrl = getEnv("DATABASE_URL");
|
|
19
|
+
* const jwtSecret = getEnv("JWT_SECRET");
|
|
20
|
+
*
|
|
21
|
+
* // Optional — returns undefined (or a typed default) when absent
|
|
22
|
+
* const logLevel = getEnvOptional("LOG_LEVEL") ?? "info";
|
|
23
|
+
* const port = Number(getEnvOptional("PORT") ?? "3000");
|
|
24
|
+
*/
|
|
25
|
+
/**
|
|
26
|
+
* getEnv
|
|
27
|
+
* Returns the value of a required environment variable.
|
|
28
|
+
* Throws at call time (typically during service startup) if the variable
|
|
29
|
+
* is absent or empty — this is intentional: missing required config should
|
|
30
|
+
* crash the process before it begins serving traffic.
|
|
31
|
+
*
|
|
32
|
+
* Empty string ("") is treated as missing because it is almost always an
|
|
33
|
+
* accidental misconfiguration (e.g. `SECRET=` with no value in a .env file).
|
|
34
|
+
*/
|
|
35
|
+
export declare function getEnv(name: string): string;
|
|
36
|
+
/**
|
|
37
|
+
* getEnvOptional
|
|
38
|
+
* Returns the value of an optional environment variable, or undefined
|
|
39
|
+
* if it is absent or empty. Use with a nullish coalescing default:
|
|
40
|
+
*
|
|
41
|
+
* const logLevel = getEnvOptional("LOG_LEVEL") ?? "info";
|
|
42
|
+
*
|
|
43
|
+
* Returns undefined (not empty string) so callers can safely use `??`
|
|
44
|
+
* and `||` without needing to guard against empty strings separately.
|
|
45
|
+
*/
|
|
46
|
+
export declare function getEnvOptional(name: string): string | undefined;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* ENVIRONMENT HELPERS (@discover-cloud/shared)
|
|
4
|
+
* ───────────────────────────────────────────────
|
|
5
|
+
* Typed accessors for process.env values.
|
|
6
|
+
*
|
|
7
|
+
* Why not read process.env directly?
|
|
8
|
+
* - process.env values are always string | undefined. Reading them inline
|
|
9
|
+
* forces every callsite to handle undefined or cast — this pushes that
|
|
10
|
+
* contract to one place.
|
|
11
|
+
* - getEnv() fails fast at startup (before serving any traffic) if a
|
|
12
|
+
* required variable is absent, surfacing misconfiguration immediately
|
|
13
|
+
* rather than at runtime inside a request handler.
|
|
14
|
+
* - Centralised access makes it straightforward to add validation, type
|
|
15
|
+
* coercion, or secret-redaction logic later without touching callsites.
|
|
16
|
+
*
|
|
17
|
+
* Usage:
|
|
18
|
+
* // Required — throws at startup if missing
|
|
19
|
+
* const dbUrl = getEnv("DATABASE_URL");
|
|
20
|
+
* const jwtSecret = getEnv("JWT_SECRET");
|
|
21
|
+
*
|
|
22
|
+
* // Optional — returns undefined (or a typed default) when absent
|
|
23
|
+
* const logLevel = getEnvOptional("LOG_LEVEL") ?? "info";
|
|
24
|
+
* const port = Number(getEnvOptional("PORT") ?? "3000");
|
|
25
|
+
*/
|
|
26
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
27
|
+
exports.getEnv = getEnv;
|
|
28
|
+
exports.getEnvOptional = getEnvOptional;
|
|
29
|
+
/**
|
|
30
|
+
* getEnv
|
|
31
|
+
* Returns the value of a required environment variable.
|
|
32
|
+
* Throws at call time (typically during service startup) if the variable
|
|
33
|
+
* is absent or empty — this is intentional: missing required config should
|
|
34
|
+
* crash the process before it begins serving traffic.
|
|
35
|
+
*
|
|
36
|
+
* Empty string ("") is treated as missing because it is almost always an
|
|
37
|
+
* accidental misconfiguration (e.g. `SECRET=` with no value in a .env file).
|
|
38
|
+
*/
|
|
39
|
+
function getEnv(name) {
|
|
40
|
+
const value = process.env[name];
|
|
41
|
+
if (!value) {
|
|
42
|
+
throw new Error(`Missing required environment variable: ${name}. ` +
|
|
43
|
+
`Ensure it is set in your .env file or deployment environment.`);
|
|
44
|
+
}
|
|
45
|
+
return value;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* getEnvOptional
|
|
49
|
+
* Returns the value of an optional environment variable, or undefined
|
|
50
|
+
* if it is absent or empty. Use with a nullish coalescing default:
|
|
51
|
+
*
|
|
52
|
+
* const logLevel = getEnvOptional("LOG_LEVEL") ?? "info";
|
|
53
|
+
*
|
|
54
|
+
* Returns undefined (not empty string) so callers can safely use `??`
|
|
55
|
+
* and `||` without needing to guard against empty strings separately.
|
|
56
|
+
*/
|
|
57
|
+
function getEnvOptional(name) {
|
|
58
|
+
const value = process.env[name];
|
|
59
|
+
// Normalise empty string to undefined — same convention as getEnv.
|
|
60
|
+
return value === "" ? undefined : value;
|
|
61
|
+
}
|
package/dist/utils/index.d.ts
CHANGED
package/dist/utils/index.js
CHANGED
|
@@ -17,3 +17,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
__exportStar(require("./response.utils"), exports);
|
|
18
18
|
__exportStar(require("./logger.utils"), exports);
|
|
19
19
|
__exportStar(require("./date.utils"), exports);
|
|
20
|
+
__exportStar(require("./env.utils"), exports);
|