Middlewares
Normally, Gaman Middleware is located in the src/middlewares/** folder, so if you use autoComposeMiddleware((ctx, next) => next()) it will be automatically registered by the system.

Middleware here acts as a guard for the request system, middleware will be executed first when a request comes in, so before interceptor then handler, middleware will be executed first.
Middleware Definition
Section titled “Middleware Definition”composeMiddleware()
Section titled “composeMiddleware()”Here we use composeMiddleware which means it will not be automatically registered to the middlewares data, so you have to manually register it to index.ts
import { composeMiddleware } from "@gaman/core"
export default composeMiddleware((ctx, next) => { return next();});After that, you have to manually register it to the src/index.ts file
import { AppMiddleware } from "./middlewares/AppMiddleware.ts"import { AuthMiddleware } from "./middlewares/AuthMiddleware.ts"
defineBootstrap((app) => { app.mount(AppMiddleware());})Or it can be registered to a specific router, here is the example:
import { AppMiddleware } from "./middlewares/AppMiddleware.ts"import { AuthMiddleware } from "./middlewares/AuthMiddleware.ts"
export default autoComposeRoutes((route) => { route .get('/generate-token', Handler) .middleware(AppMiddleware()) // single middleware .middleware([AppMiddleware(), AuthMiddleware()]) // multi middleware
route.group('/user', (route) => { route.post('/create', Handler) route.get('/profile', Handler) }).middleware(AppMiddleware()) // single middleware .middleware([AppMiddleware(), AuthMiddleware()]) // multi middleware})If you put middleware in a group, the routes inside will also be affected.
autoComposeMiddleware()
Section titled “autoComposeMiddleware()”Here we use autoComposeMiddleware which means it will be automatically registered to the middlewares data, with the requirement that it must be in the src/middlewares/** folder.
import { autoComposeMiddleware } from "@gaman/core"
export default autoComposeMiddleware((ctx, next) => { return next();})This will be automatically registered into the middlewares data, so you don’t need to register manually.
Middleware Config
Section titled “Middleware Config”Here Gaman has prepared default middleware config that you can use.
Priority
Section titled “Priority”priority is a very useful config, the higher the priority, the earlier the process will be executed than others.
import { Priority } from "@gaman/common"
export default composeMiddleware((ctx, next) => { return next();}, { priority: Priority.NORMAL})Includes
Section titled “Includes”includes here functions to filter which route or path to run the middleware.
export default composeMiddleware((ctx, next) => { return next()}, { includes: [ { path: '/user{/*splat}' // means path /user and after will be affected by middleware }, { path: '/blog/*splat' // means after path /blog/ will be middleware except path /blog itself method: ['GET', "POST"] // so only GET and POST methods }, "/dashboard" // path /dashboard will be affected by middleware ]})Excludes
Section titled “Excludes”excludes here functions to filter which route or path will not run the middleware.
export default composeMiddleware((ctx, next) => { return next()}, { excludes: [ { path: '/profile{/*splat}' // means path /profile and after will not be affected by middleware }, { path: '/setting/*splat' // means after path /setting/ will not be middleware except path /setting itself method: ['GET', "POST"] }, "/" // root path will not be affected by middleware ]})Middleware config can also be set when registering to the route, but only composeMiddleware not with autoComposeMiddleware because it’s already automatic global middleware.
Example:
defineBootstrap((app) => { app.mount(AppMiddleware({ priority: Priority.HIGH, includes: ['/dashboard'] }))})
// AppRoutes.tscomposeRoutes((route) => { route.get('/', Handler).middleware(AppMiddleware({ excludes: [{ path: '/api/*splat' }] }))})