Hoppa till huvudinnehållet
Version: 9.x

Mellanprogram

Inofficiell Beta-översättning

Denna sida har översatts av PageTurner AI (beta). Inte officiellt godkänd av projektet. Hittade du ett fel? Rapportera problem →

Du kan lägga till mellanprogram till en hel router med metoden middleware(). Mellanprogrammen kommer att omsluta anropet till proceduren och måste skicka vidare dess returvärde.

Auktorisering

I exemplet nedan kommer alla anrop till admin.* att säkerställa att användaren är "admin" innan någon fråga eller mutation körs.

ts
trpc
.router<Context>()
.query('foo', {
resolve() {
return 'bar';
},
})
.merge(
'admin.',
trpc
.router<Context>()
.middleware(async (opts) => {
if (!opts.ctx.user?.isAdmin) {
throw new TRPCError({ code: 'UNAUTHORIZED' });
}
return opts.next();
})
.query('secretPlace', {
resolve() {
return 'a key';
},
}),
);
ts
trpc
.router<Context>()
.query('foo', {
resolve() {
return 'bar';
},
})
.merge(
'admin.',
trpc
.router<Context>()
.middleware(async (opts) => {
if (!opts.ctx.user?.isAdmin) {
throw new TRPCError({ code: 'UNAUTHORIZED' });
}
return opts.next();
})
.query('secretPlace', {
resolve() {
return 'a key';
},
}),
);
tips

Se Felhantering för att lära dig mer om TRPCError som kastas i exemplet ovan.

Loggning

I exemplet nedan loggas tidsåtgång för frågor automatiskt.

ts
trpc
.router<Context>()
.middleware(async ({ path, type, next }) => {
const start = Date.now();
const result = await next();
const durationMs = Date.now() - start;
result.ok
? logMock('OK request timing:', { path, type, durationMs })
: logMock('Non-OK request timing', { path, type, durationMs });
return result;
})
.query('foo', {
resolve() {
return 'bar';
},
})
.query('abc', {
resolve() {
return 'def';
},
});
ts
trpc
.router<Context>()
.middleware(async ({ path, type, next }) => {
const start = Date.now();
const result = await next();
const durationMs = Date.now() - start;
result.ok
? logMock('OK request timing:', { path, type, durationMs })
: logMock('Non-OK request timing', { path, type, durationMs });
return result;
})
.query('foo', {
resolve() {
return 'bar';
},
})
.query('abc', {
resolve() {
return 'def';
},
});

Kontextbyte

Ett mellanprogram kan ersätta routerns kontext, och efterföljande procedurer kommer att få det nya kontextvärdet:

ts
interface Context {
// user is nullable
user?: {
id: string;
};
}
trpc
.router<Context>()
.middleware((opts) => {
if (!opts.ctx.user) {
throw new TRPCError({ code: 'UNAUTHORIZED' });
}
return opts.next({
ctx: {
...opts.ctx,
user: opts.ctx.user, // user value is known to be non-null now
},
});
})
.query('userId', {
async resolve({ ctx }) {
return ctx.user.id;
},
});
ts
interface Context {
// user is nullable
user?: {
id: string;
};
}
trpc
.router<Context>()
.middleware((opts) => {
if (!opts.ctx.user) {
throw new TRPCError({ code: 'UNAUTHORIZED' });
}
return opts.next({
ctx: {
...opts.ctx,
user: opts.ctx.user, // user value is known to be non-null now
},
});
})
.query('userId', {
async resolve({ ctx }) {
return ctx.user.id;
},
});

Hjälpfunktionen createProtectedRouter()

Denna hjälpfunktion kan användas var som helst i din applikationsstruktur för att säkerställa att efterföljande procedurer är auktoriserade.

server/createRouter.ts
tsx
import * as trpc from '@trpc/server';
import { Context } from './context';
export function createProtectedRouter() {
return trpc.router<Context>().middleware((opts) => {
if (!opts.ctx.user) {
throw new trpc.TRPCError({ code: 'UNAUTHORIZED' });
}
return opts.next({
ctx: {
...opts.ctx,
// infers that `user` is non-nullable to downstream procedures
user: opts.ctx.user,
},
});
});
}
server/createRouter.ts
tsx
import * as trpc from '@trpc/server';
import { Context } from './context';
export function createProtectedRouter() {
return trpc.router<Context>().middleware((opts) => {
if (!opts.ctx.user) {
throw new trpc.TRPCError({ code: 'UNAUTHORIZED' });
}
return opts.next({
ctx: {
...opts.ctx,
// infers that `user` is non-nullable to downstream procedures
user: opts.ctx.user,
},
});
});
}

Råindata

Ett mellanprogram kan komma åt råindatan som kommer att skickas till en procedur. Detta kan användas för autentisering eller annan förbearbetning i mellanprogrammet som kräver åtkomst till procedurens indata, och är särskilt användbart vid kombination med kontextbyte.

försiktighet

rawInput som skickas till ett mellanprogram har inte validerats av procedurens input-schema/validerare, så var försiktig när du använder det! Därför har rawInput typen unknown. För mer information se #1059.

ts
const inputSchema = z.object({ userId: z.string() });
trpc
.router<Context>()
.middleware(async ({ next, rawInput, ctx }) => {
const result = inputSchema.safeParse(rawInput);
if (!result.success) throw new TRPCError({ code: 'BAD_REQUEST' });
const { userId } = result.data;
// Check user id auth
return next({ ctx: { ...ctx, userId } });
})
.query('userId', {
input: inputSchema,
resolve({ ctx }) {
return ctx.userId;
},
});
ts
const inputSchema = z.object({ userId: z.string() });
trpc
.router<Context>()
.middleware(async ({ next, rawInput, ctx }) => {
const result = inputSchema.safeParse(rawInput);
if (!result.success) throw new TRPCError({ code: 'BAD_REQUEST' });
const { userId } = result.data;
// Check user id auth
return next({ ctx: { ...ctx, userId } });
})
.query('userId', {
input: inputSchema,
resolve({ ctx }) {
return ctx.userId;
},
});