diff --git a/template/package.json b/template/package.json index 9657a9e9..362006a6 100644 --- a/template/package.json +++ b/template/package.json @@ -2,6 +2,7 @@ "name": "{{ asyncapi.info().title() | kebabCase }}", "description": "{{ asyncapi.info().description() | oneLine }}", "version": "{{ asyncapi.info().version() }}", + "main": "./src/api", "scripts": { "start": "node src/api/index.js" }, diff --git a/template/src/api/handlers/$$channel$$.js b/template/src/api/handlers/$$channel$$.js index 07007cd6..61420fbb 100644 --- a/template/src/api/handlers/$$channel$$.js +++ b/template/src/api/handlers/$$channel$$.js @@ -1,24 +1,24 @@ {%- if channel.hasPublish() and channel.publish().ext('x-lambda') %}const fetch = require('node-fetch');{%- endif %} const handler = module.exports = {}; -const middlewares = []; +{% if channel.hasPublish() %} +const {{ channel.publish().id() }}Middlewares = []; /** - * Registers a middleware function to be executed during request processing. + * Registers a middleware function for the {{ channel.publish().id() }} 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.registerMiddleware = (middlewareFn) => { +handler.{{ channel.publish().id() }} = (middlewareFn) => { if (typeof middlewareFn !== 'function') { throw new TypeError('middlewareFn must be a function'); } - middlewares.push(middlewareFn); + {{ channel.publish().id() }}Middlewares.push(middlewareFn); } -{% if channel.hasPublish() %} /** * {{ channel.publish().summary() }} * @@ -35,7 +35,7 @@ handler.registerMiddleware = (middlewareFn) => { {%- endfor %} {%- endif %} */ -handler.{{ channel.publish().id() }} = async ({message}) => { +handler._{{ channel.publish().id() }} = async ({message}) => { {%- if channel.publish().ext('x-lambda') %} {%- set lambda = channel.publish().ext('x-lambda') %} fetch('{{ lambda.url }}', { @@ -49,7 +49,7 @@ handler.{{ channel.publish().id() }} = async ({message}) => { .then(json => console.log(json)) .catch(err => { throw err; }); {%- else %} - for (const middleware of middlewares) { + for (const middleware of {{ channel.publish().id() }}Middlewares) { await middleware(message); } {%- endif %} @@ -58,6 +58,23 @@ handler.{{ channel.publish().id() }} = async ({message}) => { {%- endif %} {%- if channel.hasSubscribe() %} +const {{ channel.subscribe().id() }}Middlewares = []; + +/** + * Registers a middleware function for the {{ channel.subscribe().id() }} 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.{{ channel.subscribe().id() }} = (middlewareFn) => { + if (typeof middlewareFn !== 'function') { + throw new TypeError('middlewareFn must be a function'); + } + {{ channel.subscribe().id() }}Middlewares.push(middlewareFn); +} + /** * {{ channel.subscribe().summary() }} * @@ -74,8 +91,8 @@ handler.{{ channel.publish().id() }} = async ({message}) => { {%- endfor %} {%- endif %} */ -handler.{{ channel.subscribe().id() }} = async ({message}) => { - for (const middleware of middlewares) { +handler._{{ channel.subscribe().id() }} = async ({message}) => { + for (const middleware of {{ channel.subscribe().id() }}Middlewares) { await middleware(message); } }; diff --git a/template/src/api/index.js b/template/src/api/index.js index 531357b7..f1a72131 100644 --- a/template/src/api/index.js +++ b/template/src/api/index.js @@ -54,14 +54,35 @@ app.useOutbound(errorLogger); app.useOutbound(logger); app.useOutbound(json2string); -app - .listen() - .then((adapters) => { - console.log(cyan.underline(`${config.app.name} ${config.app.version}`), gray('is ready!'), '\n'); - adapters.forEach(adapter => { - console.log('🔗 ', adapter.name(), gray('is connected!')); - }); - }) - .catch(console.error); +function init() { + app + .listen() + .then((adapters) => { + console.log(cyan.underline(`${config.app.name} ${config.app.version}`), gray('is ready!'), '\n'); + adapters.forEach(adapter => { + console.log('🔗 ', adapter.name(), gray('is connected!')); + }); + }) + .catch(console.error); +} + +const handlers = { +{%- for channelName, channel in asyncapi.channels() -%} +{% if channel.hasPublish() %} + {{ channel.publish().id() }}: require('./handlers/{{ channelName | convertToFilename }}').{{ channel.publish().id() }}, +{%- endif -%} +{% if channel.hasSubscribe() %} + {{ channel.subscribe().id() }}: require('./handlers/{{ channelName | convertToFilename }}').{{ channel.subscribe().id() }}, +{% endif %} +{%- endfor -%} +}; + +const client = { + app, + init, + ...handlers +}; -module.exports = app; +module.exports = { + client +}; diff --git a/template/src/api/routes/$$channel$$.js b/template/src/api/routes/$$channel$$.js index ff1ed0c7..9f5b0779 100644 --- a/template/src/api/routes/$$channel$$.js +++ b/template/src/api/routes/$$channel$$.js @@ -29,14 +29,14 @@ router.use('{{ channelName | toHermesTopic }}', async (message, next) => { } catch { }; {% endfor -%} if (nValidated === 1) { - await {{ channelName | camelCase }}Handler.{{ channel.publish().id() }}({message}); + await {{ channelName | camelCase }}Handler._{{ channel.publish().id() }}({message}); next() } else { throw new Error(`${nValidated} of {{ channel.publish().messages().length }} message schemas matched when exactly 1 should match`); } {% else %} await validateMessage(message.payload,'{{ channelName }}','{{ channel.publish().message().name() }}','publish'); - await {{ channelName | camelCase }}Handler.{{ channel.publish().id() }}({message}); + await {{ channelName | camelCase }}Handler._{{ channel.publish().id() }}({message}); next(); {% endif %} } catch (e) { @@ -61,14 +61,14 @@ router.useOutbound('{{ channelName | toHermesTopic }}', async (message, next) => nValidated = await validateMessage(message.payload,'{{ channelName }}','{{ channel.subscribe().message(i).name() }}','subscribe', nValidated); {% endfor -%} if (nValidated === 1) { - await {{ channelName | camelCase }}Handler.{{ channel.subscribe().id() }}({message}); + await {{ channelName | camelCase }}Handler._{{ channel.subscribe().id() }}({message}); next() } else { throw new Error(`${nValidated} of {{ channel.subscribe().messages().length }} message schemas matched when exactly 1 should match`); } {% else %} await validateMessage(message.payload,'{{ channelName }}','{{ channel.subscribe().message().name() }}','subscribe'); - await {{ channelName | camelCase }}Handler.{{ channel.subscribe().id() }}({message}); + await {{ channelName | camelCase }}Handler._{{ channel.subscribe().id() }}({message}); next(); {% endif %} } catch (e) {