import { IJsonSchemaTypes } from "../../types/IJSONSchema"; // Symbols for the Property Registery: const _registeredOpenAPIMethods_ = Symbol('_registeredOpenAPIMethods_'); const _registeredOpenAPIParams_ = Symbol('_registeredOpenAPIParams_'); // Interfaces for the Class export interface IExportToOpenAPIParameters { uri?: string } export interface IOpenAPIServiceParameters { // Additional Convert-Function. convertfunction?: (req) => any[] // Custom overwritten parameters?: { in: 'path' | 'body' | 'query', // The element provided in the name MUST BE part of the Parameters. name: string, required: boolean, type: IJsonSchemaTypes }[], description?: string, resultDescription?: string, tags?: string[], // The Operation ID. Defaultly will be generated on the Base URI and the Method-URI operationId?: string } export interface IExportMethodToOpenAPIParameters extends IOpenAPIServiceParameters { // If no Parameter is used => Get, otherwise Post. // Overwrite Manually. method?: Array<'POST' | 'GET'> | 'POST' | 'GET', } export interface IExportPropertyToOpenAPIParameters { uri?: string, readonly?: boolean } /** * Decorator used to export a Class API over openAPI * @param options */ export function exportsElementsToOpenAPI(options: IExportToOpenAPIParameters) { return function (Base: T) { return class extends Base { constructor(...args: any[]) { super(...args); const _this = this as any; // extract the Registered Methods of the Class. const registeredMethods = Base.prototype[_registeredOpenAPIMethods_] as Map; const registeredParams = Base.prototype[_registeredOpenAPIParams_] as Map; // Online if they are present, iterate over them if (registeredMethods) { _this.__OpenAPIRegisterdMethods = (cb: (methodName: string, callback: (...args) => Promise, options: IExportMethodToOpenAPIParameters) => 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.__OpenAPIRegisterdParams = (cb: (methodName: string, callback: (...args) => Promise, options: IExportPropertyToOpenAPIParameters) => 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 exportMethodToOpenAPI(options: IExportMethodToOpenAPIParameters) { return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) { target[_registeredOpenAPIMethods_] = target[_registeredOpenAPIMethods_] || new Map(); // Here we just add some information that class decorator will use target[_registeredOpenAPIMethods_].set(propertyKey, options); }; } /** * Decorator, will create a POST and GET api for the Parameter. * @param options */ export function exportPropertyToOpenAPI(options: IExportPropertyToOpenAPIParameters) { return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) { target[_registeredOpenAPIParams_] = target[_registeredOpenAPIParams_] || new Map(); // Here we just add some information that class decorator will use target[_registeredOpenAPIParams_].set(propertyKey, options); }; }