Coder Social home page Coder Social logo

trpc-on-the-app-router's Introduction

This is a Next.js project bootstrapped with create-next-app.

Getting Started

First, run the development server:

npm run dev
# or
yarn dev
# or
pnpm dev

Open http://localhost:3000 with your browser to see the result.

You can start editing the page by modifying app/page.tsx. The page auto-updates as you edit the file.

This project uses next/font to automatically optimize and load Inter, a custom Google Font.

Learn More

To learn more about Next.js, take a look at the following resources:

You can check out the Next.js GitHub repository - your feedback and contributions are welcome!

Deploy on Vercel

The easiest way to deploy your Next.js app is to use the Vercel Platform from the creators of Next.js.

Check out our Next.js deployment documentation for more details.

trpc-on-the-app-router's People

Contributors

jherr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

trpc-on-the-app-router's Issues

onError

this is a cool example, thanks!

is there a recommended way to globally handle trpc errors on the frontend?

e.g. i want to show a toast when a BAD_REQUEST error comes in?

i know the setup with the next adapter, but is there a similar approach for the createTRPCReact adapter?

How to create nested routes?

When I try to create a nested route, it gives me an error.

const nestedRouter = router({
  greet: publicProcedure.query(() => "nested hello world"),
});


export const appRouter = router({
  greet: publicProcedure.query(async()=> "hello world"),
  nested: nestedRouter,
});

When I go to http://localhost:3000/api/trpc/greet it works, but not when I go to
http://localhost:3000/api/trpc/nested/greet. The error is

404
This page could not be found.

How can I make nested routes work with app dir?

nit: Possible misleading context definition in `createCaller`?

Thanks!

First of all, thank you for arguably the only NextJS 14 w/ App router + tRPC with an SSR example on the whole of the internets ๐ŸŽ‰ !

Ok, on to the nit:

The createCaller function is designed to take in a context object (or a function that returns a context object), but here you are passing in a terminating link in links. It doesn't appear 1/ that this terminating link is used anywhere in the example codebase or 2/ that passing in a terminating link here would even be advisable.

For trpc newbs like me, this one could be misleading, unless I'm missing some magic here?

Thoughts?

export const serverClient = appRouter.createCaller({
links: [
httpBatchLink({
url: "http://localhost:3000/api/trpc",
}),
],
});

Seems it would be better to pass in a context with say, the current session info from an auth provider, or a db connection reference, or just an {}?

createCaller impossible to use with context that requires NextRequest

I tried using this example to create a caller for my app which had both drizzle and clerk in the context, but it was impossible to make authed calls without the req/NextRequest object, so I tried replacing it with createTRPCProxyClient and it worked just fine like so:

const serverClient = createTRPCProxyClient<AppRouter>({
  transformer,
  links: [
    httpBatchLink({
      url: "http://localhost:3000/api/trpc",
    }),
  ],
});

I also had to add the req to the handler in the api route file like so:

const handler = (req: Request) =>
  fetchRequestHandler({
    endpoint: "/api/trpc",
    req,
    router: appRouter,
    createContext: () => createContext({ req }),
  });

With my createContext looking like this

export const createContextInner = async ({ auth }: AuthContextProps) => {
  return {
    auth,
    db,
  };
};

export const createTRPCContext = async ({ req }: { req: Request }) => {
  // @ts-ignore
  return await createContextInner({ auth: getAuth(req) });
};

Still this example is incredible and I'm very grateful that you came up with it, incredible work!!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.