added tons of features
This commit is contained in:
36
middlewares/chain.ts
Normal file
36
middlewares/chain.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
/***************************************
|
||||
*
|
||||
* Based on
|
||||
* https://medium.com/@tanzimhossain2/implementing-multiple-middleware-in-next-js-combining-nextauth-and-internationalization-28d5435d3187
|
||||
*
|
||||
**************************************/
|
||||
import {NextMiddlewareResult} from 'next/dist/server/web/types'
|
||||
import {NextResponse} from 'next/server'
|
||||
import type {NextFetchEvent, NextRequest} from 'next/server'
|
||||
|
||||
export type CustomMiddleware = (
|
||||
request: NextRequest,
|
||||
event: NextFetchEvent,
|
||||
response: NextResponse
|
||||
) => NextMiddlewareResult | Promise<NextMiddlewareResult>
|
||||
|
||||
type MiddlewareFactory = (middleware: CustomMiddleware) => CustomMiddleware
|
||||
export function chain(
|
||||
functions: MiddlewareFactory[],
|
||||
index = 0
|
||||
): CustomMiddleware {
|
||||
const current = functions[index]
|
||||
|
||||
if (current) {
|
||||
const next = chain(functions, index + 1)
|
||||
return current(next)
|
||||
}
|
||||
|
||||
return (
|
||||
request: NextRequest,
|
||||
event: NextFetchEvent,
|
||||
response: NextResponse
|
||||
) => {
|
||||
return response
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user