Skip to content

Commit

Permalink
Merge branch 'master' into integration-testing
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushik-rishi committed Oct 2, 2023
2 parents 159e04e + a0c8f75 commit 0dea140
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 24 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<operationId>` 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.register<operationId>Middleware` 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)
Expand Down Expand Up @@ -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}`,
Expand Down Expand Up @@ -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);
});
}
Expand Down
6 changes: 6 additions & 0 deletions filters/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions template/src/api/handlers/$$channel$$.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down Expand Up @@ -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');
}
Expand Down
4 changes: 2 additions & 2 deletions template/src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%}
};
Expand Down
32 changes: 16 additions & 16 deletions test/__snapshots__/integration.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit 0dea140

Please sign in to comment.