Creating a service class instance within an interceptor in NestJS

When working with interceptors in NestJS (view documentation), I encountered a situation where I needed to call a service within the interceptor. Here is the approach I took:

export class HttpInterceptor implements NestInterceptor {
    constructor(private configService:ConfigService){}
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    let request = context.switchToHttp().getRequest();
    const apikey= this.configService.get('apikey');
    const hash=this.configService.get('hash');
    request.params= {apikey:apikey ,hash:hash,ts:Date.now()}
    return next
  }
}

This is how the ConfigService looks like:

export class ConfigService {
  private readonly envConfig: { [key: string]: string };

  constructor(filePath: string) {    
    this.envConfig = dotenv.parse(fs.readFileSync(path.join(__dirname, filePath)));
  }

  get(key: string): string {
    return this.envConfig[key];
  }
}

Unfortunately, when trying to use the ConfigService within the interceptor, I encountered an error message indicating that the service was undefined:

Cannot read property 'get' of undefined

I have verified that I have correctly instantiated the ConfigService, so I am puzzled as to why it cannot be accessed within the interceptor.

Answer №1

When it comes to dependency injection, global interceptors that are registered outside of any module cannot inject dependencies because this occurs outside the scope of any module.

Therefore, if you are using main.ts and have

app.useGlobalInterceptors(new HttpInterceptor());
you will need to either change it to
app.useGlobalInterceptors(new HttpInterceptor(new ConfigService()));

or you can bind the interceptor in a specific module with

import { APP_INTERCEPTOR } from '@nestjs/core';
@Module({
  providers: [
    ConfigService,
    {
      provide: APP_INTERCEPTOR,
      useClass: HttpInterceptor,
    },
  ],
})
export class YourModule {}

Alternatively, you can bind the interceptor in a controller with

@UseInterceptors(HttpInterceptor)
export class YourController {}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Deciphering TS2345: "The argument supplied, known as 'typeof MyComponent', cannot be assigned to the specified parameter type"

I am facing an issue while attempting to integrate a Typescript React component with react-onclickoutside. The error message that I encounter is as follows: TS2345: Argument of type 'typeof MyComponent' is not assignable to parameter of type &apo ...

Using TypeScript generics to create reusable type definitions for reducers

I have a common reducer function that needs to be properly typed. Here's what I have come up with: export interface WithInvalidRows<T extends { [K in keyof T]: InvalidCell[] }> { invalidRows: T; } interface AddPayload<S extends WithInval ...

Overlooking errors in RxJs observables when using Node JS SSE and sharing a subscription

There is a service endpoint for SSE that shares a subscription if the consumer with the same key is already subscribed. If there is an active subscription, the data is polled from another client. The issue arises when the outer subscription fails to catch ...

What's the quickest method for duplicating an array?

What is the quickest method for duplicating an array? I wanted to create a game, but I found that Array.filter was performing too slowly, so I developed a new function: Array.prototype.removeIf = function(condition: Function): any[] { var copy: any[] ...

Create a function that is identical to the original, but omits the final

I am currently working on defining a type B that functions similarly to type A, but without the last parameter. I have attempted the solution below, however, it is still requiring 2 parameters instead of just one. type Callback = (msg: string) => void; ...

Navigable outside graphic key in AmCharts version 4

Is it possible to achieve a scrollable legend in AmCharts 4 similar to this AmCharts 3 tutorial? With the absence of legend.divId in AmCharts 4, controlling legend size seems challenging as it is all rendered as a single SVG element with limited control us ...

Why is the 'as' keyword important in TypeScript?

class Superhero { name: string = '' } const superheroesList: Superhero[] = []; const superheroesList2 = [] as Superhero[]; As I was exploring TypeScript, I stumbled upon these two distinct methods of declaring an array. This got me thinking w ...

A guide to building a versatile higher-order function using TypeScript

I'm struggling with creating a function that can add functionality to another function in a generic way. Here's my current approach: /** * Creates a function that first calls originalFunction, followed by newFunction. * The created function re ...

Tips on using services for storing and fetching list data in Angular

I currently have two components, the "add-expense" component and the "view-list" component. The "add-expense" component collects expense details from a form and stores them as an object. My goal is to add this object to an empty list within the "expense-li ...

What is the best way to divide a string into an array containing both linked and non-linked elements?

I'm struggling to find the right solution to my problem. I need to create a view that is enclosed in a clickable div. The content will consist of plain text mixed with clickable URLs - the issue arises when clicking on a link also triggers the method ...

Transfer Typescript Project to Visual Studio Code

When I first started my project, I used the Typescript HTML Application Template project template. It worked well and set up a project for me. However, now I want to transition to using VSCode. The issue I'm facing is figuring out which switches and c ...

When you call setTimeout from a static function, it does not get executed

Having a problem with starting a timer in my utility typescript class. The static function initTimer() uses setTimeout but when called from a react component, the timer doesn't start. StyleWrapper.tsx const StyleWrapper: FC = (props) => { cons ...

Unable to utilize a generic model in mongoose due to the error: The argument 'x' is not compatible with the parameter type MongooseFilterQuery

Attempting to include a generic Mongoose model as a parameter in a function is my current challenge. import mongoose, { Document, Model, Schema } from 'mongoose'; interface User { name: string; age: number; favouriteAnimal: string; ...

Merge topics together in RxJS like zip

Is it possible to create an observable that combines two subjects in a unique way, different from the zip function? The goal is to combine two subjects so that when both have emitted values, the latest of their values is emitted. Then, after both emit at ...

When using Angular 10 or later, the mouseclick event will consistently trigger Angular's change detection mechanism

When I bind an event to elements on a component's HTML page, the component continues to detect changes even when the element event is fired. Despite setting the change detection mode of the component to OnPush, this behavior persists. To test this, I ...

Error: Module 'react' not found. Please make sure it is installed and correctly imported

Recently, I've been working on developing a React app using TypeScript. To kickstart the project, I used yarn create react-app (name) --use-pnp --typescript. However, I encountered an issue with the TS linter repeatedly showing the error: Cannot find ...

Error: JWT encountered an issue - the secretOrPrivateKey parameter needs to be set in nest.js

auth.module.ts import { Module } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { JwtModule, JwtService } from '@nestjs/jwt'; import { MongooseModule } from '@nestjs/mongoose'; import { ...

Error: setPosition function only accepts values of type LatLng or LatLngLiteral. The property 'lat' must be a numerical value in agm-core agm-overlay

Currently, I am utilizing Angular Maps powered by Google @agm-core and agm-overlay to implement custom markers. However, when using the (boundsChange) function on the agm-map component, an error occurs with the message "invalidValueError: setPosition: not ...

Utilizing Angular Font Awesome within the google.maps.InfoWindow feature

Showcasing a Font Awesome 5 icon within a Google Maps Info Window (API) Font Awesome 5 functions correctly in my Angular application when utilized in the HTML templates. However, when using the google.maps.InfoWindow outside of Angular, I encounter diffic ...

The requested property map cannot be found within the Array

I am working on a project using redux with react typescript. I have an external JSON file that contains employee data categorized by department id. To properly map the data with types in my application, I have created specific types. Check out this demo o ...