-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat!: enable usage of generated app as a library without its code modification #220
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Welcome to AsyncAPI. Thanks a lot for creating your first pull request. Please check out our contributors guide useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should also modify template/src/api/index.js
file and at the end of the file add exports.app = app;
additionally please extend the readme, by adding a new section, with h3 under the CLI
section called Adding custom code
and:
- write explanation that it is recommended to not modify the code of generated application but rather use it as an API to start a server and register additional custom code
- provide this example code
// You require the generated server. Running this code starts the server
// App exposes API to send messages
const { app } = require('./output/src/api/index');
// Requiring generated handler that you should use if you want to react on consumed messages
const receiveLightMeasurementHandler = require('./output/src/api/handlers/smartylighting-streetlights-1-0-event-{streetlightId}-lighting-measured');
// Requiring generated handler that you should use if you want to react on produced messages
const sendDimLightHandler = require('./output/src/api/handlers/smartylighting-streetlights-1-0-action-{streetlightId}-dim');
/**
*
*
* Example of how to work with generated code as a consumer
*
*
*/
// Writing your custom logic that should be triggered when your app receives as message from a given channel
function triggeredByLightMeasurementHandler(message) {
console.log('reading message in triggeredByLightMeasurementHandler', message.payload);
console.log('sending another message in triggeredByLightMeasurementHandler to another channel');
app.send({percentage: 1}, {}, 'smartylighting/streetlights/1/0/action/1/dim');
}
// Registering your custom logic in a channel-specific handler
// triggeredByLightMeasurementHandler function is called once the app gets message sent to the channel like:
// "mqtt pub -t 'smartylighting/streetlights/1/0/event/123/lighting/measured' -h 'test.mosquitto.org' -m '{"id": 1, "lumens": 3, "sentAt": "2017-06-07T12:34:32.000Z"}'"
receiveLightMeasurementHandler.registerMiddleware(triggeredByLightMeasurementHandler);
/**
*
*
* Example of how to process a message before it is sent to the broker
*
*
*/
// Writing your custom logic that should be triggered when your app receives as message from a given channel
function triggeredByDimLight(message) {
console.log('reading message in triggeredByDimLight before it is sent to the broker', message.payload.percentage);
}
// Registering your custom logic in a channel-specific handler
// triggeredByDimLight function is called once the app sends a message to the channel
// For example "triggeredByLightMeasurementHandler" sends a message to some channel using "app.send" and before it is sent, you want to perform some other actions
sendDimLightHandler.registerMiddleware(triggeredByDimLight);
/**
*
*
* Example of how to produce a message using API of generated app independently from the handlers
*
*
*/
(function myLoop (i) {
setTimeout(() => {
console.log('producing custom message');
app.send({percentage: 1}, {}, 'smartylighting/streetlights/1/0/action/1/turn/on');
if (--i) myLoop(i);
}, 1000);
}(3));
- explain that anyone can run this code and test by sending a custom message
mqtt pub -t 'smartylighting/streetlights/1/0/event/123/lighting/measured' -h 'test.mosquitto.org' -m '{"id": 1, "lumens": 3, "sentAt": "2017-06-07T12:34:32.000Z"}'
last but not least change the PR title to feat!:
as this is going to be a breaking change and we need to release it as new major
also call it like feat!: make it possible to use generated app without its code modification
-> title should say not want you did but what it changed
you could also extend the description of the PR with the example code sample to showcase what is the change
@derberg can you please review 🙂 ? |
@derberg 🤔 Thinking from a user point of view, I'd rather be happy if I can access all the handlers by just importing the library, The ideal end usage should look something like this const { client } = require("./server-library")
client.init();
client.subscribeToSmartylightingStreetlights10EventLightingMeasuredHandler.registerMiddleware((messagge) => {
console.log("hit the subscribe payload", messagge.payload);
}) the code should ideally look more shorter, if not for the long name smartylighting...blablabla 😄 |
yeah, in both cases, path or function name, the name is crap 😄 but afaik it is because we do not have explicitly a channel
yes, eventually this is what we should target:
thing is that these
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lemme know if you agree with suggested plan, and I merge
Sure, will create the issue. If there's a route which has both the publish and subscribe operations on a single channel/route, the Example AsyncAPI File -> https://github.com/kaushik-rishi/templates-collection-nodejs-templates/blob/master/mqtt-pub-sub-single-route-new-template/asyncapi.yaml Example generated code -> https://github.com/kaushik-rishi/templates-collection-nodejs-templates/blob/master/mqtt-pub-sub-single-route-new-template/src/api/handlers/smartylighting-streetlights-1-0-action-%7BstreetlightId%7D-turn-on.js |
you also have to update snapshots for tests: and yeah, good catch about pub/sub |
Sure :), the arrays and the functions should be different as well. |
I’ve updated the docs with the new way to use the library/template. Currently i feel using operationId for the function names is fine, in the path forward i’d like to move the operationId into using the channel’s name in pascal / camel case or whatever, but i’d like to do it after we start rewriting the templates to new react renderer template. Path forward
|
@derberg ready to merge 🙂 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well done 👏🏼
Kudos, SonarCloud Quality Gate passed! |
@derberg oops! fixed it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WELL DONE 👏🏼
/rtm |
🎉 This PR is included in version 1.0.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
@allcontributors please add @kaushik-rishi for code,docs,test |
I've put up a pull request to add @kaushik-rishi! 🎉 |
Related issue(s)
#219