Skip to content

Commit abf9b5f

Browse files
committed
refactor functions
1 parent 5e48038 commit abf9b5f

File tree

1 file changed

+40
-34
lines changed

1 file changed

+40
-34
lines changed

packages/astro/src/integration/index.ts

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -177,40 +177,7 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => {
177177
return;
178178
}
179179

180-
/**
181-
* Astro lowercases the parametrized route. Joining segments manually is recommended to get the correct casing of the routes.
182-
* Recommendation in comment: https://github.com/withastro/astro/issues/13885#issuecomment-2934203029
183-
* Function Reference: https://github.com/joanrieu/astro-typed-links/blob/b3dc12c6fe8d672a2bc2ae2ccc57c8071bbd09fa/package/src/integration.ts#L16
184-
*/
185-
const joinSegments = (segments: RoutePart[][]): string => {
186-
const parthArray = segments.map(segment =>
187-
segment.map(routePart => (routePart.dynamic ? `[${routePart.content}]` : routePart.content)).join(''),
188-
);
189-
190-
return `/${parthArray.join('/')}`;
191-
};
192-
193-
try {
194-
const serverInitContent = readFileSync(sentryServerInitPath, 'utf8');
195-
196-
const updatedServerInitContent = `${serverInitContent}\nglobalThis["__sentryRouteInfo"] = ${JSON.stringify(
197-
routes.map(route => {
198-
return {
199-
...route,
200-
patternCaseSensitive: joinSegments(route.segments), // Store parametrized routes with correct casing on `globalThis` to be able to use them on the server during runtime
201-
patternRegex: route.patternRegex.source, // using `source` to be able to serialize the regex
202-
};
203-
}),
204-
null,
205-
2,
206-
)};`;
207-
208-
writeFileSync(sentryServerInitPath, updatedServerInitContent, 'utf8');
209-
210-
debug.log('Successfully added route pattern information to Sentry server file:', sentryServerInitPath);
211-
} catch (error) {
212-
debug.warn(`Failed to write to sentry client init file at ${sentryServerInitPath}:`, error);
213-
}
180+
includeRouteDataToConfigFile(sentryServerInitPath, routes);
214181
},
215182
},
216183
};
@@ -318,3 +285,42 @@ export function getUpdatedSourceMapSettings(
318285

319286
return { previousUserSourceMapSetting, updatedSourceMapSetting };
320287
}
288+
289+
/**
290+
* Join Astro route segments into a case-sensitive single path string.
291+
*
292+
* Astro lowercases the parametrized route. Joining segments manually is recommended to get the correct casing of the routes.
293+
* Recommendation in comment: https://github.com/withastro/astro/issues/13885#issuecomment-2934203029
294+
* Function Reference: https://github.com/joanrieu/astro-typed-links/blob/b3dc12c6fe8d672a2bc2ae2ccc57c8071bbd09fa/package/src/integration.ts#L16
295+
*/
296+
function joinRouteSegments(segments: RoutePart[][]): string {
297+
const parthArray = segments.map(segment =>
298+
segment.map(routePart => (routePart.dynamic ? `[${routePart.content}]` : routePart.content)).join(''),
299+
);
300+
301+
return `/${parthArray.join('/')}`;
302+
}
303+
304+
function includeRouteDataToConfigFile(sentryInitPath: string, routes: IntegrationResolvedRoute[]): void {
305+
try {
306+
const serverInitContent = readFileSync(sentryInitPath, 'utf8');
307+
308+
const updatedServerInitContent = `${serverInitContent}\nglobalThis["__sentryRouteInfo"] = ${JSON.stringify(
309+
routes.map(route => {
310+
return {
311+
...route,
312+
patternCaseSensitive: joinRouteSegments(route.segments), // Store parametrized routes with correct casing on `globalThis` to be able to use them on the server during runtime
313+
patternRegex: route.patternRegex.source, // using `source` to be able to serialize the regex
314+
};
315+
}),
316+
null,
317+
2,
318+
)};`;
319+
320+
writeFileSync(sentryInitPath, updatedServerInitContent, 'utf8');
321+
322+
debug.log('Successfully added route pattern information to Sentry config file:', sentryInitPath);
323+
} catch (error) {
324+
debug.warn(`Failed to write to Sentry config file at ${sentryInitPath}:`, error);
325+
}
326+
}

0 commit comments

Comments
 (0)