nope/lib/parsers/open-api/OpenApiParser.ts

132 lines
4.8 KiB
TypeScript
Raw Normal View History

2020-11-09 14:28:09 +00:00
import { readFile } from "fs/promises";
2020-11-09 06:42:24 +00:00
import * as handlebars from 'handlebars';
import { join, relative } from "path";
2020-11-09 14:28:09 +00:00
import { Logger } from 'winston';
2020-11-09 06:42:24 +00:00
import { createFile, createPath } from "../../helpers/fileMethods";
import { deepClone } from "../../helpers/objectMethods";
2020-11-09 14:28:09 +00:00
import { replaceAll } from "../../helpers/stringMethods";
import { IJsonSchema } from '../../types/IJSONSchema';
import { INopeDescriptorFunctionParameter } from "../../types/nope/nopeDescriptor.interface";
import { IParsableDescription } from "../../types/nope/nopeModule.interface";
2020-11-09 06:42:24 +00:00
/**
* Helper function to merge the Parameters into 1 description.
* @param elements
*/
function _unifySchema(elements: INopeDescriptorFunctionParameter[]){
const ret: IJsonSchema = {
type: 'object',
properties: {},
required: []
}
for (const item of elements){
// Assign the Schema
ret[item.name] = item.schema;
// If the Element is Optional,
if (!item.optional){
ret.required.push(item.name)
}
}
return ret;
}
/**
* Function, to parse a description to an Open-API Element.
* @param description
* @param options
*/
export async function parseToOpenAPI(description: IParsableDescription, options: {
outputDir: string,
logger?: Logger,
}){
const _description = deepClone(description);
// load the Template.
const template = await readFile(join(process.cwd(), 'lib' , 'parsers', 'open-api' ,'templates', 'openApiSchema.handlebars'), {
encoding: 'utf-8'
})
// Renderfuncting
const renderAPI = handlebars.compile(template);
await createPath(join(options.outputDir));
// Now iterate over the Methods of the Module and find parsable Methods.
for (const [idx, method] of _description.methods.entries()){
// Test if the Method contains some functions in the input / Output:
const parsedInputs = JSON.stringify(method.schema.inputs);
const parsedOutput = JSON.stringify(method.schema.outputs);
if (!parsedInputs.includes('"function"') && !parsedOutput.includes('"function"')){
// The Method should be parseable.
// 1. Specify the Mode (No Params = GET, else POST)
(method as any).mode = method.schema.inputs.length > 0 ? 'POST' : 'GET';
// Now adapt the Schema of the Method.
// Iterate over the Inputs, add a Description if not provided
// And determine whehter the Parameters are required or not.
method.schema.inputs = method.schema.inputs.map(param => {
// Provide a Description. (If not provided)
param.description = param.description || 'Not provided';
// Open API uses "required" instead of optional => invert
param.optional = !param.optional;
// Parse the Schema in here.
(param as any).parsedSchema = JSON.stringify(param.schema);
return param;
});
// Make shure the Return type isnt an array
method.schema.outputs = Array.isArray(method.schema.outputs) ?
_unifySchema(method.schema.outputs) :
method.schema.outputs;
// Add an Description to the result.
(method as any).resultDescription = method.schema.outputs.description || 'Not Provided';
// And add a Schema for the Return type.
(method as any).parsedOutput = JSON.stringify(method.schema.outputs);
// Determine the Filename.
const fileDir = join(options.outputDir, _description.name, '{instance}' ,'methods');
const fileName = join(fileDir, method.id+'.ts');
// Determine the Import Pathes.
const imports = [
{
dir: join(process.cwd(), 'lib', 'types', 'nope'),
fileName: 'nopeDispatcher.interface',
name: 'pathOfDispatcher'
},
{
dir: join(process.cwd(), 'lib', 'helpers'),
fileName: 'dispatcherPathes',
name: 'pathOfHelper'
}
]
for (const imp of imports){
const relativDir = relative(fileDir, imp.dir);
2020-11-09 14:28:09 +00:00
(method as any)[imp.name] = replaceAll(join(relativDir, imp.fileName), '\\', '/');
2020-11-09 06:42:24 +00:00
}
// Write down the Schema:
await createFile(
// Generate the Path.
fileName,
renderAPI(method)
);
if (options.logger) {
options.logger.info('Generated -> ' + fileName);
}
2020-11-10 07:56:22 +00:00
} else if (options.logger){
options.logger.warn('parser can not convert: "' + description.name + '.' + method.id + '". The Function contains functions as parameters')
2020-11-09 06:42:24 +00:00
}
}
}