8
votes

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.

3
if you call an external api you should use http/axios or something and you create a client with headers. This call is independent from nestjs. Personally I use axios client and nestjs cannot bind interceptors because it does not know anything about the inner logic. - tano
This answer worked for me - myol

3 Answers

7
votes

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.

7
votes

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;
            }),
        );
  }
}
0
votes

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;
    }
};