nope/lib/openapi/nopeOpenAPIDecorators.ts
2020-09-07 23:23:18 +02:00

116 lines
4.1 KiB
TypeScript

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
}
export interface IExportFunctionToOpenAPIParameters {
uri?: string
}
/**
* 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
*/
export function exportMethodToOpenAPI(options: IExportMethodToOpenAPIParameters) {
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
*/
export function exportPropertyToOpenAPI(options: IExportPropertyToOpenAPIParameters) {
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);
};
}
/**
* Decorator, that will export the Function to a Dispatcher
* @param func The Function
* @param options The Options.
*/
export function exportFunctionToOpenAPI<T>(func: T, options: IExportFunctionToOpenAPIParameters) {
return func;
}