tRPCttRPC
Powered by
MayankM
tRPC•2y ago
Mayank

Question: Is there a way to have a base interface for Trpc router?

Hi, So I am trying to see if there is way to keep methods inside trpc routes consistent,

Like in case of classes you could define an interface with required functions. Is there a way to do this in Trpc router aswell?

Like ex:

interface A { function X(L: string){} }

class B implements A {} // error missing function X
interface A { function X(L: string){} }

class B implements A {} // error missing function X


Similarly I would like to know if its possible to do with trpc router routes aswell. As I have multiple routes with similar methods.


What i tried

interface MyMethods {
  stateSubscribe: (
    input: undefined,
    opts: { onData: (state: string) => void },
  ) => void;
}

export const campaignRouter = createTRPCRouter<MyMethods>({
  createCampaign: protectedProcedure.query(({ ctx }) => {
    throw new TRPCError({
      message: "Not implemented",
      code: "NOT_IMPLEMENTED",
    });
  }),
  stateSubscribe: protectedProcedure.query(({ ctx }) => {
    throw new TRPCError({
      message: "Not implemented",
      code: "NOT_IMPLEMENTED",
    });
  }),
});
interface MyMethods {
  stateSubscribe: (
    input: undefined,
    opts: { onData: (state: string) => void },
  ) => void;
}

export const campaignRouter = createTRPCRouter<MyMethods>({
  createCampaign: protectedProcedure.query(({ ctx }) => {
    throw new TRPCError({
      message: "Not implemented",
      code: "NOT_IMPLEMENTED",
    });
  }),
  stateSubscribe: protectedProcedure.query(({ ctx }) => {
    throw new TRPCError({
      message: "Not implemented",
      code: "NOT_IMPLEMENTED",
    });
  }),
});


Any help would be wonderful.
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Is there any easy way to create a Type for a trpc router
JokcyJJokcy / ❓-help
3y ago
Hello, is there any way to create a base router with common procedures? like an interface
I am a DevII am a Dev / ❓-help
3y ago
Is there a way to configure a trpc router to require an output() schema to be specified?
cssetianCcssetian / ❓-help
2y ago