{"id":20850037,"url":"https://github.com/cristobalgvera/nestjs-environment","last_synced_at":"2026-05-01T03:38:45.249Z","repository":{"id":163458549,"uuid":"638930756","full_name":"cristobalgvera/nestjs-environment","owner":"cristobalgvera","description":"Environment module configuration library that allows you to easily setup and validate your environment with minimal configuration.","archived":false,"fork":false,"pushed_at":"2023-06-21T02:14:26.000Z","size":203,"stargazers_count":0,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-26T15:31:37.583Z","etag":null,"topics":["config","environment","library","nestjs"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cristobalgvera.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-05-10T12:07:16.000Z","updated_at":"2023-05-13T04:00:39.000Z","dependencies_parsed_at":null,"dependency_job_id":"9447285b-a337-446f-a2ba-239b4e31f93b","html_url":"https://github.com/cristobalgvera/nestjs-environment","commit_stats":{"total_commits":55,"total_committers":2,"mean_commits":27.5,"dds":0.1454545454545455,"last_synced_commit":"41b07936a2ce2eace2233c66be97118a2b9ed09f"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/cristobalgvera/nestjs-environment","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cristobalgvera%2Fnestjs-environment","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cristobalgvera%2Fnestjs-environment/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cristobalgvera%2Fnestjs-environment/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cristobalgvera%2Fnestjs-environment/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cristobalgvera","download_url":"https://codeload.github.com/cristobalgvera/nestjs-environment/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cristobalgvera%2Fnestjs-environment/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32484352,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-30T13:12:12.517Z","status":"online","status_checked_at":"2026-05-01T02:00:05.856Z","response_time":64,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["config","environment","library","nestjs"],"created_at":"2024-11-18T03:07:47.354Z","updated_at":"2026-05-01T03:38:45.235Z","avatar_url":"https://github.com/cristobalgvera.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# NestJS Environment\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"http://nestjs.com/\" target=\"blank\"\u003e\n    \u003cimg src=\"https://nestjs.com/img/logo-small.svg\" width=\"200\" alt=\"Nest Logo\" /\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\nEnvironment module configuration library that allows you to easily setup\nand validate your environment with minimal configuration.\n\n- Type safe environment usage.\n- Complex environments variables (arrays and objects) validation.\n\n## Installation\n\n```bash\nnpm install @cristobalgvera/nestjs-environment\n```\n\nBy the moment, the unique accepted source of validation is `Joi` (there are\nplans to expand to any source of validation like `Zod`), so, you have to\ninstall it too.\n\n```bash\nnpm install joi\n```\n\n## Usage\n\nThe most basic usage possible is the following:\n\n1. Create the `Environment` class. It needs to implements `BaseEnvironment`.\n\n   ```ts\n   // environment/environment.model.ts\n\n   import { BaseEnvironment } from '@cristobalgvera/nestjs-environment';\n\n   export class Environment implements BaseEnvironment {\n     NODE_ENV: string; // or 'development' | 'test' | 'production'\n   }\n   ```\n\n1. Create a validation schema for that `Environment` using `Joi`. Remember to\n   create the test file too.\n\n   ```ts\n   // environment/environment.schema.ts\n\n   import * as Joi from 'joi';\n   import { Environment } from './environment.model';\n\n   export const environmentSchema = Joi.object\u003cEnvironment, true\u003e({\n     NODE_ENV: Joi.string().default('development'),\n   });\n   ```\n\n1. Import the `EnvironmentModule` using the `forRoot` static method and provide\n   the `Environment` class and the validation schema.\n\n   ```ts\n   // app.module.ts\n\n   import { EnvironmentModule } from '@cristobalgvera/nestjs-environment';\n   import { Module } from '@nestjs/common';\n   import { Environment, environmentSchema } from './environment';\n\n   @Module({\n     imports: [\n       EnvironmentModule.forRoot({\n         environmentClass: Environment,\n         validationSchema: environmentSchema,\n       }),\n     ],\n   })\n   export class AppModule {}\n   ```\n\n1. Use the `EnvironmentService` in any place you want through DI. You need\n   to provide as generic parameter the `Environment` class previously created\n   and it will be type-safe.\n\n   ```ts\n   // test.service.ts\n\n   import { EnvironmentService } from '@cristobalgvera/nestjs-environment';\n   import { Injectable } from '@nestjs/common';\n   import { Environment } from './environment';\n\n   @Injectable()\n   export class TestService {\n     constructor(\n       private readonly environmentService: EnvironmentService\u003cEnvironment\u003e,\n     ) {}\n\n     getNodeEnvironment(): string {\n       const NODE_ENV = this.environmentService.get('NODE_ENV');\n       //        ^? const NODE_ENV: string\n       return NODE_ENV;\n     }\n   }\n   ```\n\n1. Finally, to test it, simply mock it.\n\n   ```ts\n   // test.service.spec.ts\n\n   import { TestBed } from '@automock/jest';\n   import { EnvironmentService } from '@cristobalgvera/nestjs-environment';\n   import { Environment } from './environment';\n   import { TestService } from './test.service';\n\n   describe('TestService', () =\u003e {\n     let underTest: TestService;\n     let environmentService: EnvironmentService\u003cEnvironment\u003e;\n\n     beforeEach(() =\u003e {\n       const { unit, unitRef } = TestBed.create(TestService).compile();\n\n       underTest = unit;\n       environmentService = unitRef.get(EnvironmentService);\n     });\n\n     describe('getNodeEnvironment', () =\u003e {\n       const environment = {\n         NODE_ENV: 'development',\n       } as Readonly\u003cEnvironment\u003e;\n\n       beforeEach(() =\u003e {\n         jest\n           .spyOn(environmentService, 'get')\n           .mockImplementation((key) =\u003e environment[key]);\n       });\n\n       it('should return the node environment', () =\u003e {\n         const expected = environment.NODE_ENV;\n\n         const actual = underTest.getNodeEnvironment();\n\n         expect(actual).toEqual(expected);\n       });\n     });\n   });\n   ```\n\n### Advance usage\n\nFollowing the same structure described above, you can create complex\nenvironment variables that can be validated. This complex types can be\narrays or custom objects that has an inner validation too.\n\nThe unique change you have to do it the following:\n\n1. Add the `ParseEnvironment` decorator to the complex types.\n\n   ```ts\n   // environment/environment.model.ts\n\n   import {\n     BaseEnvironment,\n     ParseEnvironment,\n   } from '@cristobalgvera/nestjs-environment';\n   import { User } from './user.model';\n\n   export class Environment implements BaseEnvironment {\n     NODE_ENV: 'development' | 'test' | 'production';\n     PORT: number;\n     IS_SWAGGER_ENABLED: boolean;\n\n     @ParseEnvironment() // \u003c-- Use this to parse arrays of primitive values\n     ALLOWED_IPS: string[];\n\n     @ParseEnvironment({ toClass: true }) // \u003c-- Use this to parse classes\n     INTERNAL_USER: User;\n\n     @ParseEnvironment({ toClass: true }) // \u003c-- Use this to parse arrays of classes\n     EXTERNAL_USERS: User[];\n   }\n   ```\n\n1. Create the validation schema. Note the usage of a `userSchema` to validate\n   the users. This way you can easily test each schema separately.\n\n   ```ts\n   // environment/environment.schema.ts\n\n   import * as Joi from 'joi';\n   import { Environment } from './environment.model';\n   import { userSchema } from './user.schema'; // Users can have their own schema\n\n   export const environmentSchema = Joi.object\u003cEnvironment, true\u003e({\n     NODE_ENV: Joi.string()\n       .valid('development', 'test', 'production')\n       .default('development'),\n     PORT: Joi.number().port().default(8080),\n     IS_SWAGGER_ENABLED: Joi.boolean().default(true),\n     ALLOWED_IPS: Joi.array().items(Joi.string().ip()).required(),\n     INTERNAL_USER: userSchema.required(),\n     EXTERNAL_USERS: Joi.array().items(userSchema).required(),\n   });\n   ```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcristobalgvera%2Fnestjs-environment","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcristobalgvera%2Fnestjs-environment","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcristobalgvera%2Fnestjs-environment/lists"}