Skip to content

v7.80.0 breaks tRPC middleware handler (Typescript) #9536

Description

@spaceemotion

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/node

SDK Version

7.80.0

Framework Version

No response

Link to Sentry event

No response

SDK Setup

The following downgrade of all dependencies worked, the combination of all latest doesnt:

    "@sentry/esbuild-plugin": "^2.10.0",
-   "@sentry/node": "^7.80.0",
-   "@sentry/serverless": "^7.80.0",
+   "@sentry/node": "^7.79.0",
+   "@sentry/serverless": "^7.79.0",
-   "@trpc/server": "^10.43.3",
+   "@trpc/server": "^10.43.2",

Here is my trpc setup:

import { Handlers } from '@sentry/node';
import { TRPCError, initTRPC } from '@trpc/server';
import superjson from 'superjson';

import { type Context } from './context';

export const t = initTRPC
  .context<Context>()
  .create({
    transformer: superjson,
  });

export const middleware = t.middleware;
export const router = t.router;

const sentryMiddleware = middleware(
  // 1.
  Handlers.trpcMiddleware({
    attachRpcInput: false,
  }),
);

const isAuthenticated = middleware(({ next, ctx }) => {
  if (ctx.user === null) {
    throw new TRPCError({
      code: 'UNAUTHORIZED',
      message: 'Unauthorized',
    });
  }

  return next({
    ctx: {
      ...ctx,
      user: ctx.user,
    },
  });
});

export const publicProcedure = t.procedure.use(sentryMiddleware);
export const protectedProcedure = t.procedure.use(sentryMiddleware).use(isAuthenticated); // 2.

Depending on which combination of versions i tried, no. 1 had a type error (the middleware is now async), or no. 2 had the wrong context. Swapping the Auth middleware with Sentry had a similar result (type errors)

Steps to Reproduce

  1. Create a tRPC server with latest sentry integration
  2. Try to compile with typescript

Expected Result

No Type errors, project compiles

Actual Result

Error: ../server/src/trpc.ts(17,3): error TS[23](https://github.com/spaceemotion/novelcrafter/actions/runs/6843475593/job/18606103847#step:11:24)45: Argument of type '<T>({ path, type, next, rawInput }: TrpcMiddlewareArguments<T>) => Promise<T>' is not assignable to parameter of type 'MiddlewareFunction<{ _config: RootConfig<{ ctx: { user: { id: string; } | null; req: FastifyRequest<RouteGenericInterface, RawServerDefault, IncomingMessage, FastifySchema, FastifyTypeProviderDefault, unknown, FastifyBaseLogger, ResolveFastifyRequestType<...>>; res: FastifyReply<...>; }; meta: object; errorShape: De...'.
  Type 'Promise<Promise<MiddlewareResult<{ _config: RootConfig<{ ctx: { user: { id: string; } | null; req: FastifyRequest<RouteGenericInterface, RawServerDefault, IncomingMessage, ... 4 more ..., ResolveFastifyRequestType<...>>; res: FastifyReply<...>; }; meta: object; errorShape: DefaultErrorShape; transformer: typeof Supe...' is not assignable to type 'Promise<MiddlewareResult<ProcedureParams<AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>>'.
    Type 'Promise<MiddlewareResult<{ _config: RootConfig<{ ctx: { user: { id: string; } | null; req: FastifyRequest<RouteGenericInterface, RawServerDefault, IncomingMessage, ... 4 more ..., ResolveFastifyRequestType<...>>; res: FastifyReply<...>; }; meta: object; errorShape: DefaultErrorShape; transformer: typeof SuperJSON; }...' is not assignable to type 'MiddlewareResult<ProcedureParams<AnyRootConfig, unknown, unknown, unknown, unknown, unknown, unknown>>'.
Error: ../server/src/trpc.ts(39,73): error TS2345: Argument of type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { user: { id: string; } | null; req: FastifyRequest<RouteGenericInterface, RawServerDefault, IncomingMessage, FastifySchema, FastifyTypeProviderDefault, unknown, FastifyBaseLogger, ResolveFastifyRequestType<...>>; res: FastifyReply<...>; }; meta: object; errorShape: Def...' is not assignable to parameter of type 'MiddlewareBuilder<{ _config: RootConfig<{ ctx: { user: { id: string; } | null; req: FastifyRequest<RouteGenericInterface, RawServerDefault, IncomingMessage, ... 4 more ..., ResolveFastifyRequestType<...>>; res: FastifyReply<...>; }; meta: object; errorShape: DefaultErrorShape; transformer: typeof SuperJSON; }>; ... ...'.
  Type 'import("/home/runner/work/novelcrafter/novelcrafter/application/node_modules/.pnpm/@trpc+server@10.43.3/node_modules/@trpc/server/dist/core/middleware").MiddlewareBuilder<{ _config: import("/home/runner/work/novelcrafter/novelcrafter/application/node_modules/.pnpm/@trpc+server@10.43.3/node_modules/@trpc/server/dist/...' is not assignable to type 'import("/home/runner/work/novelcrafter/novelcrafter/application/node_modules/.pnpm/@trpc+server@10.43.3/node_modules/@trpc/server/dist/core/middleware").MiddlewareBuilder<{ _config: import("/home/runner/work/novelcrafter/novelcrafter/application/node_modules/.pnpm/@trpc+server@10.43.3/node_modules/@trpc/server/dist/...'. Two different types with this name exist, but they are unrelated.
    The types of 'unstable_pipe(...)._middlewares' are incompatible between these types.
      Type 'import("/home/runner/work/novelcrafter/novelcrafter/application/node_modules/.pnpm/@trpc+server@10.43.3/node_modules/@trpc/server/dist/core/middleware").MiddlewareFunction<{ _config: import("/home/runner/work/novelcrafter/novelcrafter/application/node_modules/.pnpm/@trpc+server@10.43.3/node_modules/@trpc/server/dist...' is not assignable to type 'import("/home/runner/work/novelcrafter/novelcrafter/application/node_modules/.pnpm/@trpc+server@10.43.3/node_modules/@trpc/server/dist/core/middleware").MiddlewareFunction<{ _config: import("/home/runner/work/novelcrafter/novelcrafter/application/node_modules/.pnpm/@trpc+server@10.43.3/node_modules/@trpc/server/dist...'. Two different types with this name exist, but they are unrelated.
        Type 'import("/home/runner/work/novelcrafter/novelcrafter/application/node_modules/.pnpm/@trpc+server@10.43.3/node_modules/@trpc/server/dist/core/middleware").MiddlewareFunction<{ _config: import("/home/runner/work/novelcrafter/novelcrafter/application/node_modules/.pnpm/@trpc+server@10.43.3/node_modules/@trpc/server/dist...' is not assignable to type 'import("/home/runner/work/novelcrafter/novelcrafter/application/node_modules/.pnpm/@trpc+server@10.43.3/node_modules/@trpc/server/dist/core/middleware").MiddlewareFunction<{ _config: import("/home/runner/work/novelcrafter/novelcrafter/application/node_modules/.pnpm/@trpc+server@10.43.3/node_modules/@trpc/server/dist...'. Two different types with this name exist, but they are unrelated.
          Types of parameters 'opts' and 'opts' are incompatible.
            Type '{ ctx: {}; type: "query" | "mutation" | "subscription"; path: string; input: unique symbol; rawInput: unknown; meta: object | undefined; next: { (): Promise<MiddlewareResult<{ _config: RootConfig<{ ctx: { user: { id: string; } | null; req: FastifyRequest<...>; res: FastifyReply<...>; }; meta: object; errorShape: Def...' is not assignable to type '{ ctx: { user: { id: string; } | null; req: FastifyRequest<RouteGenericInterface, RawServerDefault, IncomingMessage, FastifySchema, FastifyTypeProviderDefault, unknown, FastifyBaseLogger, ResolveFastifyRequestType<...>>; res: FastifyReply<...>; }; ... 5 more ...; next: { ...; }; }'.
              Types of property 'ctx' are incompatible.
                Type '{}' is missing the following properties from type '{ user: { id: string; } | null; req: FastifyRequest<RouteGenericInterface, RawServerDefault, IncomingMessage, FastifySchema, FastifyTypeProviderDefault, unknown, FastifyBaseLogger, ResolveFastifyRequestType<...>>; res: FastifyReply<...>; }': user, req, res

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions