Skip to content

Commit

Permalink
chore: update express plugin middlewares
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Jan 15, 2025
1 parent 3e71812 commit 71893f1
Showing 1 changed file with 4 additions and 14 deletions.
18 changes: 4 additions & 14 deletions packages/express/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@ declare module 'express-serve-static-core' {
}
}

interface MiddlewareHook {
(host: Router, instance: Kopflos): Promise<void> | void
}

declare module '@kopflos-cms/core' {
interface KopflosPlugin {
beforeMiddleware?: MiddlewareHook
afterMiddleware?: MiddlewareHook
beforeMiddleware?: (host: Router) => Promise<void> | void
afterMiddleware?: (host: Router) => Promise<void> | void
}
}

Expand All @@ -36,7 +32,7 @@ export default async (options: KopflosConfig): Promise<{ middleware: RequestHand

const router = Router()

await registerMiddlewares(router, kopflos, kopflos.plugins.map(plugin => plugin.beforeMiddleware))
await Promise.all(kopflos.plugins.map(plugin => plugin.beforeMiddleware?.(router)))

router
.use((req, res, next) => {
Expand Down Expand Up @@ -79,16 +75,10 @@ export default async (options: KopflosConfig): Promise<{ middleware: RequestHand
.otherwise((stream) => res.send(stream))
})

await registerMiddlewares(router, kopflos, kopflos.plugins.map(plugin => plugin.afterMiddleware))
await Promise.all(kopflos.plugins.map(plugin => plugin.afterMiddleware?.(router)))

return {
middleware: router,
instance: kopflos,
}
}

async function registerMiddlewares(router: Router, kopflos: Kopflos, hooks: Array<MiddlewareHook | undefined>) {
for (const hook of hooks) {
await hook?.(router, kopflos)
}
}

0 comments on commit 71893f1

Please sign in to comment.