@cripty2001/utils 0.0.231 → 0.0.233

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
@@ -1,5 +1,5 @@
1
1
  import { Whispr } from "@cripty2001/whispr";
2
- import type { TSchema } from "@sinclair/typebox";
2
+ import type { Static, TSchema } from "@sinclair/typebox";
3
3
  export type JSONEncodable = number | string | boolean | JSONEncodable[] | null | {
4
4
  [key: string]: JSONEncodable;
5
5
  };
@@ -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,18 +103,31 @@ 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
- * @see getEnv, with the additional constraint that the environment variable must be a valid JSON object.
110
- * Note: the default value is not used instead of an invalid JSON object. An invalid JSON object therefore throws an error.
124
+ * @see getEnv, with the additional constraint that the environment variable must be valid JSON.
125
+ * Note: the default value is not used instead of invalid JSON or a schema mismatch. Those therefore throw.
111
126
  *
112
- * @param schema The schema to use to validate the environment variable value
127
+ * @param schema Optional TypeBox schema.
113
128
  */
114
- export declare function getEnvJson<T>(key: string, defaultValue?: T, schema?: TSchema): T;
129
+ export declare function getEnvJson<T>(key: string, defaultValue?: T): T;
130
+ export declare function getEnvJson<S extends TSchema>(key: string, defaultValue: Static<S> | undefined, schema: S): Static<S>;
115
131
  export declare const CURRENT_TS_MS: () => Whispr<number>;
116
132
  export declare function getClock(rate: number): Whispr<number>;
117
133
  export declare function timediff2HumanReadable(diffMs: number): string;
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;
@@ -23,6 +24,7 @@ exports.timediff2HumanReadable = timediff2HumanReadable;
23
24
  exports.fn2promise = fn2promise;
24
25
  exports.safeApply = safeApply;
25
26
  const whispr_1 = require("@cripty2001/whispr");
27
+ const value_1 = require("@sinclair/typebox/value");
26
28
  const lodash_1 = require("lodash");
27
29
  const U32_LIMIT = 0x1_0000_0000;
28
30
  function randomU32() {
@@ -224,6 +226,12 @@ function randBetween(min, max) {
224
226
  return min;
225
227
  return min + randomBelow(span);
226
228
  }
229
+ class GetenvHandledError extends Error {
230
+ constructor(message) {
231
+ super(message);
232
+ }
233
+ }
234
+ exports.GetenvHandledError = GetenvHandledError;
227
235
  /**
228
236
  * Get an environment variable, convert it using the given converter function.
229
237
  * If the environment variable is not defined, the default value is used.
@@ -231,7 +239,7 @@ function randBetween(min, max) {
231
239
  *
232
240
  * @param key The environment variable key
233
241
  * @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
242
+ * @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
243
  *
236
244
  * @returns The converted environment variable value, or the default value if provided
237
245
  *
@@ -251,26 +259,61 @@ function getEnv(key, defaultValue, converter = (s) => s) {
251
259
  return converter(value);
252
260
  }
253
261
  catch (e) {
254
- throw new Error(`Environment variable ${key} cannot be converted to the desired type.`);
262
+ const message = e instanceof GetenvHandledError ? `(${e.message})` : "";
263
+ throw new Error(`Environment variable ${key} cannot be converted to the desired type.${message}`);
255
264
  }
256
265
  })();
257
266
  return converted;
258
267
  }
259
268
  /**
260
- * @see getEnv, with the additional constraint that the environment variable must be a valid integer.
261
- * Note: the default value is not used instead of an invalid integer. An invalid integer therefore throws an error.
269
+ * @see getEnv, returning a string.
270
+ *
271
+ * @param allowEmpty Whether to allow empty strings
262
272
  */
263
- function getEnvInt(key, defaultValue) {
264
- return getEnv(key, defaultValue, (s) => parseInt(s));
273
+ function getEnvString(key, allowEmpty = false, defaultValue) {
274
+ return getEnv(key, defaultValue, (s) => {
275
+ if (s === "" && !allowEmpty)
276
+ throw new GetenvHandledError(`Empty string.`);
277
+ return s;
278
+ });
265
279
  }
266
280
  /**
267
- * @see getEnv, with the additional constraint that the environment variable must be a valid JSON object.
268
- * Note: the default value is not used instead of an invalid JSON object. An invalid JSON object therefore throws an error.
281
+ * @see getEnv, with the additional constraint that the environment variable must be a valid integer.
269
282
  *
270
- * @param schema The schema to use to validate the environment variable value
283
+ * @param constraints The constraints (min and max) to apply to the environment variable value
284
+ *
285
+ * Note: the default value is not used instead of an invalid integer. An invalid integer therefore throws an error.
271
286
  */
287
+ function getEnvInt(key, constraints = {}, defaultValue) {
288
+ return getEnv(key, defaultValue, (s) => {
289
+ if (!/^[+-]?\d+$/.test(s))
290
+ throw new GetenvHandledError(`Invalid integer.`);
291
+ const parsed = Number(s);
292
+ if (!Number.isInteger(parsed))
293
+ throw new GetenvHandledError(`Invalid integer.`);
294
+ const MIN = constraints.min ?? Number.MIN_SAFE_INTEGER;
295
+ if (parsed < MIN)
296
+ throw new GetenvHandledError(`Less than minimum: ${MIN}.`);
297
+ const MAX = constraints.max ?? Number.MAX_SAFE_INTEGER;
298
+ if (parsed > MAX)
299
+ throw new GetenvHandledError(`Greater than maximum: ${MAX}.`);
300
+ return parsed;
301
+ });
302
+ }
272
303
  function getEnvJson(key, defaultValue, schema) {
273
- return getEnv(key, defaultValue, (s) => JSON.parse(s));
304
+ return getEnv(key, defaultValue, (s) => {
305
+ const parsed = (() => {
306
+ try {
307
+ return JSON.parse(s);
308
+ }
309
+ catch {
310
+ throw new GetenvHandledError(`Invalid JSON.`);
311
+ }
312
+ })();
313
+ if (schema === undefined)
314
+ return parsed;
315
+ return value_1.Value.Parse(schema, parsed);
316
+ });
274
317
  }
275
318
  const [currentTsMs, setCurrentTsMs] = whispr_1.Whispr.create(Date.now());
276
319
  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.233",
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": {