{"id":19774351,"url":"https://github.com/vinks/nest-feign","last_synced_at":"2025-04-30T18:33:05.868Z","repository":{"id":41391613,"uuid":"168360928","full_name":"vinks/nest-feign","owner":"vinks","description":"Feign is a nest http decorators library that makes writing nodejs http clients easier.","archived":false,"fork":false,"pushed_at":"2018-12-19T03:54:18.000Z","size":95,"stargazers_count":2,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2023-03-12T06:28:06.483Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":false,"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/vinks.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-01-30T14:57:42.000Z","updated_at":"2023-02-16T01:22:26.000Z","dependencies_parsed_at":"2022-09-06T14:52:07.753Z","dependency_job_id":null,"html_url":"https://github.com/vinks/nest-feign","commit_stats":null,"previous_names":[],"tags_count":null,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vinks%2Fnest-feign","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vinks%2Fnest-feign/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vinks%2Fnest-feign/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vinks%2Fnest-feign/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vinks","download_url":"https://codeload.github.com/vinks/nest-feign/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224219538,"owners_count":17275477,"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":[],"created_at":"2024-11-12T05:12:42.090Z","updated_at":"2024-11-12T05:12:42.713Z","avatar_url":"https://github.com/vinks.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003ca href=\"http://nestjs.com/\" target=\"blank\"\u003e\u003cimg src=\"https://nestjs.com/img/logo_text.svg\" width=\"320\" alt=\"Nest Logo\" /\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n## Description\n\nA component of [nestcloud](http://github.com/nest-cloud/nestcloud). NestCloud is a nest framework micro-service solution.\n  \n[中文文档](https://nestcloud.org/solutions/http-ke-hu-duan)\n\nThis is a [Nest](https://github.com/nestjs/nest) module for writing nestjs http clients easier.\n\n## Installation\n\n```bash\n$ npm i --save nest-feign nest-consul-loadbalance nest-consul consul\n```\n\n## Quick Start\n\n#### Import Module\n\n```typescript\nimport { Module } from '@nestjs/common';\nimport { FeignModule, CONSUL_LOADBALANCE } from 'nest-feign';\n\n@Module({\n  imports: [FeignModule.register({\n    adapter: '', // If use nest-consul-loadbalance module, please set CONSUL_LOADBALANCE\n    axiosConfig: {},\n  })],\n})\nexport class ApplicationModule {}\n```\n\n#### Injection\n\nUserClient:\n\n```typescript\nimport { Injectable } from \"@nestjs/common\";\nimport { Loadbalanced, Get, Query, Post, Body, Param, Put, Delete } from \"nest-feign\";\n​\n@Injectable()\n@Loadbalanced('user-service') // open lb support\nexport class UserClient {\n    @Get('/users')\n    getUsers(@Query('role') role: string) {\n    }\n    \n    @Get('http://test.com/users')\n    @Loadbalanced(false) // close lb support\n    getRemoteUsers() {\n    }\n    \n    @Post('/users')\n    createUser(@Body('user') user: any) {\n    }\n    \n    @Put('/users/:userId')\n    updateUser(@Param('userId') userId: string, @Body('user') user: any) {\n    }\n    \n    @Delete('/users/:userId')\n    deleteUser(@Param('userId') userId: string) {\n       \n    }\n}\n```\n\nUserService:\n\n```typescript\nexport class UserService {\n    constructor(private readonly userClient: UserClient) {}\n    \n    doCreateUser() {\n        this.userClient.createUser({name: 'test'});\n    }\n}\n```\n\n## API\n\n### Get\\|Post\\|Put\\|Delete\\|Options\\|Head\\|Patch\\|Trace\\(uri: string, options?: AxiosRequestConfig\\): MethodDecorator\n\nRoute decorator.\n\n| field | type | description |\n| :--- | :--- | :--- |\n| uri | string | the url |\n| options | object | axios config，see [axios](https://github.com/axios/axios) |\n\n### Param\\|Body\\|Query\\|Header\\(field?: string\\): ParameterDecorator\n\nParameter decorator.\n\n| field | type | description |\n| :--- | :--- | :--- |\n| field | string | the field name |\n\n### SetHeader\\|SetQuery\\|SetParam\\|SetBody\\(field: string, value: any\\): MethodDecorator\n\nconstant parameter decorator\n\n| field | type | description |\n| :--- | :--- | :--- |\n| field | string | the field name  |\n| value | string \\| number \\| object | the field value |\n\n### Response\\(\\): MethodDecorator\n\nIf set this decorator, it will return full http response.\n\n### ResponseHeader\\(\\): MethodDecorator\n\nIf set this decorator, it will return response.headers.\n\n### ResponseBody\\(\\): MethodDecorator\n\nIt's a default decorator, it will return response.data.\n\n### ResponseType\\(type: string\\): MethodDecorator\n\nset response data type, eg: 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream', default 'json'\n\n### ResponseEncode\\(type: string\\): MethodDecorator\n\nSet response data encode, default 'utf8'\n\n### Loadbalanced\\(service: string \\| boolean\\): ClassDecorator \\| MethodDecorator\n\nOpen or close lb support.\n\n### Middleware\u0026lt;T extends IMiddleware\u0026gt;\\(middleware: { new\\(\\): T }\\)\n\nadd middleware，such as：\n\nAddHeaderMiddleware.ts:\n```typescript\nimport { IMiddleware } from \"nest-feign\";\nimport { AxiosResponse, AxiosRequestConfig } from 'axios';\n\nexport class AddHeaderMiddleware implements IMiddleware {\n    send(request: AxiosRequestConfig): (response: AxiosResponse) =\u003e void {\n        request.headers['x-service'] = 'service-name';\n        return function (response: AxiosResponse) {\n            console.log(response.data);\n        };\n    }\n}\n```\n\nArticleClient.ts:\n```typescript\nimport { Injectable } from \"@nestjs/common\";\nimport { Get, Middleware } from \"nest-feign\";\nimport { AddHeaderMiddleware } from \"./middlewares/AddHeaderMiddleware\";\n\n@Injectable()\n@Middleware(AddHeaderMiddleware)\nexport class ArticleClient {\n    @Get('https://api.apiopen.top/recommendPoetry')\n    getArticles() {\n    }\n}\n```\n\nmiddleware processing：\n\n```typescript\n@Middleware(Middleware1)\n@Middleware(Middleware2)\nexport class Client {\n\n    @Middleware(Middleware3)\n    @Middleware(Middleware4)\n    getArticles() {\n    }\n}\n```\n\nconsole:\n```text\nmiddleware4 request\nmiddleware3 request\nmiddleware2 request\nmiddleware1 request\nmiddleware1 response\nmiddleware2 response\nmiddleware3 response\nmiddleware4 response\n```\n\n\n## Stay in touch\n\n- Author - [Miaowing](https://github.com/miaowing)\n\n## License\n\n  Nest is [MIT licensed](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvinks%2Fnest-feign","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvinks%2Fnest-feign","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvinks%2Fnest-feign/lists"}