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: types to infer middleware params #5110

Open
1 task done
luixo opened this issue Nov 27, 2023 · 0 comments
Open
1 task done

feat: types to infer middleware params #5110

luixo opened this issue Nov 27, 2023 · 0 comments

Comments

@luixo
Copy link
Contributor

luixo commented Nov 27, 2023

Describe the feature you'd like to request

Hi,
I have a procedure (technically, it's called procedure builder as an internal type) that is updating context. I want to reuse object of this context in an external function, but I have hard time extracting it.
E.g.:

import { initTRPC, TRPCError } from "@trpc/server";

type UnauthorizedContext = {
  req: NextApiRequest;
  res: NextApiResponse;
};

const { router, procedure, middleware } = initTRPC.context<UnauthorizedContext>().create();

const authProcedure = t.procedure.use(
  t.middleware(async ({ ctx, next }) => {
    const authObject = getAuthObject(ctx.req);
    if (!authObject) {
      throw new TRPCError({
        code: "UNAUTHORIZED",
        message: "Not authorized",
      });
    }
    return next({
      ctx: { ...ctx, auth: authObject, },
    });
  }),
);

type Auth = {
  // this is type I want to infer
}
const getDataByAuth = (auth: Auth) => {
  // do stuff
}

const procedure = authProcedure
  .mutation(async ({ ctx }) => {
    const data = getDataByAuth(ctx.auth);
    return data;
  });

export const appRouter = t.router({ procedure });

Currently I infer type like that:

type ProcedureParams<P extends ProcedureBuilder<any>> = P extends ProcedureBuilder<infer X> ? X : never;
type ProcedureContext<P extends ProcedureBuilder<any>> = ProcedureParams<P>["_ctx_out"];
type Auth = ProcedureContext<typeof authProcedure>

I believe this might be extract as a helper.

Describe the solution you'd like to see

type ProcedureParams<P extends ProcedureBuilder<any>> = P extends ProcedureBuilder<infer X> ? X : never;
export type inferProcedureContext<P extends ProcedureBuilder<any>> = ProcedureParams<P>["_ctx_out"];
// This might be useful to someone as well though I don't have a use case
export type inferProcedureMeta<P extends ProcedureBuilder<any>> = ProcedureParams<P>["_meta"];

Describe alternate solutions

Bring your own type inferrers

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
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

1 participant