1 | "use strict";
|
2 | class Routes {
|
3 | constructor(appName) {
|
4 | this.appName = appName || "";
|
5 | this.routesHandle = {
|
6 | get: {},
|
7 | post: {},
|
8 | };
|
9 | }
|
10 | get(obj) {
|
11 | for (const [key, value] of Object.entries(obj)) {
|
12 | this.routesHandle.get["/" + this.appName + key] = this.initCallback(value);
|
13 | }
|
14 | }
|
15 | post(obj) {
|
16 | for (const [key, value] of Object.entries(obj)) {
|
17 | this.routesHandle.post["/" + this.appName + key] = this.initCallback(value);
|
18 | }
|
19 | }
|
20 | mergeAppRoutes(appRoutesHandle) {
|
21 | const self = this;
|
22 | for (const [method, value] of Object.entries(this.routesHandle)) {
|
23 | this.routesHandle[method] = Object.assign({}, value, appRoutesHandle[method]);
|
24 | }
|
25 | }
|
26 | initCallback(path) {
|
27 | const self = this;
|
28 | return async (ctx) => {
|
29 | const Cb = require(bun.globalPath.APP_PATH + "/" + self.appName + path);
|
30 | const render = ctx.render;
|
31 | ctx.render = async (_path, params) => {
|
32 | await render(self.appName + "/" + _path, params);
|
33 | };
|
34 | const renderHtml = ctx.renderHtml;
|
35 | ctx.renderHtml = async (_path, params) => {
|
36 | return await renderHtml(self.appName + "/" + _path, params);
|
37 | };
|
38 | const oCb = new Cb();
|
39 | oCb.beforeExecute && await oCb.beforeExecute.call(oCb, ctx);
|
40 | await oCb.execute.call(oCb, ctx);
|
41 | oCb.afterExecute && await oCb.afterExecute.call(oCb, ctx);
|
42 | };
|
43 | }
|
44 | }
|
45 | module.exports = Routes;
|
46 |
|
\ | No newline at end of file |