-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathmiddleware.ts
More file actions
41 lines (32 loc) · 1.63 KB
/
middleware.ts
File metadata and controls
41 lines (32 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { withSentry } from '@sentry/cloudflare';
import { applySdkMetadata, type BaseTransportOptions, debug, type Integration, type Options } from '@sentry/core';
import type { Context, Hono, MiddlewareHandler } from 'hono';
import { requestHandler, responseHandler } from '../shared/middlewareHandlers';
export interface HonoOptions extends Options<BaseTransportOptions> {
context?: Context;
}
const filterHonoIntegration = (integration: Integration): boolean => integration.name !== 'Hono';
export const sentry = (app: Hono, options: HonoOptions | undefined = {}): MiddlewareHandler => {
const isDebug = options.debug;
isDebug && debug.log('Initialized Sentry Hono middleware (Cloudflare)');
applySdkMetadata(options, 'hono');
const { integrations: userIntegrations } = options;
withSentry(
() => ({
...options,
// Always filter out the Hono integration from user-provided integrations (or when nothing is specified).
// The Hono integration is already set up by withSentry, so adding it again would cause double-capturing (and non-parametrized URLs).
integrations: Array.isArray(userIntegrations)
? defaults => [...defaults.filter(filterHonoIntegration), ...userIntegrations.filter(filterHonoIntegration)]
: typeof userIntegrations === 'function'
? defaults => userIntegrations(defaults).filter(filterHonoIntegration)
: defaults => defaults.filter(filterHonoIntegration),
}),
app,
);
return async (context, next) => {
requestHandler(context);
await next(); // Handler runs in between Request above ⤴ and Response below ⤵
responseHandler(context);
};
};