This commit is contained in:
Amaury
2021-05-19 12:55:10 +02:00
commit 2c67212790
15 changed files with 412 additions and 0 deletions

5
src/implementableApi.ts Normal file
View File

@ -0,0 +1,5 @@
import EventEmitter from "events";
export class ImplementableApi extends EventEmitter {
}

4
src/index.ts Normal file
View File

@ -0,0 +1,4 @@
// export * as Router from "./router"
export {Router} from "./router"
export {ImplementableApi} from "./implementableApi"

40
src/router.ts Normal file
View File

@ -0,0 +1,40 @@
import { ImplementableApi } from "./implementableApi";
namespace Router {
export type Config = {
events: {
"name": string,
"type": 0 | 1
}[];
routes: {
"serviceName": string,
"eventAccepted": string[] | undefined,
"eventEmitted": string[] | undefined
}[]
}
}
class Router {
api_array: ImplementableApi[] = [];
config: Router.Config;
constructor(readonly path: string) {
const tmp_config: Router.Config = require(path);
this.config = tmp_config;
// if(tmp_config["events"] === )
}
public addApi<T extends ImplementableApi>(new_api: T) {
const new_api_conf = this.config.routes.find((elmt) => elmt.serviceName === new_api.constructor.name);
// test if fun exist
for(let ev in new_api_conf?.eventAccepted)
if(ev !in new_api)
throw new Error(`The class ${new_api.constructor.name} haven't the expected function`);
//test if events call exist
for(let ev in new_api_conf?.eventEmitted)
if(ev !in new_api.eventNames)
throw `The class ${new_api.constructor.name} haven't the well defined events`;
}
}
export { Router };