add trpc v10 (#4683)
* revert me later * let's see if this builds * fix dupe proc * fix: v10 works * fix type error * fix type error * fix type errors * fix more * add example procedure * spreading not needed * Update yarn.lock * Revert "revert me later" This reverts commit 0c8c15d0577e0c287223039c7b6958b2b0c12c69. Co-authored-by: Chris Bautista <[email protected]> Co-authored-by: Peer Richelsen <[email protected]> Co-authored-by: Omar López <[email protected]> Co-authored-by: Alex van Andel <[email protected]>
This commit is contained in:
36 files changed
+197
-189
No files matched your search
@@ -6,10 +6,11 @@
|
||||
"version": "1.0.0",
|
||||
"main": "index.ts",
|
||||
"dependencies": {
|
||||
"@trpc/client": "^9.27.0",
|
||||
"@trpc/next": "^9.27.0",
|
||||
"@trpc/react": "^9.27.0",
|
||||
"@trpc/server": "^9.27.0",
|
||||
"@tanstack/react-query": "^4.3.9",
|
||||
"@trpc/client": "^10.0.0-proxy-beta.5",
|
||||
"@trpc/next": "^10.0.0-proxy-beta.5",
|
||||
"@trpc/react": "^10.0.0-proxy-beta.5",
|
||||
"@trpc/server": "^10.0.0-proxy-beta.5",
|
||||
"superjson": "1.9.1",
|
||||
"zod": "^3.18.0"
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import superjson from "superjson";
|
||||
|
||||
import { createReactQueryHooks } from "../react";
|
||||
import { createReactQueryHooks } from "@trpc/react";
|
||||
|
||||
// ℹ️ Type-only import:
|
||||
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export
|
||||
import type { inferProcedureInput, inferProcedureOutput } from "../server";
|
||||
@@ -12,6 +13,8 @@ import type { AppRouter } from "../server/routers/_app";
|
||||
*/
|
||||
export const trpc = createReactQueryHooks<AppRouter>();
|
||||
|
||||
export const proxy = trpc.proxy;
|
||||
|
||||
export const transformer = superjson;
|
||||
/**
|
||||
* This is a helper method to infer the output of a query resolver
|
||||
|
||||
@@ -6,6 +6,7 @@ import { Context } from "./createContext";
|
||||
|
||||
/**
|
||||
* Helper function to create a router with context
|
||||
* @deprecated
|
||||
*/
|
||||
export function createRouter() {
|
||||
return trpc.router<Context>().middleware(async ({ path, type, next }) => {
|
||||
@@ -17,6 +18,9 @@ export function createRouter() {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
export function createProtectedRouter() {
|
||||
return createRouter().middleware(({ ctx, next }) => {
|
||||
if (!ctx.user || !ctx.session) {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
/**
|
||||
* This file contains the root router of your tRPC-backend
|
||||
*/
|
||||
import superjson from "superjson";
|
||||
|
||||
import { createRouter } from "../createRouter";
|
||||
import { publicProcedure, t } from "../trpc";
|
||||
import { publicRouter } from "./public";
|
||||
import { viewerRouter } from "./viewer";
|
||||
|
||||
/**
|
||||
@@ -11,18 +11,22 @@ import { viewerRouter } from "./viewer";
|
||||
* If you want to use SSG, you need export this
|
||||
* @link https://trpc.io/docs/ssg
|
||||
* @link https://trpc.io/docs/router
|
||||
* @deprecated
|
||||
*/
|
||||
export const appRouter = createRouter()
|
||||
/**
|
||||
* Add data transformers
|
||||
* @link https://trpc.io/docs/data-transformers
|
||||
*/
|
||||
.transformer(superjson)
|
||||
export const legacyRouter = createRouter()
|
||||
/**
|
||||
* Optionally do custom error (type safe!) formatting
|
||||
* @link https://trpc.io/docs/error-formatting
|
||||
*/
|
||||
// .formatError(({ shape, error }) => { })
|
||||
.merge("viewer.", viewerRouter);
|
||||
.merge("viewer.", viewerRouter)
|
||||
.interop();
|
||||
|
||||
const v10Router = t.router({
|
||||
hello: publicProcedure.query(() => "hello"),
|
||||
public: publicRouter,
|
||||
});
|
||||
|
||||
export const appRouter = t.mergeRouters(legacyRouter, v10Router);
|
||||
|
||||
export type AppRouter = typeof appRouter;
|
||||
@@ -0,0 +1,5 @@
|
||||
import { t, publicProcedure } from "../trpc";
|
||||
|
||||
export const publicRouter = t.router({
|
||||
session: publicProcedure.query(({ ctx }) => ctx.session),
|
||||
});
|
||||
@@ -55,11 +55,6 @@ import { workflowsRouter } from "./viewer/workflows";
|
||||
|
||||
// things that unauthenticated users can query about themselves
|
||||
const publicViewerRouter = createRouter()
|
||||
.query("session", {
|
||||
resolve({ ctx }) {
|
||||
return ctx.session;
|
||||
},
|
||||
})
|
||||
.query("i18n", {
|
||||
async resolve({ ctx }) {
|
||||
const { locale, i18n } = ctx;
|
||||
|
||||
@@ -96,8 +96,8 @@ export const webhookRouter = createProtectedRouter()
|
||||
input: z.object({
|
||||
webhookId: z.string().optional(),
|
||||
}),
|
||||
resolve({ ctx, input }) {
|
||||
return ctx.prisma.webhook.findUniqueOrThrow({
|
||||
async resolve({ ctx, input }) {
|
||||
return await ctx.prisma.webhook.findUniqueOrThrow({
|
||||
where: {
|
||||
id: input.webhookId,
|
||||
},
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
import superjson from "superjson";
|
||||
|
||||
import { initTRPC, TRPCError } from "@trpc/server";
|
||||
|
||||
import { Context } from "./createContext";
|
||||
|
||||
export const t = initTRPC.context<Context>().create({
|
||||
transformer: superjson,
|
||||
});
|
||||
|
||||
const perfMiddleware = t.middleware(async ({ path, type, next }) => {
|
||||
performance.mark("Start");
|
||||
const result = await next();
|
||||
performance.mark("End");
|
||||
performance.measure(`[${result.ok ? "OK" : "ERROR"}][$1] ${type} '${path}'`, "Start", "End");
|
||||
return result;
|
||||
});
|
||||
|
||||
const isAuthedMiddleware = t.middleware(({ ctx, next }) => {
|
||||
if (!ctx.user || !ctx.session) {
|
||||
throw new TRPCError({ code: "UNAUTHORIZED" });
|
||||
}
|
||||
return next({
|
||||
ctx: {
|
||||
// infers that `user` and `session` are non-nullable to downstream procedures
|
||||
session: ctx.session,
|
||||
user: ctx.user,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
export const publicProcedure = t.procedure.use(perfMiddleware);
|
||||
export const authedProcedure = t.procedure.use(perfMiddleware).use(isAuthedMiddleware);
|
||||
Reference in new issue
Block a user