{"id":18519254,"url":"https://github.com/alaricode/nestjs-consul","last_synced_at":"2025-04-09T08:32:09.205Z","repository":{"id":40712679,"uuid":"281999990","full_name":"AlariCode/nestjs-consul","owner":"AlariCode","description":"NestJS consul package allows you to load configs from consul and work with key/values.","archived":false,"fork":false,"pushed_at":"2023-11-23T17:42:24.000Z","size":720,"stargazers_count":12,"open_issues_count":5,"forks_count":6,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-24T04:05:02.127Z","etag":null,"topics":["consul","javascript","nestjs","nodejs","typescript"],"latest_commit_sha":null,"homepage":"https://purpleschool.ru","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AlariCode.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-07-23T16:17:13.000Z","updated_at":"2024-07-03T20:14:38.000Z","dependencies_parsed_at":"2023-02-06T01:46:00.312Z","dependency_job_id":null,"html_url":"https://github.com/AlariCode/nestjs-consul","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlariCode%2Fnestjs-consul","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlariCode%2Fnestjs-consul/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlariCode%2Fnestjs-consul/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlariCode%2Fnestjs-consul/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AlariCode","download_url":"https://codeload.github.com/AlariCode/nestjs-consul/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248003292,"owners_count":21031765,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["consul","javascript","nestjs","nodejs","typescript"],"created_at":"2024-11-06T17:15:48.611Z","updated_at":"2025-04-09T08:32:08.784Z","avatar_url":"https://github.com/AlariCode.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# NestJS consul config Module\n\n![alt cover](https://github.com/AlariCode/nestjs-consul/raw/master/img/logo.jpg)\n\n**More NestJS libs on [alariblog.ru](https://purpleschool.ru)**\n**Updated for NestJS 9**\n\n[![npm version](https://badgen.net/npm/v/nestjs-consul)](https://www.npmjs.com/package/nestjs-dotenv)\n[![npm version](https://badgen.net/npm/license/nestjs-consul)](https://www.npmjs.com/package/nestjs-dotenv)\n[![npm version](https://badgen.net/github/open-issues/AlariCode/nestjs-consul)](https://github.com/AlariCode/nestjs-dotenv/issues)\n[![npm version](https://badgen.net/github/prs/AlariCode/nestjs-consul)](https://github.com/AlariCode/nestjs-dotenv/pulls)\n\nNestJS consul package allows you to load configs from consul and work with key/values.\n\n```bash\nnpm i nestjs-consul\n```\n\nThen register module in your root app.module\n\n```javascript\nimport { ConsulModule } from 'nestjs-consul';\n\n@Module({\n\timports: [\n\t\t// ...\n\t\tConsulModule.forRoot \u003c\n\t\t\tYourConfig \u003e\n\t\t\t{\n\t\t\t\tkeys: [{ key: 'your/keypath' }],\n\t\t\t\tupdateCron: '* * * * *',\n\t\t\t\tconnection: {\n\t\t\t\t\tprotocol: 'http',\n\t\t\t\t\tport: 8500,\n\t\t\t\t\thost: '192.168.0.1',\n\t\t\t\t\ttoken: 'mutoken',\n\t\t\t\t},\n\t\t\t},\n\t],\n})\nexport class AppModule {}\n```\n\nIf you want to inject dependency into `forRoot` method like Configuration service, use `forRootAsync`:\n\n```javascript\nimport { ConsulModule } from 'nestjs-consul';\n\n@Module({\n\timports: [\n\t\t// ...\n\t\tConfigModule,\n\t\tConsulModule.forRootAsync({\n\t\t\timports: [ConfigModule],\n\t\t\tinject: [ConfigService],\n\t\t\tuseFactory: async (configService: ConfigService) =\u003e {\n\t\t\t\treturn {\n\t\t\t\t\tkeys: [{ key: 'am-cli/test' }],\n\t\t\t\t\tconnection: {\n\t\t\t\t\t\tprotocol: 'http',\n\t\t\t\t\t\tport: configService.getIP(),\n\t\t\t\t\t\thost: '192.168.0.1',\n\t\t\t\t\t\ttoken: 'admin',\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t},\n\t\t}),\n\t],\n})\nexport class AppModule {}\n```\n\n-   **keys** (IConsulKeys[]?) - array of keys and required status from which you want to load values. If key was not found and it was required, app with throw an exception. If it was not required - you will see warning. If no keys specified, no initial configs will be loaded.\n-   **updateCron** (string) - cron string. If specified, will update configs on cron.\n-   **protocol** ('http' | 'https') - consul protocol.\n-   **protocol** ('http' | 'https') - consul protocol.\n-   **port** (number) - consul port.\n-   **host** (string) - consul host.\n-   **token** (string) - consul auth token.\n-   **YourConfig** (interface) - interface, that describes you entire config. This will allow you to use type save configs. In this example it would be like:\n\n```javascript\nexport interface YourConfig {\n\t'your/keypath': {\n\t\tvalue1: number,\n\t\tvalue2: string,\n\t};\n}\n```\n\nThen you can use your configs with public property `configs` of `ConsuleService`.\n\n```javascript\nimport { ConsuleService } from 'nestjs-consul';\n\nconstructor(\n\tprivate readonly consul: ConsuleService\n) {}\n\nmyMethod() {\n    const configs = this.consul.configs;\n}\n\n```\n\n## Additional methods\n\n### update()\n\nReturns `Promise\u003cvoid\u003e`. Force updates your config.\n\n```javascript\nawait this.consul.update();\n```\n\n### get\u003cT\u003e(key: string)\n\nReturns `Promise\u003cT\u003e`. Gets a value from consul with `key` and `T` type.\n\n```javascript\nconst value = (await this.consul.get) \u003c MyInterface \u003e 'my/key';\n```\n\n### set\u003cT\u003e(key: string, value: T)\n\nReturns `Promise\u003cboolean\u003e`. Sets a value to consul with `key` and `T` type. Returns `true` if succeeded.\n\n```javascript\nconst response = (await this.consul.set) \u003c MyInterface \u003e ('my/key', value);\n```\n\n### delete(key: string)\n\nReturns `Promise\u003cboolean\u003e`. Deletes a value from consul with `key`. Returns `true` if succeeded.\n\n```javascript\nconst response = await this.consul.delete('my/key');\n```\n\n## Running tests\n\nTo run e2e tests you need to have consul instance started and run test for the firs time (to add config key).\n\nThen run tests with.\n\n```\nnpm run test\n```\n\n![alt cover](https://github.com/AlariCode/nestjs-consul/raw/master/img/tests.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falaricode%2Fnestjs-consul","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falaricode%2Fnestjs-consul","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falaricode%2Fnestjs-consul/lists"}