@rsdk/env 6.0.0-next.0
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/environment.class.d.ts +92 -0
- package/dist/environment.class.js +80 -0
- package/dist/environment.class.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/package.json +17 -0
- package/src/environment.class.ts +156 -0
- package/src/index.ts +1 -0
- package/tsconfig.build.json +12 -0
- package/tsconfig.json +7 -0
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { PropertyParser } from '@rsdk/common';
|
|
2
|
+
export type EnvVariable<T> = {
|
|
3
|
+
/** The parsed value of the environment variable */
|
|
4
|
+
value: T;
|
|
5
|
+
/** The actual environment variable key that was used to retrieve the value */
|
|
6
|
+
key: string;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Class for managing environment variables with optional application name prefix.
|
|
10
|
+
* Provides methods to access both required and optional environment variables,
|
|
11
|
+
* with support for type conversion through parsers.
|
|
12
|
+
*
|
|
13
|
+
* Environment variables can be accessed either with or without an application name prefix.
|
|
14
|
+
* For example, if appName is "myapp" and the variable name is "port":
|
|
15
|
+
* - Will check MYAPP_PORT first
|
|
16
|
+
* - Will fall back to PORT if MYAPP_PORT is not set
|
|
17
|
+
*/
|
|
18
|
+
export declare class Env {
|
|
19
|
+
private readonly appName?;
|
|
20
|
+
constructor(appName?: string | undefined);
|
|
21
|
+
/**
|
|
22
|
+
* Gets an optional environment variable. If the variable doesn't exist,
|
|
23
|
+
* returns undefined instead of throwing an error.
|
|
24
|
+
*
|
|
25
|
+
* @param variableName Name of the environment variable to look up
|
|
26
|
+
* @param parser Optional parser to convert the environment variable value to the desired type
|
|
27
|
+
* @returns EnvVariable instance if the variable exists, undefined otherwise
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* // Get optional string value
|
|
31
|
+
* env.optional('DATABASE_URL')
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* // Get optional number value
|
|
35
|
+
* env.optional('PORT', new NumberParser())
|
|
36
|
+
*/
|
|
37
|
+
optional(variableName: string): EnvVariable<string> | undefined;
|
|
38
|
+
optional<T>(variableName: string, parser: PropertyParser<T>): EnvVariable<T> | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Gets a required environment variable. Throws an error if the variable
|
|
41
|
+
* doesn't exist in any form (neither prefixed nor unprefixed).
|
|
42
|
+
*
|
|
43
|
+
* @param variableName Name of the environment variable to look up
|
|
44
|
+
* @param parser Optional parser to convert the environment variable value to the desired type
|
|
45
|
+
* @returns EnvVariable instance containing the value and the actual key used
|
|
46
|
+
* @throws Error if neither prefixed nor unprefixed variable exists
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* // Get required string value
|
|
50
|
+
* env.required('API_KEY')
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* // Get required boolean value
|
|
54
|
+
* env.required('DEBUG_MODE', new BooleanParser())
|
|
55
|
+
*/
|
|
56
|
+
required(variableName: string): EnvVariable<string>;
|
|
57
|
+
required<T>(variableName: string, parser: PropertyParser<T>): EnvVariable<T>;
|
|
58
|
+
/**
|
|
59
|
+
* Checks if any of the environment variables for the given name are set.
|
|
60
|
+
* Considers both prefixed (with appName) and unprefixed versions.
|
|
61
|
+
*
|
|
62
|
+
* @param variableName Name of the environment variable to check
|
|
63
|
+
* @returns true if any of the environment variables are set, false otherwise
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* if (env.isSet('DATABASE_URL')) {
|
|
67
|
+
* // Handle database connection
|
|
68
|
+
* }
|
|
69
|
+
*/
|
|
70
|
+
isSet(variableName: string): boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Internal method to retrieve environment variable value.
|
|
73
|
+
* Handles both raw string values and parsed values using provided parser.
|
|
74
|
+
*
|
|
75
|
+
* @param keys Array of possible environment variable keys to check
|
|
76
|
+
* @param parser Optional parser to convert the environment variable value
|
|
77
|
+
* @returns EnvVariable instance if found, undefined otherwise
|
|
78
|
+
* @internal
|
|
79
|
+
*/
|
|
80
|
+
private get;
|
|
81
|
+
/**
|
|
82
|
+
* Internal method to generate possible environment variable keys.
|
|
83
|
+
* Creates two versions of the key:
|
|
84
|
+
* 1. With app name prefix (if appName is provided)
|
|
85
|
+
* 2. Without prefix
|
|
86
|
+
*
|
|
87
|
+
* @param variableName Base name of the environment variable
|
|
88
|
+
* @returns Array of possible environment variable keys in UPPER_SNAKE_CASE
|
|
89
|
+
* @internal
|
|
90
|
+
*/
|
|
91
|
+
private keys;
|
|
92
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Env = void 0;
|
|
4
|
+
const common_1 = require("@rsdk/common");
|
|
5
|
+
/**
|
|
6
|
+
* Class for managing environment variables with optional application name prefix.
|
|
7
|
+
* Provides methods to access both required and optional environment variables,
|
|
8
|
+
* with support for type conversion through parsers.
|
|
9
|
+
*
|
|
10
|
+
* Environment variables can be accessed either with or without an application name prefix.
|
|
11
|
+
* For example, if appName is "myapp" and the variable name is "port":
|
|
12
|
+
* - Will check MYAPP_PORT first
|
|
13
|
+
* - Will fall back to PORT if MYAPP_PORT is not set
|
|
14
|
+
*/
|
|
15
|
+
class Env {
|
|
16
|
+
appName;
|
|
17
|
+
constructor(appName) {
|
|
18
|
+
this.appName = appName;
|
|
19
|
+
}
|
|
20
|
+
optional(variableName, parser) {
|
|
21
|
+
return parser
|
|
22
|
+
? this.get(this.keys(variableName), parser)
|
|
23
|
+
: this.get(this.keys(variableName));
|
|
24
|
+
}
|
|
25
|
+
required(variableName, parser) {
|
|
26
|
+
const keys = this.keys(variableName);
|
|
27
|
+
const variable = parser ? this.get(keys, parser) : this.get(keys);
|
|
28
|
+
if (!variable) {
|
|
29
|
+
throw new Error((0, common_1.text) `
|
|
30
|
+
Please provide one of the following environment variables:
|
|
31
|
+
${keys.join(', ')}`);
|
|
32
|
+
}
|
|
33
|
+
return variable;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Checks if any of the environment variables for the given name are set.
|
|
37
|
+
* Considers both prefixed (with appName) and unprefixed versions.
|
|
38
|
+
*
|
|
39
|
+
* @param variableName Name of the environment variable to check
|
|
40
|
+
* @returns true if any of the environment variables are set, false otherwise
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* if (env.isSet('DATABASE_URL')) {
|
|
44
|
+
* // Handle database connection
|
|
45
|
+
* }
|
|
46
|
+
*/
|
|
47
|
+
isSet(variableName) {
|
|
48
|
+
return this.keys(variableName).some((x) => x in process.env);
|
|
49
|
+
}
|
|
50
|
+
get(keys, parser) {
|
|
51
|
+
for (const key of keys) {
|
|
52
|
+
if (key in process.env) {
|
|
53
|
+
const value = parser
|
|
54
|
+
? parser.parse(process.env[key])
|
|
55
|
+
: process.env[key];
|
|
56
|
+
return { key, value };
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Internal method to generate possible environment variable keys.
|
|
63
|
+
* Creates two versions of the key:
|
|
64
|
+
* 1. With app name prefix (if appName is provided)
|
|
65
|
+
* 2. Without prefix
|
|
66
|
+
*
|
|
67
|
+
* @param variableName Base name of the environment variable
|
|
68
|
+
* @returns Array of possible environment variable keys in UPPER_SNAKE_CASE
|
|
69
|
+
* @internal
|
|
70
|
+
*/
|
|
71
|
+
keys(variableName) {
|
|
72
|
+
const keys = [common_1.Case.toUpperSnake(variableName)];
|
|
73
|
+
if (this.appName) {
|
|
74
|
+
keys.unshift(common_1.Case.toUpperSnake(`${this.appName}_${variableName}`));
|
|
75
|
+
}
|
|
76
|
+
return keys;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
exports.Env = Env;
|
|
80
|
+
//# sourceMappingURL=environment.class.js.map
|
|
@@ -0,0 +1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,OAAO,CAAC,GAAG,EAAE,CAAC;gBACvB,MAAM,KAAK,GAAG,MAAM;oBAClB,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAE,CAAC;oBACjC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAE,CAAC;gBAEtB,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC;YACxB,CAAC;QACH,CAAC;QAED,OAAO,SAAS,CAAC;IACnB,CAAC;IAED;;;;;;;;;OASG;IACK,IAAI,CAAC,YAAoB;QAC/B,MAAM,IAAI,GAAG,CAAC,aAAI,CAAC,YAAY,CAAC,YAAY,CAAC,CAAC,CAAC;QAE/C,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YACjB,IAAI,CAAC,OAAO,CAAC,aAAI,CAAC,YAAY,CAAC,GAAG,IAAI,CAAC,OAAO,IAAI,YAAY,EAAE,CAAC,CAAC,CAAC;QACrE,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AAtID,kBAsIC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './environment.class';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./environment.class"), exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,sDAAoC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rsdk/env",
|
|
3
|
+
"version": "6.0.0-next.0",
|
|
4
|
+
"description": "Tools for working with environment variables",
|
|
5
|
+
"license": "Apache License 2.0",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"url": "https://github.com/R-Vision/rsdk"
|
|
11
|
+
},
|
|
12
|
+
"main": "dist/index.js",
|
|
13
|
+
"peerDependencies": {
|
|
14
|
+
"@rsdk/common": "*"
|
|
15
|
+
},
|
|
16
|
+
"gitHead": "215cccea23d95118dd8b6af3ce11c6a35ce19c30"
|
|
17
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import type { PropertyParser } from '@rsdk/common';
|
|
2
|
+
import { Case, text } from '@rsdk/common';
|
|
3
|
+
|
|
4
|
+
export type EnvVariable<T> = {
|
|
5
|
+
/** The parsed value of the environment variable */
|
|
6
|
+
value: T;
|
|
7
|
+
|
|
8
|
+
/** The actual environment variable key that was used to retrieve the value */
|
|
9
|
+
key: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Class for managing environment variables with optional application name prefix.
|
|
14
|
+
* Provides methods to access both required and optional environment variables,
|
|
15
|
+
* with support for type conversion through parsers.
|
|
16
|
+
*
|
|
17
|
+
* Environment variables can be accessed either with or without an application name prefix.
|
|
18
|
+
* For example, if appName is "myapp" and the variable name is "port":
|
|
19
|
+
* - Will check MYAPP_PORT first
|
|
20
|
+
* - Will fall back to PORT if MYAPP_PORT is not set
|
|
21
|
+
*/
|
|
22
|
+
export class Env {
|
|
23
|
+
constructor(private readonly appName?: string) {}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Gets an optional environment variable. If the variable doesn't exist,
|
|
27
|
+
* returns undefined instead of throwing an error.
|
|
28
|
+
*
|
|
29
|
+
* @param variableName Name of the environment variable to look up
|
|
30
|
+
* @param parser Optional parser to convert the environment variable value to the desired type
|
|
31
|
+
* @returns EnvVariable instance if the variable exists, undefined otherwise
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* // Get optional string value
|
|
35
|
+
* env.optional('DATABASE_URL')
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* // Get optional number value
|
|
39
|
+
* env.optional('PORT', new NumberParser())
|
|
40
|
+
*/
|
|
41
|
+
optional(variableName: string): EnvVariable<string> | undefined;
|
|
42
|
+
optional<T>(
|
|
43
|
+
variableName: string,
|
|
44
|
+
parser: PropertyParser<T>,
|
|
45
|
+
): EnvVariable<T> | undefined;
|
|
46
|
+
optional<T>(
|
|
47
|
+
variableName: string,
|
|
48
|
+
parser?: PropertyParser<T>,
|
|
49
|
+
): EnvVariable<string> | EnvVariable<T> | undefined {
|
|
50
|
+
return parser
|
|
51
|
+
? this.get(this.keys(variableName), parser)
|
|
52
|
+
: this.get(this.keys(variableName));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Gets a required environment variable. Throws an error if the variable
|
|
57
|
+
* doesn't exist in any form (neither prefixed nor unprefixed).
|
|
58
|
+
*
|
|
59
|
+
* @param variableName Name of the environment variable to look up
|
|
60
|
+
* @param parser Optional parser to convert the environment variable value to the desired type
|
|
61
|
+
* @returns EnvVariable instance containing the value and the actual key used
|
|
62
|
+
* @throws Error if neither prefixed nor unprefixed variable exists
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* // Get required string value
|
|
66
|
+
* env.required('API_KEY')
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* // Get required boolean value
|
|
70
|
+
* env.required('DEBUG_MODE', new BooleanParser())
|
|
71
|
+
*/
|
|
72
|
+
required(variableName: string): EnvVariable<string>;
|
|
73
|
+
required<T>(variableName: string, parser: PropertyParser<T>): EnvVariable<T>;
|
|
74
|
+
required<T>(
|
|
75
|
+
variableName: string,
|
|
76
|
+
parser?: PropertyParser<T>,
|
|
77
|
+
): EnvVariable<string> | EnvVariable<T> {
|
|
78
|
+
const keys = this.keys(variableName);
|
|
79
|
+
const variable = parser ? this.get(keys, parser) : this.get(keys);
|
|
80
|
+
|
|
81
|
+
if (!variable) {
|
|
82
|
+
throw new Error(text`
|
|
83
|
+
Please provide one of the following environment variables:
|
|
84
|
+
${keys.join(', ')}`);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return variable;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Checks if any of the environment variables for the given name are set.
|
|
92
|
+
* Considers both prefixed (with appName) and unprefixed versions.
|
|
93
|
+
*
|
|
94
|
+
* @param variableName Name of the environment variable to check
|
|
95
|
+
* @returns true if any of the environment variables are set, false otherwise
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* if (env.isSet('DATABASE_URL')) {
|
|
99
|
+
* // Handle database connection
|
|
100
|
+
* }
|
|
101
|
+
*/
|
|
102
|
+
isSet(variableName: string): boolean {
|
|
103
|
+
return this.keys(variableName).some((x) => x in process.env);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Internal method to retrieve environment variable value.
|
|
108
|
+
* Handles both raw string values and parsed values using provided parser.
|
|
109
|
+
*
|
|
110
|
+
* @param keys Array of possible environment variable keys to check
|
|
111
|
+
* @param parser Optional parser to convert the environment variable value
|
|
112
|
+
* @returns EnvVariable instance if found, undefined otherwise
|
|
113
|
+
* @internal
|
|
114
|
+
*/
|
|
115
|
+
private get(keys: string[]): EnvVariable<string> | undefined;
|
|
116
|
+
private get<T>(
|
|
117
|
+
keys: string[],
|
|
118
|
+
parser: PropertyParser<T>,
|
|
119
|
+
): EnvVariable<T> | undefined;
|
|
120
|
+
private get<T>(
|
|
121
|
+
keys: string[],
|
|
122
|
+
parser?: PropertyParser<T>,
|
|
123
|
+
): EnvVariable<string> | EnvVariable<T | string> | undefined {
|
|
124
|
+
for (const key of keys) {
|
|
125
|
+
if (key in process.env) {
|
|
126
|
+
const value = parser
|
|
127
|
+
? parser.parse(process.env[key]!)
|
|
128
|
+
: process.env[key]!;
|
|
129
|
+
|
|
130
|
+
return { key, value };
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return undefined;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Internal method to generate possible environment variable keys.
|
|
139
|
+
* Creates two versions of the key:
|
|
140
|
+
* 1. With app name prefix (if appName is provided)
|
|
141
|
+
* 2. Without prefix
|
|
142
|
+
*
|
|
143
|
+
* @param variableName Base name of the environment variable
|
|
144
|
+
* @returns Array of possible environment variable keys in UPPER_SNAKE_CASE
|
|
145
|
+
* @internal
|
|
146
|
+
*/
|
|
147
|
+
private keys(variableName: string): string[] {
|
|
148
|
+
const keys = [Case.toUpperSnake(variableName)];
|
|
149
|
+
|
|
150
|
+
if (this.appName) {
|
|
151
|
+
keys.unshift(Case.toUpperSnake(`${this.appName}_${variableName}`));
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return keys;
|
|
155
|
+
}
|
|
156
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './environment.class';
|