nope/lib/decorators.ts

88 lines
2.7 KiB
TypeScript
Raw Normal View History

2020-08-21 14:49:48 +00:00
// Symbols for the Property Registery:
const _registedMethods_ = Symbol('_registedMethods_');
const _registedParams_ = 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 <T extends { new(...args: any[]): {} }>(Base: T) {
return class extends Base {
constructor(...args: any[]) {
super(...args);
// Adding the Path Option.
(this as any).__path = options.url;
// extract the Registered Methods of the Class.
const registedMethods = Base.prototype[_registedMethods_] as Map<string, IExportMethodParameters>;
const registedParams = Base.prototype[_registedParams_] as Map<string, IExportPropertyParameters>;
// Online if they are present, iterate over them
if (registedMethods) {
registedMethods.forEach((options, methodName) => {
// Provide the code that should be exectuted for every
// Registered Function
unicorn.test.push([methodName, options]);
console.log(unicorn.test)
});
}
// Online if they are present, iterate over them
if (registedParams) {
registedParams.forEach((options, parameterName) => {
// Provide the code that should be exectuted for every
// Registered Function
unicorn.test.push([parameterName, options]);
console.log(unicorn.test)
});
}
}
};
}
}
/**
* Decorator, used to export the Method.
* @param options
*/
export function exportMethod(options: IExportMethodParameters = {}) {
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
target[_registedMethods_] = target[_registedMethods_] || new Map<string, IExportMethodParameters>();
// Here we just add some information that class decorator will use
target[_registedMethods_].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[_registedParams_] = target[_registedParams_] || new Map<string, IExportPropertyParameters>();
// Here we just add some information that class decorator will use
target[_registedParams_].set(propertyKey, options);
};
}