diff --git a/template/src/api/handlers/handler.js b/template/src/api/handlers/handler.js index 2fb4fea7..593ee94f 100644 --- a/template/src/api/handlers/handler.js +++ b/template/src/api/handlers/handler.js @@ -8,14 +8,11 @@ import { File } from '@asyncapi/generator-react-sdk'; const OPTIONS_MESSAGE_HEADERS_STRING = 'options.message.headers'; -function receiveHandler(operation) { - if (!operation.isReceive()) { - return ''; - } - +function handler(operation) { const operationId = operation.id(); const message = operation.messages().all()[0]; - const lambdaChannel = operation.extensions().get('x-lambda'); + + const lambdaChannel = operation.isReceive() && operation.extensions().get('x-lambda'); const exportedHandler = ` /** @@ -80,77 +77,10 @@ function receiveHandler(operation) { `; return ` - ${lambdaChannel ? 'const fetch = require("node-fetch");' : ''} + ${ lambdaChannel ? 'const fetch = require("node-fetch");' : ''} const ${operationId}Middlewares = []; - ${exportedHandler} - - ${privateHandlerLogic} - `; -} - -function sendHandler(operation) { - if (!operation.isSend()) { - return ''; - } - - const operationId = operation.id(); - const message = operation.messages().all()[0]; - - const exportedHandler = ` - /** - * Registers a middleware function for the ${operationId} operation to be executed during request processing. - * - * Middleware functions have access to options object that you can use to access the message content and other helper functions - * - * @param {function} middlewareFn - The middleware function to be registered. - * @throws {TypeError} If middlewareFn is not a function. - */ - handler.${convertOpertionIdToMiddlewareFn(operationId)} = (middlewareFn) => { - if (typeof middlewareFn !== 'function') { - throw new TypeError('middlewareFn must be a function'); - } - ${operationId}Middlewares.push(middlewareFn); - } - `; - - const privateHandlerLogic = ` - /** - * ${operation.hasSummary() ? operation.summary() : ''} - * - * @param {object} options - * @param {object} options.message - ${ - message.headers() - ? Object.entries(message.headers().properties()) - .map(([fieldName, field]) => { - return docline(field, fieldName, OPTIONS_MESSAGE_HEADERS_STRING); - }) - .join('\n') - : '' - } - * - ${ - message.payload() - ? Object.entries(message.payload().properties()) - .map(([fieldName, field]) => { - return docline(field, fieldName, OPTIONS_MESSAGE_HEADERS_STRING); - }) - .join('\n') - : '' - } - */ - handler._${operationId} = async ({message}) => { - for (const middleware of ${operationId}Middlewares) { - await middleware(message); - } - }; - `; - - return ` - const ${operationId}Middlewares = []; - ${exportedHandler} ${privateHandlerLogic} @@ -172,12 +102,7 @@ export default function handlerRender({ `; for (const operation of channel.operations()) { - if (operation.isSend()) { - routeHandler += sendHandler(operation); - } - if (operation.isReceive()) { - routeHandler += receiveHandler(operation); - } + routeHandler += handler(operation); } return {routeHandler}; });