routeHandlerWrapperTemplate.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { _nullishCoalesce, _optionalChain } from '@sentry/utils';
  2. import * as Sentry from '@sentry/nextjs';
  3. import { requestAsyncStorage } from '__SENTRY_NEXTJS_REQUEST_ASYNC_STORAGE_SHIM__';
  4. import * as serverComponentModule from '__SENTRY_WRAPPING_TARGET_FILE__';
  5. export * from '__SENTRY_WRAPPING_TARGET_FILE__';
  6. export { default } from '__SENTRY_WRAPPING_TARGET_FILE__';
  7. function wrapHandler(handler, method) {
  8. // Running the instrumentation code during the build phase will mark any function as "dynamic" because we're accessing
  9. // the Request object. We do not want to turn handlers dynamic so we skip instrumentation in the build phase.
  10. if (process.env.NEXT_PHASE === 'phase-production-build') {
  11. return handler;
  12. }
  13. if (typeof handler !== 'function') {
  14. return handler;
  15. }
  16. return new Proxy(handler, {
  17. apply: (originalFunction, thisArg, args) => {
  18. let sentryTraceHeader = undefined;
  19. let baggageHeader = undefined;
  20. let headers = undefined;
  21. // We try-catch here just in case the API around `requestAsyncStorage` changes unexpectedly since it is not public API
  22. try {
  23. // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
  24. const requestAsyncStore = requestAsyncStorage.getStore() ;
  25. sentryTraceHeader = _nullishCoalesce(_optionalChain([requestAsyncStore, 'optionalAccess', _ => _.headers, 'access', _2 => _2.get, 'call', _3 => _3('sentry-trace')]), () => ( undefined));
  26. baggageHeader = _nullishCoalesce(_optionalChain([requestAsyncStore, 'optionalAccess', _4 => _4.headers, 'access', _5 => _5.get, 'call', _6 => _6('baggage')]), () => ( undefined));
  27. headers = _optionalChain([requestAsyncStore, 'optionalAccess', _7 => _7.headers]);
  28. } catch (e) {
  29. /** empty */
  30. }
  31. // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
  32. return Sentry.wrapRouteHandlerWithSentry(originalFunction , {
  33. method,
  34. parameterizedRoute: '__ROUTE__',
  35. sentryTraceHeader,
  36. baggageHeader,
  37. headers,
  38. }).apply(thisArg, args);
  39. },
  40. });
  41. }
  42. // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
  43. const GET = wrapHandler(serverComponentModule.GET , 'GET');
  44. // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
  45. const POST = wrapHandler(serverComponentModule.POST , 'POST');
  46. // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
  47. const PUT = wrapHandler(serverComponentModule.PUT , 'PUT');
  48. // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
  49. const PATCH = wrapHandler(serverComponentModule.PATCH , 'PATCH');
  50. // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
  51. const DELETE = wrapHandler(serverComponentModule.DELETE , 'DELETE');
  52. // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
  53. const HEAD = wrapHandler(serverComponentModule.HEAD , 'HEAD');
  54. // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
  55. const OPTIONS = wrapHandler(serverComponentModule.OPTIONS , 'OPTIONS');
  56. export { DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT };