mirror of
https://github.com/hansputera/tiktok-dl.git
synced 2026-04-05 19:51:57 +02:00
feat(apps.web.middleware): add apply middleware
Signed-off-by: Hanif Dwy Putra S <47862061+hansputera@users.noreply.github.com>
This commit is contained in:
31
apps/web/middleware/apply.ts
Normal file
31
apps/web/middleware/apply.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import {NextApiRequest, NextApiResponse} from 'next';
|
||||
|
||||
type Middleware = (
|
||||
request: NextApiRequest,
|
||||
response: NextApiResponse,
|
||||
) => Promise<undefined>;
|
||||
|
||||
export const applyRoute = (
|
||||
route: (req: NextApiRequest, res: NextApiResponse) => Promise<void>,
|
||||
middlewares: Middleware[],
|
||||
) => {
|
||||
return async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
const middleware:
|
||||
| {
|
||||
message: string;
|
||||
status: number;
|
||||
}
|
||||
| undefined = await Promise.all(middlewares.map((m) => m(req, res)))
|
||||
.catch((e) => ({
|
||||
message: e.message,
|
||||
status: 500,
|
||||
}))
|
||||
.then(() => undefined);
|
||||
if (middleware)
|
||||
return res.status(middleware.status).json({
|
||||
message: middleware.message,
|
||||
statusCode: middleware.status,
|
||||
});
|
||||
else return route(req, res);
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user