@edirect/config
Advanced tools
| { | ||
| "name": "@edirect/config", | ||
| "version": "11.0.63", | ||
| "main": "./dist/src/index.js", | ||
| "types": "./dist/src/index.d.ts", | ||
| "exports": { | ||
| ".": { | ||
| "import": "./dist/src/index.js", | ||
| "default": "./dist/src/index.js", | ||
| "require": "./dist/src/index.js", | ||
| "types": "./dist/src/index.d.ts" | ||
| }, | ||
| "./package.json": "./package.json" | ||
| }, | ||
| "files": [ | ||
| "dist" | ||
| ], | ||
| "dependencies": { | ||
| "@nestjs/common": "^11.2.1", | ||
| "dotenv": "17.4.2", | ||
| "tslib": "^2.8.1" | ||
| }, | ||
| "type": "commonjs" | ||
| } |
+103
| # @edirect/config | ||
| Global NestJS configuration module for eDirect applications. Provides environment variable loading via `dotenv` and a `ConfigService` for accessing configuration values across the application. | ||
| ## Features | ||
| - Loads `.{NODE_ENV}.env` file automatically on startup (e.g., `.development.env`, `.production.env`) | ||
| - Global module — register once, inject `ConfigService` anywhere | ||
| - Simple `get(key)` / `getConfig()` API | ||
| - Dual injection tokens: class token (`ConfigService`) and string constant (`CONFIG_SERVICE_TOKEN`) | ||
| ## Installation | ||
| ```sh | ||
| pnpm add @edirect/config | ||
| # or | ||
| npm install @edirect/config | ||
| ``` | ||
| ## Usage | ||
| ### Register in your AppModule | ||
| ```ts | ||
| import { Module } from '@nestjs/common'; | ||
| import { ConfigModule } from '@edirect/config'; | ||
| @Module({ | ||
| imports: [ConfigModule], | ||
| }) | ||
| export class AppModule {} | ||
| ``` | ||
| Because `ConfigModule` is decorated with `@Global()`, you only need to import it once at the root module. `ConfigService` will be available for injection in all feature modules automatically. | ||
| ### Inject ConfigService | ||
| ```ts | ||
| import { Injectable } from '@nestjs/common'; | ||
| import { ConfigService } from '@edirect/config'; | ||
| @Injectable() | ||
| export class MyService { | ||
| constructor(private readonly config: ConfigService) {} | ||
| doSomething() { | ||
| const dbUrl = this.config.get('DATABASE_URL'); | ||
| const port = this.config.get('PORT') ?? '3000'; | ||
| } | ||
| } | ||
| ``` | ||
| ## API | ||
| ### `ConfigService` | ||
| | Method | Signature | Description | | ||
| | ------------- | ------------------------------------ | ---------------------------------------------------------------------------------------------- | | ||
| | `constructor` | `(filePath?: string)` | Optionally pass a `.env` file path. Automatically called with `.{NODE_ENV}.env` by the module. | | ||
| | `get` | `(key: string): string \| undefined` | Returns the value for the given environment variable key from `process.env`. | | ||
| | `getConfig` | `(): IStringMapper` | Returns all environment variables as a `Record<string, string>`. | | ||
| ### `CONFIG_SERVICE_TOKEN` | ||
| An alternative injection token (string constant) for `ConfigService`. Useful when you need to inject by token rather than by class: | ||
| ```ts | ||
| import { Inject } from '@nestjs/common'; | ||
| import { CONFIG_SERVICE_TOKEN, ConfigService } from '@edirect/config'; | ||
| constructor(@Inject(CONFIG_SERVICE_TOKEN) private config: ConfigService) {} | ||
| ``` | ||
| ## Environment File Convention | ||
| The module automatically loads `.{NODE_ENV}.env` from the working directory: | ||
| | `NODE_ENV` | File loaded | | ||
| | ----------------------- | ------------------ | | ||
| | `development` (default) | `.development.env` | | ||
| | `staging` | `.staging.env` | | ||
| | `production` | `.production.env` | | ||
| | `test` | `.test.env` | | ||
| **Example `.development.env`:** | ||
| ```env | ||
| PORT=3000 | ||
| DATABASE_URL=mongodb://localhost:27017/mydb | ||
| REDIS_URL=redis://localhost:6379 | ||
| LOGS_LEVEL=debug | ||
| ``` | ||
| > **Note:** Never commit `.env` files containing sensitive credentials to version control. | ||
| ## Exports | ||
| ```ts | ||
| export { ConfigModule } from './config/config.module'; | ||
| export { ConfigService } from './config/config.service'; | ||
| export { CONFIG_SERVICE_TOKEN } from './config/config.constants'; | ||
| export type { IStringMapper } from './config/config.interfaces'; | ||
| ``` |
| /** | ||
| * Thrown when a required environment variable is absent or empty. | ||
| * Used by `ConfigService.getOrThrow()` and by modules that declare their own required config. | ||
| */ | ||
| export declare class ConfigMissingError extends Error { | ||
| readonly missingKey: string; | ||
| constructor(missingKey: string); | ||
| } | ||
| //# sourceMappingURL=config-missing.error.d.ts.map |
| {"version":3,"file":"config-missing.error.d.ts","sourceRoot":"","sources":["../../../src/config/config-missing.error.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,qBAAa,kBAAmB,SAAQ,KAAK;IAC/B,QAAQ,CAAC,UAAU,EAAE,MAAM;gBAAlB,UAAU,EAAE,MAAM;CAIxC"} |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.ConfigMissingError = void 0; | ||
| /** | ||
| * Thrown when a required environment variable is absent or empty. | ||
| * Used by `ConfigService.getOrThrow()` and by modules that declare their own required config. | ||
| */ | ||
| class ConfigMissingError extends Error { | ||
| missingKey; | ||
| constructor(missingKey) { | ||
| super(`Missing required environment variable: "${missingKey}"`); | ||
| this.missingKey = missingKey; | ||
| this.name = 'ConfigMissingError'; | ||
| } | ||
| } | ||
| exports.ConfigMissingError = ConfigMissingError; |
| export declare const CONFIG_SERVICE_TOKEN = "CONFIG_SERVICE"; | ||
| //# sourceMappingURL=config.constants.d.ts.map |
| {"version":3,"file":"config.constants.d.ts","sourceRoot":"","sources":["../../../src/config/config.constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,oBAAoB,mBAAmB,CAAC"} |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.CONFIG_SERVICE_TOKEN = void 0; | ||
| exports.CONFIG_SERVICE_TOKEN = 'CONFIG_SERVICE'; |
| export interface IStringMapper { | ||
| [key: string]: string; | ||
| } | ||
| //# sourceMappingURL=config.interfaces.d.ts.map |
| {"version":3,"file":"config.interfaces.d.ts","sourceRoot":"","sources":["../../../src/config/config.interfaces.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,aAAa;IAC5B,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC;CACvB"} |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); |
| export declare class ConfigModule { | ||
| } | ||
| //# sourceMappingURL=config.module.d.ts.map |
| {"version":3,"file":"config.module.d.ts","sourceRoot":"","sources":["../../../src/config/config.module.ts"],"names":[],"mappings":"AAKA,qBAKa,YAAY;CAAG"} |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.ConfigModule = void 0; | ||
| const tslib_1 = require("tslib"); | ||
| const common_1 = require("@nestjs/common"); | ||
| const config_providers_1 = require("./config.providers"); | ||
| const config_service_1 = require("./config.service"); | ||
| let ConfigModule = class ConfigModule { | ||
| }; | ||
| exports.ConfigModule = ConfigModule; | ||
| exports.ConfigModule = ConfigModule = tslib_1.__decorate([ | ||
| (0, common_1.Global)(), | ||
| (0, common_1.Module)({ | ||
| providers: [...config_providers_1.ConfigProviders], | ||
| exports: [config_service_1.ConfigService], | ||
| }) | ||
| ], ConfigModule); |
| import { Provider } from '@nestjs/common'; | ||
| export declare const ConfigProviders: Provider[]; | ||
| //# sourceMappingURL=config.providers.d.ts.map |
| {"version":3,"file":"config.providers.d.ts","sourceRoot":"","sources":["../../../src/config/config.providers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAK1C,eAAO,MAAM,eAAe,EAAE,QAAQ,EAWrC,CAAC"} |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.ConfigProviders = void 0; | ||
| const config_service_1 = require("./config.service"); | ||
| const config_constants_1 = require("./config.constants"); | ||
| exports.ConfigProviders = [ | ||
| { | ||
| provide: config_service_1.ConfigService, | ||
| useValue: new config_service_1.ConfigService(`.${process.env.NODE_ENV || 'development'}.env`), | ||
| }, | ||
| { | ||
| provide: config_constants_1.CONFIG_SERVICE_TOKEN, | ||
| useExisting: config_service_1.ConfigService, | ||
| }, | ||
| ]; |
| import { IStringMapper } from './config.interfaces'; | ||
| export declare class ConfigService { | ||
| constructor(filePath?: string); | ||
| get(key: string): string | undefined; | ||
| /** | ||
| * Returns the value of an environment variable, or throws `ConfigMissingError` if it is | ||
| * absent or empty. Modules should use this for variables they strictly require. | ||
| * | ||
| * @example | ||
| * const url = configService.getOrThrow('MONGO_URL'); | ||
| */ | ||
| getOrThrow(key: string): string; | ||
| getConfig(): IStringMapper; | ||
| } | ||
| //# sourceMappingURL=config.service.d.ts.map |
| {"version":3,"file":"config.service.d.ts","sourceRoot":"","sources":["../../../src/config/config.service.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD,qBACa,aAAa;gBACZ,QAAQ,CAAC,EAAE,MAAM;IAW7B,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS;IAIpC;;;;;;OAMG;IACH,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM;IAQ/B,SAAS,IAAI,aAAa;CAG3B"} |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.ConfigService = void 0; | ||
| const tslib_1 = require("tslib"); | ||
| const common_1 = require("@nestjs/common"); | ||
| const dotenv_1 = require("dotenv"); | ||
| const fs_1 = require("fs"); | ||
| const config_missing_error_1 = require("./config-missing.error"); | ||
| let ConfigService = class ConfigService { | ||
| constructor(filePath) { | ||
| try { | ||
| if (filePath && (0, fs_1.existsSync)(filePath)) { | ||
| process.env = { ...process.env, ...(0, dotenv_1.parse)((0, fs_1.readFileSync)(filePath)) }; | ||
| } | ||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| } | ||
| catch (error) { | ||
| /* empty */ | ||
| } | ||
| } | ||
| get(key) { | ||
| return process.env[key]; | ||
| } | ||
| /** | ||
| * Returns the value of an environment variable, or throws `ConfigMissingError` if it is | ||
| * absent or empty. Modules should use this for variables they strictly require. | ||
| * | ||
| * @example | ||
| * const url = configService.getOrThrow('MONGO_URL'); | ||
| */ | ||
| getOrThrow(key) { | ||
| const value = process.env[key]; | ||
| if (!value) { | ||
| throw new config_missing_error_1.ConfigMissingError(key); | ||
| } | ||
| return value; | ||
| } | ||
| getConfig() { | ||
| return process.env; | ||
| } | ||
| }; | ||
| exports.ConfigService = ConfigService; | ||
| exports.ConfigService = ConfigService = tslib_1.__decorate([ | ||
| (0, common_1.Injectable)(), | ||
| tslib_1.__metadata("design:paramtypes", [String]) | ||
| ], ConfigService); |
| export * from './config/config.module'; | ||
| export * from './config/config.service'; | ||
| export * from './config/config.interfaces'; | ||
| export * from './config/config-missing.error'; | ||
| //# sourceMappingURL=index.d.ts.map |
| {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,cAAc,yBAAyB,CAAC;AACxC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,+BAA+B,CAAC"} |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| const tslib_1 = require("tslib"); | ||
| tslib_1.__exportStar(require("./config/config.module"), exports); | ||
| tslib_1.__exportStar(require("./config/config.service"), exports); | ||
| tslib_1.__exportStar(require("./config/config.interfaces"), exports); | ||
| tslib_1.__exportStar(require("./config/config-missing.error"), exports); |
Sorry, the diff of this file is not supported yet
+19
-2
| { | ||
| "name": "@edirect/config", | ||
| "version": "11.0.62", | ||
| "version": "11.0.63", | ||
| "packageScope": "@edirect", | ||
| "main": "./dist/src/index.js", | ||
@@ -23,3 +24,19 @@ "types": "./dist/src/index.d.ts", | ||
| }, | ||
| "type": "commonjs" | ||
| "nx": { | ||
| "name": "@edirect/config", | ||
| "targets": { | ||
| "build": { | ||
| "executor": "@nx/js:tsc", | ||
| "options": { | ||
| "main": "{workspaceRoot}/packages/edirect-config/src/index.ts", | ||
| "tsConfig": "{workspaceRoot}/packages/edirect-config/tsconfig.lib.json", | ||
| "outputPath": "{workspaceRoot}/packages/edirect-config/dist", | ||
| "assets": [ | ||
| "{workspaceRoot}/packages/edirect-config/package.json", | ||
| "{workspaceRoot}/packages/edirect-config/README.md" | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Empty package
Supply chain riskPackage does not contain any code. It may be removed, is name squatting, or the result of a faulty package publish.
15509
290.95%26
1200%141
Infinity%0
-100%8
700%