nope/lib/dispatcher/nopeDispatcherDecorators.ts
Martin Karkowski 3743aeacf6 Reformat file
2020-08-24 13:34:48 +02:00

80 lines
3.2 KiB
TypeScript

// Symbols for the Property Registery:
const _registeredDispatcherMethods_ = Symbol('_registeredDispatcherMethods_');
const _registeredDispatcherParams_ = Symbol('_registeredDispatcherParams_');
// Interfaces for the Class
export interface IExportToDispatcherParameters {
}
export interface IExportMethodToDispatcherParameters {
url?: string,
}
export interface IExportPropertyToDispatcherParameters {
url?: string,
readonly?: boolean
}
/**
* Decorator used to export a Class API over openAPI
* @param options
*/
export function exportsElementsToDispatcher(options: IExportToDispatcherParameters) {
return function <T extends { new(...args: any[]): {} }>(Base: T) {
return class extends Base {
constructor(...args: any[]) {
super(...args);
const _this = this as any;
// extract the Registered Methods of the Class.
const registeredMethods = Base.prototype[_registeredDispatcherMethods_] as Map<string, IExportMethodToDispatcherParameters>;
const registeredParams = Base.prototype[_registeredDispatcherParams_] as Map<string, IExportPropertyToDispatcherParameters>;
// Online if they are present, iterate over them
if (registeredMethods) {
_this.__dispatcherRegisterdMethods = (cb: (methodName: string, callback: (...args) => Promise<any>, options: IExportMethodToDispatcherParameters) => void) => {
registeredMethods.forEach((options, methodName) => {
// Callback the Method
cb(methodName, async (...args) => _this[methodName](...args), options);
});
}
}
// Online if they are present, iterate over them
if (registeredParams) {
_this.__dispatcherRegisterdParams = (cb: (methodName: string, callback: (...args) => Promise<any>, options: IExportPropertyToDispatcherParameters) => void) => {
registeredParams.forEach((options, methodName) => {
// Callback the Method
cb(methodName, async (...args) => _this[methodName](...args), options);
});
}
}
}
};
}
}
/**
* Decorator, used to export the Method.
* @param options
*/
export function exportMethodToDispatcher(options: IExportMethodToDispatcherParameters = {}) {
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
target[_registeredDispatcherMethods_] = target[_registeredDispatcherMethods_] || new Map<string, IExportMethodToDispatcherParameters>();
// Here we just add some information that class decorator will use
target[_registeredDispatcherMethods_].set(propertyKey, options);
};
}
/**
* Decorator, will create a POST and GET api for the Parameter.
* @param options
*/
export function exportPropertyToDispatcher(options: IExportPropertyToDispatcherParameters = {}) {
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
target[_registeredDispatcherParams_] = target[_registeredDispatcherParams_] || new Map<string, IExportPropertyToDispatcherParameters>();
// Here we just add some information that class decorator will use
target[_registeredDispatcherParams_].set(propertyKey, options);
};
}