nope/lib/parsers/open-api/templates/function.ts.handlebars
Martin Karkowski a5b82ebb9e adding mqtt
2020-11-15 20:11:25 +01:00

62 lines
1.8 KiB
Handlebars

// Automatic Genearted File for NopeModule
// To update run `npm run build:backend`
import { INopeDispatcher } from "{{pathOfDispatcher}}";
import { Operation } from "express-openapi";
export default function (_dispatcher: INopeDispatcher) {
let operations = {
{{mode}}
};
// Function to Parse the Inputs
const parseParams = (req) => {
return [{{#each schema.inputs}}req.body.{{name}}{{#unless @last}}, {{/unless}}{{/each}}]
}
// Define the Action.
async function {{mode}}(req, res, next) {
try {
// Transform the Operation to the Callback of the Dispatcher.
const result = await _dispatcher.performCall('{{id}}', parseParams(req))
// Finish the Task.
res.status(200).json(result)
} catch (e) {
// An error Occourd =>
res.status(500).json(e)
}
}
// Define the apiDoc for this specific Funtion
{{mode}}.apiDoc = {
tags: ['{{tag}}'],
{{#if methodDescription}}summary: '{{methodDescription}}',{{/if}}
{{#if operationId}}operationId: '{{operationId}}',{{/if}}
parameters: [
{{#if hasInput}}
{
name: '{{name}}',
in: "body",
description: '{{description}}',
required: true,
schema: {{{parsedInput}}}
}
{{/if}}
],
responses: {
200: {
{{#if resultDescription}}description: '{{resultDescription}}',{{/if}}
schema: {{{parsedOutput}}}
},
default: {
description: 'An error occurred',
schema: {
additionalProperties: true
}
}
}
} as Operation;
return operations;
}