Skip to content

Commit 889d1a8

Browse files
authored
fix: pattern exclude when no request url
added support for request-less invocations (Eg event handler controllers, kafka) (#11)
1 parent c2fb3a4 commit 889d1a8

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/interceptors/request.interceptor.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,22 @@ describe('RequestInterceptor', () => {
6464
);
6565
});
6666

67+
it('should handle request with empty request context', async () => {
68+
const emptyRequestContext = { };
69+
(mockContext.switchToHttp().getRequest as jest.Mock).mockReturnValue(emptyRequestContext);
70+
71+
const observable = await interceptor.intercept(mockContext, mockCallHandler);
72+
await lastValueFrom(observable);
73+
74+
expect(ContextLogger.updateContext).toHaveBeenCalledWith(
75+
expect.objectContaining({
76+
requestMethod: undefined,
77+
requestUrl: undefined
78+
})
79+
);
80+
expect(mockLogger.debug).toHaveBeenCalled();
81+
});
82+
6783
it('should skip context and logging for excluded routes', async () => {
6884
mockRequest.url = '/health';
6985

src/interceptors/request.interceptor.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,16 @@ export class RequestInterceptor implements NestInterceptor {
2424
next: CallHandler
2525
): Promise<Observable<any>> {
2626
const request = context.switchToHttp().getRequest();
27-
if (this.options.exclude?.some(pattern => request.url.indexOf(pattern) === 0)) {
27+
28+
if (
29+
request.url &&
30+
this.options.exclude?.some(
31+
(pattern) => request.url.indexOf(pattern) === 0
32+
)
33+
) {
2834
return next.handle();
2935
}
30-
36+
3137
const startTime = new Date();
3238

3339
// Base context

0 commit comments

Comments
 (0)