@open-kingdom/shared-poly-util-env-config 0.0.2-4

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/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # @open-kingdom/shared-poly-util-env-config
2
+
3
+ Centralized configuration service for accessing environment variables. Works in both Node.js and browser environments.
4
+
5
+ ## Usage
6
+
7
+ ### Backend (Node.js)
8
+
9
+ ```typescript
10
+ import { createConfigService, nodeEnvAdapter } from '@open-kingdom/shared-poly-util-env-config';
11
+
12
+ const envKeys = ['PORT', 'JWT_SECRET', 'JWT_EXPIRES_IN', 'BASE_URL'] as const;
13
+ const configService = createConfigService(envKeys, nodeEnvAdapter);
14
+
15
+ // Type-safe access to environment variables
16
+ const port = configService.get('PORT', '3000');
17
+ const secret = configService.getOrThrow('JWT_SECRET');
18
+ ```
19
+
20
+ ### Frontend (Vite)
21
+
22
+ ```typescript
23
+ import { createConfigService, createBrowserEnvAdapter } from '@open-kingdom/shared-poly-util-env-config';
24
+
25
+ const envKeys = ['VITE_API_URL', 'VITE_APP_NAME'] as const;
26
+ const configService = createConfigService(envKeys, createBrowserEnvAdapter(import.meta));
27
+
28
+ const apiUrl = configService.get('VITE_API_URL');
29
+ ```
30
+
31
+ ## API
32
+
33
+ ### `createConfigService(keys, adapter)`
34
+
35
+ Factory function to create a typed ConfigService instance.
36
+
37
+ - `keys`: Array of environment variable keys (used for type inference)
38
+ - `adapter`: Either `nodeEnvAdapter` or result of `createBrowserEnvAdapter(import.meta)`
39
+
40
+ ### `createBrowserEnvAdapter(importMeta)`
41
+
42
+ Factory function to create a browser environment adapter.
43
+
44
+ - `importMeta`: Pass `import.meta` from your Vite app
45
+
46
+ ### `ConfigService<TKeys>`
47
+
48
+ - `get(key)`: Returns the value or undefined
49
+ - `get(key, defaultValue)`: Returns the value or the default
50
+ - `getOrThrow(key)`: Returns the value or throws if not set
51
+ - `has(key)`: Returns true if the variable is set
@@ -0,0 +1,2 @@
1
+ export * from './lib/config-service.js';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from './lib/config-service.js';
@@ -0,0 +1,19 @@
1
+ export type EnvAdapter = {
2
+ get(key: string): string | undefined;
3
+ };
4
+ export declare const nodeEnvAdapter: EnvAdapter;
5
+ interface ViteImportMeta {
6
+ env: Record<string, string | undefined>;
7
+ }
8
+ export declare function createBrowserEnvAdapter(importMeta: ViteImportMeta): EnvAdapter;
9
+ export declare class ConfigService<TKeys extends string = string> {
10
+ private readonly adapter;
11
+ constructor(adapter: EnvAdapter);
12
+ get(key: TKeys): string | undefined;
13
+ get(key: TKeys, defaultValue: string): string;
14
+ getOrThrow(key: TKeys): string;
15
+ has(key: TKeys): boolean;
16
+ }
17
+ export declare function createConfigService<TKeys extends readonly string[]>(_keys: TKeys, adapter: EnvAdapter): ConfigService<TKeys[number]>;
18
+ export {};
19
+ //# sourceMappingURL=config-service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config-service.d.ts","sourceRoot":"","sources":["../../src/lib/config-service.ts"],"names":[],"mappings":"AACA,MAAM,MAAM,UAAU,GAAG;IACvB,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;CACtC,CAAC;AAGF,eAAO,MAAM,cAAc,EAAE,UAE5B,CAAC;AAGF,UAAU,cAAc;IACtB,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC;CACzC;AAGD,wBAAgB,uBAAuB,CACrC,UAAU,EAAE,cAAc,GACzB,UAAU,CAIZ;AAGD,qBAAa,aAAa,CAAC,KAAK,SAAS,MAAM,GAAG,MAAM;IAC1C,OAAO,CAAC,QAAQ,CAAC,OAAO;gBAAP,OAAO,EAAE,UAAU;IAGhD,GAAG,CAAC,GAAG,EAAE,KAAK,GAAG,MAAM,GAAG,SAAS;IACnC,GAAG,CAAC,GAAG,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,GAAG,MAAM;IAM7C,UAAU,CAAC,GAAG,EAAE,KAAK,GAAG,MAAM;IAW9B,GAAG,CAAC,GAAG,EAAE,KAAK,GAAG,OAAO;CAGzB;AAGD,wBAAgB,mBAAmB,CAAC,KAAK,SAAS,SAAS,MAAM,EAAE,EACjE,KAAK,EAAE,KAAK,EACZ,OAAO,EAAE,UAAU,GAClB,aAAa,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAE9B"}
@@ -0,0 +1,36 @@
1
+ // Node.js adapter - reads from process.env
2
+ export const nodeEnvAdapter = {
3
+ get: (key) => process.env[key],
4
+ };
5
+ // Browser adapter factory - pass import.meta from your Vite app
6
+ export function createBrowserEnvAdapter(importMeta) {
7
+ return {
8
+ get: (key) => importMeta.env[key],
9
+ };
10
+ }
11
+ // Configuration service for type-safe environment variable access
12
+ export class ConfigService {
13
+ adapter;
14
+ constructor(adapter) {
15
+ this.adapter = adapter;
16
+ }
17
+ get(key, defaultValue) {
18
+ return this.adapter.get(key) ?? defaultValue;
19
+ }
20
+ // Get value or throw if not set
21
+ getOrThrow(key) {
22
+ const value = this.adapter.get(key);
23
+ if (value === undefined) {
24
+ throw new Error(`Environment variable ${key} is not set`);
25
+ }
26
+ return value;
27
+ }
28
+ // Check if variable is set
29
+ has(key) {
30
+ return this.adapter.get(key) !== undefined;
31
+ }
32
+ }
33
+ // Factory to create a typed ConfigService
34
+ export function createConfigService(_keys, adapter) {
35
+ return new ConfigService(adapter);
36
+ }
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@open-kingdom/shared-poly-util-env-config",
3
+ "version": "0.0.2-4",
4
+ "type": "module",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ "./package.json": "./package.json",
10
+ ".": {
11
+ "development": "./src/index.ts",
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.js",
14
+ "default": "./dist/index.js"
15
+ }
16
+ },
17
+ "files": [
18
+ "dist",
19
+ "!**/*.tsbuildinfo"
20
+ ],
21
+ "nx": {
22
+ "tags": [
23
+ "scope:shared",
24
+ "type:util",
25
+ "environment:poly"
26
+ ]
27
+ },
28
+ "dependencies": {
29
+ "tslib": "^2.3.0"
30
+ }
31
+ }