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