Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor
  • Loading branch information
nicohrubec committed Jan 27, 2026
commit f7f4c22892a95f347082156091974861c34bf71b
72 changes: 26 additions & 46 deletions packages/tanstackstart-react/src/vite/autoInstrumentMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,65 +12,45 @@ type WrapResult = {
};

/**
* Wraps global middleware arrays (requestMiddleware, functionMiddleware) in createStart() files.
* Core function that wraps middleware arrays matching the given regex.
*/
export function wrapGlobalMiddleware(code: string, id: string, debug: boolean): WrapResult {
function wrapMiddlewareArrays(code: string, id: string, debug: boolean, regex: RegExp): WrapResult {
const skipped: string[] = [];
let didWrap = false;

const transformed = code.replace(
/(requestMiddleware|functionMiddleware)\s*:\s*\[([^\]]*)\]/g,
(match: string, key: string, contents: string) => {
const objContents = arrayToObjectShorthand(contents);
if (objContents) {
didWrap = true;
if (debug) {
// eslint-disable-next-line no-console
console.log(`[Sentry] Auto-wrapping ${key} in ${id}`);
}
return `${key}: wrapMiddlewaresWithSentry(${objContents})`;
}
// Track middlewares that couldn't be auto-wrapped
// Skip if we matched whitespace only
if (contents.trim()) {
skipped.push(key);
const transformed = code.replace(regex, (match: string, key: string, contents: string) => {
const objContents = arrayToObjectShorthand(contents);
if (objContents) {
didWrap = true;
if (debug) {
// eslint-disable-next-line no-console
console.log(`[Sentry] Auto-wrapping ${key} in ${id}`);
}
return match;
},
);
return `${key}: wrapMiddlewaresWithSentry(${objContents})`;
}
// Track middlewares that couldn't be auto-wrapped
// Skip if we matched whitespace only
if (contents.trim()) {
skipped.push(key);
}
return match;
});

return { code: transformed, didWrap, skipped };
}

/**
* Wraps global middleware arrays (requestMiddleware, functionMiddleware) in createStart() files.
*/
export function wrapGlobalMiddleware(code: string, id: string, debug: boolean): WrapResult {
return wrapMiddlewareArrays(code, id, debug, /(requestMiddleware|functionMiddleware)\s*:\s*\[([^\]]*)\]/g);
}

/**
* Wraps route middleware arrays in createFileRoute() files.
*/
export function wrapRouteMiddleware(code: string, id: string, debug: boolean): WrapResult {
const skipped: string[] = [];
let didWrap = false;

const transformed = code.replace(
/(\s+)(middleware)\s*:\s*\[([^\]]*)\]/g,
(match: string, whitespace: string, key: string, contents: string) => {
const objContents = arrayToObjectShorthand(contents);
if (objContents) {
didWrap = true;
if (debug) {
// eslint-disable-next-line no-console
console.log(`[Sentry] Auto-wrapping route ${key} in ${id}`);
}
return `${whitespace}${key}: wrapMiddlewaresWithSentry(${objContents})`;
}
// Track middlewares that couldn't be auto-wrapped
// Skip if we matched whitespace only
if (contents.trim()) {
skipped.push(`route ${key}`);
}
return match;
},
);

return { code: transformed, didWrap, skipped };
return wrapMiddlewareArrays(code, id, debug, /(middleware)\s*:\s*\[([^\]]*)\]/g);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ export const Route = createFileRoute('/foo')({
const result = wrapRouteMiddleware(code, '/app/routes/foo.ts', false);

expect(result.didWrap).toBe(false);
expect(result.skipped).toContain('route middleware');
expect(result.skipped).toContain('middleware');
});

it('wraps both route-level and handler-level middleware in same file', () => {
Expand Down Expand Up @@ -323,7 +323,7 @@ export const Route = createFileRoute('/foo')({
`;
wrapRouteMiddleware(code, '/app/routes/foo.ts', true);

expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('Auto-wrapping route middleware'));
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('Auto-wrapping middleware'));

consoleLogSpy.mockRestore();
});
Expand Down
Loading