This is a Nest module for writing nestjs http clients easier.
$ npm i --save @nestcloud/http
import { Module } from '@nestjs/common';
import { HttpModule } from '@nestcloud/http';
@Module({
imports: [
HttpModule.forRoot(),
],
})
export class AppModule {
}
http:
axios:
timeout: 1000
import { Injectable } from "@nestjs/common";
import { Get, Query, Post, Body, Param, Put, Delete } from "@nestcloud/http";
@Injectable()
export class UserClient {
@Get('http://test.com/users')
getUsers(@Query('role') role: string) {
}
@Post('http://test.com/users')
createUser(@Body('user') user: any) {
}
@Put('http://test.com/users/:userId')
updateUser(@Param('userId') userId: string, @Body('user') user: any) {
}
@Delete('http://test.com/users/:userId')
deleteUser(@Param('userId') userId: string) {
}
}
import { Injectable } from "@nestjs/common";
import { Loadbalanced, Get, Query } from "@nestcloud/http";
@Injectable()
// enable loadbalance supports, need import @nestcloud/loadbalance module at first.
@Loadbalanced('user-service')
export class UserClient {
@Get('/users')
getUsers(@Query('role') role: string) {
}
@Get('http://test.com/users')
// disable loadbalance supports.
@Loadbalanced(false)
getRemoteUsers() {
}
}
import { Injectable } from '@nestjs/common';
import { Interceptor } from "@nestcloud/http";
import { AxiosResponse, AxiosRequestConfig } from 'axios';
@Injectable()
export class AddHeaderInterceptor implements Interceptor {
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);
}
}
import { Injectable } from "@nestjs/common";
import { Get, UseInterceptors } from "@nestcloud/http";
import { AddHeaderInterceptor } from "./middlewares/AddHeaderInterceptor";
@Injectable()
@UseInterceptors(AddHeaderInterceptor)
export class ArticleClient {
@Get('https://api.apiopen.top/recommendPoetry')
getArticles() {
}
}
examples:
@UseInterceptors(Interceptor1)
@UseInterceptors(Interceptor2)
export class Client {
@UseInterceptors(Interceptor3)
@UseInterceptors(Interceptor4)
getArticles() {
}
}
result:
interceptor1 request
interceptor2 request
interceptor3 request
interceptor4 request
interceptor4 response
interceptor3 response
interceptor2 response
interceptor1 response
Import @nestcloud/brakes
module at first.
import { Module } from '@nestjs/common';
import { BrakesModule } from '@nestcloud/brakes';
@Module({
imports: [
BrakesModule.forRoot(),
],
})
export class AppModule {
}
Write a fallback class.
import { Fallback } from '@nestcloud/brakes';
import { BadRequestException, Injectable } from '@nestjs/common';
@Injectable()
export class UserFallback implements Fallback {
config() {
return {};
}
fallback(...params) {
throw new BadRequestException('fallback invoke');
}
healthCheck() {
}
}
Use fallback.
import { Injectable } from '@nestjs/common';
import { Get } from '@nestcloud/http';
import { UseFallback } from '@nestcloud/brakes';
import { UserFallback } from './UserFallback';
@Injectable()
@UseFallback(UserFallback)
export class UserClient {
@Get('/users')
getUsers(): any {
}
}
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 |
Parameter decorator.
field | type | description |
---|---|---|
field | string | the field name |
constant parameter decorator
field | type | description |
---|---|---|
field | string | the field name |
value | any | the field value |
If set this decorator, it will return full http response.
If set this decorator, it will return response.headers.
It's a default decorator, it will return response.data.
set response data type, eg: 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream', default 'json'
Set response data encode, default 'utf8'
Open or close lb support.
Use interceptor, supports dynamic import and inject.
- Author - NestCloud
NestCloud is MIT licensed.