Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

createCaller impossible to use with context that requires NextRequest #3

Open
Bahnasawy opened this issue Aug 25, 2023 · 1 comment

Comments

@Bahnasawy
Copy link

Bahnasawy commented Aug 25, 2023

I tried using this example to create a caller for my app which had both drizzle and clerk in the context, but it was impossible to make authed calls without the req/NextRequest object, so I tried replacing it with createTRPCProxyClient and it worked just fine like so:

const serverClient = createTRPCProxyClient<AppRouter>({
  transformer,
  links: [
    httpBatchLink({
      url: "http://localhost:3000/api/trpc",
    }),
  ],
});

I also had to add the req to the handler in the api route file like so:

const handler = (req: Request) =>
  fetchRequestHandler({
    endpoint: "/api/trpc",
    req,
    router: appRouter,
    createContext: () => createContext({ req }),
  });

With my createContext looking like this

export const createContextInner = async ({ auth }: AuthContextProps) => {
  return {
    auth,
    db,
  };
};

export const createTRPCContext = async ({ req }: { req: Request }) => {
  // @ts-ignore
  return await createContextInner({ auth: getAuth(req) });
};

Still this example is incredible and I'm very grateful that you came up with it, incredible work!!

@tanjunior
Copy link

There is a new import { experimental_createTRPCNextAppDirServer } from "@trpc/next/app-dir/server";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants