diff --git a/README.md b/README.md index d07678f9..07dc4d66 100644 --- a/README.md +++ b/README.md @@ -109,7 +109,7 @@ Consider a scenario where you intend to introduce a new channel or section to th To avoid this, user code remains external to the generated code, functioning as an independent entity that consumes the generated code as a library. By adopting this approach, the user code remains unaffected during template regenerations. -Facilitating this separation involves creating handlers and associating them with their respective routes. These handlers can then be seamlessly integrated into the template's workflow by importing the appropriate methods to register the handlers. In doing so, the template's `client.` method becomes the bridge between the user-written handlers and the generated code. This can be used to register middlewares for specific methods on specific channels. +Facilitating this separation involves creating handlers and associating them with their respective routes. These handlers can then be seamlessly integrated into the template's workflow by importing the appropriate methods to register the handlers. In doing so, the template's `client.registerMiddleware` method becomes the bridge between the user-written handlers and the generated code. This can be used to register middlewares for specific methods on specific channels. > The AsyncAPI file used for the example is [here](https://bit.ly/asyncapi) @@ -139,7 +139,7 @@ function testPublish() { // the passed handler function is called once the app sends a message to the channel // For example `client.app.send` sends a message to some channel using and before it is sent, you want to perform some other actions // in such a case, you can register middlewares like below - client.turnOn((message) => { // `turnOn` is the respective operationId + client.registerTurnOnMiddleware((message) => { // `turnOn` is the respective operationId console.log("hitting the middleware before publishing the message"); console.log( `sending turn on message to streetlight ${message.params.streetlightId}`, @@ -169,11 +169,11 @@ function testSubscribe() { // Registering your custom logic in a channel-specific handler // the passed handler functions are called once the app gets message sent to the channel - client.receiveLightMeasurement((message) => { // `recieveLightMeasurement` is the respective operationId + client.registerReceiveLightMeasurementMiddleware((message) => { // `recieveLightMeasurement` is the respective operationId console.log("recieved in middleware 1", message.payload); }); - client.receiveLightMeasurement((message) => { + client.registerReceiveLightMeasurementMiddleware((message) => { console.log("recieved in middleware 2", message.payload); }); } diff --git a/filters/all.js b/filters/all.js index 91b0603d..b68290da 100644 --- a/filters/all.js +++ b/filters/all.js @@ -132,6 +132,12 @@ function trimLastChar(string) { } filter.trimLastChar = trimLastChar; +function convertOpertionIdToMiddlewareFn(operationId) { + const capitalizedOperationId = operationId.charAt(0).toUpperCase() + operationId.slice(1); + return "register" + capitalizedOperationId + "Middleware"; +} +filter.convertOpertionIdToMiddlewareFn = convertOpertionIdToMiddlewareFn; + function toJS(objFromJSON, indent = 2) { if (typeof objFromJSON !== 'object' || Array.isArray(objFromJSON)) { // not an object, stringify using native function diff --git a/template/src/api/handlers/$$channel$$.js b/template/src/api/handlers/$$channel$$.js index 61420fbb..037fc5e8 100644 --- a/template/src/api/handlers/$$channel$$.js +++ b/template/src/api/handlers/$$channel$$.js @@ -12,7 +12,7 @@ const {{ channel.publish().id() }}Middlewares = []; * @param {function} middlewareFn - The middleware function to be registered. * @throws {TypeError} If middlewareFn is not a function. */ -handler.{{ channel.publish().id() }} = (middlewareFn) => { +handler.{{ channel.publish().id() | convertOpertionIdToMiddlewareFn }} = (middlewareFn) => { if (typeof middlewareFn !== 'function') { throw new TypeError('middlewareFn must be a function'); } @@ -68,7 +68,7 @@ const {{ channel.subscribe().id() }}Middlewares = []; * @param {function} middlewareFn - The middleware function to be registered. * @throws {TypeError} If middlewareFn is not a function. */ -handler.{{ channel.subscribe().id() }} = (middlewareFn) => { +handler.{{ channel.subscribe().id() | convertOpertionIdToMiddlewareFn }} = (middlewareFn) => { if (typeof middlewareFn !== 'function') { throw new TypeError('middlewareFn must be a function'); } diff --git a/template/src/api/index.js b/template/src/api/index.js index f1a72131..5ceef743 100644 --- a/template/src/api/index.js +++ b/template/src/api/index.js @@ -69,10 +69,10 @@ function init() { const handlers = { {%- for channelName, channel in asyncapi.channels() -%} {% if channel.hasPublish() %} - {{ channel.publish().id() }}: require('./handlers/{{ channelName | convertToFilename }}').{{ channel.publish().id() }}, + {{ channel.publish().id() | convertOpertionIdToMiddlewareFn }}: require('./handlers/{{ channelName | convertToFilename }}').{{ channel.publish().id() | convertOpertionIdToMiddlewareFn }}, {%- endif -%} {% if channel.hasSubscribe() %} - {{ channel.subscribe().id() }}: require('./handlers/{{ channelName | convertToFilename }}').{{ channel.subscribe().id() }}, + {{ channel.subscribe().id() | convertOpertionIdToMiddlewareFn }}: require('./handlers/{{ channelName | convertToFilename }}').{{ channel.subscribe().id() | convertOpertionIdToMiddlewareFn }}, {% endif %} {%- endfor -%} }; diff --git a/test/__snapshots__/integration.test.js.snap b/test/__snapshots__/integration.test.js.snap index 3b09679c..b38455b0 100644 --- a/test/__snapshots__/integration.test.js.snap +++ b/test/__snapshots__/integration.test.js.snap @@ -15,7 +15,7 @@ const dimLightMiddlewares = []; * @param {function} middlewareFn - The middleware function to be registered. * @throws {TypeError} If middlewareFn is not a function. */ -handler.dimLight = (middlewareFn) => { +handler.registerDimLightMiddleware = (middlewareFn) => { if (typeof middlewareFn !== 'function') { throw new TypeError('middlewareFn must be a function'); } @@ -54,7 +54,7 @@ const turnOffMiddlewares = []; * @param {function} middlewareFn - The middleware function to be registered. * @throws {TypeError} If middlewareFn is not a function. */ -handler.turnOff = (middlewareFn) => { +handler.registerTurnOffMiddleware = (middlewareFn) => { if (typeof middlewareFn !== 'function') { throw new TypeError('middlewareFn must be a function'); } @@ -93,7 +93,7 @@ const turnOnMiddlewares = []; * @param {function} middlewareFn - The middleware function to be registered. * @throws {TypeError} If middlewareFn is not a function. */ -handler.turnOn = (middlewareFn) => { +handler.registerTurnOnMiddleware = (middlewareFn) => { if (typeof middlewareFn !== 'function') { throw new TypeError('middlewareFn must be a function'); } @@ -132,7 +132,7 @@ const receiveLightMeasurementMiddlewares = []; * @param {function} middlewareFn - The middleware function to be registered. * @throws {TypeError} If middlewareFn is not a function. */ -handler.receiveLightMeasurement = (middlewareFn) => { +handler.registerReceiveLightMeasurementMiddleware = (middlewareFn) => { if (typeof middlewareFn !== 'function') { throw new TypeError('middlewareFn must be a function'); } @@ -303,12 +303,12 @@ function init() { } const handlers = { - receiveLightMeasurement: require('./handlers/smartylighting-streetlights-1-0-event-{streetlightId}-lighting-measured').receiveLightMeasurement, - turnOn: require('./handlers/smartylighting-streetlights-1-0-action-{streetlightId}-turn-on').turnOn, + registerReceiveLightMeasurementMiddleware: require('./handlers/smartylighting-streetlights-1-0-event-{streetlightId}-lighting-measured').registerReceiveLightMeasurementMiddleware, + registerTurnOnMiddleware: require('./handlers/smartylighting-streetlights-1-0-action-{streetlightId}-turn-on').registerTurnOnMiddleware, - turnOff: require('./handlers/smartylighting-streetlights-1-0-action-{streetlightId}-turn-off').turnOff, + registerTurnOffMiddleware: require('./handlers/smartylighting-streetlights-1-0-action-{streetlightId}-turn-off').registerTurnOffMiddleware, - dimLight: require('./handlers/smartylighting-streetlights-1-0-action-{streetlightId}-dim').dimLight, + registerDimLightMiddleware: require('./handlers/smartylighting-streetlights-1-0-action-{streetlightId}-dim').registerDimLightMiddleware, }; const client = { @@ -386,7 +386,7 @@ const dimLightMiddlewares = []; * @param {function} middlewareFn - The middleware function to be registered. * @throws {TypeError} If middlewareFn is not a function. */ -handler.dimLight = (middlewareFn) => { +handler.registerDimLightMiddleware = (middlewareFn) => { if (typeof middlewareFn !== 'function') { throw new TypeError('middlewareFn must be a function'); } @@ -425,7 +425,7 @@ const turnOffMiddlewares = []; * @param {function} middlewareFn - The middleware function to be registered. * @throws {TypeError} If middlewareFn is not a function. */ -handler.turnOff = (middlewareFn) => { +handler.registerTurnOffMiddleware = (middlewareFn) => { if (typeof middlewareFn !== 'function') { throw new TypeError('middlewareFn must be a function'); } @@ -464,7 +464,7 @@ const turnOnMiddlewares = []; * @param {function} middlewareFn - The middleware function to be registered. * @throws {TypeError} If middlewareFn is not a function. */ -handler.turnOn = (middlewareFn) => { +handler.registerTurnOnMiddleware = (middlewareFn) => { if (typeof middlewareFn !== 'function') { throw new TypeError('middlewareFn must be a function'); } @@ -503,7 +503,7 @@ const receiveLightMeasurementMiddlewares = []; * @param {function} middlewareFn - The middleware function to be registered. * @throws {TypeError} If middlewareFn is not a function. */ -handler.receiveLightMeasurement = (middlewareFn) => { +handler.registerReceiveLightMeasurementMiddleware = (middlewareFn) => { if (typeof middlewareFn !== 'function') { throw new TypeError('middlewareFn must be a function'); } @@ -674,12 +674,12 @@ function init() { } const handlers = { - receiveLightMeasurement: require('./handlers/smartylighting-streetlights-1-0-event-{streetlightId}-lighting-measured').receiveLightMeasurement, - turnOn: require('./handlers/smartylighting-streetlights-1-0-action-{streetlightId}-turn-on').turnOn, + registerReceiveLightMeasurementMiddleware: require('./handlers/smartylighting-streetlights-1-0-event-{streetlightId}-lighting-measured').registerReceiveLightMeasurementMiddleware, + registerTurnOnMiddleware: require('./handlers/smartylighting-streetlights-1-0-action-{streetlightId}-turn-on').registerTurnOnMiddleware, - turnOff: require('./handlers/smartylighting-streetlights-1-0-action-{streetlightId}-turn-off').turnOff, + registerTurnOffMiddleware: require('./handlers/smartylighting-streetlights-1-0-action-{streetlightId}-turn-off').registerTurnOffMiddleware, - dimLight: require('./handlers/smartylighting-streetlights-1-0-action-{streetlightId}-dim').dimLight, + registerDimLightMiddleware: require('./handlers/smartylighting-streetlights-1-0-action-{streetlightId}-dim').registerDimLightMiddleware, }; const client = {