-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
359ba9f
commit 6993967
Showing
10 changed files
with
239 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
/* eslint-disable import/no-extraneous-dependencies, no-param-reassign, functional/prefer-readonly-type, functional/immutable-data */ | ||
import ejs from 'ejs' | ||
import { Hono } from 'hono' | ||
import { serveStatic } from 'hono/bun' | ||
|
||
import type { | ||
AppControllerRoute, AppViewRoute, BullBoardQueues, ControllerHandlerReturnType, IServerAdapter, UIConfig, | ||
} from '@bull-board/api/dist/typings/app' | ||
import type { Context, Env } from 'hono' | ||
|
||
export default class HonoAdapter<HonoEnv extends Env> implements IServerAdapter { | ||
protected app: Hono<HonoEnv> | ||
|
||
protected basePath: string | ||
|
||
protected bullBoardQueues: BullBoardQueues | undefined | ||
|
||
protected errorHandler: ((error: Error) => ControllerHandlerReturnType) | undefined | ||
|
||
protected uiConfig?: UIConfig | ||
|
||
protected statics: { route?: string, path?: string } = {} | ||
|
||
protected entryRoute?: AppViewRoute | ||
|
||
protected viewPath?: string | ||
|
||
protected apiRoutes?: Hono<HonoEnv> | ||
|
||
protected nodeModulesRootPath: string = '.' | ||
|
||
constructor(app: Hono<HonoEnv>) { | ||
this.app = app | ||
this.basePath = '' | ||
this.uiConfig = {} | ||
} | ||
|
||
setBasePath(path: string): HonoAdapter<HonoEnv> { | ||
this.basePath = path | ||
this.app.basePath(path) | ||
return this | ||
} | ||
|
||
setNodeModulesRootPath(path: string): HonoAdapter<HonoEnv> { | ||
this.nodeModulesRootPath = path | ||
return this | ||
} | ||
|
||
setStaticPath(staticsRoute: string, staticsPath: string): HonoAdapter<HonoEnv> { | ||
staticsPath = staticsPath.replaceAll(/\/.*\/node_modules/gm, '/node_modules') | ||
this.statics = { route: staticsRoute, path: staticsPath } | ||
return this | ||
} | ||
|
||
setViewsPath(viewPath: string): HonoAdapter<HonoEnv> { | ||
this.viewPath = viewPath | ||
return this | ||
} | ||
|
||
setErrorHandler(handler: (error: Error) => ControllerHandlerReturnType): this { | ||
this.errorHandler = handler | ||
return this | ||
} | ||
|
||
setApiRoutes(routes: readonly AppControllerRoute[]): HonoAdapter<HonoEnv> { | ||
if (!this.errorHandler) { | ||
throw new Error(`Please call 'setErrorHandler' before using 'registerPlugin'`) | ||
} else if (!this.bullBoardQueues) { | ||
throw new Error(`Please call 'setQueues' before using 'registerPlugin'`) | ||
} | ||
|
||
const router = new Hono<HonoEnv>() | ||
routes.forEach((route) => { | ||
// @ts-expect-error hey | ||
router[route.method.toString().toLowerCase()](route.route.toString(), async (c: Context) => { | ||
try { | ||
const response = await route.handler({ | ||
queues: this.bullBoardQueues as BullBoardQueues, | ||
params: c.req.param(), | ||
query: c.req.query(), | ||
}) | ||
return c.json(response.body, response.status || 200) | ||
} catch (e) { | ||
if (!this.errorHandler || !(e instanceof Error)) { | ||
throw e | ||
} | ||
|
||
const response = this.errorHandler(e) | ||
if (typeof response.body === 'string') { | ||
return c.text(response.body, response.status) | ||
} | ||
return c.json(response.body, response.status) | ||
} | ||
}) | ||
}) | ||
|
||
this.apiRoutes = router | ||
return this | ||
} | ||
|
||
setEntryRoute(routeDef: AppViewRoute): HonoAdapter<HonoEnv> { | ||
this.entryRoute = routeDef | ||
return this | ||
} | ||
|
||
setQueues(bullBoardQueues: BullBoardQueues): HonoAdapter<HonoEnv> { | ||
this.bullBoardQueues = bullBoardQueues | ||
return this | ||
} | ||
|
||
setUIConfig(config?: UIConfig): HonoAdapter<HonoEnv> { | ||
this.uiConfig = config as UIConfig | ||
return this | ||
} | ||
|
||
getRouter() { | ||
return this.app // Return the Hono application instance | ||
} | ||
|
||
registerPlugin() { | ||
if (!this.statics.path || !this.statics.route) { | ||
throw new Error(`Please call 'setStaticPath' before using 'registerPlugin'`) | ||
} else if (!this.entryRoute) { | ||
throw new Error(`Please call 'setEntryRoute' before using 'registerPlugin'`) | ||
} else if (!this.viewPath) { | ||
throw new Error(`Please call 'setViewsPath' before using 'registerPlugin'`) | ||
} else if (!this.apiRoutes) { | ||
throw new Error(`Please call 'setApiRoutes' before using 'registerPlugin'`) | ||
} else if (!this.bullBoardQueues) { | ||
throw new Error(`Please call 'setQueues' before using 'registerPlugin'`) | ||
} else if (!this.errorHandler) { | ||
throw new Error(`Please call 'setErrorHandler' before using 'registerPlugin'`) | ||
} | ||
|
||
this.app.basePath(this.basePath).get( | ||
`${this.statics.route}/*`, | ||
serveStatic({ | ||
root: this.nodeModulesRootPath, // needs to be adjusted for monorepos with node_modules at another level | ||
rewriteRequestPath: (path) => { | ||
const newPath = path.replace([this.basePath, this.statics.route].join(''), this.statics.path as string) | ||
return newPath | ||
}, | ||
}), | ||
) | ||
|
||
this.app.basePath(this.basePath).route('/', this.apiRoutes) | ||
|
||
// eslint-disable-next-line @typescript-eslint/unbound-method | ||
const { method, handler } = this.entryRoute | ||
const routes = this.entryRoute.route as readonly string[] | ||
routes.forEach((route) => { | ||
this.app.basePath(this.basePath)[method](route, async (c: Context) => { | ||
// eslint-disable-next-line @typescript-eslint/await-thenable | ||
const { name: fileName, params } = await handler({ basePath: this.basePath, uiConfig: this.uiConfig || {} }) | ||
const template = await ejs.renderFile(`${this.viewPath}/${fileName}`, params) | ||
return c.html(template) | ||
}) | ||
}) | ||
|
||
return this | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
version: "3" | ||
services: | ||
redis: | ||
image: redis:7 | ||
ports: | ||
- "6379:6379" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters