You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* @param options - The options for getting the boolean.
144
-
* @param options.key - The key of the environment variable.
145
-
* @param options.defaultValue - The default value to return if the environment variable is not set.
146
-
* @param options.errorMessage - Optional error message to throw if the environment variable is not set and no default value is provided. Defaults to `"Environment variable <key> is required"`.
147
-
*/
148
-
constgetBooleanFromEnv=({
149
-
key,
150
-
defaultValue,
151
-
errorMessage,
152
-
}: GetBooleanFromEnvOptions): boolean=>{
153
-
constvalue=getStringFromEnv({
154
-
key,
155
-
defaultValue: String(defaultValue),
156
-
errorMessage,
157
-
});
158
-
159
-
constparsedValue=value.toLowerCase();
160
-
161
-
if(parsedValue!=='true'&&parsedValue!=='false'){
162
-
thrownewError(`Environment variable ${key} must be a boolean`);
* By default, the value is parsed as a boolean. You can also provide an option to extend the parsing of the boolean value to include common string representations.
174
147
*
175
148
* @example
176
149
* ```ts
177
-
* import { getTruthyBooleanFromEnv } from '@aws-lambda-powertools/commons/utils/env';
150
+
* import { getBooleanFromEnv } from '@aws-lambda-powertools/commons/utils/env';
178
151
*
179
-
* const myEnvVar = getTruthyBooleanFromEnv({
152
+
* const myEnvVar = getBooleanFromEnv({
180
153
* key: 'MY_ENV_VAR',
181
-
* errorMessage: 'MY_ENV_VAR is required for this function',
154
+
* defaultValue: true,
155
+
* extendedParsing: true,
182
156
* });
183
157
* ```
184
158
*
185
-
* By default, the value is trimmed before being converted to a boolean and always required.
186
-
*
187
-
* You can also provide a default value, which will be returned if the environment variable is not set instead of throwing an error.
188
-
*
189
-
* @example
190
-
* ```ts
191
-
* import { getTruthyBooleanFromEnv } from '@aws-lambda-powertools/commons/utils/env';
159
+
* The following values are considered `true`:
160
+
* - `"true"`
161
+
* - `"1"`
162
+
* - `"yes"`
163
+
* - `"on"`
164
+
* - `"y"`
192
165
*
193
-
* const myEnvVar = getTruthyBooleanFromEnv({
194
-
* key: 'MY_ENV_VAR',
195
-
* defaultValue: true,
196
-
* });
197
-
* ```
166
+
* The following values are considered `false`:
167
+
* - `"false"`
168
+
* - `"0"`
169
+
* - `"no"`
170
+
* - `"off"`
171
+
* - `"n"`
198
172
*
199
-
* @param options - The options for getting the truthy boolean.
173
+
* @param options - The options for getting the boolean.
200
174
* @param options.key - The key of the environment variable.
201
175
* @param options.defaultValue - The default value to return if the environment variable is not set.
202
176
* @param options.errorMessage - Optional error message to throw if the environment variable is not set and no default value is provided. Defaults to `"Environment variable <key> is required"`.
177
+
* @param options.extendedParsing - Whether to extend the parsing of the boolean value to include common string representations like `'1'`, `'y'`, `'yes'`, `'t'`, `'true'`, `'on'` for `true` and `'0'`, `'n'`, `'no'`, `'f'`, `'false'`, `'off'` for `false`.
* import { getFalsyBooleanFromEnv } from '@aws-lambda-powertools/commons/utils/env';
228
-
*
229
-
* const myEnvVar = getFalsyBooleanFromEnv({
230
-
* key: 'MY_ENV_VAR',
231
-
* errorMessage: 'MY_ENV_VAR is required for this function',
232
-
* });
233
-
* ```
234
-
*
235
-
* By default, the value is trimmed before being converted to a boolean and always required.
236
-
*
237
-
* You can also provide a default value, which will be returned if the environment variable is not set instead of throwing an error.
238
-
*
239
-
* @example
240
-
* ```ts
241
-
* import { getFalsyBooleanFromEnv } from '@aws-lambda-powertools/commons/utils/env';
242
-
*
243
-
* const myEnvVar = getFalsyBooleanFromEnv({
244
-
* key: 'MY_ENV_VAR',
245
-
* defaultValue: false,
246
-
* });
247
-
* ```
248
-
*
249
-
* @param options - The options for getting the falsy boolean.
250
-
* @param options.key - The key of the environment variable.
251
-
* @param options.defaultValue - The default value to return if the environment variable is not set.
252
-
* @param options.errorMessage - Optional error message to throw if the environment variable is not set and no default value is provided. Defaults to `"Environment variable <key> is required"`.
253
-
*/
254
-
constgetFalsyBooleanFromEnv=({
255
-
key,
256
-
defaultValue,
257
-
errorMessage,
258
-
}: GetBooleanFromEnvOptions): boolean=>{
259
-
constvalue=getStringFromEnv({
260
-
key,
261
-
defaultValue: String(defaultValue),
262
-
errorMessage,
263
-
});
264
-
returnfalsyValues.has(value.toLowerCase());
202
+
if(parsedValue!=='true'&&parsedValue!=='false'){
203
+
thrownewError(`Environment variable ${key} must be a boolean`);
0 commit comments