Skip to content

Commit 92b7b43

Browse files
committed
improve request types
1 parent 8b20bcd commit 92b7b43

File tree

4 files changed

+29
-18
lines changed

4 files changed

+29
-18
lines changed

packages/nestjs/src/setup.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
} from '@sentry/core';
2323
import type { Observable } from 'rxjs';
2424
import { isExpectedError } from './helpers';
25+
import type { FastifyRequest, ExpressRequest } from '@sentry/node';
2526

2627
/**
2728
* Note: We cannot use @ syntax to add the decorators, so we add them directly below the classes as function wrappers.
@@ -52,18 +53,16 @@ class SentryTracingInterceptor implements NestInterceptor {
5253
}
5354

5455
if (context.getType() === 'http') {
55-
const req = context.switchToHttp().getRequest();
56-
/* eslint-disable @typescript-eslint/no-unsafe-member-access, @sentry-internal/sdk/no-optional-chaining */
57-
if (req.routeOptions && req.routeOptions.url) {
56+
const req = context.switchToHttp().getRequest() as FastifyRequest | ExpressRequest;
57+
if ('routeOptions' in req && req.routeOptions && req.routeOptions.url) {
5858
// fastify case
5959
getIsolationScope().setTransactionName(
60-
`${req.routeOptions.method?.toUpperCase() || 'GET'} ${req.routeOptions.url}`,
60+
`${(req.routeOptions.method || 'GET').toUpperCase()} ${req.routeOptions.url}`,
6161
);
62-
} else if (req.route && req.route.path) {
62+
} else if ('route' in req && req.route && req.route.path) {
6363
// express case
64-
getIsolationScope().setTransactionName(`${req.method?.toUpperCase() || 'GET'} ${req.route.path}`);
64+
getIsolationScope().setTransactionName(`${(req.method || 'GET').toUpperCase()} ${req.route.path}`);
6565
}
66-
/* eslint-enable @typescript-eslint/no-unsafe-member-access, @sentry-internal/sdk/no-optional-chaining */
6766
}
6867

6968
return next.handle();

packages/node/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,5 @@ export type {
166166
User,
167167
Span,
168168
} from '@sentry/core';
169+
170+
export type { FastifyRequest, ExpressRequest } from './integrations/tracing/nest/types';

packages/node/src/integrations/tracing/nest/nest.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,17 @@ export function setupNestErrorHandler(app: MinimalNestJsApp, baseFilter: NestJsE
8787
}
8888

8989
if (context.getType() === 'http') {
90+
// getRequest() returns either a FastifyRequest or ExpressRequest, depending on the used adapter
9091
const req = context.switchToHttp().getRequest();
91-
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
92-
if (req.routeOptions && req.routeOptions.url) {
92+
if ('routeOptions' in req && req.routeOptions && req.routeOptions.url) {
9393
// fastify case
9494
getIsolationScope().setTransactionName(
9595
`${req.routeOptions.method?.toUpperCase() || 'GET'} ${req.routeOptions.url}`,
9696
);
97-
} else if (req.route && req.route.path) {
97+
} else if ('route' in req && req.route && req.route.path) {
9898
// express case
9999
getIsolationScope().setTransactionName(`${req.method?.toUpperCase() || 'GET'} ${req.route.path}`);
100100
}
101-
/* eslint-enable @typescript-eslint/no-unsafe-member-access, @sentry-internal/sdk/no-optional-chaining */
102101
}
103102

104103
return next.handle();

packages/node/src/integrations/tracing/nest/types.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22

3+
// Partial extract of FastifyRequest interface
4+
// https://github.com/fastify/fastify/blob/87f9f20687c938828f1138f91682d568d2a31e53/types/request.d.ts#L41
5+
export interface FastifyRequest {
6+
routeOptions?: {
7+
method?: string;
8+
url?: string;
9+
};
10+
}
11+
12+
// Partial extract of ExpressRequest interface
13+
export interface ExpressRequest {
14+
route?: {
15+
path?: string;
16+
};
17+
method?: string;
18+
}
19+
320
export interface MinimalNestJsExecutionContext {
421
getType: () => string;
522

623
switchToHttp: () => {
724
// minimal request object
825
// according to official types, all properties are required but
926
// let's play it safe and assume they're optional
10-
getRequest: () => {
11-
routeOptions?: any;
12-
route?: {
13-
path?: string;
14-
};
15-
method?: string;
16-
};
27+
getRequest: () => FastifyRequest | ExpressRequest;
1728
};
1829

1930
_sentryInterceptorInstrumented?: boolean;

0 commit comments

Comments
 (0)