init
This commit is contained in:
5
src/implementableApi.ts
Normal file
5
src/implementableApi.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import EventEmitter from "events";
|
||||
|
||||
export class ImplementableApi extends EventEmitter {
|
||||
|
||||
}
|
4
src/index.ts
Normal file
4
src/index.ts
Normal file
@ -0,0 +1,4 @@
|
||||
// export * as Router from "./router"
|
||||
export {Router} from "./router"
|
||||
|
||||
export {ImplementableApi} from "./implementableApi"
|
40
src/router.ts
Normal file
40
src/router.ts
Normal 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 };
|
Reference in New Issue
Block a user