From 8329148f2942ba89f0a584599914f76c7cfd643b Mon Sep 17 00:00:00 2001 From: Ashish Kumar Date: Mon, 17 Jun 2024 02:41:26 +0530 Subject: [PATCH] chore: added index.ts for better import --- .npmignore | 4 +- package.json | 4 +- .../environment-config.module.d.ts | 4 -- .../environment-config.module.js | 38 -------------- .../environment-config.service.d.ts | 30 ----------- .../environment-config.service.js | 52 ------------------- src/environment-config/prop.decorator.d.ts | 21 -------- src/environment-config/prop.decorator.js | 28 ---------- src/index.ts | 3 ++ 9 files changed, 7 insertions(+), 177 deletions(-) delete mode 100644 src/environment-config/environment-config.module.d.ts delete mode 100644 src/environment-config/environment-config.module.js delete mode 100644 src/environment-config/environment-config.service.d.ts delete mode 100644 src/environment-config/environment-config.service.js delete mode 100644 src/environment-config/prop.decorator.d.ts delete mode 100644 src/environment-config/prop.decorator.js create mode 100644 src/index.ts diff --git a/.npmignore b/.npmignore index d78ca99..a66baf1 100644 --- a/.npmignore +++ b/.npmignore @@ -3,5 +3,5 @@ /src/**/*.test.ts # Exclude test files from dist -/dist/**/*.spec.js -/dist/**/*.test.js +/dist/test +src \ No newline at end of file diff --git a/package.json b/package.json index 2af9222..88658c5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@nodeflip/nest-env-config", - "version": "1.0.0", + "version": "1.0.1", "description": "An environment configuration module for NestJS that allows for easy configuration using decorators.", "main": "dist/index.js", "scripts": { @@ -33,7 +33,7 @@ "author": "Ashish Kumar", "license": "MIT", "publishConfig": { - "registry": "https://registry.npmjs.org/nodeflip" + "registry": "https://registry.npmjs.org" }, "private": false, "repository": "https://github.com/nodeflip/nest-env-config", diff --git a/src/environment-config/environment-config.module.d.ts b/src/environment-config/environment-config.module.d.ts deleted file mode 100644 index 95ef82b..0000000 --- a/src/environment-config/environment-config.module.d.ts +++ /dev/null @@ -1,4 +0,0 @@ -import { DynamicModule } from "@nestjs/common"; -export declare class EnvironmentConfigModule { - static forRoot(configClass: T): DynamicModule; -} diff --git a/src/environment-config/environment-config.module.js b/src/environment-config/environment-config.module.js deleted file mode 100644 index c6e5dbb..0000000 --- a/src/environment-config/environment-config.module.js +++ /dev/null @@ -1,38 +0,0 @@ -"use strict"; -var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { - var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; - if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); - 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; - return c > 3 && r && Object.defineProperty(target, key, r), r; -}; -exports.__esModule = true; -exports.EnvironmentConfigModule = void 0; -var common_1 = require("@nestjs/common"); -var config_1 = require("@nestjs/config"); -var environment_config_service_1 = require("./environment-config.service"); -var EnvironmentConfigModule = /** @class */ (function () { - function EnvironmentConfigModule() { - } - EnvironmentConfigModule_1 = EnvironmentConfigModule; - EnvironmentConfigModule.forRoot = function (configClass) { - var environmentConfigServiceProvider = { - provide: environment_config_service_1.EnvironmentConfigService, - useFactory: function (configService) { - return new environment_config_service_1.EnvironmentConfigService(configService, configClass); - }, - inject: [config_1.ConfigService] - }; - return { - module: EnvironmentConfigModule_1, - imports: [config_1.ConfigModule.forRoot({ isGlobal: true })], - providers: [environmentConfigServiceProvider, config_1.ConfigService], - exports: [environment_config_service_1.EnvironmentConfigService] - }; - }; - var EnvironmentConfigModule_1; - EnvironmentConfigModule = EnvironmentConfigModule_1 = __decorate([ - (0, common_1.Module)({}) - ], EnvironmentConfigModule); - return EnvironmentConfigModule; -}()); -exports.EnvironmentConfigModule = EnvironmentConfigModule; diff --git a/src/environment-config/environment-config.service.d.ts b/src/environment-config/environment-config.service.d.ts deleted file mode 100644 index b7b7d2e..0000000 --- a/src/environment-config/environment-config.service.d.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { ConfigService } from "@nestjs/config"; -import { PropClassType } from "./prop.decorator"; -/** - * A service that provides configuration settings by extracting them from - * the environment variables and mapping them to a specified configuration class. - * - * @template T - The type of the configuration class. - */ -export declare class EnvironmentConfigService { - private readonly configService; - /** - * The configuration object containing the mapped environment variables. - */ - readonly config: T; - /** - * Creates an instance of EnvironmentConfigService. - * - * @param {ConfigService} configService - The ConfigService instance to access environment variables. - * @param {PropClassType} configClass - The configuration class type that defines the properties and their default values. - */ - constructor(configService: ConfigService, configClass: PropClassType); - /** - * Maps the environment variables to the configuration class properties. - * - * @private - * @param {T} configClass - The configuration class instance. - * @returns {T} The configuration object with properties populated from environment variables or their default values. - */ - private getConfigObject; -} diff --git a/src/environment-config/environment-config.service.js b/src/environment-config/environment-config.service.js deleted file mode 100644 index df342fd..0000000 --- a/src/environment-config/environment-config.service.js +++ /dev/null @@ -1,52 +0,0 @@ -"use strict"; -var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { - var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; - if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); - 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; - return c > 3 && r && Object.defineProperty(target, key, r), r; -}; -exports.__esModule = true; -exports.EnvironmentConfigService = void 0; -var common_1 = require("@nestjs/common"); -/** - * A service that provides configuration settings by extracting them from - * the environment variables and mapping them to a specified configuration class. - * - * @template T - The type of the configuration class. - */ -var EnvironmentConfigService = /** @class */ (function () { - /** - * Creates an instance of EnvironmentConfigService. - * - * @param {ConfigService} configService - The ConfigService instance to access environment variables. - * @param {PropClassType} configClass - The configuration class type that defines the properties and their default values. - */ - function EnvironmentConfigService(configService, configClass) { - this.configService = configService; - this.config = this.getConfigObject(configClass); - } - /** - * Maps the environment variables to the configuration class properties. - * - * @private - * @param {T} configClass - The configuration class instance. - * @returns {T} The configuration object with properties populated from environment variables or their default values. - */ - EnvironmentConfigService.prototype.getConfigObject = function (configClass) { - var _this = this; - var configObject = {}; - var cls = configClass; - if (cls["__configProps"]) { - cls["__configProps"].forEach(function (prop) { - configObject[prop.propertyKey] = - _this.configService.get(prop.envName) || prop.defaultValue; - }); - } - return configObject; - }; - EnvironmentConfigService = __decorate([ - (0, common_1.Injectable)() - ], EnvironmentConfigService); - return EnvironmentConfigService; -}()); -exports.EnvironmentConfigService = EnvironmentConfigService; diff --git a/src/environment-config/prop.decorator.d.ts b/src/environment-config/prop.decorator.d.ts deleted file mode 100644 index 6956879..0000000 --- a/src/environment-config/prop.decorator.d.ts +++ /dev/null @@ -1,21 +0,0 @@ -/** - * A decorator function for defining class properties based on environment variables. - * - * This decorator allows you to define class properties that will be populated - * with values from environment variables. If the environment variable is not - * set, an optional default value can be provided. - * - * @param envName - The name of the environment variable to use for this property. - * @param defaultValue - An optional default value to use if the environment variable is not set. - * - * @returns A property decorator function. - */ -export interface ConfigProp { - propertyKey: string; - envName: string; - defaultValue: any; -} -export type PropClassType = T & { - __configProps: ConfigProp[]; -}; -export declare const Prop: (envName: string, defaultValue?: T) => (target: any, propertyKey: string) => void; diff --git a/src/environment-config/prop.decorator.js b/src/environment-config/prop.decorator.js deleted file mode 100644 index 4147da2..0000000 --- a/src/environment-config/prop.decorator.js +++ /dev/null @@ -1,28 +0,0 @@ -"use strict"; -/** - * A decorator function for defining class properties based on environment variables. - * - * This decorator allows you to define class properties that will be populated - * with values from environment variables. If the environment variable is not - * set, an optional default value can be provided. - * - * @param envName - The name of the environment variable to use for this property. - * @param defaultValue - An optional default value to use if the environment variable is not set. - * - * @returns A property decorator function. - */ -exports.__esModule = true; -exports.Prop = void 0; -var Prop = function (envName, defaultValue) { - return function (target, propertyKey) { - if (!target.constructor["__configProps"]) { - target.constructor["__configProps"] = []; - } - target.constructor["__configProps"].push({ - propertyKey: propertyKey, - envName: envName, - defaultValue: defaultValue - }); - }; -}; -exports.Prop = Prop; diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..5774f7b --- /dev/null +++ b/src/index.ts @@ -0,0 +1,3 @@ +export * from "./environment-config/environment-config.module"; +export * from "./environment-config/environment-config.service"; +export * from "./environment-config/prop.decorator";