|
1 | 1 | # next-trpc |
2 | | -A simple typed rpc interface to easily type api endpoints in a app router nextjs application |
| 2 | + |
| 3 | +A simple typed rpc interface to easily type api endpoints in an app router nextjs application. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```sh |
| 8 | +npm install @creatorem/next-trpc |
| 9 | +``` |
| 10 | + |
| 11 | +<br/> |
| 12 | + |
| 13 | +## Setup |
| 14 | + |
| 15 | +### Create a router file |
| 16 | + |
| 17 | +Contains your api endpoints. |
| 18 | + |
| 19 | +`trpc/router.ts` |
| 20 | + |
| 21 | +```ts |
| 22 | +import { router, endpoint } from "@creatorem/next-trpc"; |
| 23 | +import z from "zod"; |
| 24 | + |
| 25 | +export const appRouter = router({ |
| 26 | + getUser: endpoint.action(async ({ db }) => { |
| 27 | + return await myAsyncFunction(); |
| 28 | + }), |
| 29 | + greeting: endpoint |
| 30 | + .input( |
| 31 | + // you can add zod typechecking to your entry params |
| 32 | + z.object({ |
| 33 | + name: z.string(), |
| 34 | + age: z.coerce.number(), |
| 35 | + }) |
| 36 | + ) |
| 37 | + .action(({ name, age }) => { |
| 38 | + return `Hi my name is ${name}, and I am ${age} years old.`; |
| 39 | + }), |
| 40 | +}); |
| 41 | + |
| 42 | +export type AppRouter = typeof appRouter; |
| 43 | +``` |
| 44 | + |
| 45 | +### Connect your router to an api endpoint. |
| 46 | + |
| 47 | +`app/api/trpc/[trpc]/route.ts` |
| 48 | + |
| 49 | +```ts |
| 50 | +import { createTrpcAPI } from "@creatorem/next-trpc/server"; |
| 51 | +import { appRouter } from "~/trpc/router"; |
| 52 | + |
| 53 | +const handler = createTrpcAPI({ |
| 54 | + router: appRouter, |
| 55 | +}); |
| 56 | + |
| 57 | +export { handler as GET, handler as POST }; |
| 58 | +``` |
| 59 | + |
| 60 | +### Start fetching with a type safe client! |
| 61 | + |
| 62 | +`trpc/client.ts` |
| 63 | + |
| 64 | +```ts |
| 65 | +import { envs } from "~/envs"; |
| 66 | +import { createTrpcClient } from "@creatorem/next-trpc/client"; |
| 67 | +import { type AppRouter } from "./router"; |
| 68 | + |
| 69 | +const url = envs().NEXT_PUBLIC_YOUR_APP_URL + "/api/trpc"; |
| 70 | + |
| 71 | +export const trpc = createTrpcClient<AppRouter>({ |
| 72 | + url, |
| 73 | + headers: async () => { |
| 74 | + // add custom headers like Authorization to make it works with auth logic |
| 75 | + return { |
| 76 | + /* Authorization: `Bearer ${jwt!}` */ |
| 77 | + }; |
| 78 | + }, |
| 79 | +}); |
| 80 | +``` |
| 81 | + |
| 82 | +Done ! |
| 83 | + |
| 84 | +## Usage |
| 85 | + |
| 86 | +Now you can use the `trpc` client and server side. |
| 87 | + |
| 88 | +`app/layout.tsx` |
| 89 | + |
| 90 | +```tsx |
| 91 | +import React from "react"; |
| 92 | +import { redirect } from "next/navigation"; |
| 93 | +import { trpc } from "~/trpc/client"; |
| 94 | + |
| 95 | +export default async function Layout( |
| 96 | + props: React.PropsWithChildren |
| 97 | +): Promise<React.JSX.Element> { |
| 98 | + const user = await trpc.getUser.fetch(); |
| 99 | + |
| 100 | + if (!user) { |
| 101 | + return redirect(/* path to login page */); |
| 102 | + } |
| 103 | + |
| 104 | + return <>{props.children}</>; |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +### Integrated useQuery hook usage |
| 109 | + |
| 110 | +We offer a client side only function to create client object that pre-implement the `useQuery` hook from `@tanstack/react-query` package. |
| 111 | + |
| 112 | +You need to have `@tanstack/react-query` installed. |
| 113 | + |
| 114 | +```sh |
| 115 | +npm install @tanstack/react-query |
| 116 | +``` |
| 117 | + |
| 118 | +Then you can create the following file : |
| 119 | + |
| 120 | +`trpc/query-client.ts` |
| 121 | + |
| 122 | +```ts |
| 123 | +import "client-only"; |
| 124 | + |
| 125 | +import { envs } from "~/envs"; |
| 126 | +import { createTrpcQueryClient } from "@creatorem/next-trpc/query-client"; |
| 127 | +import { type AppRouter } from "./router"; |
| 128 | + |
| 129 | +const url = envs().NEXT_PUBLIC_YOUR_APP_URL + "/api/trpc"; |
| 130 | + |
| 131 | +export const clientTrpc = createTrpcQueryClient<AppRouter>({ |
| 132 | + url, |
| 133 | + headers: async () => { |
| 134 | + // add custom headers like Authorization to make it works with auth logic |
| 135 | + return { |
| 136 | + /* Authorization: `Bearer ${jwt!}` */ |
| 137 | + }; |
| 138 | + }, |
| 139 | +}); |
| 140 | +``` |
| 141 | + |
| 142 | +Now you can do : |
| 143 | + |
| 144 | +```tsx |
| 145 | +"use client"; |
| 146 | + |
| 147 | +import React from "react"; |
| 148 | +import { clientTrpc } from "~/trpc/query-client"; |
| 149 | + |
| 150 | +export const MyClientComponent: React.FC<React.PropsWithChildren> = (props) => { |
| 151 | + const { data: user } = clientTrpc.getUser.useQuery(); |
| 152 | + /* ... */ |
| 153 | + |
| 154 | + return <>{props.children}</>; |
| 155 | +}; |
| 156 | +``` |
| 157 | + |
| 158 | +## Use a router context |
| 159 | + |
| 160 | +You can use a context object to pass data to all your endpoint callbacks. |
| 161 | + |
| 162 | +`trpc/router.ts` |
| 163 | + |
| 164 | +```ts {4-7,9} |
| 165 | +import { CtxRouter, endpoint } from "@creatorem/next-trpc"; |
| 166 | +import z from "zod"; |
| 167 | + |
| 168 | +export const createContext = async () => { |
| 169 | + /* your own context logic here ... */ |
| 170 | + return { db /* let's say db is a database client. */ }; |
| 171 | +}; |
| 172 | + |
| 173 | +const ctx = new CtxRouter<Awaited<ReturnType<typeof createContext>>>(); |
| 174 | + |
| 175 | +export const appRouter = ctx.router({ |
| 176 | + getUser: endpoint.action(async ({ db }) => { |
| 177 | + return await db.user.get(); |
| 178 | + }), |
| 179 | + greeting: endpoint |
| 180 | + .input( |
| 181 | + // you can add zod typechecking to your entry params |
| 182 | + z.object({ |
| 183 | + name: z.string(), |
| 184 | + age: z.coerce.number(), |
| 185 | + }) |
| 186 | + ) |
| 187 | + .action(({ name, age }) => { |
| 188 | + return `Hi my name is ${name}, and I am ${age} years old.`; |
| 189 | + }), |
| 190 | +}); |
| 191 | + |
| 192 | +export type AppRouter = typeof appRouter; |
| 193 | +``` |
| 194 | + |
| 195 | +Next pass your context to the nextjs api endpoint. |
| 196 | + |
| 197 | +`app/api/trpc/[trpc]/route.ts` |
| 198 | + |
| 199 | +```ts {6} |
| 200 | +import { createTrpcAPI } from "@creatorem/next-trpc/server"; |
| 201 | +import { appRouter, createContext } from "~/trpc/router"; |
| 202 | + |
| 203 | +const handler = createTrpcAPI({ |
| 204 | + router: appRouter, |
| 205 | + ctx: createContext, |
| 206 | +}); |
| 207 | + |
| 208 | +export { handler as GET, handler as POST }; |
| 209 | +``` |
0 commit comments