Skip to content

Commit f7f4c22

Browse files
committed
refactor
1 parent e985b3e commit f7f4c22

File tree

2 files changed

+28
-48
lines changed

2 files changed

+28
-48
lines changed

packages/tanstackstart-react/src/vite/autoInstrumentMiddleware.ts

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -12,65 +12,45 @@ type WrapResult = {
1212
};
1313

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

21-
const transformed = code.replace(
22-
/(requestMiddleware|functionMiddleware)\s*:\s*\[([^\]]*)\]/g,
23-
(match: string, key: string, contents: string) => {
24-
const objContents = arrayToObjectShorthand(contents);
25-
if (objContents) {
26-
didWrap = true;
27-
if (debug) {
28-
// eslint-disable-next-line no-console
29-
console.log(`[Sentry] Auto-wrapping ${key} in ${id}`);
30-
}
31-
return `${key}: wrapMiddlewaresWithSentry(${objContents})`;
32-
}
33-
// Track middlewares that couldn't be auto-wrapped
34-
// Skip if we matched whitespace only
35-
if (contents.trim()) {
36-
skipped.push(key);
21+
const transformed = code.replace(regex, (match: string, key: string, contents: string) => {
22+
const objContents = arrayToObjectShorthand(contents);
23+
if (objContents) {
24+
didWrap = true;
25+
if (debug) {
26+
// eslint-disable-next-line no-console
27+
console.log(`[Sentry] Auto-wrapping ${key} in ${id}`);
3728
}
38-
return match;
39-
},
40-
);
29+
return `${key}: wrapMiddlewaresWithSentry(${objContents})`;
30+
}
31+
// Track middlewares that couldn't be auto-wrapped
32+
// Skip if we matched whitespace only
33+
if (contents.trim()) {
34+
skipped.push(key);
35+
}
36+
return match;
37+
});
4138

4239
return { code: transformed, didWrap, skipped };
4340
}
4441

42+
/**
43+
* Wraps global middleware arrays (requestMiddleware, functionMiddleware) in createStart() files.
44+
*/
45+
export function wrapGlobalMiddleware(code: string, id: string, debug: boolean): WrapResult {
46+
return wrapMiddlewareArrays(code, id, debug, /(requestMiddleware|functionMiddleware)\s*:\s*\[([^\]]*)\]/g);
47+
}
48+
4549
/**
4650
* Wraps route middleware arrays in createFileRoute() files.
4751
*/
4852
export function wrapRouteMiddleware(code: string, id: string, debug: boolean): WrapResult {
49-
const skipped: string[] = [];
50-
let didWrap = false;
51-
52-
const transformed = code.replace(
53-
/(\s+)(middleware)\s*:\s*\[([^\]]*)\]/g,
54-
(match: string, whitespace: string, key: string, contents: string) => {
55-
const objContents = arrayToObjectShorthand(contents);
56-
if (objContents) {
57-
didWrap = true;
58-
if (debug) {
59-
// eslint-disable-next-line no-console
60-
console.log(`[Sentry] Auto-wrapping route ${key} in ${id}`);
61-
}
62-
return `${whitespace}${key}: wrapMiddlewaresWithSentry(${objContents})`;
63-
}
64-
// Track middlewares that couldn't be auto-wrapped
65-
// Skip if we matched whitespace only
66-
if (contents.trim()) {
67-
skipped.push(`route ${key}`);
68-
}
69-
return match;
70-
},
71-
);
72-
73-
return { code: transformed, didWrap, skipped };
53+
return wrapMiddlewareArrays(code, id, debug, /(middleware)\s*:\s*\[([^\]]*)\]/g);
7454
}
7555

7656
/**

packages/tanstackstart-react/test/vite/autoInstrumentMiddleware.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ export const Route = createFileRoute('/foo')({
285285
const result = wrapRouteMiddleware(code, '/app/routes/foo.ts', false);
286286

287287
expect(result.didWrap).toBe(false);
288-
expect(result.skipped).toContain('route middleware');
288+
expect(result.skipped).toContain('middleware');
289289
});
290290

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

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

328328
consoleLogSpy.mockRestore();
329329
});

0 commit comments

Comments
 (0)