✨ add throttle middleware
Create throttling on middleware to pause requests until a specified time before continuing. (default time 2s)
This commit is contained in:
@ -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);
|
||||
}
|
||||
};
|
||||
6
src/middleware/system/throttle.middleware.ts
Normal file
6
src/middleware/system/throttle.middleware.ts
Normal 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));
|
||||
});
|
||||
Reference in New Issue
Block a user