@kumix/utils 0.1.0 → 0.1.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.
@@ -61,7 +61,7 @@ function getEnv(key, fallback) {
61
61
  *
62
62
  * // Environment-specific configuration
63
63
  * const apiUrl = NODE_ENV === 'production'
64
- * ? 'https://api.kumix.com'
64
+ * ? 'https://api.kumix.io'
65
65
  * : 'http://localhost:3001';
66
66
  * ```
67
67
  */
@@ -92,10 +92,13 @@ const NODE_ENV = getEnv("NODE_ENV");
92
92
  */
93
93
  const isDevelopment = NODE_ENV === "development";
94
94
  /**
95
- * Checks if the application is running in staging mode
95
+ * Checks if the application is running in staging mode.
96
96
  * Used for testing production-like environments before deployment.
97
97
  *
98
- * @returns True if NODE_ENV is 'test', false otherwise
98
+ * Maps to `NODE_ENV === "staging"`. Note: the test runner (Vitest/Jest) sets
99
+ * `NODE_ENV=test`; for that case see `isTest`.
100
+ *
101
+ * @returns True if NODE_ENV is 'staging', false otherwise
99
102
  *
100
103
  * @example
101
104
  * ```ts
@@ -103,17 +106,16 @@ const isDevelopment = NODE_ENV === "development";
103
106
  *
104
107
  * if (isStaging) {
105
108
  * // Use staging API endpoints
106
- * apiUrl = 'https://staging-api.kumix.com';
109
+ * apiUrl = 'https://staging-api.kumix.io';
107
110
  * }
108
- *
109
- * // Staging-specific analytics
110
- * const analyticsConfig = {
111
- * enabled: isStaging || isProduction,
112
- * debug: isStaging
113
- * };
114
111
  * ```
115
112
  */
116
- const isStaging = NODE_ENV === "test";
113
+ const isStaging = NODE_ENV === "staging";
114
+ /**
115
+ * Checks if the application is running under the test runner (Vitest/Jest).
116
+ * Both runners set `NODE_ENV=test`.
117
+ */
118
+ const isTest = NODE_ENV === "test";
117
119
  /**
118
120
  * Checks if the application is running in production mode
119
121
  * Used for enabling production optimizations and features.
@@ -221,11 +223,12 @@ const LOG_LEVEL = getEnv("LOG_LEVEL");
221
223
  * });
222
224
  * ```
223
225
  */
226
+ const parsedLevel = LOG_LEVEL !== void 0 ? Number(LOG_LEVEL) : NaN;
224
227
  const logger = createConsola({
225
- level: isDevelopment ? 5 : LOG_LEVEL ? Number(LOG_LEVEL) : 3,
228
+ level: isDevelopment ? 5 : Number.isFinite(parsedLevel) ? parsedLevel : 3,
226
229
  formatOptions: { date: false }
227
230
  });
228
231
  //#endregion
229
- export { NODE_ENV as a, isProduction as c, LOG_LEVEL as i, isStaging as l, API_PORT as n, PORT as o, DEBUG as r, isDevelopment as s, logger as t, getEnv as u };
232
+ export { NODE_ENV as a, isProduction as c, getEnv as d, LOG_LEVEL as i, isStaging as l, API_PORT as n, PORT as o, DEBUG as r, isDevelopment as s, logger as t, isTest as u };
230
233
 
231
- //# sourceMappingURL=logger-BOB35dQN.js.map
234
+ //# sourceMappingURL=logger-qDWnDXis.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger-qDWnDXis.js","names":[],"sources":["../src/env.ts","../src/constants/development.ts","../src/functions/logging/logger.ts"],"sourcesContent":["/**\n * Runtime-agnostic environment variable reader\n * Provides safe environment variable access across Bun, Node, Deno, and Workers\n * without throwing when the host runtime does not expose `process` or `Deno`.\n */\n\n/** Minimal Deno global shape we rely on (full Deno namespace is not in std types). */\ndeclare const Deno: { env: { get(key: string): string | undefined } } | undefined;\n\n/**\n * Reads an environment variable in a runtime-agnostic way.\n * Tries `process.env` first (Node/Bun), then falls back to `Deno.env`,\n * and finally to the provided fallback value. Failures are swallowed so\n * the function is safe to call in any JavaScript runtime, including\n * Workers where neither global is available.\n *\n * @param key - The name of the environment variable to read\n * @param fallback - Optional value to return when the variable is unset or empty\n * @returns The environment value, or `fallback` if unavailable\n *\n * @example\n * ```ts\n * import { getEnv } from '@kumix/utils';\n *\n * // Read with a fallback\n * const port = getEnv('PORT', '3000');\n * // Example output: '3000' if PORT is unset\n *\n * // Read optional config\n * const databaseUrl = getEnv('DATABASE_URL');\n * // Example output: 'postgres://...' or undefined\n *\n * // Use across runtimes (Node, Bun, Deno, Workers)\n * const apiKey = getEnv('API_KEY');\n * if (!apiKey) throw new Error('API_KEY is required');\n * ```\n */\nexport function getEnv(key: string, fallback?: string): string | undefined {\n try {\n if (typeof process !== \"undefined\" && process.env) {\n return process.env[key] || fallback;\n }\n } catch {\n // process.env doesn't exist in Workers\n }\n try {\n if (typeof Deno !== \"undefined\") {\n const env = Deno.env;\n if (env && typeof env.get === \"function\") {\n return env.get(key) || fallback;\n }\n }\n } catch {\n // Deno not available\n }\n return fallback;\n}\n","/**\n * Development and environment constants\n * Provides environment detection and development-specific configuration values\n */\n\nimport { getEnv } from \"../env\";\n\n/**\n * Node environment variable\n * Indicates the current environment in which the application is running (e.g., 'development', 'production', 'staging').\n *\n * @example\n * ```ts\n * import { NODE_ENV } from '@/utils/constants';\n *\n * if (NODE_ENV === 'development') {\n * // Enable dev-only features\n * console.log('Development mode enabled');\n * }\n *\n * // Environment-specific configuration\n * const apiUrl = NODE_ENV === 'production'\n * ? 'https://api.kumix.io'\n * : 'http://localhost:3001';\n * ```\n */\nexport const NODE_ENV = getEnv(\"NODE_ENV\");\n\n/**\n * Checks if the application is running in development mode\n * Useful for enabling development-specific features and debugging.\n *\n * @returns True if NODE_ENV is 'development', false otherwise\n *\n * @example\n * ```ts\n * import { isDevelopment } from '@/utils/constants';\n *\n * if (isDevelopment) {\n * // Enable hot reloading and dev tools\n * console.log('Development mode enabled');\n * enableDevTools();\n * }\n *\n * // Conditional imports\n * if (isDevelopment) {\n * import('./dev-utils').then(devUtils => {\n * devUtils.setupDevEnvironment();\n * });\n * }\n * ```\n */\nexport const isDevelopment = NODE_ENV === \"development\";\n\n/**\n * Checks if the application is running in staging mode.\n * Used for testing production-like environments before deployment.\n *\n * Maps to `NODE_ENV === \"staging\"`. Note: the test runner (Vitest/Jest) sets\n * `NODE_ENV=test`; for that case see `isTest`.\n *\n * @returns True if NODE_ENV is 'staging', false otherwise\n *\n * @example\n * ```ts\n * import { isStaging } from '@/utils/constants';\n *\n * if (isStaging) {\n * // Use staging API endpoints\n * apiUrl = 'https://staging-api.kumix.io';\n * }\n * ```\n */\nexport const isStaging = NODE_ENV === \"staging\";\n\n/**\n * Checks if the application is running under the test runner (Vitest/Jest).\n * Both runners set `NODE_ENV=test`.\n */\nexport const isTest = NODE_ENV === \"test\";\n\n/**\n * Checks if the application is running in production mode\n * Used for enabling production optimizations and features.\n *\n * @returns True if NODE_ENV is 'production', false otherwise\n *\n * @example\n * ```ts\n * import { isProduction } from '@/utils/constants';\n *\n * if (isProduction) {\n * // Enable analytics and error tracking\n * enableAnalytics();\n * enableErrorTracking();\n * }\n *\n * // Performance optimizations\n * const shouldMinify = isProduction;\n * const shouldCompress = isProduction;\n * ```\n */\nexport const isProduction = NODE_ENV === \"production\";\n\n/**\n * The port number on which the application server runs.\n * Defaults to '3000' if the PORT environment variable is not set.\n *\n * @example\n * app.listen(PORT, () => {\n * console.log(`Server running on port ${PORT}`);\n * });\n */\nexport const PORT = getEnv(\"PORT\") || \"3000\";\n\n/**\n * The port number on which the api application server runs.\n * Defaults to '3001' if the API_PORT environment variable is not set.\n *\n * @example\n * app.listen(API_PORT, () => {\n * console.log(`Server running on port ${API_PORT}`);\n * });\n */\nexport const API_PORT = getEnv(\"API_PORT\") || \"3001\";\n\n/**\n * Indicates whether debug mode is enabled (true in development mode).\n *\n * @example\n * if (DEBUG) {\n * console.log('Debugging enabled');\n * }\n */\nexport const DEBUG = !!isDevelopment;\n\n/**\n * Log level for Consola or other logging libraries.\n * Controls the verbosity of logs output to the console.\n *\n * @example\n * if (LOG_LEVEL === 'debug') {\n * consola.debug('Debugging enabled');\n * }\n */\nexport const LOG_LEVEL = getEnv(\"LOG_LEVEL\");\n","/**\n * Centralized logging utility for the application\n * Provides consistent logging interface using Consola with custom formatting\n * Used throughout the application to replace console.log statements\n */\n\nimport { createConsola } from \"consola\";\n\nimport { isDevelopment, LOG_LEVEL } from \"../../constants/development\";\n\n/**\n * Application logger instance with consistent formatting\n *\n * Provides structured logging with different levels (info, warn, error, debug)\n * and consistent formatting across the application. Replaces direct console usage.\n *\n * @example\n * ```typescript\n * import { logger } from '@kumix/utils';\n *\n * // Basic logging\n * logger.info('User logged in successfully');\n * logger.warn('API rate limit approaching');\n * logger.error('Database connection failed');\n * logger.debug('Processing user data', { userId: 123 });\n *\n * // With context data\n * logger.info('User action completed', {\n * userId: user.id,\n * action: 'profile_update',\n * timestamp: new Date().toISOString()\n * });\n *\n * // Error logging with stack trace\n * try {\n * await riskyOperation();\n * } catch (error) {\n * logger.error('Operation failed', { error, context: 'user-service' });\n * }\n *\n * // Performance logging\n * const startTime = Date.now();\n * await someOperation();\n * logger.debug('Operation completed', {\n * duration: Date.now() - startTime,\n * operation: 'data-processing'\n * });\n * ```\n */\n// Parse the configured log level safely. Non-numeric values would previously\n// fall through as `NaN`, leaving the logger at an indeterminate level.\nconst parsedLevel = LOG_LEVEL !== undefined ? Number(LOG_LEVEL) : Number.NaN;\nconst resolvedLevel = isDevelopment ? 5 : Number.isFinite(parsedLevel) ? parsedLevel : 3;\n\nexport const logger = createConsola({\n level: resolvedLevel,\n formatOptions: {\n date: false,\n },\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqCA,SAAgB,OAAO,KAAa,UAAuC;CACzE,IAAI;EACF,IAAI,OAAO,YAAY,eAAe,QAAQ,KAC5C,OAAO,QAAQ,IAAI,QAAQ;CAE/B,QAAQ,CAER;CACA,IAAI;EACF,IAAI,OAAO,SAAS,aAAa;GAC/B,MAAM,MAAM,KAAK;GACjB,IAAI,OAAO,OAAO,IAAI,QAAQ,YAC5B,OAAO,IAAI,IAAI,GAAG,KAAK;EAE3B;CACF,QAAQ,CAER;CACA,OAAO;AACT;;;;;;;;;;;;;;;;;;;;;;;;;;AC9BA,MAAa,WAAW,OAAO,UAAU;;;;;;;;;;;;;;;;;;;;;;;;;AA0BzC,MAAa,gBAAgB,aAAa;;;;;;;;;;;;;;;;;;;;AAqB1C,MAAa,YAAY,aAAa;;;;;AAMtC,MAAa,SAAS,aAAa;;;;;;;;;;;;;;;;;;;;;;AAuBnC,MAAa,eAAe,aAAa;;;;;;;;;;AAWzC,MAAa,OAAO,OAAO,MAAM,KAAK;;;;;;;;;;AAWtC,MAAa,WAAW,OAAO,UAAU,KAAK;;;;;;;;;AAU9C,MAAa,QAAQ,CAAC,CAAC;;;;;;;;;;AAWvB,MAAa,YAAY,OAAO,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AC9F3C,MAAM,cAAc,cAAc,KAAA,IAAY,OAAO,SAAS,IAAI;AAGlE,MAAa,SAAS,cAAc;CAClC,OAHoB,gBAAgB,IAAI,OAAO,SAAS,WAAW,IAAI,cAAc;CAIrF,eAAe,EACb,MAAM,MACR;AACF,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"server.d.ts","names":[],"sources":["../src/functions/crypto/generate-random-string.ts","../src/functions/crypto/jwt.ts","../src/functions/crypto/password.ts","../src/functions/datetime/parse-datetime.ts"],"mappings":";;;;;;AA+BA;;;;AAAmD;;;;ACrBpC;;;;;;;;;;;;;;AAsBV;AAAA;;iBDDW,oBAAA,CAAqB,MAAc;;;AAAA;;;AAAA,UChBzC,UAAA;EAAA;EAER,MAAA;;EAEA,KAAA;EAFA;EAIA,IAAA;EAAA;EAEA,WAAA;EAEA;EAAA,GAAA;EAIC;EAFD,GAAA;EAKA;EAAA,CAHC,GAAA;EAGE;EADH,GAAA;EACA,GAAA;AAAA;;;;UAMQ,UAAA;EAMA;EAJR,SAAA;EAUQ;EARR,MAAA;;EAEA,QAAA;AAAA;;;;UAMQ,qBAAA;EAMH;EAJL,OAAA;EAwCyB;EAtCzB,OAAA,GAAU,UAAU;EAyCI;EAvCxB,KAAA;AAAA;;;;;AAuCwB;AAiF1B;;;;;;;;;;;AAIwB;AAgFxB;;;;AAAoD;AAoCpD;;;;AAA0C;AAuC1C;;;;AAAiD;;iBAnPjC,WAAA,CACd,OAAA,EAAS,UAAA,EAC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G,aAAA,GAAiB,GAAA,EAAK,IAAA,cAAgB,IAAI"}
1
+ {"version":3,"file":"server.d.ts","names":[],"sources":["../src/functions/crypto/generate-random-string.ts","../src/functions/crypto/jwt.ts","../src/functions/crypto/password.ts","../src/functions/datetime/parse-datetime.ts"],"mappings":";;;;;;AA+BA;;;;AAAmD;;;;ACrBpC;;;;;;;;;;;;;;AAsBV;AAAA;;iBDDW,oBAAA,CAAqB,MAAc;;;AAAA;;;AAAA,UChBzC,UAAA;EAAA;EAER,MAAA;;EAEA,KAAA;EAFA;EAIA,IAAA;EAAA;EAEA,WAAA;EAEA;EAAA,GAAA;EAIC;EAFD,GAAA;EAKA;EAAA,CAHC,GAAA;EAGE;EADH,GAAA;EACA,GAAA;AAAA;;;;UAMQ,UAAA;EAMA;EAJR,SAAA;EAUQ;EARR,MAAA;;EAEA,QAAA;AAAA;;;;UAMQ,qBAAA;EAMH;EAJL,OAAA;EAwCyB;EAtCzB,OAAA,GAAU,UAAU;EAyCI;EAvCxB,KAAA;AAAA;;;;;AAuCwB;AAiF1B;;;;;;;;;;;AAIwB;AAkFxB;;;;AAAoD;AAuCpD;;;;AAA0C;AAuC1C;;;;AAAiD;;iBAxPjC,WAAA,CACd,OAAA,EAAS,UAAA,EACT,MAAA,UACA,OAAA,GAAS,UAAe;;;;;;ACvEd;AAAA;;;;AAUL;AAiCP;;;;;;;;;AAGU;AAkFV;;;;;;;;;AAG+B;AAwE/B;;;;AAEgD;AAgDhD;;iBDrGgB,SAAA,CACd,KAAA,UACA,MAAA,UACA,OAAA;EAAW,MAAA;EAAiB,QAAA;AAAA,IAC3B,qBAAqB;;;;;;ACwGhB;;;;AClPR;;;;;;;;AAAuD;;;;;;;;;;;iBF4NvC,SAAA,CAAU,KAAA,WAAgB,UAAU;;;;;;;;;;;;;;;;;;;;;;;iBAuCpC,YAAA,CAAa,KAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAuC1B,mBAAA,CAAoB,KAAa;;;;;;ADrTjD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