-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathmiddleware.ts
More file actions
53 lines (46 loc) · 2.04 KB
/
middleware.ts
File metadata and controls
53 lines (46 loc) · 2.04 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
46
47
48
49
50
51
52
53
import { createMiddleware } from '@tanstack/react-start';
import { wrapMiddlewaresWithSentry } from '@sentry/tanstackstart-react';
// Global request middleware - runs on every request
// NOTE: This is exported unwrapped to test auto-instrumentation via the Vite plugin
export const globalRequestMiddleware = createMiddleware().server(async ({ next }) => {
console.log('Global request middleware executed');
return next();
});
// Global function middleware - runs on every server function
// NOTE: This is exported unwrapped to test auto-instrumentation via the Vite plugin
export const globalFunctionMiddleware = createMiddleware({ type: 'function' }).server(async ({ next }) => {
console.log('Global function middleware executed');
return next();
});
// Server function middleware
const serverFnMiddleware = createMiddleware({ type: 'function' }).server(async ({ next }) => {
console.log('Server function middleware executed');
return next();
});
// Server route request middleware
const serverRouteRequestMiddleware = createMiddleware().server(async ({ next }) => {
console.log('Server route request middleware executed');
return next();
});
// Early return middleware - returns without calling next()
const earlyReturnMiddleware = createMiddleware({ type: 'function' }).server(async () => {
console.log('Early return middleware executed - not calling next()');
return { earlyReturn: true, message: 'Middleware returned early without calling next()' };
});
// Error middleware - throws an exception
const errorMiddleware = createMiddleware({ type: 'function' }).server(async () => {
console.log('Error middleware executed - throwing error');
throw new Error('Middleware Error Test');
});
// Manually wrap middlewares with Sentry (for middlewares that won't be auto-instrumented)
export const [
wrappedServerFnMiddleware,
wrappedServerRouteRequestMiddleware,
wrappedEarlyReturnMiddleware,
wrappedErrorMiddleware,
] = wrapMiddlewaresWithSentry({
serverFnMiddleware,
serverRouteRequestMiddleware,
earlyReturnMiddleware,
errorMiddleware,
});