-
Notifications
You must be signed in to change notification settings - Fork 3
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some minor naming things I'd like input on.
src/trpc/server/initTRPC.ts
Outdated
/** | ||
* Create procedure resolver | ||
* Builder object for creating procedures | ||
*/ | ||
resolver: pipedResolver<TContext>(), | ||
procedure: createProcedure<TContext>(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be called builder
or something else perhaps?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think procedure
is fine, maybe procedureBuilder
if you want to be more clear about what it is, although I think it's clear enough already.
src/trpc/server/Params.ts
Outdated
/** | ||
* @internal | ||
*/ | ||
export interface Params< | ||
TContextIn = unknown, | ||
TContextOut = unknown, | ||
TInputIn = unknown, | ||
TInputOut = unknown, | ||
TOutputIn = unknown, | ||
TOutputOut = unknown, | ||
> { | ||
/** | ||
* @internal | ||
*/ | ||
_ctx_in: TContextIn; | ||
/** | ||
* @internal | ||
*/ | ||
_ctx_out: TContextOut; | ||
/** | ||
* @internal | ||
*/ | ||
_output_in: TOutputIn; | ||
/** | ||
* @internal | ||
*/ | ||
_output_out: TOutputOut; | ||
/** | ||
* @internal | ||
*/ | ||
_input_in: TInputIn; | ||
/** | ||
* @internal | ||
*/ | ||
_input_out: TInputOut; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mmkal I guess this is what you referred to as an options-bag? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried to write a reply to this on my phone but too hard! Yes this is pretty much what I was thinking. A couple of ways it could be simplified even further occur to me tho:
- No linear generics at all. This might prove impossible if the compiler loses track of property types somewhere along the way, but here's a simplified example:
interface Params {
input: unknown
output: unknown
context: unknown
}
const getClientThatUsesSpecificParams = <P extends Params>(params: P) => {
return {
useInput: (input: P['input']) => 123,
getOutput: () => params.output,
}
}
const specific = getClientThatUsesSpecificParams({
input: {foo: 'abc'},
output: [123, 456],
context: {bar: 987},
})
TypeScript doesn't need specific typeargs for TInput
, TOutput
, TContext
to keep track of them:
- Maybe there could be an IO type to generalise the
_in
and_out
suffixes?
interface IO<I, O> {
in: I
out: O
}
interface Params<
TContextIn = unknown,
TContextOut = unknown,
TInputIn = unknown,
TInputOut = unknown,
TOutputIn = unknown,
TOutputOut = unknown,
> {
ctx: IO<TContextIn, TContextOut>
input: IO<TInputIn, TInputOut>
output: IO<TOutputIn, TOutputOut>
}
- Both together!?!!?
interface IO {
input: unknown
output: unknown
}
interface Params {
ctx: IO
input: IO
output: IO
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made #35. Feel free to do PR, I have some decent testing and it's easy to run with yarn dev.
These are all internal types so easy to refactor later too, but would be nice to have it clean
I love this |
It looks so good! I only have two minor questions:
export const appRouter = trpc.mergeRouters({
post: postRouter
}, health);
|
I've let this marinate, and I think chaining for the resolver API could be nicer. I've here created a POC that you can compare side-by-side with the
main
branch. Closes #25 & Closes #24.Notes
throw Error
below; imaginethrow TRPCError({ code: '..'})
input
before or after your middlewares run, it's up to you!§1 Basics
§1.0 Setting up tRPC
§1.1 Creating a router
§1.2 Defining a procedure
Details about the procedure builder
Simplified to be more readable - see full implementation in https://github.com/trpc/v10-playground/blob/katt/procedure-chains/src/trpc/server/procedure.ts
§1.3 Adding input parser
§1.4 Procedure with middleware
§2 Intermediate 🍿
§2.1 Define a reusable middleware
§2.2 Create a bunch of procedures that are all protected
§2.3 Define an
output
schema§2.4 Merging routers
§3 Advanced 🧙
Compose dynamic combos of middlewares/input parsers