|
| 1 | +import type { Span } from '../../types/span'; |
| 2 | +import type { MaxRequestBodySize } from '../../utils/request'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Options that every integration for incoming HTTP requests should |
| 6 | + * support, in each runtime. |
| 7 | + * |
| 8 | + * `TRequest` and `TResponse` are the request and response types that the |
| 9 | + * runtime passes to callbacks: `HttpIncomingMessage` and |
| 10 | + * `HttpServerResponse` for `node:http`, or the web `Request` and |
| 11 | + * `Response` for `Deno.serve`, `Bun.serve` and Cloudflare. |
| 12 | + * |
| 13 | + * Runtime options types do not extend this type. Each one declares its |
| 14 | + * own fields, with docs for that runtime, and uses |
| 15 | + * {@link CheckServerRequestOptions} to check them against this type. |
| 16 | + * Then a new field here fails to compile in each runtime, until the |
| 17 | + * runtime supports it or lists it as unsupported. |
| 18 | + */ |
| 19 | +export interface ServerRequestOptions<TRequest, TResponse> { |
| 20 | + /** |
| 21 | + * Whether to create spans for incoming requests. |
| 22 | + * Defaults to the client's tracing configuration. |
| 23 | + */ |
| 24 | + spans?: boolean; |
| 25 | + |
| 26 | + /** |
| 27 | + * Whether to create [Sessions](https://docs.sentry.io/product/releases/health/#sessions) for incoming |
| 28 | + * requests, to track the health and crash-free rate of releases. |
| 29 | + * |
| 30 | + * @default `true` |
| 31 | + */ |
| 32 | + sessions?: boolean; |
| 33 | + |
| 34 | + /** |
| 35 | + * Number of milliseconds until sessions are flushed as a session aggregate. |
| 36 | + * |
| 37 | + * @default `60000` (60s) |
| 38 | + */ |
| 39 | + sessionFlushingDelayMS?: number; |
| 40 | + |
| 41 | + /** |
| 42 | + * Controls the maximum size of incoming request bodies attached to events. |
| 43 | + * |
| 44 | + * @default 'medium' |
| 45 | + */ |
| 46 | + maxRequestBodySize?: MaxRequestBodySize; |
| 47 | + |
| 48 | + /** |
| 49 | + * Do not capture the request body for incoming requests where the given callback returns `true`. |
| 50 | + * |
| 51 | + * `url` is the full URL of the request, including the query string. |
| 52 | + */ |
| 53 | + ignoreRequestBody?: (url: string, request: TRequest) => boolean; |
| 54 | + |
| 55 | + /** |
| 56 | + * Do not create spans for incoming requests where the given callback returns `true`. |
| 57 | + * |
| 58 | + * `urlPath` is the path of the request, including the query string. |
| 59 | + */ |
| 60 | + ignoreIncomingRequests?: (urlPath: string, request: TRequest) => boolean; |
| 61 | + |
| 62 | + /** |
| 63 | + * Whether to ignore common static asset requests (favicon.ico, robots.txt, etc.) |
| 64 | + * when creating spans. |
| 65 | + * |
| 66 | + * @default `true` |
| 67 | + */ |
| 68 | + ignoreStaticAssets?: boolean; |
| 69 | + |
| 70 | + /** |
| 71 | + * A hook to change the span for an incoming request, after the span is created. |
| 72 | + */ |
| 73 | + onSpanCreated?: (span: Span, request: TRequest) => void; |
| 74 | + |
| 75 | + /** |
| 76 | + * A hook to change the span for an incoming request one last time, before it ends. |
| 77 | + */ |
| 78 | + onSpanEnd?: (span: Span, request: TRequest, response: TResponse) => void; |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * `true` when `TOptions` matches {@link ServerRequestOptions}, else a |
| 83 | + * string that names the problem. Use it with a type that accepts only |
| 84 | + * `true`, so a mismatch fails to compile: |
| 85 | + * |
| 86 | + * ```ts |
| 87 | + * type _Check = AssertTrue<CheckServerRequestOptions<MyOptions, Request, Response, 'sessions'>>; |
| 88 | + * ``` |
| 89 | + * |
| 90 | + * `TUnsupported` lists the fields that the runtime does not support. |
| 91 | + * |
| 92 | + * The check fails when: |
| 93 | + * - `TOptions` does not declare a field, and `TUnsupported` does not |
| 94 | + * list it. |
| 95 | + * - `TUnsupported` lists a field that `TOptions` declares. |
| 96 | + * - A callback written for the shared type does not fit the field in |
| 97 | + * `TOptions`. |
| 98 | + */ |
| 99 | +export type CheckServerRequestOptions< |
| 100 | + TOptions, |
| 101 | + TRequest, |
| 102 | + TResponse, |
| 103 | + TUnsupported extends keyof ServerRequestOptions<TRequest, TResponse> = never, |
| 104 | +> = [MissingFields<TOptions, TRequest, TResponse, TUnsupported>] extends [never] |
| 105 | + ? [Extract<TUnsupported, keyof TOptions>] extends [never] |
| 106 | + ? SupportedFields< |
| 107 | + ServerRequestOptions<TRequest, TResponse>, |
| 108 | + TRequest, |
| 109 | + TResponse, |
| 110 | + TUnsupported |
| 111 | + > extends SupportedFields<TOptions, TRequest, TResponse, TUnsupported> |
| 112 | + ? true |
| 113 | + : 'A field type does not match ServerRequestOptions' |
| 114 | + : `Listed as unsupported, but declared: ${Extract<TUnsupported, keyof TOptions> & string}` |
| 115 | + : `Missing field: ${MissingFields<TOptions, TRequest, TResponse, TUnsupported> & string}`; |
| 116 | + |
| 117 | +type MissingFields<TOptions, TRequest, TResponse, TUnsupported> = Exclude< |
| 118 | + keyof ServerRequestOptions<TRequest, TResponse>, |
| 119 | + TUnsupported | keyof TOptions |
| 120 | +>; |
| 121 | + |
| 122 | +type SupportedFields<T, TRequest, TResponse, TUnsupported> = Pick< |
| 123 | + T, |
| 124 | + Extract<keyof T, Exclude<keyof ServerRequestOptions<TRequest, TResponse>, TUnsupported>> |
| 125 | +>; |
| 126 | + |
| 127 | +/** Accepts only `true`. Use it with {@link CheckServerRequestOptions}. */ |
| 128 | +export type AssertTrue<T extends true> = T; |
0 commit comments