Skip to content

Commit

Permalink
chore: repo maintenance 2024-07-22 (#1122)
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusmarminge authored Jul 22, 2024
1 parent 2e60079 commit 47efa78
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 171 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20.12
20.15
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"name": "create-t3-turbo",
"private": true,
"engines": {
"node": ">=20.12.0"
"node": ">=20.15.0"
},
"packageManager": "pnpm@9.5.0",
"packageManager": "pnpm@9.6.0",
"scripts": {
"build": "turbo run build",
"clean": "git clean -xdf node_modules",
Expand Down
47 changes: 36 additions & 11 deletions packages/api/src/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,37 @@ export const createCallerFactory = t.createCallerFactory;
*/
export const createTRPCRouter = t.router;

/**
* Middleware for timing procedure execution and adding an articifial delay in development.
*
* You can remove this if you don't like it, but it can help catch unwanted waterfalls by simulating
* network latency that would occur in production but not in local development.
*/
const timingMiddleware = t.middleware(async ({ next, path }) => {
const start = Date.now();

if (t._config.isDev) {
// artificial delay in dev 100-500ms
const waitMs = Math.floor(Math.random() * 400) + 100;
await new Promise((resolve) => setTimeout(resolve, waitMs));
}

const result = await next();

const end = Date.now();
console.log(`[TRPC] ${path} took ${end - start}ms to execute`);

return result;
});

/**
* Public (unauthed) procedure
*
* This is the base piece you use to build new queries and mutations on your
* tRPC API. It does not guarantee that a user querying is authorized, but you
* can still access user session data if they are logged in
*/
export const publicProcedure = t.procedure;
export const publicProcedure = t.procedure.use(timingMiddleware);

/**
* Protected (authenticated) procedure
Expand All @@ -107,14 +130,16 @@ export const publicProcedure = t.procedure;
*
* @see https://trpc.io/docs/procedures
*/
export const protectedProcedure = t.procedure.use(({ ctx, next }) => {
if (!ctx.session?.user) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}
return next({
ctx: {
// infers the `session` as non-nullable
session: { ...ctx.session, user: ctx.session.user },
},
export const protectedProcedure = t.procedure
.use(timingMiddleware)
.use(({ ctx, next }) => {
if (!ctx.session?.user) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}
return next({
ctx: {
// infers the `session` as non-nullable
session: { ...ctx.session, user: ctx.session.user },
},
});
});
});
Loading

0 comments on commit 47efa78

Please sign in to comment.