@cripty2001/utils 0.0.231 → 0.0.232

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/index.d.ts CHANGED
@@ -85,6 +85,9 @@ export declare function parseQuery(query: string | Record<string, any> | URLSear
85
85
  * @throws If `max - min + 1` is greater than 2^32
86
86
  */
87
87
  export declare function randBetween(min: number, max: number): number;
88
+ export declare class GetenvHandledError extends Error {
89
+ constructor(message: string);
90
+ }
88
91
  /**
89
92
  * Get an environment variable, convert it using the given converter function.
90
93
  * If the environment variable is not defined, the default value is used.
@@ -92,7 +95,7 @@ export declare function randBetween(min: number, max: number): number;
92
95
  *
93
96
  * @param key The environment variable key
94
97
  * @param defaultValue The default value to use if the environment variable is not defined
95
- * @param converter The function to use to convert the environment variable value to the desired type
98
+ * @param converter The function to use to convert the environment variable value to the desired type. If the converter throws a GetenvHandledError, that message is surfaced to the user. Otherwise, a generic error message is thrown.
96
99
  *
97
100
  * @returns The converted environment variable value, or the default value if provided
98
101
  *
@@ -100,11 +103,23 @@ export declare function randBetween(min: number, max: number): number;
100
103
  * @throws If the conversion throws an error
101
104
  */
102
105
  export declare function getEnv<R>(key: string, defaultValue?: R, converter?: (string: string) => R): R;
106
+ /**
107
+ * @see getEnv, returning a string.
108
+ *
109
+ * @param allowEmpty Whether to allow empty strings
110
+ */
111
+ export declare function getEnvString(key: string, allowEmpty?: boolean, defaultValue?: string): string;
103
112
  /**
104
113
  * @see getEnv, with the additional constraint that the environment variable must be a valid integer.
114
+ *
115
+ * @param constraints The constraints (min and max) to apply to the environment variable value
116
+ *
105
117
  * Note: the default value is not used instead of an invalid integer. An invalid integer therefore throws an error.
106
118
  */
107
- export declare function getEnvInt(key: string, defaultValue?: number): number;
119
+ export declare function getEnvInt(key: string, constraints?: {
120
+ min?: number;
121
+ max?: number;
122
+ }, defaultValue?: number): number;
108
123
  /**
109
124
  * @see getEnv, with the additional constraint that the environment variable must be a valid JSON object.
110
125
  * Note: the default value is not used instead of an invalid JSON object. An invalid JSON object therefore throws an error.
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CURRENT_TS_MS = void 0;
3
+ exports.CURRENT_TS_MS = exports.GetenvHandledError = void 0;
4
4
  exports.randomBelow = randomBelow;
5
5
  exports.getRandom = getRandom;
6
6
  exports.getRandomId = getRandomId;
@@ -16,6 +16,7 @@ exports.stableLog = stableLog;
16
16
  exports.parseQuery = parseQuery;
17
17
  exports.randBetween = randBetween;
18
18
  exports.getEnv = getEnv;
19
+ exports.getEnvString = getEnvString;
19
20
  exports.getEnvInt = getEnvInt;
20
21
  exports.getEnvJson = getEnvJson;
21
22
  exports.getClock = getClock;
@@ -224,6 +225,12 @@ function randBetween(min, max) {
224
225
  return min;
225
226
  return min + randomBelow(span);
226
227
  }
228
+ class GetenvHandledError extends Error {
229
+ constructor(message) {
230
+ super(message);
231
+ }
232
+ }
233
+ exports.GetenvHandledError = GetenvHandledError;
227
234
  /**
228
235
  * Get an environment variable, convert it using the given converter function.
229
236
  * If the environment variable is not defined, the default value is used.
@@ -231,7 +238,7 @@ function randBetween(min, max) {
231
238
  *
232
239
  * @param key The environment variable key
233
240
  * @param defaultValue The default value to use if the environment variable is not defined
234
- * @param converter The function to use to convert the environment variable value to the desired type
241
+ * @param converter The function to use to convert the environment variable value to the desired type. If the converter throws a GetenvHandledError, that message is surfaced to the user. Otherwise, a generic error message is thrown.
235
242
  *
236
243
  * @returns The converted environment variable value, or the default value if provided
237
244
  *
@@ -251,17 +258,44 @@ function getEnv(key, defaultValue, converter = (s) => s) {
251
258
  return converter(value);
252
259
  }
253
260
  catch (e) {
254
- throw new Error(`Environment variable ${key} cannot be converted to the desired type.`);
261
+ const message = e instanceof GetenvHandledError ? `(${e.message})` : "";
262
+ throw new Error(`Environment variable ${key} cannot be converted to the desired type.${message}`);
255
263
  }
256
264
  })();
257
265
  return converted;
258
266
  }
267
+ /**
268
+ * @see getEnv, returning a string.
269
+ *
270
+ * @param allowEmpty Whether to allow empty strings
271
+ */
272
+ function getEnvString(key, allowEmpty = false, defaultValue) {
273
+ return getEnv(key, defaultValue, (s) => {
274
+ if (s === "" && !allowEmpty)
275
+ throw new GetenvHandledError(`Empty string.`);
276
+ return s;
277
+ });
278
+ }
259
279
  /**
260
280
  * @see getEnv, with the additional constraint that the environment variable must be a valid integer.
281
+ *
282
+ * @param constraints The constraints (min and max) to apply to the environment variable value
283
+ *
261
284
  * Note: the default value is not used instead of an invalid integer. An invalid integer therefore throws an error.
262
285
  */
263
- function getEnvInt(key, defaultValue) {
264
- return getEnv(key, defaultValue, (s) => parseInt(s));
286
+ function getEnvInt(key, constraints = {}, defaultValue) {
287
+ return getEnv(key, defaultValue, (s) => {
288
+ const parsed = parseInt(s);
289
+ if (isNaN(parsed))
290
+ throw new GetenvHandledError(`Invalid integer.`);
291
+ const MIN = constraints.min ?? Number.MIN_SAFE_INTEGER;
292
+ if (parsed < MIN)
293
+ throw new GetenvHandledError(`Less than minimum: ${MIN}.`);
294
+ const MAX = constraints.max ?? Number.MAX_SAFE_INTEGER;
295
+ if (parsed > MAX)
296
+ throw new GetenvHandledError(`Greater than maximum: ${MAX}.`);
297
+ return parsed;
298
+ });
265
299
  }
266
300
  /**
267
301
  * @see getEnv, with the additional constraint that the environment variable must be a valid JSON object.
@@ -270,7 +304,10 @@ function getEnvInt(key, defaultValue) {
270
304
  * @param schema The schema to use to validate the environment variable value
271
305
  */
272
306
  function getEnvJson(key, defaultValue, schema) {
273
- return getEnv(key, defaultValue, (s) => JSON.parse(s));
307
+ const value = getEnv(key, defaultValue, (s) => JSON.parse(s));
308
+ return schema ?
309
+ schema.parse(value) :
310
+ value;
274
311
  }
275
312
  const [currentTsMs, setCurrentTsMs] = whispr_1.Whispr.create(Date.now());
276
313
  let currentTsMsInterval = null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cripty2001/utils",
3
- "version": "0.0.231",
3
+ "version": "0.0.232",
4
4
  "description": "Internal Set of utils. If you need them use them, otherwise go to the next package ;)",
5
5
  "homepage": "https://github.com/cripty2001/utils#readme",
6
6
  "bugs": {