3.7.4 • Published 5 years ago

nest-feign v3.7.4

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

Description

A component of nestcloud. NestCloud is a nest framework micro-service solution.

中文文档

This is a Nest module for writing nestjs http clients easier.

Installation

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

Quick Start

Import Module

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:

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:

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.

fieldtypedescription
uristringthe url
optionsobjectaxios config,see axios

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

Parameter decorator.

fieldtypedescription
fieldstringthe field name

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

constant parameter decorator

fieldtypedescription
fieldstringthe field name
valuestring | number | objectthe 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<T extends IInterceptor>(interceptor: { new(): T })

add interceptor,such as:

AddHeaderInterceptor.ts:

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:

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:

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

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

console:

interceptor1 request
interceptor2 request
interceptor3 request
interceptor4 request
interceptor4 response
interceptor3 response
interceptor2 response
interceptor1 response

Stay in touch

License

NestCloud is MIT licensed.

3.7.4

5 years ago

3.7.3

5 years ago

3.7.2

5 years ago

3.7.1

5 years ago

3.7.0

5 years ago

3.6.0

5 years ago

3.5.0

5 years ago

3.4.2

5 years ago

3.4.1

5 years ago

3.4.0

5 years ago

3.3.0

6 years ago

3.2.0

6 years ago

3.1.0

6 years ago

3.0.0

6 years ago

3.0.0-alpha4

6 years ago

3.0.0-alpha3

6 years ago

3.0.0-alpha2

6 years ago

3.0.0-alpha1

6 years ago

2.0.0

6 years ago

1.6.0

6 years ago

1.5.0

6 years ago

1.4.0

6 years ago

1.3.0

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago

0.4.0

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago