Skip to content

Commit 903addf

Browse files
authored
feat(types): Add PolymorphicRequest type (#5744)
This is a change which comes out of a code review[1] of #5703, moving the `CrossPlatformType` from `@sentry/utils` to `@sentry/types` and renaming it `PolymorphicRequest` (to match the existing `PolymorphicEvent`). Because it affects a number of files, I decided to pull it into its own PR, just to not confuse things in that one. [1] #5703 (comment)
1 parent f1f03f2 commit 903addf

File tree

8 files changed

+97
-103
lines changed

8 files changed

+97
-103
lines changed

packages/node/src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export type {
22
Breadcrumb,
33
BreadcrumbHint,
4+
PolymorphicRequest,
45
Request,
56
SdkInfo,
67
Event,
@@ -15,7 +16,7 @@ export type {
1516
Thread,
1617
User,
1718
} from '@sentry/types';
18-
export type { AddRequestDataToEventOptions, CrossPlatformRequest } from '@sentry/utils';
19+
export type { AddRequestDataToEventOptions } from '@sentry/utils';
1920

2021
export type { NodeOptions } from './types';
2122

packages/node/src/requestDataDeprecated.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,19 @@
66

77
/* eslint-disable deprecation/deprecation */
88
/* eslint-disable @typescript-eslint/no-explicit-any */
9-
import { Event, ExtractedNodeRequestData } from '@sentry/types';
9+
import { Event, ExtractedNodeRequestData, PolymorphicRequest } from '@sentry/types';
1010
import {
1111
addRequestDataToEvent,
1212
AddRequestDataToEventOptions,
13-
CrossPlatformRequest,
1413
extractRequestData as _extractRequestData,
1514
} from '@sentry/utils';
1615
import * as cookie from 'cookie';
1716
import * as url from 'url';
1817

1918
/**
20-
* @deprecated `Handlers.ExpressRequest` is deprecated and will be removed in v8. Use `CrossPlatformRequest` instead.
19+
* @deprecated `Handlers.ExpressRequest` is deprecated and will be removed in v8. Use `PolymorphicRequest` instead.
2120
*/
22-
export type ExpressRequest = CrossPlatformRequest;
21+
export type ExpressRequest = PolymorphicRequest;
2322

2423
/**
2524
* Normalizes data from the request object, accounting for framework differences.

packages/node/src/sdk.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
/* eslint-disable max-lines */
22
import { getCurrentHub, getIntegrationsToSetup, initAndBind, Integrations as CoreIntegrations } from '@sentry/core';
33
import { getMainCarrier, setHubOnCarrier } from '@sentry/hub';
4-
import { Event, ExtractedNodeRequestData, SessionStatus, StackParser } from '@sentry/types';
4+
import { Event, ExtractedNodeRequestData, PolymorphicRequest, SessionStatus, StackParser } from '@sentry/types';
55
import {
66
addRequestDataToEvent as _addRequestDataToEvent,
77
AddRequestDataToEventOptions,
88
createStackParser,
9-
CrossPlatformRequest,
109
extractRequestData as _extractRequestData,
1110
getGlobalObject,
1211
logger,
@@ -294,7 +293,7 @@ function startSessionTracking(): void {
294293
*/
295294
export function addRequestDataToEvent(
296295
event: Event,
297-
req: CrossPlatformRequest,
296+
req: PolymorphicRequest,
298297
options?: Omit<AddRequestDataToEventOptions, 'deps'>,
299298
): Event {
300299
return _addRequestDataToEvent(event, req, {
@@ -318,7 +317,7 @@ export function addRequestDataToEvent(
318317
* @returns An object containing normalized request data
319318
*/
320319
export function extractRequestData(
321-
req: CrossPlatformRequest,
320+
req: PolymorphicRequest,
322321
options?: {
323322
include?: string[];
324323
},

packages/node/test/requestdata.test.ts

+10-11
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66

77
// TODO (v8 / #5257): Remove everything above
88

9-
import { Event, TransactionSource, User } from '@sentry/types';
9+
import { Event, PolymorphicRequest, TransactionSource, User } from '@sentry/types';
1010
import {
1111
addRequestDataToEvent,
1212
AddRequestDataToEventOptions,
13-
CrossPlatformRequest,
1413
extractPathForTransaction,
1514
extractRequestData as newExtractRequestData,
1615
} from '@sentry/utils';
@@ -43,7 +42,7 @@ describe.each([parseRequest, addRequestDataToEvent])(
4342
} else {
4443
return [
4544
event,
46-
req as CrossPlatformRequest,
45+
req as PolymorphicRequest,
4746
{
4847
include,
4948
deps: {
@@ -277,7 +276,7 @@ describe.each([oldExtractRequestData, newExtractRequestData])(
277276
return [req as ExpressRequest, include] as Parameters<typeof oldExtractRequestData>;
278277
} else {
279278
return [
280-
req as CrossPlatformRequest,
279+
req as PolymorphicRequest,
281280
{
282281
include,
283282
deps: {
@@ -496,7 +495,7 @@ describe('extractPathForTransaction', () => {
496495
baseUrl: '/api/users',
497496
route: { path: '/:id/details' },
498497
originalUrl: '/api/users/123/details',
499-
} as CrossPlatformRequest,
498+
} as PolymorphicRequest,
500499
{ path: true, method: true },
501500
'GET /api/users/:id/details',
502501
'route' as TransactionSource,
@@ -508,7 +507,7 @@ describe('extractPathForTransaction', () => {
508507
baseUrl: '/api/users',
509508
route: { path: '/:id/details' },
510509
originalUrl: '/api/users/123/details',
511-
} as CrossPlatformRequest,
510+
} as PolymorphicRequest,
512511
{ path: true, method: false },
513512
'/api/users/:id/details',
514513
'route' as TransactionSource,
@@ -520,7 +519,7 @@ describe('extractPathForTransaction', () => {
520519
baseUrl: '/api/users',
521520
route: { path: '/:id/details' },
522521
originalUrl: '/api/users/123/details',
523-
} as CrossPlatformRequest,
522+
} as PolymorphicRequest,
524523
{ path: false, method: true },
525524
'GET',
526525
'route' as TransactionSource,
@@ -532,7 +531,7 @@ describe('extractPathForTransaction', () => {
532531
baseUrl: '/api/users',
533532
route: { path: '/:id/details' },
534533
originalUrl: '/api/users/123/details',
535-
} as CrossPlatformRequest,
534+
} as PolymorphicRequest,
536535
{ path: false, method: false },
537536
'',
538537
'route' as TransactionSource,
@@ -543,7 +542,7 @@ describe('extractPathForTransaction', () => {
543542
method: 'get',
544543
baseUrl: '/api/users',
545544
originalUrl: '/api/users/123/details',
546-
} as CrossPlatformRequest,
545+
} as PolymorphicRequest,
547546
{ path: true, method: true },
548547
'GET /api/users/123/details',
549548
'url' as TransactionSource,
@@ -552,7 +551,7 @@ describe('extractPathForTransaction', () => {
552551
'%s',
553552
(
554553
_: string,
555-
req: CrossPlatformRequest,
554+
req: PolymorphicRequest,
556555
options: { path?: boolean; method?: boolean },
557556
expectedRoute: string,
558557
expectedSource: TransactionSource,
@@ -570,7 +569,7 @@ describe('extractPathForTransaction', () => {
570569
baseUrl: '/api/users',
571570
route: { path: '/:id/details' },
572571
originalUrl: '/api/users/123/details',
573-
} as CrossPlatformRequest;
572+
} as PolymorphicRequest;
574573

575574
const [route, source] = extractPathForTransaction(req, {
576575
path: true,

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

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
/* eslint-disable max-lines */
2-
import { Integration, Transaction } from '@sentry/types';
3-
import {
4-
CrossPlatformRequest,
5-
extractPathForTransaction,
6-
getNumberOfUrlSegments,
7-
isRegExp,
8-
logger,
9-
} from '@sentry/utils';
2+
import { Integration, PolymorphicRequest, Transaction } from '@sentry/types';
3+
import { extractPathForTransaction, getNumberOfUrlSegments, isRegExp, logger } from '@sentry/utils';
104

115
type Method =
126
| 'all'
@@ -39,8 +33,8 @@ type Router = {
3933
[method in Method]: (...args: any) => any; // eslint-disable-line @typescript-eslint/no-explicit-any
4034
};
4135

42-
/* Extend the CrossPlatformRequest type with a patched parameter to build a reconstructed route */
43-
type PatchedRequest = CrossPlatformRequest & { _reconstructedRoute?: string };
36+
/* Extend the PolymorphicRequest type with a patched parameter to build a reconstructed route */
37+
type PatchedRequest = PolymorphicRequest & { _reconstructedRoute?: string };
4438

4539
/* Types used for patching the express router prototype */
4640
type ExpressRouter = Router & {

packages/types/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export type { Mechanism } from './mechanism';
3838
export type { ExtractedNodeRequestData, HttpHeaderValue, Primitive, WorkerLocation } from './misc';
3939
export type { ClientOptions, Options } from './options';
4040
export type { Package } from './package';
41-
export type { PolymorphicEvent } from './polymorphics';
41+
export type { PolymorphicEvent, PolymorphicRequest } from './polymorphics';
4242
export type { QueryParams, Request } from './request';
4343
export type { Runtime } from './runtime';
4444
export type { CaptureContext, Scope, ScopeContext } from './scope';

packages/types/src/polymorphics.ts

+67
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/**
22
* Event-like interface that's usable in browser and node.
33
*
4+
* Note: Here we mean the kind of events handled by event listeners, not our `Event` type.
5+
*
46
* Property availability taken from https://developer.mozilla.org/en-US/docs/Web/API/Event#browser_compatibility
57
*/
68
export interface PolymorphicEvent {
@@ -9,3 +11,68 @@ export interface PolymorphicEvent {
911
readonly target?: unknown;
1012
readonly currentTarget?: unknown;
1113
}
14+
15+
/** A `Request` type compatible with Node, Express, browser, etc., because everything is optional */
16+
export type PolymorphicRequest = BaseRequest &
17+
BrowserRequest &
18+
NodeRequest &
19+
ExpressRequest &
20+
KoaRequest &
21+
NextjsRequest;
22+
23+
type BaseRequest = {
24+
method?: string;
25+
url?: string;
26+
};
27+
28+
type BrowserRequest = BaseRequest;
29+
30+
type NodeRequest = BaseRequest & {
31+
headers?: {
32+
[key: string]: string | string[] | undefined;
33+
};
34+
protocol?: string;
35+
socket?: {
36+
encrypted?: boolean;
37+
remoteAddress?: string;
38+
};
39+
};
40+
41+
type KoaRequest = NodeRequest & {
42+
host?: string;
43+
hostname?: string;
44+
ip?: string;
45+
originalUrl?: string;
46+
};
47+
48+
type NextjsRequest = NodeRequest & {
49+
cookies?: {
50+
[key: string]: string;
51+
};
52+
query?: {
53+
[key: string]: any;
54+
};
55+
};
56+
57+
type ExpressRequest = NodeRequest & {
58+
baseUrl?: string;
59+
body?: string | { [key: string]: any };
60+
host?: string;
61+
hostname?: string;
62+
ip?: string;
63+
originalUrl?: string;
64+
route?: {
65+
path: string;
66+
stack: [
67+
{
68+
name: string;
69+
},
70+
];
71+
};
72+
query?: {
73+
[key: string]: any;
74+
};
75+
user?: {
76+
[key: string]: any;
77+
};
78+
};

0 commit comments

Comments
 (0)