nestcloud icon indicating copy to clipboard operation
nestcloud copied to clipboard

请问如何在getServiceNodeInfo函数中获得Post返回的数据做二次加工,同时对Post传递参数,在返回到controller中

Open xietianbao80 opened this issue 4 years ago • 1 comments

import { Injectable, Body, Inject } from "@nestjs/common";
import { Loadbalanced } from "@nestcloud/loadbalance";
import { Get, ost } from "@nestcloud/feign";
// import { AXIOS_INSTANCE_PROVIDER } from "@nestcloud/http/http.constants";

@Injectable()
// enable loadbalance supports, need import @nestcloud/loadbalance module at first.
@Loadbalanced("service-test")
export class ServiceNodeClient {
  // constructor(@Inject(AXIOS_INSTANCE_PROVIDER) private readonly httpService: HttpClient) {}
  // constructor() {}

  @Post("/service-test/get-service-test-info-by-caller-test")
  async getServiceNodeInfo() {}

  @Get("http://test.com/users")
  // disable loadbalance supports.
  @Loadbalanced(false)
  getRemoteUsers() {}
}

请问如何在getServiceNodeInfo函数中获得Post返回的数据做二次加工,在返回到controller中,同时对Post传递参数

xietianbao80 avatar Sep 02 '20 16:09 xietianbao80

可以使用 Interceptor 对数据进行处理:

import { Injectable } from '@nestjs/common';
import { Interceptor } from '@nestcloud/http';
import { AxiosResponse } from 'axios';

@Injectable()
export class TestInterceptor implements Interceptor {
  onResponse(response: AxiosResponse): AxiosResponse {
    response.data = { message: 'hello' };
    return response;
  }
}

import { Injectable } from '@nestjs/common';
import { Loadbalanced } from '@nestcloud/loadbalance';
import { Get, Post, UseInterceptors } from '@nestcloud/http';
import { TestInterceptor } from './TestInterceptor';

@Injectable()
@Loadbalanced('service-test')
export class ServiceNodeClient {
  @Post('/service-test/get-service-test-info-by-caller-test')
  @UseInterceptors(TestInterceptor)
  async getServiceNodeInfo() {
  }

  @Get('http://test.com/users')
  @Loadbalanced(false)
  getRemoteUsers() {
  }
}

miaowing avatar Sep 07 '20 08:09 miaowing