// ... .input(z.object({ storeId: z.string(), itemId: z.string() })) // ...
// server/middleware/rate-limited.tsimport { TRPCError } from '@trpc/server';import type { RateLimitOptions } from '@fastify/rate-limit';import { middleware } from '../constants/trpc.js';const rateLimited = (options: RateLimitOptions) => middleware( async ({ ctx, next, }) => { try { await new Promise( ( resolve, reject, ) => { ctx .app .rateLimit(options) .bind(ctx.app)(ctx.req, ctx.res) .then(resolve) .catch((error) => { reject(new TRPCError({ code: 'TOO_MANY_REQUESTS', message: (error as Error).message, })); }); }, ); return next(); } catch (error) { ctx.app.log.error(error); if (error instanceof TRPCError) throw error; throw new TRPCError({ code: 'INTERNAL_SERVER_ERROR', message: (error as Error).message, }); } },);export default rateLimited;