Barakonda
async middleware
is it possible to define an async middleware?
I want to do something like this but it throws errors when i start the server
const dbMiddleware = middleware(async ({ path, ctx, next, input }) => {
ctx.path = path;
let session: Pool | null = null;
try {
session = await getSession();
ctx.query = session.query;
let result = await next({ ctx });
if (session)
await session.end();
return result;
} catch (e: any) {
if (session)
await session.end();
}
});
4 replies
pipe/unstable_pipe
I cant seem to find it? according to the docs I can chain middlewares using pipe in v10 but the feature isn't there
this is a simple example from the docs
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;
});
I want to do something like this checkTokenMW.unstable_pipe(loggerMW) - or like in the example entering the middleware code directly
but there is no method to do this, am i doing something wrong or maybe how to do this changed and the docs aren't updated?
can you please help me figure this out?4 replies
input using z.or not working properly
i have an input like this
let input = z.object({
name: z.string().optional()
}).or(z.object({
id: z.number().optional()
}));
when I call the route with { name: "123" } I get in the input { name: "123" } properly like I should,
but when I call the same route with { id: 2 } then I get in the input {} - the rawInput is correctly { id: 2 } but
the data is not transferred to input, when I switch the order of the input like this
let input = z.object({
id: z.number().optional()
}).or(z.object({
name: z.string().optional()
}));
then now, id is the one that works and name gives {}
this is probably a bug, should I post this in the git or maybe it wasn't supposed to work at all?
12 replies
How can I disable batching with fastify adapter?
I cant seem to find a way to disable batching for my server, and this link doesnt help me much https://trpc.io/docs/v9/links#disabling-request-batching
what am I missing? I fell like I miss a sentence or 2 from the docs....
3 replies
Is it possible to split the router definition with the imlementation?
I want to define the server router(input\output\meta) in a separate package from the server package
I want to have a package that will contain the types I use + the router definition, another for the server itself(will use the router definition like an interface),
and lastly the client package which ill only need to import the types package to work
is this possible?
2 replies