Skip to content
Snippets Groups Projects
Commit 0a28dd66 authored by Steve Reis's avatar Steve Reis
Browse files

WIP: interceptor add headers

parent 9e37209d
No related branches found
No related tags found
No related merge requests found
...@@ -5,20 +5,31 @@ import { ...@@ -5,20 +5,31 @@ import {
ExecutionContext, ExecutionContext,
CallHandler, CallHandler,
} from '@nestjs/common'; } from '@nestjs/common';
import { GqlExecutionContext } from '@nestjs/graphql';
import { IncomingMessage } from 'http';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
@Injectable() @Injectable()
export class HeadersInterceptor implements NestInterceptor { export class HeadersInterceptor implements NestInterceptor {
constructor(private httpService: HttpService) {} constructor(private httpService: HttpService) {}
intercept(context: ExecutionContext, next: CallHandler): Observable<any> { intercept(context: GqlExecutionContext, next: CallHandler): Observable<any> {
const request = context.switchToHttp().getRequest<Request>(); // cleaner : add only the auth header (should find the name)
//console.log(context.switchToHttp().getRequest()); switch (context.getType()) {
case 'http': {
console.log('interceptor headers', request); const ctx = context.switchToHttp();
const request = ctx.getRequest<Request>();
//this.httpService.axiosRef.defaults.headers = request.headers; this.httpService.axiosRef.defaults.headers = request.headers;
break;
}
case 'graphql': {
const ctx = GqlExecutionContext.create(context);
const req: IncomingMessage = ctx.getContext().req;
this.httpService.axiosRef.defaults.headers = req.headers;
break;
}
}
return next.handle(); return next.handle();
} }
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment