@forinda/kickjs-config 0.3.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Felix Orinda
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,54 @@
1
+ import { z } from 'zod';
2
+
3
+ /**
4
+ * Base environment schema with common server variables.
5
+ * Users extend this with their own application-specific vars.
6
+ */
7
+ declare const baseEnvSchema: z.ZodObject<{
8
+ PORT: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
9
+ NODE_ENV: z.ZodDefault<z.ZodEnum<{
10
+ development: "development";
11
+ production: "production";
12
+ test: "test";
13
+ }>>;
14
+ LOG_LEVEL: z.ZodDefault<z.ZodString>;
15
+ }, z.core.$strip>;
16
+ /**
17
+ * Define a custom env schema by extending the base.
18
+ * Returns a loader function that validates process.env.
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * const envSchema = defineEnv((base) =>
23
+ * base.extend({
24
+ * DATABASE_URL: z.string().url(),
25
+ * JWT_SECRET: z.string().min(32),
26
+ * REDIS_URL: z.string().url().optional(),
27
+ * })
28
+ * )
29
+ *
30
+ * const env = loadEnv(envSchema)
31
+ * ```
32
+ */
33
+ declare function defineEnv<T extends z.ZodRawShape>(extend: (base: typeof baseEnvSchema) => z.ZodObject<any>): z.ZodObject<any>;
34
+ /** Parse and validate process.env against a Zod schema. Caches result per schema. */
35
+ declare function loadEnv<T extends z.ZodObject<any>>(schema?: T): z.infer<T>;
36
+ /** Get a single typed environment variable value */
37
+ declare function getEnv<K extends string>(key: K): any;
38
+ /** Reset cached env (useful for testing) */
39
+ declare function resetEnvCache(): void;
40
+ type Env = z.infer<typeof baseEnvSchema>;
41
+
42
+ /** Injectable service for accessing typed environment configuration */
43
+ declare class ConfigService {
44
+ private env;
45
+ /** Get an env variable by key */
46
+ get<T = any>(key: string): T;
47
+ /** Get all env config (readonly) */
48
+ getAll(): Readonly<Record<string, any>>;
49
+ isProduction(): boolean;
50
+ isDevelopment(): boolean;
51
+ isTest(): boolean;
52
+ }
53
+
54
+ export { ConfigService, type Env, baseEnvSchema, defineEnv, getEnv, loadEnv, resetEnvCache };
package/dist/index.js ADDED
@@ -0,0 +1,87 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // src/env.ts
5
+ import { z } from "zod";
6
+ import "dotenv/config";
7
+ var baseEnvSchema = z.object({
8
+ // Server
9
+ PORT: z.coerce.number().default(3e3),
10
+ NODE_ENV: z.enum([
11
+ "development",
12
+ "production",
13
+ "test"
14
+ ]).default("development"),
15
+ LOG_LEVEL: z.string().default("info")
16
+ });
17
+ var cachedEnv = null;
18
+ var cachedSchema = null;
19
+ function defineEnv(extend) {
20
+ return extend(baseEnvSchema);
21
+ }
22
+ __name(defineEnv, "defineEnv");
23
+ function loadEnv(schema) {
24
+ const s = schema || baseEnvSchema;
25
+ if (cachedEnv && cachedSchema === s) return cachedEnv;
26
+ cachedSchema = s;
27
+ cachedEnv = s.parse(process.env);
28
+ return cachedEnv;
29
+ }
30
+ __name(loadEnv, "loadEnv");
31
+ function getEnv(key) {
32
+ const env = loadEnv();
33
+ return env[key];
34
+ }
35
+ __name(getEnv, "getEnv");
36
+ function resetEnvCache() {
37
+ cachedEnv = null;
38
+ cachedSchema = null;
39
+ }
40
+ __name(resetEnvCache, "resetEnvCache");
41
+
42
+ // src/config-service.ts
43
+ import { Service } from "@forinda/kickjs-core";
44
+ function _ts_decorate(decorators, target, key, desc) {
45
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
46
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
47
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
48
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
49
+ }
50
+ __name(_ts_decorate, "_ts_decorate");
51
+ var ConfigService = class {
52
+ static {
53
+ __name(this, "ConfigService");
54
+ }
55
+ env = loadEnv();
56
+ /** Get an env variable by key */
57
+ get(key) {
58
+ return this.env[key];
59
+ }
60
+ /** Get all env config (readonly) */
61
+ getAll() {
62
+ return Object.freeze({
63
+ ...this.env
64
+ });
65
+ }
66
+ isProduction() {
67
+ return this.env.NODE_ENV === "production";
68
+ }
69
+ isDevelopment() {
70
+ return this.env.NODE_ENV === "development";
71
+ }
72
+ isTest() {
73
+ return this.env.NODE_ENV === "test";
74
+ }
75
+ };
76
+ ConfigService = _ts_decorate([
77
+ Service()
78
+ ], ConfigService);
79
+ export {
80
+ ConfigService,
81
+ baseEnvSchema,
82
+ defineEnv,
83
+ getEnv,
84
+ loadEnv,
85
+ resetEnvCache
86
+ };
87
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/env.ts","../src/config-service.ts"],"sourcesContent":["import { z } from 'zod'\nimport 'dotenv/config'\n\n/**\n * Base environment schema with common server variables.\n * Users extend this with their own application-specific vars.\n */\nexport const baseEnvSchema = z.object({\n // Server\n PORT: z.coerce.number().default(3000),\n NODE_ENV: z.enum(['development', 'production', 'test']).default('development'),\n LOG_LEVEL: z.string().default('info'),\n})\n\n/** Cached env config to avoid re-parsing — keyed by schema reference */\nlet cachedEnv: any = null\nlet cachedSchema: any = null\n\n/**\n * Define a custom env schema by extending the base.\n * Returns a loader function that validates process.env.\n *\n * @example\n * ```ts\n * const envSchema = defineEnv((base) =>\n * base.extend({\n * DATABASE_URL: z.string().url(),\n * JWT_SECRET: z.string().min(32),\n * REDIS_URL: z.string().url().optional(),\n * })\n * )\n *\n * const env = loadEnv(envSchema)\n * ```\n */\nexport function defineEnv<T extends z.ZodRawShape>(\n extend: (base: typeof baseEnvSchema) => z.ZodObject<any>,\n): z.ZodObject<any> {\n return extend(baseEnvSchema)\n}\n\n/** Parse and validate process.env against a Zod schema. Caches result per schema. */\nexport function loadEnv<T extends z.ZodObject<any>>(schema?: T): z.infer<T> {\n const s = schema || baseEnvSchema\n // Re-parse if schema changed or no cache yet\n if (cachedEnv && cachedSchema === s) return cachedEnv\n cachedSchema = s\n cachedEnv = s.parse(process.env)\n return cachedEnv\n}\n\n/** Get a single typed environment variable value */\nexport function getEnv<K extends string>(key: K): any {\n const env = loadEnv()\n return (env as any)[key]\n}\n\n/** Reset cached env (useful for testing) */\nexport function resetEnvCache(): void {\n cachedEnv = null\n cachedSchema = null\n}\n\nexport type Env = z.infer<typeof baseEnvSchema>\n","import { Service } from '@forinda/kickjs-core'\nimport { loadEnv } from './env'\n\n/** Injectable service for accessing typed environment configuration */\n@Service()\nexport class ConfigService {\n private env: Record<string, any> = loadEnv()\n\n /** Get an env variable by key */\n get<T = any>(key: string): T {\n return this.env[key] as T\n }\n\n /** Get all env config (readonly) */\n getAll(): Readonly<Record<string, any>> {\n return Object.freeze({ ...this.env })\n }\n\n isProduction(): boolean {\n return this.env.NODE_ENV === 'production'\n }\n\n isDevelopment(): boolean {\n return this.env.NODE_ENV === 'development'\n }\n\n isTest(): boolean {\n return this.env.NODE_ENV === 'test'\n }\n}\n"],"mappings":";;;;AAAA,SAASA,SAAS;AAClB,OAAO;AAMA,IAAMC,gBAAgBC,EAAEC,OAAO;;EAEpCC,MAAMF,EAAEG,OAAOC,OAAM,EAAGC,QAAQ,GAAA;EAChCC,UAAUN,EAAEO,KAAK;IAAC;IAAe;IAAc;GAAO,EAAEF,QAAQ,aAAA;EAChEG,WAAWR,EAAES,OAAM,EAAGJ,QAAQ,MAAA;AAChC,CAAA;AAGA,IAAIK,YAAiB;AACrB,IAAIC,eAAoB;AAmBjB,SAASC,UACdC,QAAwD;AAExD,SAAOA,OAAOd,aAAAA;AAChB;AAJgBa;AAOT,SAASE,QAAoCC,QAAU;AAC5D,QAAMC,IAAID,UAAUhB;AAEpB,MAAIW,aAAaC,iBAAiBK,EAAG,QAAON;AAC5CC,iBAAeK;AACfN,cAAYM,EAAEC,MAAMC,QAAQC,GAAG;AAC/B,SAAOT;AACT;AAPgBI;AAUT,SAASM,OAAyBC,KAAM;AAC7C,QAAMF,MAAML,QAAAA;AACZ,SAAQK,IAAYE,GAAAA;AACtB;AAHgBD;AAMT,SAASE,gBAAAA;AACdZ,cAAY;AACZC,iBAAe;AACjB;AAHgBW;;;AC1DhB,SAASC,eAAe;;;;;;;;AAKjB,IAAMC,gBAAN,MAAMA;SAAAA;;;EACHC,MAA2BC,QAAAA;;EAGnCC,IAAaC,KAAgB;AAC3B,WAAO,KAAKH,IAAIG,GAAAA;EAClB;;EAGAC,SAAwC;AACtC,WAAOC,OAAOC,OAAO;MAAE,GAAG,KAAKN;IAAI,CAAA;EACrC;EAEAO,eAAwB;AACtB,WAAO,KAAKP,IAAIQ,aAAa;EAC/B;EAEAC,gBAAyB;AACvB,WAAO,KAAKT,IAAIQ,aAAa;EAC/B;EAEAE,SAAkB;AAChB,WAAO,KAAKV,IAAIQ,aAAa;EAC/B;AACF;;;;","names":["z","baseEnvSchema","z","object","PORT","coerce","number","default","NODE_ENV","enum","LOG_LEVEL","string","cachedEnv","cachedSchema","defineEnv","extend","loadEnv","schema","s","parse","process","env","getEnv","key","resetEnvCache","Service","ConfigService","env","loadEnv","get","key","getAll","Object","freeze","isProduction","NODE_ENV","isDevelopment","isTest"]}
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@forinda/kickjs-config",
3
+ "version": "0.3.0",
4
+ "description": "Zod-based environment validation and typed configuration for KickJS",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/index.js",
11
+ "types": "./dist/index.d.ts"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "dependencies": {
18
+ "dotenv": "^17.3.1",
19
+ "zod": "^4.3.6",
20
+ "@forinda/kickjs-core": "0.3.0"
21
+ },
22
+ "devDependencies": {
23
+ "@swc/core": "^1.7.28",
24
+ "@types/node": "^24.5.2",
25
+ "tsup": "^8.5.0",
26
+ "typescript": "^5.9.2",
27
+ "vitest": "^3.2.4"
28
+ },
29
+ "publishConfig": {
30
+ "access": "public"
31
+ },
32
+ "license": "MIT",
33
+ "author": "Felix Orinda",
34
+ "engines": {
35
+ "node": ">=20.0"
36
+ },
37
+ "scripts": {
38
+ "build": "tsup",
39
+ "dev": "tsup --watch",
40
+ "test": "vitest run",
41
+ "test:watch": "vitest",
42
+ "typecheck": "tsc --noEmit",
43
+ "clean": "rm -rf dist .turbo",
44
+ "lint": "tsc --noEmit"
45
+ }
46
+ }