# nest-feign

> Feign is a nest http decorators library that makes writing nodejs http clients easier.

Latest version **3.7.4** (published 2019-03-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install nest-feign
pnpm add nest-feign
yarn add nest-feign
bun add nest-feign
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.7.4 |
| Published | 2019-03-04 |
| First published | 2018-06-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 118.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Miaowing |
| Maintainers | zfeng |

## Links

- npm: https://www.npmjs.com/package/nest-feign
- Repository: https://github.com/nest-cloud/nest-feign
- Homepage: https://github.com/nest-cloud/nest-feign#readme
- Issues: https://github.com/nest-cloud/nest-feign/issues
- npm.io page: https://npm.io/package/nest-feign

## Dependencies (6)

- [brakes](https://npm.io/package/brakes.md) ^2.6.0
- [uri-params](https://npm.io/package/uri-params.md) ^0.1.3
- [nest-common](https://npm.io/package/nest-common.md) 0.0.1
- [@types/lodash](https://npm.io/package/@types/lodash.md) ^4.14.108
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.12
- [nest-consul-loadbalance](https://npm.io/package/nest-consul-loadbalance.md) ^3.2.1

## Recent versions

- 3.7.4 (latest) — 2019-03-04
- 3.7.3 — 2019-02-28
- 3.7.2 — 2019-02-27
- 3.7.1 — 2019-02-27
- 3.7.0 — 2019-02-14
- 3.6.0 — 2019-02-14
- 3.5.0 — 2018-12-14
- 3.4.2 — 2018-12-05
- 3.4.1 — 2018-11-26
- 3.4.0 — 2018-11-21
- 3.3.0 — 2018-11-01
- 3.2.0 — 2018-11-01
- 3.1.0 — 2018-09-26
- 3.0.0 — 2018-09-10
- 3.0.0-alpha4 — 2018-09-10
- … 15 more at https://npm.io/package/nest-feign/versions

## README

<p align="center">
  <a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo_text.svg" width="320" alt="Nest Logo" /></a>
</p>

## Description

A component of [nestcloud](http://github.com/nest-cloud/nestcloud). NestCloud is a nest framework micro-service solution.
  
[中文文档](https://nestcloud.org/solutions/http-ke-hu-duan)

This is a [Nest](https://github.com/nestjs/nest) module for writing nestjs http clients easier.

## Installation

```bash
$ npm i --save nest-feign nest-consul-loadbalance nest-consul consul
```

## Quick Start

#### Import Module

```typescript
import { Module } from '@nestjs/common';
import { FeignModule, CONSUL_LOADBALANCE } from 'nest-feign';

@Module({
  imports: [FeignModule.register({
    dependencies: [], // If use nest-consul-loadbalance module, please set NEST_CONSUL_LOADBALANCE
    axiosConfig: {},
  })],
})
export class ApplicationModule {}
```

#### Injection

UserClient:

```typescript
import { Injectable } from "@nestjs/common";
import { Loadbalanced, Get, Query, Post, Body, Param, Put, Delete } from "nest-feign";
​
@Injectable()
@Loadbalanced('user-service') // open lb support
export class UserClient {
    @Get('/users')
    getUsers(@Query('role') role: string) {
    }
    
    @Get('http://test.com/users')
    @Loadbalanced(false) // close lb support
    getRemoteUsers() {
    }
    
    @Post('/users')
    createUser(@Body('user') user: any) {
    }
    
    @Put('/users/:userId')
    updateUser(@Param('userId') userId: string, @Body('user') user: any) {
    }
    
    @Delete('/users/:userId')
    deleteUser(@Param('userId') userId: string) {
       
    }
}
```

UserService:

```typescript
export class UserService {
    constructor(private readonly userClient: UserClient) {}
    
    doCreateUser() {
        this.userClient.createUser({name: 'test'});
    }
}
```

## API

### Get\|Post\|Put\|Delete\|Options\|Head\|Patch\|Trace\(uri: string, options?: AxiosRequestConfig\): MethodDecorator

Route decorator.

| field | type | description |
| :--- | :--- | :--- |
| uri | string | the url |
| options | object | axios config，see [axios](https://github.com/axios/axios) |

### Param\|Body\|Query\|Header\(field?: string\): ParameterDecorator

Parameter decorator.

| field | type | description |
| :--- | :--- | :--- |
| field | string | the field name |

### SetHeader\|SetQuery\|SetParam\|SetBody\(field: string, value: any\): MethodDecorator

constant parameter decorator

| field | type | description |
| :--- | :--- | :--- |
| field | string | the field name  |
| value | string \| number \| object | the field value |

### Response\(\): MethodDecorator

If set this decorator, it will return full http response.

### ResponseHeader\(\): MethodDecorator

If set this decorator, it will return response.headers.

### ResponseBody\(\): MethodDecorator

It's a default decorator, it will return response.data.

### ResponseType\(type: string\): MethodDecorator

set response data type, eg: 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream', default 'json'

### ResponseEncode\(type: string\): MethodDecorator

Set response data encode, default 'utf8'

### Loadbalanced\(service: string \| boolean\): ClassDecorator \| MethodDecorator

Open or close lb support.

### Interceptor&lt;T extends IInterceptor&gt;\(interceptor: { new\(\): T }\)

add interceptor，such as：

AddHeaderInterceptor.ts:
```typescript
import { IInterceptor } from "nest-feign";
import { AxiosResponse, AxiosRequestConfig } from 'axios';

export class AddHeaderInterceptor implements IInterceptor {
    onRequest(request: AxiosRequestConfig): AxiosRequestConfig {
        request.headers['x-service'] = 'service-name';
        return request;
    }
    
    onResponse(response: AxiosResponse): AxiosResponse {
        return response;
    }
    
    onRequestError(error: any): any {
        return Promise.reject(error);
    }
    
    onResponseError(error: any): any {
        return Promise.reject(error);
    }
}
```

ArticleClient.ts:
```typescript
import { Injectable } from "@nestjs/common";
import { Get, Interceptor } from "nest-feign";
import { AddHeaderInterceptor } from './AddHeaderInterceptor';

@Injectable()
@Interceptor(AddHeaderInterceptor)
export class ArticleClient {
    @Get('https://api.apiopen.top/recommendPoetry')
    getArticles() {
    }
}
```

interceptor processing：

```typescript
@Interceptor(Interceptor1)
@Interceptor(Interceptor2)
export class Client {

    @Interceptor(Interceptor3)
    @Interceptor(Interceptor4)
    getArticles() {
    }
}
```

console:
```text
interceptor1 request
interceptor2 request
interceptor3 request
interceptor4 request
interceptor4 response
interceptor3 response
interceptor2 response
interceptor1 response
```


## Stay in touch

- Author - [Miaowing](https://github.com/miaowing)

## License

  NestCloud is [MIT licensed](LICENSE).

---
_Source: https://npm.io/package/nest-feign · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
