2

In developing NestJs Interceptor, before call next.handle, there is other request musted be called, so nested Observable should be applied. My question is should I use concatMap to deal with the nested observable or mergeMap would be better in the Interceptor?

import {
    CallHandler,
    ExecutionContext,
    Injectable,
    NestInterceptor
} from "@nestjs/common"
import { GqlContextType } from "@nestjs/graphql"
import { Observable } from "rxjs"
import { concatMap, mergeMap } from "rxjs/operators"
import { SsoService } from "../../sso/sso.service"
import * as grpc from "@grpc/grpc-js"

@Injectable()
export class UserInfoInterceptor implements NestInterceptor {
    constructor(private readonly ssoService: SsoService) {}

    getMetadata(reqHeaders) {
        const metadata = new grpc.Metadata()
        return metadata
    }

    intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
        if (context.getType<GqlContextType>() === "graphql") {
            const headers = context.getArgByIndex(2).req.headers

            if (!headers.authorization) return next.handle()

            const metadata = this.getMetadata(headers)
            return this.ssoService
                .validate({ accessToken: headers.authorization }, metadata)
                .pipe(concatMap(next.handle))   // concatMap or mergeMap
        } else {
            return next.handle()
        }
    }
}

pifuegtq23
  • 171
  • 2
  • 10

0 Answers0