Skip to content
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

Merged
merged 17 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 13 additions & 32 deletions template/src/api/handlers/$$channel$$.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
{%- if channel.hasPublish() and channel.publish().ext('x-lambda') %}const fetch = require('node-fetch');{%- endif %}
const handler = module.exports = {};

const middlewares = [];

handler.registerMiddleware = (middlewareFn) => {
middlewares.push(middlewareFn);
}

{% if channel.hasPublish() %}
/**
* {{ channel.publish().summary() }}
* @param {object} options
* @param {object} options.message
{%- if channel.publish().message(0).headers() %}
{%- for fieldName, field in channel.publish().message(0).headers().properties() %}
{{ field | docline(fieldName, 'options.message.headers') }}
{%- endfor %}
{%- endif %}
{%- if channel.publish().message(0).payload() %}
{%- for fieldName, field in channel.publish().message(0).payload().properties() %}
{{ field | docline(fieldName, 'options.message.payload') }}
{%- endfor %}
{%- endif %}
kaushik-rishi marked this conversation as resolved.
Show resolved Hide resolved
*/
handler.{{ channel.publish().id() }} = async ({message}) => {
{%- if channel.publish().ext('x-lambda') %}
{%- set lambda = channel.publish().ext('x-lambda') %}
Expand All @@ -30,29 +22,18 @@ handler.{{ channel.publish().id() }} = async ({message}) => {
.then(json => console.log(json))
.catch(err => { throw err; });
{%- else %}
// Implement your business logic here...
for (const middleware of middlewares) {
await middleware(message);
}
{%- endif %}
};

{%- endif %}
{%- if channel.hasSubscribe() %}
/**
* {{ channel.subscribe().summary() }}
* @param {object} options
* @param {object} options.message
{%- if channel.subscribe().message(0).headers() %}
{%- for fieldName, field in channel.subscribe().message(0).headers().properties() %}
{{ field | docline(fieldName, 'options.message.headers') }}
{%- endfor %}
{%- endif %}
{%- if channel.subscribe().message(0).payload() %}
{%- for fieldName, field in channel.subscribe().message(0).payload().properties() %}
{{ field | docline(fieldName, 'options.message.payload') }}
{%- endfor %}
{%- endif %}
*/
handler.{{ channel.subscribe().id() }} = async ({message}) => {
// Implement your business logic here...
for (const middleware of middlewares) {
await middleware(message);
}
};

{%- endif %}
4 changes: 3 additions & 1 deletion template/src/lib/message-validator.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const path = require('path');
const AsyncApiValidator = require('asyncapi-validator');

// Try to parse the payload, and increment nValidated if parsing was successful.
module.exports.validateMessage = async (payload, channelName, messageName, operation, nValidated=0) => {
const va = await AsyncApiValidator.fromSource('./asyncapi.yaml', {msgIdentifier: 'name'});
const asyncApiFilePath = path.resolve(__dirname, '../../asyncapi.yaml');
const va = await AsyncApiValidator.fromSource(asyncApiFilePath, {msgIdentifier: 'name'});
va.validate(messageName, payload, channelName, operation);
nValidated++;

Expand Down
Loading