nestjs intercept and modify outgoing http request

允我心安 提交于 2019-12-24 11:42:49

问题


So I'm likely missing something or doing something wrong. I have a NestJS application that is trying to make an http request to an external API. I'd like to be able to intercept this outgoing request and modify headers on it before executing it.

I've tried using Interceptors to no avail, the incoming http requests get intercepted but not the outgoing. Any suggestions or help would be greatly appreciated.


回答1:


Let's first deal with

I've tried using Interceptors to no avail, the incoming http requests get intercepted but not the outgoing.

According to the documentation https://docs.nestjs.com/interceptors it should be totally possible to intercept the response.

@Injectable()
export class TransformHeadersInterceptor implements NestInterceptor {
  intercept(
    context: ExecutionContext,
    call$: Observable<any>,
  ): Observable<any> {
    // Get request headers, e.g.
    const userAgent = context.switchToHttp().getRequest().headers['user-agent'];

    // Not sure if headers are writeable like this, give it a try
    context.switchToHttp().getResponse().headers['x-api-key'] = 'pretty secure';

    return call$;
  }
}

If you want to manipulate headers based on the the response data. You could tap into the data like so:

return call$.pipe(map(data => {
    // Your code here
    return data;
}));

I have some thoughts on:

I have a NestJS application that is trying to make an http request to an external API. I'd like to be able to intercept this outgoing request and modify headers on it before executing it.

So I think there two use cases. First, you have a set of default headers, that are assigned to the http client initially and send with each request. E.g.:

import { HTTP_TOKEN } from './constants';
import * as http from 'request-promise-native';

export const httpProviders: any = [
  {
    provide: HTTP_TOKEN,
    useFactory: () => {
      return http.defaults({
        headers: {
          'Accept': 'application/json',
          'Content-type': 'application/json',
          'User-agent': 'my-🍪-app',
        },
      });
    },
  },
];

And second, you create and assign headers per request. This is when you use interceptors. In the context of authentication, you could think about using a guard, like tano suggests in his answer.




回答2:


I had a similar problem modifying / adding response headers. Following code worked for me:

@Injectable()
export class TransformHeadersInterceptor implements NestInterceptor {
  intercept(
    context: ExecutionContext,
    call$: Observable<any>,
  ): Observable<any> {

    return call$.pipe(
            map((data) => {
                // pipe call to add / modify header(s) after remote method
                let req = context.switchToHttp().getRequest();
                req.res.header('x-api-key', 'pretty secure');
                return data;
            }),
        );
  }
}



回答3:


I can give an example in which I use external api calls:

import { PaginateModel, PaginateResult, Document } from 'mongoose';
import { AxiosInstance } from 'axios';
import { UseGuards, InternalServerErrorException, Injectable } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { Context } from './decorators/ctx.decorator';

@Injectable()
@UseGuards(AuthGuard('jwt'))
export abstract class ServiceBase<T extends Document> {
    protected abstract readonly path: string;

    constructor(protected readonly externals: Object, protected readonly model: PaginateModel<T>) {}

    async create(data: T, ctx: Context): Promise<T> {
        try {
            this.validate(data);
            const { lng, core } = this.separate(data);
            const catalog = new this.model(core);
            const head = await catalog.save();
            Object.assign(head, lng);
            const Authorization = ctx.token;
            const axios: AxiosInstance = this.externals[ctx.lang];
            try {
                const resp = await axios.post(`${this.path}`, head, { headers: { Authorization } });
                return resp.data;
            } catch (err) {
                // in case of any error the head record should be removed.
                catalog.remove();
                throw err;
            }
        } catch (err) {
            console.log(err);
            throw new InternalServerErrorException(err);
        }
    }

    abstract async validate(data: T): Promise<any>;

    abstract separate(data: T);

    async update(id: string, data: T, ctx: Context): Promise<T> {
        try {
            const curr = await this.model.findById(id).exec();
            const { lng, core } = this.separate(data);
            Object.assign(curr, core);
            await curr.save();
            Object.assign(core, lng);
            const Authorization = ctx.token;
            const axios: AxiosInstance = this.externals[ctx.lang];
            const resp = await axios.put(`${this.path}/${id}`, core, { headers: { Authorization } });
            return resp.data;
        } catch (err) {
            throw new InternalServerErrorException(err);
        }
    }

    async get(id: string, ctx: Context): Promise<T> {
        try {
            const Authorization = ctx.token;
            const axios: AxiosInstance = this.externals[ctx.lang];
            const resp = await axios.get(`${this.path}/${id}`, { headers: { Authorization } });
            return resp.data;
        } catch (err) {
            console.log(err);
            return null;
        }
    }

    async findOne(query: object): Promise<T> {
        const data = await this.model.findOne(query, { _class: 0 }).exec();
        return data;
    }

    async findAll(ctx: Context): Promise<T[]> {
        try {
            const Authorization = ctx.token;
            const axios: AxiosInstance = this.externals[ctx.lang];
            const resp = await axios.get(`${this.path}`, {
                headers: { Authorization },
            });
            return resp.data;
        } catch (err) {
            console.log(err);
            return null;
        }
    }

    async find(query: {} = {}, page: number, rows: number, ctx: Context): Promise<PaginateResult<T>> {
        try {
            const Authorization = ctx.token;
            const axios: AxiosInstance = this.externals[ctx.lang];
            const resp = await axios.get(`${this.path}`, {
                params: { page, rows },
                headers: { Authorization },
            });
            return resp.data;
        } catch (err) {
            console.log(err);
            return null;
        }
    }
}

where externals is:

import axios, { AxiosInstance } from 'axios';

const config = require('../../config/settings.json');

export const externalProviders = {
    provide: 'ExternalToken',
    useFactory: () => {
        const externals = {};
        for (const lang in config.externals) {
            externals[lang] = axios.create({
                baseURL: config.externals[lang],
            });
        }
        return externals;
    }
};


来源:https://stackoverflow.com/questions/52334277/nestjs-intercept-and-modify-outgoing-http-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!