add throttle middleware

Create throttling on middleware to pause requests until a specified time before continuing. (default time 2s)
This commit is contained in:
Rafi Arrafif
2025-08-24 14:02:28 +07:00
parent 7e8b5be6cd
commit 112f5188ed
2 changed files with 6 additions and 14 deletions

View File

@ -1,14 +0,0 @@
import { Context } from "elysia";
import { getCookie } from "../helpers/http/userHeader/cookies/getCookies";
import { mainErrorHandler } from "../helpers/error/handler";
import { returnErrorResponse } from "../helpers/callback/httpResponse";
export const authMiddleware = (ctx: Context) => {
try {
const cookie = getCookie(ctx);
if (!cookie.auth_token)
return returnErrorResponse(ctx.set, 401, "User Unauthorized");
} catch (error) {
return mainErrorHandler(ctx.set, error);
}
};

View File

@ -0,0 +1,6 @@
import Elysia from "elysia";
export const throttleMiddleware = (delayMs = 2000) => (app: Elysia) =>
app.onRequest(async () => {
await new Promise((resolve) => setTimeout(resolve, delayMs));
});