preparing multiple Decorators for different targets (like openapi / dispatcher)

This commit is contained in:
Martin Karkowski 2020-08-23 09:16:14 +02:00
parent c780c79211
commit a7def9cfed

View File

@ -1,17 +1,16 @@
// Symbols for the Property Registery:
const _registeredMethods_ = Symbol('_registedMethods_');
const _registeredParams_ = Symbol('_registedParams_');
const _registeredDispatcherMethods_ = Symbol('_registeredDispatcherMethods_');
const _registeredDispatcherParams_ = Symbol('_registeredDispatcherParams_');
// Interfaces for the Class
export interface IExportApiParameters {
url: string,
export interface IExportToDispatcherParameters {
}
export interface IExportMethodParameters {
export interface IExportMethodToDispatcherParameters {
url?: string,
}
export interface IExportPropertyParameters {
export interface IExportPropertyToDispatcherParameters {
url?: string,
readonly?: boolean
}
@ -24,7 +23,7 @@ export const unicorn = {
* Decorator used to export a Class API over openAPI
* @param options
*/
export function eportApi(options: IExportApiParameters) {
export function exportsElementsToDispatcher(options: IExportToDispatcherParameters) {
return function <T extends { new(...args: any[]): {} }>(Base: T) {
return class extends Base {
constructor(...args: any[]) {
@ -32,16 +31,13 @@ export function eportApi(options: IExportApiParameters) {
const _this = this as any;
// Adding the Path Option.
_this.__path = options.url;
// extract the Registered Methods of the Class.
const registeredMethods = Base.prototype[_registeredMethods_] as Map<string, IExportMethodParameters>;
const registeredParams = Base.prototype[_registeredParams_] as Map<string, IExportPropertyParameters>;
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.__dispatchRegisterdMethods = (cb: (methodName: string, callback: (...args) => Promise<any>, options: IExportMethodParameters) => void) => {
_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);
@ -51,7 +47,7 @@ export function eportApi(options: IExportApiParameters) {
// Online if they are present, iterate over them
if (registeredParams) {
_this.__dispatchRegisterdParams = (cb: (methodName: string, callback: (...args) => Promise<any>, options: IExportPropertyParameters) => void) => {
_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);
@ -67,11 +63,11 @@ export function eportApi(options: IExportApiParameters) {
* Decorator, used to export the Method.
* @param options
*/
export function exportMethod(options: IExportMethodParameters = {}) {
export function exportMethodToDispatcher(options: IExportMethodToDispatcherParameters = {}) {
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
target[_registeredMethods_] = target[_registeredMethods_] || new Map<string, IExportMethodParameters>();
target[_registeredDispatcherMethods_] = target[_registeredDispatcherMethods_] || new Map<string, IExportMethodToDispatcherParameters>();
// Here we just add some information that class decorator will use
target[_registeredMethods_].set(propertyKey, options);
target[_registeredDispatcherMethods_].set(propertyKey, options);
};
}
@ -79,10 +75,10 @@ export function exportMethod(options: IExportMethodParameters = {}) {
* Decorator, will create a POST and GET api for the Parameter.
* @param options
*/
export function exportProperty(options: IExportPropertyParameters = {}) {
export function exportPropertyToDispatcher(options: IExportPropertyToDispatcherParameters = {}) {
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
target[_registeredParams_] = target[_registeredParams_] || new Map<string, IExportPropertyParameters>();
target[_registeredDispatcherParams_] = target[_registeredDispatcherParams_] || new Map<string, IExportPropertyToDispatcherParameters>();
// Here we just add some information that class decorator will use
target[_registeredParams_].set(propertyKey, options);
target[_registeredDispatcherParams_].set(propertyKey, options);
};
}