// Symbols for the Property Registery: const _registeredMethods_ = Symbol('_registedMethods_'); const _registeredParams_ = Symbol('_registedParams_'); // Interfaces for the Class export interface IExportApiParameters { url: string, } export interface IExportMethodParameters { url?: string, } export interface IExportPropertyParameters { url?: string, readonly?: boolean } export const unicorn = { test: [] } /** * Decorator used to export a Class API over openAPI * @param options */ export function eportApi(options: IExportApiParameters) { return function (Base: T) { return class extends Base { constructor(...args: any[]) { super(...args); 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; const registeredParams = Base.prototype[_registeredParams_] as Map; // Online if they are present, iterate over them if (registeredMethods) { _this.__dispatchRegisterdMethods = (cb: (methodName: string, callback: (...args) => Promise, options: IExportMethodParameters) => 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.__dispatchRegisterdParams = (cb: (methodName: string, callback: (...args) => Promise, options: IExportPropertyParameters) => 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 exportMethod(options: IExportMethodParameters = {}) { return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) { target[_registeredMethods_] = target[_registeredMethods_] || new Map(); // Here we just add some information that class decorator will use target[_registeredMethods_].set(propertyKey, options); }; } /** * Decorator, will create a POST and GET api for the Parameter. * @param options */ export function exportProperty(options: IExportPropertyParameters = {}) { return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) { target[_registeredParams_] = target[_registeredParams_] || new Map(); // Here we just add some information that class decorator will use target[_registeredParams_].set(propertyKey, options); }; }