nope/lib/decorators.ts

88 lines
3.0 KiB
TypeScript
Raw Normal View History

2020-08-21 14:49:48 +00:00
// Symbols for the Property Registery:
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);
const _this = this as any;
2020-08-21 14:49:48 +00:00
// Adding the Path Option.
_this.__path = options.url;
2020-08-21 14:49:48 +00:00
// 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>;
2020-08-21 14:49:48 +00:00
// Online if they are present, iterate over them
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
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) {
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
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) {
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
target[_registeredParams_].set(propertyKey, options);
2020-08-21 14:49:48 +00:00
};
}