Skip to content

Commit

Permalink
reduce duplicacy in handler
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushik-rishi committed Apr 30, 2024
1 parent 78f4e44 commit afb5e63
Showing 1 changed file with 5 additions and 80 deletions.
85 changes: 5 additions & 80 deletions template/src/api/handlers/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
/**
Expand Down Expand Up @@ -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}
Expand All @@ -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 <File name={`${convertToFilename(channelName)}.js`}>{routeHandler}</File>;
});
Expand Down

0 comments on commit afb5e63

Please sign in to comment.