DEV Community

Cover image for Create elegant OpenAPI spec documentation with Rapi Doc and Vitepress
Ola B
Ola B

Posted on

Create elegant OpenAPI spec documentation with Rapi Doc and Vitepress

I recently had to create a documentation page supporting OpenAPI spec documentation. What's an OpenAPI spec documentation? A page, either self-hosted or included in your API management platform, that allows users to check what endpoints, methods, webhooks, etc., are available based on OpenAPI JSON or YAML.

I needed to find a balance between needing as many customization options as possible and using ready-to-go tools for quick setup and deployment.

And I found Rapi Doc - a web component that can be embedded anywhere.

Image description

With the component ready, I needed a tool to write documentation that supported custom components.

So I chose Vitepress. And I had two tools that I wanted to merge. How did it go? Let's find out.

Running app in dev mode

I'll skip the story of Vitepress setup - you can find the instructions on their main page.

I also created a custom RapiDoc.vue component where I embedded my rapi-doc web component.

<script setup>
import 'rapidoc'
</script>

<template>
<div>
  <rapi-doc
      spec-url = "https://petstore.swagger.io/v2/swagger.json"
      render-style = "read"
      style = "height:100vh; width:100%"
  > </rapi-doc>
</div>
</template>

<style scoped>

</style>

Enter fullscreen mode Exit fullscreen mode

I also embedded this custom component in a api-docs.md page (yes, you can embed Vue Components in Markdown, I love Vitepress for it!) so I could see it in my Vitepress documentation.

---
sidebar: false
layout: page
---

<script setup>
import RapiDoc from './components/RapiDoc.vue';
</script>

<RapiDoc />

Enter fullscreen mode Exit fullscreen mode

I ran yarn docs:dev expecting everything to go smoothly (I followed the instructions from both documentations, so it should be fine, right?)...

And I got this:

Start infinite loop

And my browser froze.

Woohoo, long live the infinite loop!

What happened? So, since rapi-doc is a web component, I need to explicitly tell Vue compiler to not parse it. To just leave it alone.

And inside my config.mts file I needed to add:

import { defineConfig } from 'vitepress'

// https://vitepress.dev/reference/site-config
export default defineConfig({
  ...
  vue: {
    template: {
      compilerOptions: {
        isCustomElement: (tag: string) => {
          return tag.indexOf('rapi-doc') >= 0;
        }
      }
    }
  },
})

Enter fullscreen mode Exit fullscreen mode

We just need to check for custom elements and inform Vue "hey, this tag is off-limits".

So, we have it, it runs!

App in dev mode

And then I tried to build it so I could set up deployment.

Building the app

I ran yarn docs:build command. And I immediately (wow, Vite, you're quick!) got this error:

Vite build error

This error means that during build-time, Vite couldn't access a self property. This can also happen if you try to access browser API (e.g., window) from server (in Nuxt or any other SSR framework, for example).

So what we can do? We can import it dynamically in runtime!

Let's change our import from this:

<script setup>
import 'rapidoc';
</script>
Enter fullscreen mode Exit fullscreen mode

To this:

<script setup>
import { onMounted } from 'vue';

onMounted(() => {
  import('rapidoc');
});
</script>
Enter fullscreen mode Exit fullscreen mode

And now build should pass with no issues! Enjoy you API spec docs!

Bonus: Dark Mode

Vitepress comes with dark mode, working out-of-the-box. But how can we make our RapiDoc documentation reacting to mode changes?

We can use Vitepress core composable - useData. It contains isDark property with information if the darkmode is enabled or not.

So let's use it inside the script section in the SFC:

<script setup>
import { useData } from 'vitepress';
import { ref } from 'vue';

const data = useData();
const theme = ref(data.isDark.value ? 'dark' : 'light')
</script>
Enter fullscreen mode Exit fullscreen mode

Now when we have a theme ref, we can pass it to the rapi-doc Web Component via attribute binding:

<template>
<div>
  <rapi-doc
      spec-url = "https://petstore.swagger.io/v2/swagger.json"
      render-style = "read"
      :theme="theme"
      style = "height:100vh; width:100%"
  > </rapi-doc>
</div>
</template>
Enter fullscreen mode Exit fullscreen mode

We need to add one more thing for dark mode to work correctly - responding to theme change.

Let's add a watcher to our script section:

watch(data.isDark, (isDark) => {
  theme.value = isDark ? 'dark' : 'light';
})
Enter fullscreen mode Exit fullscreen mode

And voila, you created API docs that react to theme changes!

Image description

Top comments (1)

Collapse
 
enzonotario profile image
Enzo Notario

Nice tutorial! I'm working on github.com/enzonotario/vitepress-o... for a better OpenAPI integration with VitePress. Any feedback is welcome!