njwags
njwags11mo ago

One router multiple adapters?

Hey all! This might be misguided question, but I'm working on building an api with tRPC that will be connected to via two clients. One is a Next app and one is a CLI built with node.js via yargs. The next app is hosted via vercel, and the api is running on the vercel instance with experimental_createTRPCNextAppDirServer and the fetchRequestHandler from the fetch adapter. The cli can connect and the web app works and all is well, but... I'm trying to write integration tests for the CLI and I was hoping to use the standalone adapter to run the api during tests. https://trpc.io/docs/server/adapters/standalone The error I'm running into is a type mismatch for the createContext option. The two adapters seem to expect different types for the createContext option. Because of the line of generics associated with the two types, the actual type error message is about 43,000 characters... So including it here isn't possible. It boils down to: '(opts?: FetchCreateContextFnOptions | undefined) => Promise<{ session: Session; responseCookies: ResponseCookies; headers: { [k: string]: string; } | undefined; }>' is not assignable to type 'NodeHTTPCreateContextFn<CreateRouterInner<RootConfig<... and so on. TL;DR; My questions are: Has anyone tried using the same router with multiple adapters? Is there a better approach to setting up a tRPC api that's used via two different adapters? Is there some other way to accomplish these kind of integration tests? Maybe I'm just making somekind of dumb typescript mistake? Thanks for any advice!
1 Reply
njwags
njwags11mo ago
I'd still be curious to hear thoughts from anyone with more tRPC knowledge, but I've currently got a solution where I use the node.js native http.createServer to start a server and then I map the node.js request instance to the Fetch API request instances, then pass it to fetchRequestHandler. I then have to map the response of that call to the node.js response instance. Looks like hack for sure, but it works...