@sentry/nextjs
Version:
Official Sentry SDK for Next.js
64 lines (56 loc) • 3.15 kB
JavaScript
import { _nullishCoalesce, _optionalChain } from '@sentry/utils';
import * as Sentry from '@sentry/nextjs';
import { requestAsyncStorage } from '__SENTRY_NEXTJS_REQUEST_ASYNC_STORAGE_SHIM__';
import * as serverComponentModule from '__SENTRY_WRAPPING_TARGET_FILE__';
export * from '__SENTRY_WRAPPING_TARGET_FILE__';
export { default } from '__SENTRY_WRAPPING_TARGET_FILE__';
function wrapHandler(handler, method) {
// Running the instrumentation code during the build phase will mark any function as "dynamic" because we're accessing
// the Request object. We do not want to turn handlers dynamic so we skip instrumentation in the build phase.
if (process.env.NEXT_PHASE === 'phase-production-build') {
return handler;
}
if (typeof handler !== 'function') {
return handler;
}
return new Proxy(handler, {
apply: (originalFunction, thisArg, args) => {
let sentryTraceHeader = undefined;
let baggageHeader = undefined;
let headers = undefined;
// We try-catch here just in case the API around `requestAsyncStorage` changes unexpectedly since it is not public API
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const requestAsyncStore = requestAsyncStorage.getStore() ;
sentryTraceHeader = _nullishCoalesce(_optionalChain([requestAsyncStore, 'optionalAccess', _ => _.headers, 'access', _2 => _2.get, 'call', _3 => _3('sentry-trace')]), () => ( undefined));
baggageHeader = _nullishCoalesce(_optionalChain([requestAsyncStore, 'optionalAccess', _4 => _4.headers, 'access', _5 => _5.get, 'call', _6 => _6('baggage')]), () => ( undefined));
headers = _optionalChain([requestAsyncStore, 'optionalAccess', _7 => _7.headers]);
} catch (e) {
/** empty */
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
return Sentry.wrapRouteHandlerWithSentry(originalFunction , {
method,
parameterizedRoute: '__ROUTE__',
sentryTraceHeader,
baggageHeader,
headers,
}).apply(thisArg, args);
},
});
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const GET = wrapHandler(serverComponentModule.GET , 'GET');
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const POST = wrapHandler(serverComponentModule.POST , 'POST');
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const PUT = wrapHandler(serverComponentModule.PUT , 'PUT');
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const PATCH = wrapHandler(serverComponentModule.PATCH , 'PATCH');
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const DELETE = wrapHandler(serverComponentModule.DELETE , 'DELETE');
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const HEAD = wrapHandler(serverComponentModule.HEAD , 'HEAD');
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const OPTIONS = wrapHandler(serverComponentModule.OPTIONS , 'OPTIONS');
export { DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT };