UNPKG

4.74 kBTypeScriptView Raw
1/**
2 * The functions here, which enrich an event with request data, are mostly for use in Node, but are safe for use in a
3 * browser context. They live here in `@sentry/utils` rather than in `@sentry/node` so that they can be used in
4 * frameworks (like nextjs), which, because of SSR, run the same code in both Node and browser contexts.
5 *
6 * TODO (v8 / #5257): Remove the note below
7 * Note that for now, the tests for this code have to live in `@sentry/node`, since they test both these functions and
8 * the backwards-compatibility-preserving wrappers which still live in `handlers.ts` there.
9 */
10import { Event, ExtractedNodeRequestData, Transaction } from '@sentry/types';
11declare type BaseRequest = {
12 method?: string;
13 url?: string;
14};
15declare type BrowserRequest = BaseRequest;
16declare type NodeRequest = BaseRequest & {
17 headers?: {
18 [key: string]: string | string[] | undefined;
19 };
20 protocol?: string;
21 socket?: {
22 encrypted?: boolean;
23 remoteAddress?: string;
24 };
25};
26declare type KoaRequest = NodeRequest & {
27 host?: string;
28 hostname?: string;
29 ip?: string;
30 originalUrl?: string;
31};
32declare type NextjsRequest = NodeRequest & {
33 cookies?: {
34 [key: string]: string;
35 };
36 query?: {
37 [key: string]: any;
38 };
39};
40declare type ExpressRequest = NodeRequest & {
41 baseUrl?: string;
42 body?: string | {
43 [key: string]: any;
44 };
45 host?: string;
46 hostname?: string;
47 ip?: string;
48 originalUrl?: string;
49 route?: {
50 path: string;
51 stack: [{
52 name: string;
53 }];
54 };
55 query?: {
56 [key: string]: any;
57 };
58 user?: {
59 [key: string]: any;
60 };
61};
62/** A `Request` type compatible with Node, Express, browser, etc., because everything is optional */
63export declare type CrossPlatformRequest = BaseRequest & BrowserRequest & NodeRequest & ExpressRequest & KoaRequest & NextjsRequest;
64declare type InjectedNodeDeps = {
65 cookie: {
66 parse: (cookieStr: string) => Record<string, string>;
67 };
68 url: {
69 parse: (urlStr: string) => {
70 query: string | null;
71 };
72 };
73};
74/**
75 * Sets parameterized route as transaction name e.g.: `GET /users/:id`
76 * Also adds more context data on the transaction from the request
77 */
78export declare function addRequestDataToTransaction(transaction: Transaction | undefined, req: CrossPlatformRequest, deps?: InjectedNodeDeps): void;
79/**
80 * Extracts complete generalized path from the request object and uses it to construct transaction name.
81 *
82 * eg. GET /mountpoint/user/:id
83 *
84 * @param req A request object
85 * @param options What to include in the transaction name (method, path, or both)
86 *
87 * @returns The fully constructed transaction name
88 */
89export declare function extractPathForTransaction(req: CrossPlatformRequest, options?: {
90 path?: boolean;
91 method?: boolean;
92}): string;
93declare type TransactionNamingScheme = 'path' | 'methodPath' | 'handler';
94/**
95 * Normalize data from the request object, accounting for framework differences.
96 *
97 * @param req The request object from which to extract data
98 * @param options.include An optional array of keys to include in the normalized data. Defaults to
99 * DEFAULT_REQUEST_INCLUDES if not provided.
100 * @param options.deps Injected, platform-specific dependencies
101 * @returns An object containing normalized request data
102 */
103export declare function extractRequestData(req: CrossPlatformRequest, options?: {
104 include?: string[];
105 deps?: InjectedNodeDeps;
106}): ExtractedNodeRequestData;
107/**
108 * Options deciding what parts of the request to use when enhancing an event
109 */
110export interface AddRequestDataToEventOptions {
111 /** Flags controlling whether each type of data should be added to the event */
112 include?: {
113 ip?: boolean;
114 request?: boolean | string[];
115 transaction?: boolean | TransactionNamingScheme;
116 user?: boolean | string[];
117 };
118 /** Injected platform-specific dependencies */
119 deps?: {
120 cookie: {
121 parse: (cookieStr: string) => Record<string, string>;
122 };
123 url: {
124 parse: (urlStr: string) => {
125 query: string | null;
126 };
127 };
128 };
129}
130/**
131 * Add data from the given request to the given event
132 *
133 * @param event The event to which the request data will be added
134 * @param req Request object
135 * @param options.include Flags to control what data is included
136 * @param options.deps Injected platform-specific dependencies
137 * @hidden
138 */
139export declare function addRequestDataToEvent(event: Event, req: CrossPlatformRequest, options?: AddRequestDataToEventOptions): Event;
140export {};
141//# sourceMappingURL=requestdata.d.ts.map
\No newline at end of file