Skip to content

fix: pattern exclude when no request url #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/interceptors/request.interceptor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ describe('RequestInterceptor', () => {
);
});

it('should handle request with empty request context', async () => {
const emptyRequestContext = { };
(mockContext.switchToHttp().getRequest as jest.Mock).mockReturnValue(emptyRequestContext);

const observable = await interceptor.intercept(mockContext, mockCallHandler);
await lastValueFrom(observable);

expect(ContextLogger.updateContext).toHaveBeenCalledWith(
expect.objectContaining({
requestMethod: undefined,
requestUrl: undefined
})
);
expect(mockLogger.debug).toHaveBeenCalled();
});

it('should skip context and logging for excluded routes', async () => {
mockRequest.url = '/health';

Expand Down
10 changes: 8 additions & 2 deletions src/interceptors/request.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@ export class RequestInterceptor implements NestInterceptor {
next: CallHandler
): Promise<Observable<any>> {
const request = context.switchToHttp().getRequest();
if (this.options.exclude?.some(pattern => request.url.indexOf(pattern) === 0)) {

if (
request.url &&
this.options.exclude?.some(
(pattern) => request.url.indexOf(pattern) === 0
)
) {
return next.handle();
}

const startTime = new Date();

// Base context
Expand Down