import Koa from 'koa'; import mount from 'koa-mount'; import Router from 'koa-router'; import { Provider } from 'oidc-provider'; import koaAuth from '@/middleware/koa-auth'; import applicationRoutes from '@/routes/application'; import sessionRoutes from '@/routes/session'; import statusRoutes from '@/routes/status'; import swaggerRoutes from '@/routes/swagger'; import userRoutes from '@/routes/user'; import { AnonymousRouter, AuthedRouter } from './types'; const createRouters = (provider: Provider) => { const anonymousRouter: AnonymousRouter = new Router(); statusRoutes(anonymousRouter); sessionRoutes(anonymousRouter, provider); userRoutes(anonymousRouter, provider); swaggerRoutes(anonymousRouter); const router: AuthedRouter = new Router(); router.use(koaAuth()); applicationRoutes(router); return [anonymousRouter, router]; }; export default function initRouter(app: Koa, provider: Provider) { const apisApp = new Koa(); for (const router of createRouters(provider)) { apisApp.use(router.routes()).use(router.allowedMethods()); } app.use(mount('/api', apisApp)); }