cal/packages/trpc/server/createRouter.ts
Alex / KATT 2f8bd3c07a
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 0c8c15d057.

Co-authored-by: Chris Bautista <chrisbautista@netflix.com>
Co-authored-by: Peer Richelsen <peeroke@gmail.com>
Co-authored-by: Omar López <zomars@me.com>
Co-authored-by: Alex van Andel <me@alexvanandel.com>
2022-09-29 16:58:29 +00:00

39 lines
984 B
TypeScript

import { performance } from "@calcom/lib/server/perfObserver";
import * as trpc from "@trpc/server";
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 }) => {
performance.mark("Start");
const result = await next();
performance.mark("End");
performance.measure(`[${result.ok ? "OK" : "ERROR"}][$1] ${type} '${path}'`, "Start", "End");
return result;
});
}
/**
* @deprecated
*/
export function createProtectedRouter() {
return createRouter().middleware(({ ctx, next }) => {
if (!ctx.user || !ctx.session) {
throw new trpc.TRPCError({ code: "UNAUTHORIZED" });
}
return next({
ctx: {
...ctx,
// infers that `user` and `session` are non-nullable to downstream procedures
session: ctx.session,
user: ctx.user,
},
});
});
}