You can intercept the request and response using NestJS middleware to inspect the data from a third-party API in between the controller and the request.
Here is an example of how you can implement this:
Middleware
interface and overrides the use()
method:import { Injectable, NestMiddleware } from '@nestjs/common';
import { Request, Response } from 'express';
@Injectable()
export class ApiLoggerMiddleware implements NestMiddleware {
use(req: Request, res: Response, next: () => void) {
console.log(`Requesting API: ${req.originalUrl}`);
console.log(`Request body: ${JSON.stringify(req.body)}`);
console.log(`Request query parameters: ${JSON.stringify(req.query)}`);
const originalSend = res.send;
res.send = function bodyWrapper(body) {
console.log(`API Response Body: ${JSON.stringify(body)}`);
originalSend.call(this, body);
};
next();
}
}
app.module.ts
file:import { Module, NestModule, MiddlewareConsumer } from '@nestjs/common';
import { ApiLoggerMiddleware } from './api.logger.middleware';
@Module({
imports: [/*your modules here*/],
controllers: [/*your controllers here*/],
})
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(ApiLoggerMiddleware).forRoutes('*');
}
}
You can customize the middleware class to log the specific data that you need to inspect from the third-party API.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-04-12 11:00:00 +0000
Seen: 13 times
Last updated: Apr 25 '21
How can a controller be configured to have both secure and open actions for a firewall?
Why is `App\Http\Controllers\PostController` not able to be invoked?
What is the reason for too many redirects occurring in nginx-ingress when force-ssl is turned on?
What does the "429 error" in the request response of Instabot mean?
Can migration files be executed in knex upon making an API request?
What is the process of performing a GET request while using OAuth1 authentication?
What does it mean when API Gateway gives a 503 Service Unavailable error?