Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandomema committed Apr 19, 2024
1 parent 26e6c6c commit 687d366
Show file tree
Hide file tree
Showing 6 changed files with 347 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/ZumitoFramework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class ZumitoFramework {
* @see {@link TranslationManager}
*/
translations: TranslationManager;
routes: any;
routes: Map<string, (req: Request, res: Response) => void>;

/**
* The database models loaded in the framework.
Expand Down Expand Up @@ -162,6 +162,7 @@ export class ZumitoFramework {
this.events = new Map();
this.translations = new TranslationManager();
this.models = [];
this.routes = new Map();
this.eventManager = new EventManager();

if (settings.logLevel) {
Expand Down Expand Up @@ -254,6 +255,9 @@ export class ZumitoFramework {
//Route Prefixes
//this.app.use("/", indexRouter);
//this.app.use("/api/", apiRouter);
this.routes.forEach((router, path) => {
this.app.use(path, router);
});

// throw 404 if URL not found
this.app.all('*', function (req, res) {
Expand Down
11 changes: 11 additions & 0 deletions src/definitions/FrameworkRouter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Request, Response } from 'express';

export abstract class FrameworkRouter {
basePath: string = '';

constructor(basePath: string) {
this.basePath = basePath;
}

abstract getRoutes(): Map<string, (req: Request, res: Response) => void>;
}
28 changes: 28 additions & 0 deletions src/definitions/Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ import {
} from 'discord.js';
import { DatabaseModel } from './DatabaseModel.js';
import { CommandManager } from '../services/CommandManager.js';
import { Request, Response } from 'express';
import { FrameworkRouter } from "./FrameworkRouter.js";

export abstract class Module {
protected path: string;
protected framework: ZumitoFramework;
protected commands: CommandManager;
protected events: Map<string, FrameworkEvent> = new Map();
protected models: Array<DatabaseModel> = [];
protected routes: Map<string, (req: Request, res: Response) => void> = new Map();


protected commandManager: CommandManager;

Expand All @@ -34,6 +38,7 @@ export abstract class Module {
await this.registerEvents();
await this.registerTranslations();
await this.registerModels();
await this.registerRoutes();
}

async registerCommands() {
Expand Down Expand Up @@ -165,4 +170,27 @@ export abstract class Module {
getModels(): Array<DatabaseModel> {
return this.models;
}

async registerRoutes(subpath = '') {
if (!fs.existsSync(path.join(this.path, 'routes', subpath))) return;
const files = fs.readdirSync(path.join(this.path, 'routes', subpath));
for (const file of files) {
if (file.endsWith('.js') || file.endsWith('.ts')) {
const Router: any = await import('file://' + `${this.path}/routes/${subpath}/${file}`).then(r => Object.values(r)[0]);
if (Router.prototype instanceof FrameworkRouter) {
const router = new Router(subpath ? '/' + subpath : '');
this.routes = new Map([...this.routes, ...router.getRoutes()]);
} else {
console.warn(`[🔄🟡 ] ${subpath}/${file} is not a valid router on module ${this.constructor.name} \n It must extend the FrameworkRouter class instead of ${Router.prototype}`);
continue;
}
} else if (fs.lstatSync(path.join(this.path, 'routes', subpath, file)).isDirectory()) {
await this.registerRoutes(path.join(subpath, file));
}
}
}

getRoutes(): Map<string, (req: Request, res: Response) => void> {
return this.routes;
}
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { TranslationManager } from './services/TranslationManager.js';
import { ZumitoFramework } from './ZumitoFramework.js';
import * as discord from 'discord.js';
import { EventParameters } from './definitions/parameters/EventParameters.js';
import { FrameworkRouter } from './types/FrameworkRouter.js';

Check failure on line 30 in src/index.ts

View workflow job for this annotation

GitHub Actions / Check

Cannot find module './types/FrameworkRouter.js' or its corresponding type declarations.

export {
ZumitoFramework,
Expand Down Expand Up @@ -54,4 +55,5 @@ export {
StatusManagerOptions,
discord,
EventParameters,
FrameworkRouter,
};
6 changes: 1 addition & 5 deletions src/services/ModuleManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,8 @@ export class ModuleManager {
this.framework.models.push(model);
});

/*
// Register module routes
this.routes = new Map([...this.routes, ...moduleInstance.getRoutes()]);
*/
this.framework.routes = new Map([...this.framework.routes, ...module.getRoutes()] as any);
}

async instanceModule(module: any, rootPath: string, name?: string) {
Expand Down
Loading

0 comments on commit 687d366

Please sign in to comment.