const t = initTRPC.context<Context>().create();
export const middleware = t.middleware;
export const router = t.router;
const checkTokenMW = middleware(async ({ctx, next}) => {
if(!ctx.req.headers.authorization){
throw new TRPCError({code: 'UNAUTHORIZED'});
}
ctx.userData.userId = 3;
return next({ctx});
});
const loggerMW = middleware(async ({path, next}) => {
const start = Date.now();
const result = await next();
const durationMs = Date.now() - start;
result.ok
? console.log('OK request %s duration: %d', { path, durationMs })
: console.log('Non-OK request %s duration: %d', { path, durationMs });
return result;
});
const t = initTRPC.context<Context>().create();
export const middleware = t.middleware;
export const router = t.router;
const checkTokenMW = middleware(async ({ctx, next}) => {
if(!ctx.req.headers.authorization){
throw new TRPCError({code: 'UNAUTHORIZED'});
}
ctx.userData.userId = 3;
return next({ctx});
});
const loggerMW = middleware(async ({path, next}) => {
const start = Date.now();
const result = await next();
const durationMs = Date.now() - start;
result.ok
? console.log('OK request %s duration: %d', { path, durationMs })
: console.log('Non-OK request %s duration: %d', { path, durationMs });
return result;
});