2020-08-24 11:35:32 +00:00
|
|
|
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 {
|
2020-08-24 21:09:16 +00:00
|
|
|
// Additional Convert-Function.
|
|
|
|
convertfunction?: (req) => any[]
|
|
|
|
// Custom overwritten
|
2020-08-24 11:35:32 +00:00
|
|
|
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,
|
2020-08-24 21:09:16 +00:00
|
|
|
resultDescription?: string,
|
2020-08-24 11:35:32 +00:00
|
|
|
tags?: string[],
|
2020-08-24 21:09:16 +00:00
|
|
|
// The Operation ID. Defaultly will be generated on the Base URI and the Method-URI
|
2020-08-24 11:35:32 +00:00
|
|
|
operationId?: string
|
|
|
|
}
|
|
|
|
|
2020-08-24 21:09:16 +00:00
|
|
|
export interface IExportMethodToOpenAPIParameters extends IOpenAPIServiceParameters {
|
|
|
|
// If no Parameter is used => Get, otherwise Post.
|
|
|
|
// Overwrite Manually.
|
|
|
|
method?: Array<'POST' | 'GET'> | 'POST' | 'GET',
|
2020-08-24 11:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface IExportPropertyToOpenAPIParameters {
|
|
|
|
uri?: string,
|
|
|
|
readonly?: boolean
|
|
|
|
}
|
|
|
|
|
2020-09-07 21:23:18 +00:00
|
|
|
export interface IExportFunctionToOpenAPIParameters {
|
|
|
|
uri?: string
|
|
|
|
}
|
|
|
|
|
2020-08-24 11:35:32 +00:00
|
|
|
/**
|
|
|
|
* Decorator used to export a Class API over openAPI
|
|
|
|
* @param options
|
|
|
|
*/
|
|
|
|
export function exportsElementsToOpenAPI(options: IExportToOpenAPIParameters) {
|
|
|
|
return function <T extends { new(...args: any[]): {} }>(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<string, IExportMethodToOpenAPIParameters>;
|
|
|
|
const registeredParams = Base.prototype[_registeredOpenAPIParams_] as Map<string, IExportPropertyToOpenAPIParameters>;
|
|
|
|
|
|
|
|
// Online if they are present, iterate over them
|
|
|
|
if (registeredMethods) {
|
|
|
|
_this.__OpenAPIRegisterdMethods = (cb: (methodName: string, callback: (...args) => Promise<any>, 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<any>, 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
|
|
|
|
*/
|
2020-08-24 21:09:16 +00:00
|
|
|
export function exportMethodToOpenAPI(options: IExportMethodToOpenAPIParameters) {
|
2020-08-24 11:35:32 +00:00
|
|
|
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
|
|
|
|
target[_registeredOpenAPIMethods_] = target[_registeredOpenAPIMethods_] || new Map<string, IExportMethodToOpenAPIParameters>();
|
|
|
|
// 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
|
|
|
|
*/
|
2020-08-24 21:09:16 +00:00
|
|
|
export function exportPropertyToOpenAPI(options: IExportPropertyToOpenAPIParameters) {
|
2020-08-24 11:35:32 +00:00
|
|
|
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
|
|
|
|
target[_registeredOpenAPIParams_] = target[_registeredOpenAPIParams_] || new Map<string, IExportPropertyToOpenAPIParameters>();
|
|
|
|
// Here we just add some information that class decorator will use
|
|
|
|
target[_registeredOpenAPIParams_].set(propertyKey, options);
|
|
|
|
};
|
2020-09-07 18:12:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decorator, that will export the Function to a Dispatcher
|
|
|
|
* @param func The Function
|
|
|
|
* @param options The Options.
|
|
|
|
*/
|
2020-09-07 21:23:18 +00:00
|
|
|
export function exportFunctionToOpenAPI<T>(func: T, options: IExportFunctionToOpenAPIParameters) {
|
2020-09-07 18:12:19 +00:00
|
|
|
return func;
|
2020-08-24 11:35:32 +00:00
|
|
|
}
|