Skip to content

Commit

Permalink
fix(core): Update trpc middleware types (#13859)
Browse files Browse the repository at this point in the history
  • Loading branch information
chargome authored Oct 3, 2024
1 parent 0b739c5 commit 5c06bd2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ export const createCallerFactory = t.createCallerFactory;
*/
export const createTRPCRouter = t.router;

const sentryMiddleware = Sentry.trpcMiddleware({
attachRpcInput: true,
});

export const publicProcedure = t.procedure.use(async opts => sentryMiddleware(opts));
export const publicProcedure = t.procedure.use(
Sentry.trpcMiddleware({
attachRpcInput: true,
}),
);
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ Sentry.addEventProcessor(event => {

export const t = initTRPC.context<Context>().create();

const sentryMiddleware = Sentry.trpcMiddleware({ attachRpcInput: true });

const procedure = t.procedure.use(async opts => sentryMiddleware(opts));
const procedure = t.procedure.use(Sentry.trpcMiddleware({ attachRpcInput: true }));

export const appRouter = t.router({
getSomething: procedure.input(z.string()).query(opts => {
Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ function captureIfError(nextResult: unknown): void {
}
}

type SentryTrpcMiddleware<T> = T extends Promise<unknown> ? T : Promise<T>;

/**
* Sentry tRPC middleware that captures errors and creates spans for tRPC procedures.
*/
export function trpcMiddleware(options: SentryTrpcMiddlewareOptions = {}) {
return async function <T>(opts: SentryTrpcMiddlewareArguments<T>): Promise<T> {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return async function <T>(opts: SentryTrpcMiddlewareArguments<T>): SentryTrpcMiddleware<T> {
const { path, type, next, rawInput, getRawInput } = opts;

const client = getClient();
Expand Down Expand Up @@ -85,6 +89,6 @@ export function trpcMiddleware(options: SentryTrpcMiddlewareOptions = {}) {
throw e;
}
},
);
) as SentryTrpcMiddleware<T>;
};
}

0 comments on commit 5c06bd2

Please sign in to comment.