feat: wrap trpc endpoint in withRateLimit
This commit is contained in:
@@ -3,12 +3,14 @@ import { createNextApiHandler } from "@trpc/server/adapters/next";
|
|||||||
|
|
||||||
import { appRouter } from "@kan/api/root";
|
import { appRouter } from "@kan/api/root";
|
||||||
import { createTRPCContext } from "@kan/api/trpc";
|
import { createTRPCContext } from "@kan/api/trpc";
|
||||||
|
import { env } from "~/env";
|
||||||
|
import { withRateLimit } from "~/utils/rateLimit";
|
||||||
|
|
||||||
const nextApiHandler = createNextApiHandler({
|
const nextApiHandler = createNextApiHandler({
|
||||||
router: appRouter,
|
router: appRouter,
|
||||||
createContext: createTRPCContext,
|
createContext: createTRPCContext,
|
||||||
onError:
|
onError:
|
||||||
process.env.NODE_ENV === "development"
|
env.NODE_ENV === "development"
|
||||||
? ({ path, error }) => {
|
? ({ path, error }) => {
|
||||||
console.error(
|
console.error(
|
||||||
`❌ tRPC failed on ${path ?? "<no-path>"}: ${error.message}`,
|
`❌ tRPC failed on ${path ?? "<no-path>"}: ${error.message}`,
|
||||||
@@ -17,11 +19,16 @@ const nextApiHandler = createNextApiHandler({
|
|||||||
: undefined,
|
: undefined,
|
||||||
});
|
});
|
||||||
|
|
||||||
export default function handler(req: NextApiRequest, res: NextApiResponse) {
|
export default withRateLimit(
|
||||||
if (req.method === "OPTIONS") {
|
{ points: 100, duration: 60 },
|
||||||
res.writeHead(200);
|
async (req: NextApiRequest, res: NextApiResponse) => {
|
||||||
return res.end();
|
if (req.method === "OPTIONS") {
|
||||||
}
|
res.writeHead(200);
|
||||||
|
res.end();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
return nextApiHandler(req, res);
|
const result = await nextApiHandler(req, res);
|
||||||
}
|
return result;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user