Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

can we somehow pass the parameter string inside constructor of interceptor in nestjs, making it more general and configurable?

I am using the following interceptor to transform the gameLevel from string to number.

@Injectable()
export class EnumStringToNumberInterceptor implements NestInterceptor {
    constructor() {}
    intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
        return next.handle().pipe(map(data => {
            if (data && data.list) {
                data.list.forEach(d => {
                    d['gameLevel'] = Number(d['gameLevel']);
                })
            } else if (data && data.gameLevel) {
                data['gameLevel'] = Number(data['gameLevel']);
            }
            return data;
        }));
      }
}


I want to pass gameLevel as key somehow in constructor something like the following –

constructor(private readonly key: string) {}

// and do something like this
d[key] = Number(d[key]);

By achieving this I will be making this interceptor more general to use it in different such use cases when key will be differing.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you have nothing being injected into the interceptor, then a simple new Interceptor('key or whatever string') works well.

If you do have something being injected, I would suggest making use of reflection and following Nest’s docs on setting up custom metadata that the interceptor can read at runtime.

Something along the lines of

export const GameLevel = (key: string) => SetMetadata('GAME_LEVEL_METADATA', key);

Then in your controller you can use @GameLevel('someLevelKey') and in the interceptor you can do something along the lines of

@Injectable()
export class EnumStringToNumberInterceptor implements NestInterceptor {
    constructor(private readonly reflector: Reflector) {}
    intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
        const level = this.reflector.getAllAndOverride('GAME_LEVEL_METADAT', [context.getClass(), context.getHandler()]);
        return next.handle().pipe(map(data => {
            if (data && data.list) {
                data.list.forEach(d => {
                    d[level] = Number(d[level]);
                })
            } else if (data && data[level]) {
                data[level] = Number(data[level]);
            }
            return data;
        }));
      }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading