import { Enforcer } from 'casbin' import type { MiddlewareHandler, Context } from 'hono' interface CasbinOptions { newEnforcer: Promise authorizer: (c: Context, enforcer: Enforcer) => Promise } export const casbin = (opt: CasbinOptions): MiddlewareHandler => { return async (c, next) => { const enforcer = await opt.newEnforcer if (!(enforcer instanceof Enforcer)) { return c.json({ error: 'Invalid enforcer' }, 500) } const isAllowed = await opt.authorizer(c, enforcer) if (!isAllowed) { return c.json({ error: 'Forbidden' }, 403) } await next() } }