Skip to content

feat: publishing AppRouter without hassle #5004

Open

Description

@olehmisar

Describe the feature you'd like to request

I am using trpc in bunch of projects that are spread across npm packages. E.g., I have a server my-server and a client my-client. In my-client code I am importing AppRouter like this:

import type { AppRouter } from `my-server`;

I want to be able to publish AppRouter types on NPM without exposing any sensitive information like my server source code or Context of the AppRouter.

Describe the solution you'd like to see

When generating .d.ts files via tsc or vite, .d.ts files contain all imported type definitions. Since AppRouter depends on Context, and Context usually contains all backend services (e.g., UsersService, PaymentService, etc), all the backend type declarations are also included in .d.ts files.

I see at least two solutions:

  • Somehow replace Context object with unknown when generating .d.ts files
  • If appRouter is defined in src/router.ts, only include dist/router.d.ts in package.json files field.

I hope trpc provides a recommended way to publish AppRouter, so developers do not need to think about it themselves.

Describe alternate solutions

Don't provide a recommended way of publishing AppRouter and leave it to devs.

Additional information

No response

👨‍👧‍👦 Contributing

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

Activity

  1. KATT commented on Nov 5, 2023

    @KATT
    Member

    #4709 does this - implemented in next major (which you can use today)

  2. olehmisar commented on Nov 5, 2023

    @olehmisar
    Author

    According to the description of the linked PR, context object is still included in the router type(although context is no longer present in procedure types). Is it possible to remove context from .d.ts altogether?

  3. KATT commented on Nov 5, 2023

    @KATT
    Member

    Off the top of my head, the @trpc/client needs

    • error formatter
    • transformer (or at least knowing if there is one)

    The rest could technically be omitted with regards to the client.

    However, in order to call router.createCaller() we'd need the Context.... potentially we could have a way to strip out stuff from routers that'd still work to create clients from.

    Feel free to hack on this, know it's quite time consuming though, but you have pretty free reign to challenge how we do things seeing that it's a major change upcoming anyway

  4. added
    ⏭ major bump needed
    on Nov 23, 2023
  5. matannahmani commented on Aug 24, 2024

    @matannahmani

    is this now available?

  6. deldrid1 commented on Sep 12, 2024

    @deldrid1

    I still don't see a very easy way to export the types to a file and then import them somewhere else.

    I ran into some circular dependencies and other errors when tryting to get tsc to emit a declaration file... Would love to see an npx command or similair to generate a file that ONLY has dependencies on @trpc/client and could be used anywhere (similar in nature to GraphQL Schema and utlimately the output you get from https://the-guild.dev/graphql/codegen)

  7. yovanoc commented on Mar 31, 2025

    @yovanoc
    Contributor

    If someone already built something and can share it would be very nice!

  8. hugo082 commented on Apr 10, 2025

    @hugo082

    Not sure it works with all configs, but it works at least with mine and may be useful to others. Here is the exposed router modifier I exports from the server:

    import type {
      AnyProcedure,
      AnyRootTypes,
      BuiltRouter,
      CreateRouterOptions,
      DecorateCreateRouterOptions,
      Simplify,
    } from '@trpc/server/unstable-core-do-not-import';
    
    type ExposedRootTypes<T extends AnyRootTypes> = Simplify<{
      ctx: any;
      meta: any;
      errorShape: any;
      transformer: T['transformer'];
    }>;
    
    type Exposed<T> = Simplify<{
      [K in keyof T]: T[K] extends AnyProcedure
        ? T[K] // keeps the procedure as it is
        : T[K] extends BuiltRouter<any, infer TRouterDef>
          ? BuiltRouter<AnyRootTypes, Exposed<TRouterDef>> // remove root types
          : T[K] extends CreateRouterOptions
            ? DecorateCreateRouterOptions<Exposed<T[K]>> // deep exposition
            : T[K];
    }>;
    
    /// From a TRPC router, deeply remove context and meta on routers / procedures
    export type ExposedRouter<T> = T extends BuiltRouter<
      infer TRootTypes,
      infer TProcedureRecord
    >
      ? BuiltRouter<ExposedRootTypes<TRootTypes>, Exposed<TProcedureRecord>>
      : never;
    
    // usage 
    export PublicRouter = ExposedRouter<PrivateRouter>

    ⚠ works with v11 only

  9. yovanoc commented on Apr 22, 2025

    @yovanoc
    Contributor

    Not sure it works with all configs, but it works at least with mine and may be useful to others. Here is the exposed router modifier I exports from the server:

    import type {
    AnyProcedure,
    AnyRootTypes,
    BuiltRouter,
    CreateRouterOptions,
    DecorateCreateRouterOptions,
    Simplify,
    } from '@trpc/server/unstable-core-do-not-import';

    type ExposedRootTypes = Simplify<{
    ctx: any;
    meta: any;
    errorShape: any;
    transformer: T['transformer'];
    }>;

    type Exposed = Simplify<{
    [K in keyof T]: T[K] extends AnyProcedure
    ? T[K] // keeps the procedure as it is
    : T[K] extends BuiltRouter<any, infer TRouterDef>
    ? BuiltRouter<AnyRootTypes, Exposed> // remove root types
    : T[K] extends CreateRouterOptions
    ? DecorateCreateRouterOptions<Exposed<T[K]>> // deep exposition
    : T[K];
    }>;

    /// From a TRPC router, deeply remove context and meta on routers / procedures
    export type ExposedRouter = T extends BuiltRouter<
    infer TRootTypes,
    infer TProcedureRecord

    ? BuiltRouter<ExposedRootTypes, Exposed>
    : never;

    // usage
    export PublicRouter = ExposedRouter
    ⚠ works with v11 only

    Image

    how did you do that ? what is your private router ?

    I try with the current source it gives never, and if I use the builtrouter on a .d.ts file in gives any :/

  10. tkrizsa commented on Nov 29, 2025

    @tkrizsa

    My workaround:
    trcp.ts:

    /**
     * Initialization of tRPC backend
     * Should be done only once per backend!
     */
    const tNoCtx = initTRPC.create();
    const t = initTRPC.context<Context>().create();
    
    /**
     * Export reusable router and procedure helpers
     * that can be used throughout the router
     */
    export const routerNoCtx = tNoCtx.router;
    export const router = t.router;
    export const publicProcedure = t.procedure;
    

    index.tx:

    const routes = {
        /* in subroutes use pure object, nested router({...}) will contain ctx as well */
        somepath: publicProcedure(...) ,
       ...
    };
    
    const appRouter = router(routes);
    const appRouterNoCtx = routerNoCtx(routes);
    
    
    export type AppRouter = typeof appRouterNoCtx;
    export type AppRouterTypes = inferRouterOutputs<typeof appRouterNoCtx>;
    
    
  11. KATT commented on Nov 2, 2026

    @KATT
    Member

    🤖 In v12 this is what contracts are for (experimental): a contract describes each procedure's input, output and errors in a package of its own, with no context or server code, and a client takes the contract as its router. You can write the contract first and implement it with t.implement(), or derive one from an existing router with experimental_toContract(router). https://v12.trpc.io/v12/docs/server/contract-first#clients-from-a-contract. v12 isn't released yet and there's no ETA.

  12. added
    next-major/maybe
    on Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    next-major/maybe⏭ major bump needed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions