2020-08-21 14:49:48 +00:00
|
|
|
// Symbols for the Property Registery:
|
2020-08-21 20:42:11 +00:00
|
|
|
const _registeredMethods_ = Symbol('_registedMethods_');
|
|
|
|
const _registeredParams_ = Symbol('_registedParams_');
|
2020-08-21 14:49:48 +00:00
|
|
|
|
|
|
|
// 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 <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
|
|
|
// Adding the Path Option.
|
2020-08-21 20:42:11 +00:00
|
|
|
_this.__path = options.url;
|
2020-08-21 14:49:48 +00:00
|
|
|
|
|
|
|
// extract the Registered Methods of the Class.
|
2020-08-21 20:42:11 +00:00
|
|
|
const registeredMethods = Base.prototype[_registeredMethods_] as Map<string, IExportMethodParameters>;
|
|
|
|
const registeredParams = Base.prototype[_registeredParams_] as Map<string, IExportPropertyParameters>;
|
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) {
|
|
|
|
_this.__dispatchRegisterdMethods = (cb: (methodName: string, callback: (...args) => Promise<any>, options: IExportMethodParameters) => void) => {
|
|
|
|
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) {
|
|
|
|
_this.__dispatchRegisterdParams = (cb: (methodName: string, callback: (...args) => Promise<any>, options: IExportPropertyParameters) => void) => {
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
export function exportMethod(options: IExportMethodParameters = {}) {
|
|
|
|
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
|
2020-08-21 20:42:11 +00:00
|
|
|
target[_registeredMethods_] = target[_registeredMethods_] || new Map<string, IExportMethodParameters>();
|
2020-08-21 14:49:48 +00:00
|
|
|
// Here we just add some information that class decorator will use
|
2020-08-21 20:42:11 +00:00
|
|
|
target[_registeredMethods_].set(propertyKey, options);
|
2020-08-21 14:49:48 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2020-08-21 20:42:11 +00:00
|
|
|
target[_registeredParams_] = target[_registeredParams_] || new Map<string, IExportPropertyParameters>();
|
2020-08-21 14:49:48 +00:00
|
|
|
// Here we just add some information that class decorator will use
|
2020-08-21 20:42:11 +00:00
|
|
|
target[_registeredParams_].set(propertyKey, options);
|
2020-08-21 14:49:48 +00:00
|
|
|
};
|
|
|
|
}
|