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

feat: add a type inferRouterFunctions<AppRouter> #5959

Open
1 task done
ghdoergeloh opened this issue Aug 22, 2024 · 2 comments
Open
1 task done

feat: add a type inferRouterFunctions<AppRouter> #5959

ghdoergeloh opened this issue Aug 22, 2024 · 2 comments

Comments

@ghdoergeloh
Copy link

ghdoergeloh commented Aug 22, 2024

Describe the feature you'd like to request

It would be nice to have the type of a procedure function. So not only the input or the output, but the function that is called by it. I needed it to create typesafe mock functions.

Describe the solution you'd like to see

I copied the GetInferenceHelpers and changed it a bit.

export type GetInferenceHelpers<
  TRoot extends AnyClientTypes,
  TRecord extends RouterRecord,
> = {
  [TKey in keyof TRecord]: TRecord[TKey] extends infer $Value
    ? $Value extends RouterRecord
      ? GetInferenceHelpers<TRoot, $Value>
      : $Value extends AnyProcedure
        ? (
            input: inferProcedureInput<$Value>,
          ) => inferTransformedProcedureOutput<TRoot, $Value>
        : never
    : never;
};

export type inferRouterFunctions<TRouter extends AnyRouter> =
  GetInferenceHelpers<
    TRouter["_def"]["_config"]["$types"],
    TRouter["_def"]["record"]
  >;

Now I can use it for my AppRouter

export type RouterFunctions = inferRouterFunctions<AppRouter>;

And finally create some mocks:

const accountList = vi.fn<RouterFunctions["account"]["list"]>(() => ({
  items: [],
  total: 0,
}));

which I can use for trpc-msw:

server.use(trpcMsw.account.list.query(accountList));

Describe alternate solutions

My workaround was to add this code into my own project.
But then I have these Imports from @trpc/server/src/unstable-core-do-not-import which is not the best practice.
So It would be nice, if you could just add the type.

Additional information

No response

👨‍👧‍👦 Contributing

  • 🙋‍♂️ Yes, I'd be down to file a PR implementing this feature!

Funding

  • You can sponsor this specific effort via a Polar.sh pledge below
  • We receive the pledge once the issue is completed & verified
Fund with Polar
@juliusmarminge
Copy link
Member

juliusmarminge commented Aug 27, 2024

Is it too much to get both inputs and outputs? Like sure this could be some syntactic sugar but isn't this already "good enough"?

const accountList = vi.fn<
  (input: RouterInputs["account"]["list"]) => Promise<RouterOutputs["account"]["list"]>
>(() => Promise.resolve({ items: [], total: 0 }));

@ghdoergeloh
Copy link
Author

There would be more type than actual code, which makes it a bit confusing. It quickly becomes a lot of types if you need to mock multiple routes. So I thought a extra type would be nice. If you add this to the original code it would be just about 6 extra lines, saving a lot of extra lines in the test files.

Therefore I would say, it is worth the tiny patch.

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