nope/lib/dispatcher/nopeDispatcherDecorators.ts

137 lines
4.3 KiB
TypeScript
Raw Normal View History

2020-08-25 22:11:26 +00:00
import { getSingleton } from "../helpers/singletonMethod";
2020-08-21 14:49:48 +00:00
// Symbols for the Property Registery:
const _registeredDispatcherMethods_ = Symbol('_registeredDispatcherMethods_');
const _registeredDispatcherParams_ = Symbol('_registeredDispatcherParams_');
2020-08-21 14:49:48 +00:00
// Interfaces for the Class
export interface IExportToDispatcherParameters {
2020-08-25 10:33:33 +00:00
uri?: string,
2020-08-21 14:49:48 +00:00
}
export interface IExportMethodToDispatcherParameters {
uri?: string,
2020-08-25 22:11:26 +00:00
}
export interface IExportFunctionToDispatcherParameters {
uri: string,
2020-08-21 14:49:48 +00:00
}
export interface IExportPropertyToDispatcherParameters {
2020-08-25 10:33:33 +00:00
uri?: string,
2020-08-21 14:49:48 +00:00
readonly?: boolean
}
const container = getSingleton('nopeBackendDispatcher.container', () => {
2020-08-25 22:11:26 +00:00
return {
methods: new Map<string, {
uri: string,
callback: (...args) => Promise<any>,
options: IExportMethodToDispatcherParameters
}>(),
parameters: new Map<string, {
uri: string,
callback: (...args) => Promise<any>,
options: IExportPropertyToDispatcherParameters
}>(),
functions: new Map<string, {
uri: string,
callback: (...args) => Promise<any>,
options: IExportFunctionToDispatcherParameters
2020-08-25 22:11:26 +00:00
}>()
}
});
2020-08-21 14:49:48 +00:00
/**
* Decorator used to export a Class API over openAPI
* @param options
*/
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);
const _this = this as any;
2020-08-21 14:49:48 +00:00
// extract the Registered Methods of the Class.
const registeredMethods = Base.prototype[_registeredDispatcherMethods_] as Map<string, IExportMethodToDispatcherParameters>;
2020-08-24 11:34:48 +00:00
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
if (registeredMethods) {
2020-08-25 22:11:26 +00:00
registeredMethods.forEach((options, methodName) => {
// Register the Methods
const uri = (options.uri || Base.prototype.name) + (options.uri || methodName);
container.instance.methods.set(uri, {
callback: async (...args) => _this[methodName](...args),
uri,
options
});
2020-08-25 22:11:26 +00:00
});
2020-08-21 14:49:48 +00:00
}
// Online if they are present, iterate over them
if (registeredParams) {
2020-08-25 22:11:26 +00:00
registeredParams.forEach((options, parameterName) => {
// Register the Methods
const uri = (options.uri || Base.prototype.name) + (options.uri || parameterName);
container.instance.parameters.set(uri, {
callback: async (...args) => _this[parameterName](...args),
uri,
options
});
2020-08-25 22:11:26 +00:00
});
2020-08-21 14:49:48 +00:00
}
}
};
}
}
/**
* Decorator, used to export the Method.
* @param options
*/
2020-08-25 10:33:33 +00:00
export function exportMethodToDispatcher(options: IExportMethodToDispatcherParameters) {
2020-08-21 14:49:48 +00:00
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
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
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-25 10:33:33 +00:00
export function exportPropertyToDispatcher(options: IExportPropertyToDispatcherParameters) {
2020-08-21 14:49:48 +00:00
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
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
target[_registeredDispatcherParams_].set(propertyKey, options);
2020-08-21 14:49:48 +00:00
};
2020-08-25 22:11:26 +00:00
}
/**
* Defintion of a Functon.
*/
export type callable<T> = {
(...args): T
}
/**
* Decorator, that will export the Function to a Dispatcher
* @param func The Function
* @param options The Options.
*/
export function exportFunctionToDispatcher<T>(func: T, options: IExportFunctionToDispatcherParameters) {
2020-08-25 22:11:26 +00:00
container.instance.functions.set(options.uri, {
callback: async (...args) => await ((func as any)(...args)),
2020-08-25 22:11:26 +00:00
options,
uri: options.uri || (func as any).name
2020-08-25 22:11:26 +00:00
});
return func;
}