API recommendations #269
Replies: 2 comments
-
Just saw this! Thanks so much for the ideas.
Prone to agree, the only con is that
Let's keep that discussion in #210
I actually disagree, I don't like when libraries occupy "generic" namespaces as I just keep on importing the wrong ones. Also for the next adapter it's easier to find it by autocompletion if it's named since we have several adapters.
There is already
Strongly disagree, it's much cleaner to read as it is now when working with it (which I've done a lot the last few months) aaand I have some plans on how we would eventually do batching etc using the current structure as well. Appreciate the input! |
Beta Was this translation helpful? Give feedback.
-
All seems reasonable! A few followups:
What are the polyfills for
That's understandable. A lot of these recommendations stem from the fact that the sample code in the docs just seems to complicated to me. The v0.x syntax was super clean and understandable, no function names to remember. I feel like it's gotten a lot more intimidating. Some of this was unavoidable due to the addition of more features, but in general I think you should prioritize clean, understandable code over collision avoidance. That's why I like the idea of merging
Sorry, I forgot to put in a code example. What I'd like to see is a const createContext = createCreateContext(opts => {
return {};
});
type Context = ReturnType<typeof createContext> The point of this is to avoid having to remember the name "CreateNextContextOptions".
You mean in the Requests devtools or something? Hadn't thought of that...makes sense. |
Beta Was this translation helpful? Give feedback.
-
Some recommendations for improvements to the API:
@trpc/server
and@trpc/client
. I think they should be merged into a singletrpc
module. It makes the out-of-the-box experience so much cleaner.@trpc
scope can be used for specific plugins/adapters etc. You could publish the Express adapter as@trpc/express
for instance. That would also let you properly specify all the requisite peerDependencies (which I don't believe are specified using the current@trpc/server/adapters/express
approach since it doesn't have its own package.json.import * as
imports are a bad user experience. I submitted a PR with a solution.createTRPCClient
=>createClient
,createNextApiHandler
=>createHandler
,CreateNextContextOptions
=>ContextOptions
. It should be standard across adapters. It's obvious that a function calledcreateHandler
imported from"@trpc/server/adapters/next"
is creating a Next.js API handler. It doesn't need to be in the name.createContext
function that can automatically typeopts
with the necessary types.path
can be added as an additional query parameter for queries and in thebody
for mutations. This would also simplify the Next/Express adapters (no need for a catchall API route).Less important:
I think there should be a way of manually typing the input of a resolver without passing a validator into
input
. This is easy to do if the resolver functions had two parameters instead of one. I have this working on a local branch (type inference still works the same).I propose this syntax/convention:
The
req
variable above if of type{ ctx: TContext, type: ProcedureType }
. This leaves the door open to future API expansion, asreq
can act like a catchall for any additional properties we want to pass into resolvers.Beta Was this translation helpful? Give feedback.
All reactions