Logo

dev-resources.site

for different kinds of informations.

Event driven API documentation made simple (Client-Side Rendering).

Published at
7/30/2022
Categories
eventdriven
asyncapi
documentation
guide
Author
m1ner
Author
5 person written this
m1ner
open
Event driven API documentation made simple (Client-Side Rendering).

📢 Original article is available on my website where you can also find my Internship Journal.


This post is a bit different from the other articles.

In my previous posts, I have written for absolute beginners, but this one is for developers looking to automate and formalize documentation generation for event-driven projects.

This guide is directed toward folks looking for instructions on how to generate documentation using their AsyncAPI files. Event-driven APIs are not the same as the synchronous APIs you usually document with OpenAPI or GraphQL. Many people use AsyncAPI now, and it is time to provide the community with a guide that shows what options the community has to render documentation on the client-side.

Oh, if you are still not sure about Client-Side Rendering or Server-Side Rendering, then you can read all about it here 👉 Website rendering for beginners (shameless plug 😉).

I will cover the usage for:

All examples will use this same AsyncAPI sample file 👇

{
  "asyncapi": "2.2.0",
  "info": {
    "title": "Account Service",
    "version": "1.0.0",
    "description": "This service is in charge of processing user signups"
  },
  "channels": {
    "user/signedup": {
      "subscribe": {
        "message": {
          "$ref": "#/components/messages/UserSignedUp"
        }
      }
    }
  },
  "components": {
    "messages": {
      "UserSignedUp": {
        "payload": {
          "type": "object",
          "properties": {
            "displayName": {
              "type": "string",
              "description": "Name of the user"
            },
            "email": {
              "type": "string",
              "format": "email",
              "description": "Email of the user"
            }
          }
        }
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

This is the expected look of the generated document 👇

document-template

All usage examples from this article are available to check on asyncapi-docs-rendering-examples repository.


React

If you wish to render documentation from your AsyncAPI file in React application, then you need to use AsyncAPI React component.

1️⃣ To install the React AsyncAPI component run the command npm install --save @asyncapi/react-component@next.

2️⃣ Now, create index.js file and type in:

import React from "react";
import ReactDOM from "react-dom";

import AsyncApiComponent from "@asyncapi/react-component";
import "@asyncapi/react-component/styles/default.min.css";

import { specMock } from "./testDoc";

const rootElement = document.getElementById("root");
ReactDOM.render(<AsyncApiComponent schema={specMock} />, rootElement);
Enter fullscreen mode Exit fullscreen mode

React AsyncAPI component is imported on the line 4.

If you are happy with AsyncAPI styling then you need to import their CSS pattern with import "@asyncapi/react-component/styles/default.min.css";

On the line 7 is where AsyncAPI sample file is imported.


Vue

If you wish to generate documentation from your AsyncAPI file in Vue application, then you need to use AsyncApiStandalone bundle.

1️⃣ React AsyncAPI component is required here as well, so again you need to run npm install --save @asyncapi/react-component@next command.

2️⃣ In your App.vue just add this code:

<template>
  <div ref="asyncapi"></div>
</template>

<script>
import AsyncApiStandalone from '@asyncapi/react-component/browser/standalone';

// AsyncAPI specification, fetched or pasted.
const schema =
'{"asyncapi":"2.4.0","info":{"title":"Account Service","version":"1.0.0","description":"This service is in charge of processing user signups"},"channels":{"user/signedup":{"subscribe":{"message":{"$ref":"#/components/messages/UserSignedUp"}}}},"components":{"messages":{"UserSignedUp":{"payload":{"type":"object","properties":{"displayName":{"type":"string","description":"Name of the user"},"email":{"type":"string","format":"email","description":"Email of the user"}}}}}}}';

const config = {}; // Configuration for component. This same as for normal React component.

export default {
  name: 'AsyncApiComponent',
  props: {
    msg: String
  },
  mounted() {
    const container = this.$refs.asyncapi;
    AsyncApiStandalone.render({ schema, config }, container);
  }
}
</script>

<style scope src="@/assets/asyncapi.min.css"></style>
Enter fullscreen mode Exit fullscreen mode

As you can see on line 6, you need to import AsyncApiStandalone bundle with import AsyncApiStandalone from '@asyncapi/react-component/browser/standalone'; command.

3️⃣ There is one more thing to do. If you like AsyncAPI styling then you need to go to 👉 node_modules/@asyncapi/react-component/style/default.min.css.

Copy that file and then paste it into your assets folder.
I renamed mine asyncapi.min.css.

You can import this in your main.js file with import './assets/asyncapi.min.css' or, as I did, add it at the end of App.vue file with <style scope src="@/assets/asyncapi.min.css"></style>.


Web Components

To generate documentation from your AsyncAPI file, you can use it as an element of an HTML webpage or as a web component in any other web framework you choose. You can do this by implementing web-react-components.

1️⃣ Just create a .html file then copy and paste this code 👇

<script src="https://unpkg.com/@asyncapi/[email protected]/lib/asyncapi-web-component.js" defer></script>

<asyncapi-component
  schema='{"asyncapi":"2.4.0","info":{"title":"Account Service","version":"1.0.0","description":"This service is in charge of processing user signups"},"channels":{"user/signedup":{"subscribe":{"message":{"$ref":"#/components/messages/UserSignedUp"}}}},"components":{"messages":{"UserSignedUp":{"payload":{"type":"object","properties":{"displayName":{"type":"string","description":"Name of the user"},"email":{"type":"string","format":"email","description":"Email of the user"}}}}}}}'

  config='{"show": {"sidebar": false}}'

  cssImportPath="https://unpkg.com/@asyncapi/[email protected]/styles/default.min.css">
</asyncapi-component>
Enter fullscreen mode Exit fullscreen mode

2️⃣ If you need support for old browsers then you need to add this script as well <script src="https://unpkg.com/@webcomponents/[email protected]/webcomponents-bundle.js"></script>.

That is it! 🤯 Just awesome!


Standalone Bundle

If you want to render documentation from your AsyncAPI file without the use of any framework but with just an HTML webpage then you will need the Standalone bundle.

1️⃣ All you need is just a basic HTML template.

2️⃣ In the head element enter <link rel="stylesheet" href="https://unpkg.com/@asyncapi/[email protected]/styles/default.min.css"> to get AsyncAPI document styling.

3️⃣ In the body element type in:

 <div id="asyncapi"></div>

    <script src="https://unpkg.com/@asyncapi/[email protected]/browser/standalone/index.js"></script>
    <script>
        AsyncApiStandalone.render({
            schema: '{"asyncapi":"2.4.0","info":{"title":"Account Service","version":"1.0.0","description":"This service is in charge of processing user signups"},"channels":{"user/signedup":{"subscribe":{"message":{"$ref":"#/components/messages/UserSignedUp"}}}},"components":{"messages":{"UserSignedUp":{"payload":{"type":"object","properties":{"displayName":{"type":"string","description":"Name of the user"},"email":{"type":"string","format":"email","description":"Email of the user"}}}}}}}'
            ,
            config: {
                show: {
                    sidebar: false,
                }
            },
        }, document.getElementById('asyncapi'));
    </script>
Enter fullscreen mode Exit fullscreen mode

<script src="https://unpkg.com/@asyncapi/[email protected]/browser/standalone/index.js"></script> fetches everything required from the bundle.

I almost forgot.

There is one more way to configure the AsyncAPI component.
You can do it through config props, this same as for normal React component.

My Web Component and Standalone Bundle usage examples have config='{"show": {"sidebar": false}} which turns a sidebar off, but if you change it to true then your rendered document will have a sidebar.

This was only a sample of what could be done. AsyncAPI can do a lot more. Check AsyncAPI documentation for more information on how to generate docs.

I hope that you liked this quick guide through AsyncAPI Client-Side documents generation.

As usual, if you find any mistakes, don't go mad in the comments section. Just let me know and together we can fix it so this article can be even better.

asyncapi Article's
30 articles in total
Favicon
List of AsyncAPI servers in MuleSoft
Favicon
AsyncAPI — A standard specification for documenting Event-Driven Applications
Favicon
AsyncAPI: a practical look
Favicon
Arquitetura Event-Driven usando AsyncAPI na prática
Favicon
AsyncAPI Codegen, a code generator from AsyncAPI spec v2 and v3.
Favicon
Integration Digest: October 2023
Favicon
An AsyncAPI Example: Building Your First Event-driven API
Favicon
How to document SSE app
Favicon
Perfectly sizing images in your API documentation
Favicon
Using OpenAPI and AsyncAPI Tags to Better Organize API Endpoints
Favicon
API Contracts - an Extended Introduction
Favicon
Why AvioBook switched from Swagger UI to Bump.sh for all of their APIs
Favicon
How to use and document polymorphism in API
Favicon
What are the different API types?
Favicon
Event driven API documentation made simple (Client-Side Rendering).
Favicon
Could AsyncApi Make A Dent on Climate Change?
Favicon
API Diff - Compare in seconds two versions of your API
Favicon
An introduction to the AsyncAPI specification
Favicon
Getting Started with CloudEvents and AsyncAPI
Favicon
Bump diff, the missing piece for an API “design-first” approach
Favicon
Mule support for Async API
Favicon
API documentation in event driven applications
Favicon
AsyncAPI Code Generation: Microservices Using Spring Cloud Stream
Favicon
(Part 2) Full automation of release with GitHub Actions and Conventional Commits for non-JS projects
Favicon
Nunjucks templating explained on the basis of AsyncAPI specification
Favicon
AsyncAPI for documentation and validation of event-driven architecture
Favicon
A Modeling Editor and Code Generator for AsyncAPI
Favicon
Designing, Documenting and Testing Event APIs for IoT Platforms
Favicon
Status update (week 11, 2019)
Favicon
Why Developers Need an Event Portal; Creating Applications that Disseminate Real-Time COVID-19 Data

Featured ones: