step 1
This commit is contained in:
@ -1,5 +1,31 @@
|
||||
import EventEmitter from "events";
|
||||
|
||||
export class ImplementableApi extends EventEmitter {
|
||||
namespace ImplementableApi {
|
||||
export type Config = {
|
||||
name: string;
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* [optional] idListener is the way to identificate the listener who's the src of the newEntries
|
||||
* the type field permit to know which type of message it is
|
||||
* rawContent field is the string content of the message
|
||||
*/
|
||||
export type Message = {
|
||||
idListener?: number;
|
||||
type: "newEntriesNotify" | "newListener";
|
||||
rawContent: any;
|
||||
}
|
||||
}
|
||||
|
||||
abstract class ImplementableApi extends EventEmitter {
|
||||
readonly name: string;
|
||||
|
||||
constructor(readonly config: ImplementableApi.Config) {
|
||||
super();
|
||||
this.name = config.name;
|
||||
}
|
||||
|
||||
public abstract receivedMessage(message: ImplementableApi.Message): void;
|
||||
}
|
||||
|
||||
export { ImplementableApi }
|
@ -1,39 +1,42 @@
|
||||
import { ImplementableApi } from "./implementableApi";
|
||||
|
||||
import { DiscordParser } from './discordParser'
|
||||
import { LogWriter } from './logWriter'
|
||||
import { PeerTubeRequester } from './peertubeRequester'
|
||||
|
||||
namespace Router {
|
||||
export type Config = {
|
||||
events: {
|
||||
"name": string,
|
||||
"type": 0 | 1
|
||||
name: string,
|
||||
type: 0 | 1
|
||||
}[];
|
||||
routes: {
|
||||
"serviceName": string,
|
||||
"eventAccepted": string[] | undefined,
|
||||
"eventEmitted": string[] | undefined
|
||||
apis: {
|
||||
apiName: string
|
||||
}[]
|
||||
}
|
||||
|
||||
export type GlobalConfig = {
|
||||
router: Config;
|
||||
discord: DiscordParser.Config;
|
||||
peertubeRequester: PeerTubeRequester.Config;
|
||||
logWriter: LogWriter.Config;
|
||||
}
|
||||
}
|
||||
|
||||
class Router {
|
||||
api_array: ImplementableApi[] = [];
|
||||
config: Router.Config;
|
||||
api_array: {[key: string]: ImplementableApi} = {};
|
||||
readonly routes: Router.Config;
|
||||
|
||||
constructor(readonly path: string) {
|
||||
const tmp_config: Router.Config = require(path);
|
||||
this.config = tmp_config;
|
||||
// if(tmp_config["events"] === )
|
||||
constructor(readonly config: Router.GlobalConfig) {
|
||||
this.routes = config.router;
|
||||
|
||||
this.api_array[config.discord.name] = new DiscordParser(config.discord);
|
||||
this.api_array[config.peertubeRequester.name] = new PeerTubeRequester(config.peertubeRequester);
|
||||
this.api_array[config.logWriter.name] = new LogWriter(config.logWriter);
|
||||
}
|
||||
|
||||
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`;
|
||||
public receivedMessage(message: ImplementableApi.Message) {
|
||||
this.routes.apis.forEach((api) => this.api_array[api.apiName].receivedMessage(message))
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user