From 7ef00c4f70344c75acfc78d5302af87d120a4172 Mon Sep 17 00:00:00 2001 From: "jonas-lt@live.dk" Date: Thu, 22 Jun 2023 16:19:35 +0200 Subject: [PATCH] update examples --- examples/social-media/backend/asyncapi.yaml | 100 ++++++++++++------ .../comments-service/asyncapi.yaml | 65 +++++++----- examples/social-media/common/servers.yaml | 3 +- examples/social-media/frontend/asyncapi.yaml | 34 ++++-- .../notification-service/asyncapi.yaml | 39 ++++--- .../social-media/public-api/asyncapi.yaml | 43 ++++---- scripts/converter/index.js | 28 ++++- 7 files changed, 205 insertions(+), 107 deletions(-) diff --git a/examples/social-media/backend/asyncapi.yaml b/examples/social-media/backend/asyncapi.yaml index 5f7cb94e5..892069483 100644 --- a/examples/social-media/backend/asyncapi.yaml +++ b/examples/social-media/backend/asyncapi.yaml @@ -1,52 +1,84 @@ -asyncapi: 2.2.0 - +asyncapi: 3.0.0 info: title: Website Backend version: 1.0.0 - servers: websiteWebSocketServer: - $ref: '../common/servers.yaml#/websiteWebSocketServer' + $ref: ../common/servers.yaml#/websiteWebSocketServer mosquitto: - url: mqtt://test.mosquitto.org + host: test.mosquitto.org protocol: mqtt + tags: + - name: 'env:production' + description: This environment is meant for production use case + - name: 'kind:remote' + description: This server is a remote server. Not exposed by the application + - name: 'visibility:public' + description: This resource is public and available to everyone bindings: mqtt: clientId: websocketServer - tags: - - name: "env:production" - description: "This environment is meant for production use case" - - name: "kind:remote" - description: "This server is a remote server. Not exposed by the application" - - name: "visibility:public" - description: "This resource is public and available to everyone" - channels: comment/liked: + address: comment/liked + messages: + subscribe.message: + $ref: ../common/messages.yaml#/commentLiked description: Notify all the services that a comment has been liked. - servers: ['mosquitto'] - subscribe: - message: - $ref: '../common/messages.yaml#/commentLiked' + servers: + - $ref: '#/servers/mosquitto' like/comment: + address: like/comment + messages: + publish.message: + $ref: ../common/messages.yaml#/likeComment description: When a comment like is received from the frontend. - servers: ['websiteWebSocketServer'] - publish: - message: - $ref: '../common/messages.yaml#/likeComment' - comment/{commentId}/changed: - description: When an event from the broker arrives telling us to update the comment likes count on the frontend. - parameters: + servers: + - $ref: '#/servers/websiteWebSocketServer' + 'comment/{commentId}/changed': + address: 'comment/{commentId}/changed' + messages: + publish.message: + $ref: ../common/messages.yaml#/commentChanged + description: >- + When an event from the broker arrives telling us to update the comment + likes count on the frontend. + servers: + - $ref: '#/servers/mosquitto' + parameters: commentId: - schema: - $ref: '../common/schemas.yaml#/commentId' - servers: ['mosquitto'] - publish: - message: - $ref: '../common/messages.yaml#/commentChanged' + schema: + $ref: ../common/schemas.yaml#/commentId update/comment/likes: + address: update/comment/likes + messages: + subscribe.message: + $ref: ../common/messages.yaml#/updateCommentLikes description: Update comment likes count in the frontend. - servers: ['websiteWebSocketServer'] - subscribe: - message: - $ref: '../common/messages.yaml#/updateCommentLikes' \ No newline at end of file + servers: + - $ref: '#/servers/websiteWebSocketServer' +operations: + comment/liked.subscribe: + action: send + channel: + $ref: '#/channels/comment~1liked' + messages: + - $ref: ../common/messages.yaml#/commentLiked + like/comment.publish: + action: receive + channel: + $ref: '#/channels/like~1comment' + messages: + - $ref: ../common/messages.yaml#/likeComment + 'comment/{commentId}/changed.publish': + action: receive + channel: + $ref: '#/channels/comment~1{commentId}~1changed' + messages: + - $ref: ../common/messages.yaml#/commentChanged + update/comment/likes.subscribe: + action: send + channel: + $ref: '#/channels/update~1comment~1likes' + messages: + - $ref: ../common/messages.yaml#/updateCommentLikes diff --git a/examples/social-media/comments-service/asyncapi.yaml b/examples/social-media/comments-service/asyncapi.yaml index ce8f93a9e..ceb65d61c 100644 --- a/examples/social-media/comments-service/asyncapi.yaml +++ b/examples/social-media/comments-service/asyncapi.yaml @@ -1,38 +1,53 @@ -asyncapi: 2.2.0 - +asyncapi: 3.0.0 info: title: Comments Service version: 1.0.0 description: This service is in charge of processing all the events related to comments. - servers: mosquitto: - url: mqtt://test.mosquitto.org + host: test.mosquitto.org protocol: mqtt + tags: + - name: 'env:production' + description: This environment is meant for production use case + - name: 'kind:remote' + description: This server is a remote server. Not exposed by the application + - name: 'visibility:public' + description: This resource is public and available to everyone bindings: mqtt: clientId: comment-service - tags: - - name: "env:production" - description: "This environment is meant for production use case" - - name: "kind:remote" - description: "This server is a remote server. Not exposed by the application" - - name: "visibility:public" - description: "This resource is public and available to everyone" - channels: comment/liked: - description: Updates the likes count in the database and sends the new count to the broker. - publish: - message: - $ref: '../common/messages.yaml#/commentLiked' - comment/{commentId}/changed: - description: Sends the new count to the broker after it has been updated in the database. - parameters: + address: comment/liked + messages: + publish.message: + $ref: ../common/messages.yaml#/commentLiked + description: >- + Updates the likes count in the database and sends the new count to the + broker. + 'comment/{commentId}/changed': + address: 'comment/{commentId}/changed' + messages: + subscribe.message: + $ref: ../common/messages.yaml#/commentChanged + description: >- + Sends the new count to the broker after it has been updated in the + database. + parameters: commentId: - schema: - $ref: '../common/schemas.yaml#/commentId' - subscribe: - message: - $ref: '../common/messages.yaml#/commentChanged' - + schema: + $ref: ../common/schemas.yaml#/commentId +operations: + comment/liked.publish: + action: receive + channel: + $ref: '#/channels/comment~1liked' + messages: + - $ref: ../common/messages.yaml#/commentLiked + 'comment/{commentId}/changed.subscribe': + action: send + channel: + $ref: '#/channels/comment~1{commentId}~1changed' + messages: + - $ref: ../common/messages.yaml#/commentChanged diff --git a/examples/social-media/common/servers.yaml b/examples/social-media/common/servers.yaml index bb32c3802..288b0cce7 100644 --- a/examples/social-media/common/servers.yaml +++ b/examples/social-media/common/servers.yaml @@ -1,3 +1,4 @@ websiteWebSocketServer: - url: ws://mycompany.com/ws + host: mycompany.com + pathname: /ws protocol: ws \ No newline at end of file diff --git a/examples/social-media/frontend/asyncapi.yaml b/examples/social-media/frontend/asyncapi.yaml index 2837fb6ff..0257336a6 100644 --- a/examples/social-media/frontend/asyncapi.yaml +++ b/examples/social-media/frontend/asyncapi.yaml @@ -1,21 +1,33 @@ -asyncapi: 2.2.0 - +asyncapi: 3.0.0 info: title: Website WebSocket Client version: 1.0.0 - servers: websiteWebSocketServer: - $ref: '../common/servers.yaml#/websiteWebSocketServer' - + $ref: ../common/servers.yaml#/websiteWebSocketServer channels: like/comment: + address: like/comment + messages: + subscribe.message: + $ref: ../common/messages.yaml#/likeComment description: Notify the backend that a comment has been liked. - subscribe: - message: - $ref: '../common/messages.yaml#/likeComment' update/comment/likes: + address: update/comment/likes + messages: + publish.message: + $ref: ../common/messages.yaml#/updateCommentLikes description: Update the UI when the comment likes count is updated. - publish: - message: - $ref: '../common/messages.yaml#/updateCommentLikes' \ No newline at end of file +operations: + like/comment.subscribe: + action: send + channel: + $ref: '#/channels/like~1comment' + messages: + - $ref: ../common/messages.yaml#/likeComment + update/comment/likes.publish: + action: receive + channel: + $ref: '#/channels/update~1comment~1likes' + messages: + - $ref: ../common/messages.yaml#/updateCommentLikes diff --git a/examples/social-media/notification-service/asyncapi.yaml b/examples/social-media/notification-service/asyncapi.yaml index be78362c9..858f3e2eb 100644 --- a/examples/social-media/notification-service/asyncapi.yaml +++ b/examples/social-media/notification-service/asyncapi.yaml @@ -1,27 +1,34 @@ -asyncapi: 2.2.0 - +asyncapi: 3.0.0 info: title: Notifications Service version: 1.0.0 - servers: mosquitto: - url: mqtt://test.mosquitto.org + host: test.mosquitto.org protocol: mqtt + tags: + - name: 'env:production' + description: This environment is meant for production use case + - name: 'kind:remote' + description: This server is a remote server. Not exposed by the application + - name: 'visibility:public' + description: This resource is public and available to everyone bindings: mqtt: clientId: notification-service - tags: - - name: "env:production" - description: "This environment is meant for production use case" - - name: "kind:remote" - description: "This server is a remote server. Not exposed by the application" - - name: "visibility:public" - description: "This resource is public and available to everyone" - channels: comment/liked: - description: When a "comment has been liked" message is received, it sends an SMS or push notification to the author. - publish: - message: - $ref: '../common/messages.yaml#/commentLiked' \ No newline at end of file + address: comment/liked + messages: + publish.message: + $ref: ../common/messages.yaml#/commentLiked + description: >- + When a "comment has been liked" message is received, it sends an SMS or + push notification to the author. +operations: + comment/liked.publish: + action: receive + channel: + $ref: '#/channels/comment~1liked' + messages: + - $ref: ../common/messages.yaml#/commentLiked diff --git a/examples/social-media/public-api/asyncapi.yaml b/examples/social-media/public-api/asyncapi.yaml index 0a7cdde79..9c6ec1b72 100644 --- a/examples/social-media/public-api/asyncapi.yaml +++ b/examples/social-media/public-api/asyncapi.yaml @@ -1,28 +1,35 @@ -asyncapi: 2.2.0 - +asyncapi: 3.0.0 info: - title: Public API - description: Public API for others to interact with the system + title: Public API version: 1.0.0 - + description: Public API for others to interact with the system servers: mosquitto: - url: mqtt://test.mosquitto.org + host: test.mosquitto.org protocol: mqtt + tags: + - name: 'env:production' + description: This environment is meant for production use case + - name: 'kind:remote' + description: This server is a remote server. Not exposed by the application + - name: 'visibility:public' + description: This resource is public and available to everyone bindings: mqtt: clientId: public-api - tags: - - name: "env:production" - description: "This environment is meant for production use case" - - name: "kind:remote" - description: "This server is a remote server. Not exposed by the application" - - name: "visibility:public" - description: "This resource is public and available to everyone" - channels: comment/liked: - description: Others are publishing to you, whenever a comment is liked, for you to do react to such an event. - publish: - message: - $ref: '../common/messages.yaml#/commentLiked' \ No newline at end of file + address: comment/liked + messages: + publish.message: + $ref: ../common/messages.yaml#/commentLiked + description: >- + Others are publishing to you, whenever a comment is liked, for you to do + react to such an event. +operations: + comment/liked.publish: + action: receive + channel: + $ref: '#/channels/comment~1liked' + messages: + - $ref: ../common/messages.yaml#/commentLiked diff --git a/scripts/converter/index.js b/scripts/converter/index.js index fec4bc976..edd1fdf56 100644 --- a/scripts/converter/index.js +++ b/scripts/converter/index.js @@ -5,11 +5,26 @@ const jsYaml = require('js-yaml'); const examplesDirectory = path.resolve(__dirname, '../../examples'); const toVersion = '3.0.0'; +/** + * This function converts a single example file into a newer version and overwrite the old one. + * + * @param {*} exampleFile full path to file + */ function convertExample(exampleFile) { console.warn(`Converting: ${exampleFile}`); const document = fs.readFileSync(exampleFile, 'utf-8'); const loadedDocument = jsYaml.load(document); - if(loadedDocument.asyncapi === toVersion) { + if(loadedDocument.asyncapi === undefined) { + //Probably encountered a common file (used in other files), ignore + console.error(` +___________________________________________________________________________________ + !!!Manual change required!!! + + ${exampleFile} is a shared resource among other AsyncAPI documents, make sure to manually inspect this! + +___________________________________________________________________________________`); + return; + } else if(loadedDocument.asyncapi === toVersion) { console.warn(`${exampleFile} is already version ${toVersion}`); return; } @@ -17,12 +32,21 @@ function convertExample(exampleFile) { fs.writeFileSync(exampleFile, convertedDocument); } +/** + * Convert all examples within a single directory and nested directories. + * + * @param {*} directoryPath full path to a directory to convert examples from. + */ async function convertExampleDir(directoryPath) { let examplesFiles = await fs.promises.readdir(directoryPath); examplesFiles = examplesFiles.map((file) => path.resolve(directoryPath, file)); const nestedDirectory = examplesFiles.filter((file) => fs.lstatSync(file).isDirectory()); + for (const dir of nestedDirectory) { + await convertExampleDir(dir); + } + // only convert .yml files - examplesFiles = examplesFiles.filter((file) => path.extname(file) === '.yml'); + examplesFiles = examplesFiles.filter((file) => path.extname(file) === '.yml' || path.extname(file) === '.yaml'); examplesFiles.forEach(convertExample); }