-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathmiddleware.ts
More file actions
45 lines (36 loc) · 1.72 KB
/
middleware.ts
File metadata and controls
45 lines (36 loc) · 1.72 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
42
43
44
45
import { withSentry } from '@sentry/cloudflare';
import { applySdkMetadata, type BaseTransportOptions, debug, type Options } from '@sentry/core';
import type { Env, Hono, MiddlewareHandler } from 'hono';
import { buildFilteredIntegrations } from '../shared/buildFilteredIntegrations';
import { requestHandler, responseHandler } from '../shared/middlewareHandlers';
import { patchAppUse } from '../shared/patchAppUse';
export interface HonoCloudflareOptions extends Options<BaseTransportOptions> {}
/**
* Sentry middleware for Hono on Cloudflare Workers.
*/
export function sentry<E extends Env>(
app: Hono<E>,
options: HonoCloudflareOptions | ((env: E['Bindings']) => HonoCloudflareOptions),
): MiddlewareHandler {
withSentry(
env => {
const honoOptions = typeof options === 'function' ? options(env as E['Bindings']) : options;
applySdkMetadata(honoOptions, 'hono', ['hono', 'cloudflare']);
honoOptions.debug && debug.log('Initialized Sentry Hono middleware (Cloudflare)');
return {
...honoOptions,
// Always filter out the Hono integration from defaults and user integrations.
// The Hono integration is already set up by withSentry, so adding it again would cause capturing too early (in Cloudflare SDK) and non-parametrized URLs.
integrations: buildFilteredIntegrations(honoOptions.integrations, true),
};
},
// Cast needed because Hono<E> exposes a narrower fetch signature than ExportedHandler<unknown>
app as unknown as ExportedHandler<unknown>,
);
patchAppUse(app);
return async (context, next) => {
requestHandler(context);
await next(); // Handler runs in between Request above ⤴ and Response below ⤵
responseHandler(context);
};
}