import { NextApiHandler , NextApiRequest , NextApiResponse } from 'next' ; import handleErrors from '../error-handling/handle-errors.middleware' ; import useragent from './useragent.middleware' ; import authenticate from '../auth/authenticate.middleware' ; import withDb from '../db/with-db.middleware' ; import { NextApiMiddleware } from '@api/shared/shared.types' ; import csrfProtection from '../auth/csrf-protection.middleware' ; import rateLimiter from './rate-limiter.middleware' ; const pipe = ( ... fns ) => ( x ) => fns . reverse ( ) . reduce ( ( acc , curr ) => curr ( acc ) , x ) ; const prepareHandler = ( fn ) => ( req , res ) => { return pipe ( handleErrors , useragent , rateLimiter , withDb , csrfProtection , authenticate ) ( fn ) ( req , res ) ; } ; export default prepareHandler ;