-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update trpc to v10 (@next) (#203)
* feat: modify installer and bp * feat: update frontend * fix: update import * fix: add protectedprocedure
- Loading branch information
1 parent
cef7027
commit bc15ece
Showing
21 changed files
with
234 additions
and
285 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,18 @@ | ||
// src/server/router/context.ts | ||
// src/server/trpc/context.ts | ||
import * as trpc from "@trpc/server"; | ||
import * as trpcNext from "@trpc/server/adapters/next"; | ||
import { unstable_getServerSession as getServerSession } from "next-auth"; | ||
|
||
import { authOptions as nextAuthOptions } from "../../pages/api/auth/[...nextauth]"; | ||
|
||
export const createContext = async ( | ||
opts?: trpcNext.CreateNextContextOptions, | ||
opts: trpcNext.CreateNextContextOptions, | ||
) => { | ||
const req = opts?.req; | ||
const res = opts?.res; | ||
|
||
const session = | ||
req && res && (await getServerSession(req, res, nextAuthOptions)); | ||
const session = await getServerSession(opts.req, opts.res, nextAuthOptions); | ||
|
||
return { | ||
req, | ||
res, | ||
session, | ||
}; | ||
}; | ||
|
||
type Context = trpc.inferAsyncReturnType<typeof createContext>; | ||
|
||
export const createRouter = () => trpc.router<Context>(); | ||
export type Context = trpc.inferAsyncReturnType<typeof createContext>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,12 @@ | ||
// src/server/router/index.ts | ||
import { createRouter } from "./context"; | ||
import superjson from "superjson"; | ||
|
||
// src/server/trpc/router/index.ts | ||
import { t } from "../utils"; | ||
import { exampleRouter } from "./example"; | ||
import { authRouter } from "./auth"; | ||
|
||
export const appRouter = createRouter() | ||
.transformer(superjson) | ||
.merge("example.", exampleRouter) | ||
.merge("auth.", authRouter); | ||
export const appRouter = t.router({ | ||
example: exampleRouter, | ||
auth: authRouter, | ||
}); | ||
|
||
// export type definition of API | ||
export type AppRouter = typeof appRouter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,10 @@ | ||
import { TRPCError } from "@trpc/server"; | ||
import { createRouter } from "./context"; | ||
import { t, authedProcedure } from "../utils"; | ||
|
||
export const authRouter = createRouter() | ||
.query("getSession", { | ||
resolve({ ctx }) { | ||
return ctx.session; | ||
}, | ||
}) | ||
.middleware(async ({ ctx, next }) => { | ||
// Any queries or mutations after this middleware will | ||
// raise an error unless there is a current session | ||
if (!ctx.session) { | ||
throw new TRPCError({ code: "UNAUTHORIZED" }); | ||
} | ||
return next(); | ||
}) | ||
.query("getSecretMessage", { | ||
async resolve({ ctx }) { | ||
return "You are logged in and can see this secret message!"; | ||
}, | ||
}); | ||
export const authRouter = t.router({ | ||
getSession: t.procedure.query(({ ctx }) => { | ||
return ctx.session; | ||
}), | ||
getSecretMessage: authedProcedure.query(() => { | ||
return "You are logged in and can see this secret message!"; | ||
}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { initTRPC, TRPCError } from "@trpc/server"; | ||
import type { Context } from "./context"; | ||
import superjson from "superjson"; | ||
|
||
export const t = initTRPC<{ ctx: Context }>()({ | ||
transformer: superjson, | ||
errorFormatter({ shape }) { | ||
return shape; | ||
}, | ||
}); | ||
|
||
export const authedProcedure = t.procedure.use(({ ctx, next }) => { | ||
if (!ctx.session || !ctx.session.user) { | ||
throw new TRPCError({ code: "UNAUTHORIZED" }); | ||
} | ||
return next({ | ||
ctx: { | ||
...ctx, | ||
// infers that `session` is non-nullable to downstream resolvers | ||
session: { ...ctx.session, user: ctx.session.user }, | ||
}, | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,15 @@ | ||
import { createRouter } from "./context"; | ||
import { t } from "../utils"; | ||
import { z } from "zod"; | ||
|
||
export const exampleRouter = createRouter() | ||
.query("hello", { | ||
input: z | ||
.object({ | ||
text: z.string().nullish(), | ||
}) | ||
.nullish(), | ||
resolve({ input }) { | ||
export const exampleRouter = t.router({ | ||
hello: t.procedure | ||
.input(z.object({ text: z.string().nullish() }).nullish()) | ||
.query(({ input }) => { | ||
return { | ||
greeting: `Hello ${input?.text ?? "world"}`, | ||
}; | ||
}, | ||
}) | ||
.query("getAll", { | ||
async resolve({ ctx }) { | ||
return await ctx.prisma.example.findMany(); | ||
}, | ||
}); | ||
}), | ||
getAll: t.procedure.query(({ ctx }) => { | ||
return ctx.prisma.example.findMany(); | ||
}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,12 @@ | ||
import { createRouter } from "./context"; | ||
import { t } from "../utils"; | ||
import { z } from "zod"; | ||
|
||
export const exampleRouter = createRouter().query("hello", { | ||
input: z | ||
.object({ | ||
text: z.string().nullish(), | ||
}) | ||
.nullish(), | ||
resolve({ input }) { | ||
return { | ||
greeting: `Hello ${input?.text ?? "world"}`, | ||
}; | ||
}, | ||
export const exampleRouter = t.router({ | ||
hello: t.procedure | ||
.input(z.object({ text: z.string().nullish() }).nullish()) | ||
.query(({ input }) => { | ||
return { | ||
greeting: `Hello ${input?.text ?? "world"}`, | ||
}; | ||
}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
// src/server/router/index.ts | ||
import { createRouter } from "./context"; | ||
import superjson from "superjson"; | ||
import { t } from "../utils"; | ||
|
||
import { exampleRouter } from "./example"; | ||
|
||
export const appRouter = createRouter() | ||
.transformer(superjson) | ||
.merge("example.", exampleRouter); | ||
export const appRouter = t.router({ | ||
example: exampleRouter, | ||
}); | ||
|
||
// export type definition of API | ||
export type AppRouter = typeof appRouter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { initTRPC } from "@trpc/server"; | ||
import type { Context } from "./context"; | ||
import superjson from "superjson"; | ||
|
||
export const t = initTRPC<{ ctx: Context }>()({ | ||
transformer: superjson, | ||
errorFormatter({ shape }) { | ||
return shape; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,42 @@ | ||
// src/utils/trpc.ts | ||
import type { AppRouter } from "../server/router"; | ||
import { createReactQueryHooks } from "@trpc/react"; | ||
import { setupTRPC } from "@trpc/next"; | ||
import type { inferProcedureInput, inferProcedureOutput } from "@trpc/server"; | ||
import type { AppRouter } from "../server/trpc/router"; | ||
import superjson from "superjson"; | ||
|
||
export const trpc = createReactQueryHooks<AppRouter>(); | ||
const getBaseUrl = () => { | ||
if (typeof window !== "undefined") return ""; // browser should use relative url | ||
if (process.env.VERCEL_URL) return `https://${process.env.VERCEL_URL}`; // SSR should use vercel url | ||
|
||
return `http://localhost:${process.env.PORT ?? 3000}`; // dev SSR should use localhost | ||
}; | ||
|
||
export const trpc = setupTRPC<AppRouter>({ | ||
config() { | ||
return { | ||
url: `${getBaseUrl()}/api/trpc`, | ||
transformer: superjson, | ||
}; | ||
}, | ||
ssr: false, | ||
}); | ||
|
||
/** | ||
* Check out tRPC docs for Inference Helpers | ||
* https://trpc.io/docs/infer-types#inference-helpers | ||
* This is a helper method to infer the output of a query resolver | ||
* @example type HelloOutput = inferQueryOutput<'hello'> | ||
*/ | ||
export type inferQueryOutput< | ||
TRouteKey extends keyof AppRouter["_def"]["queries"], | ||
> = inferProcedureOutput<AppRouter["_def"]["queries"][TRouteKey]>; | ||
|
||
export type inferQueryInput< | ||
TRouteKey extends keyof AppRouter["_def"]["queries"], | ||
> = inferProcedureInput<AppRouter["_def"]["queries"][TRouteKey]>; | ||
|
||
export type inferMutationOutput< | ||
TRouteKey extends keyof AppRouter["_def"]["mutations"], | ||
> = inferProcedureOutput<AppRouter["_def"]["mutations"][TRouteKey]>; | ||
|
||
export type inferMutationInput< | ||
TRouteKey extends keyof AppRouter["_def"]["mutations"], | ||
> = inferProcedureInput<AppRouter["_def"]["mutations"][TRouteKey]>; |
Oops, something went wrong.