vitepress-openapi
repository·main·Indexed 42 days ago
https://github.com/enzonotario/vitepress-openapiA tool for generating interactive API documentation for VitePress sites directly from OpenAPI specifications. It provides specialized components like OASpec for full specifications, OAOperation for specific API operations, and OAOperationLink for operation links. Version 0.2.5 includes composables such as useTheme for global configuration and usePlayground for managing the interactive API playground.
What's inside vitepress-openapi
- vitepress-openapi is a tool designed to automatically generate API documentation within a VitePress site using an OpenAPI specification. It allows developers to transform standard OpenAPI files into interactive, well-structured documentation pages for their VitePress-based websites.
Available components in vitepress-openapi
mainThe
vitepress-openapipackage provides several specialized components for rendering OpenAPI documentation:OASpec: Renders a complete OpenAPI specification.OAOperation: Renders a specific API operation.OAOperationLink: Renders operation links with HTTP method badges.
Understand the Playground example priority order
mainWhen multiple examples are present, the Playground resolves which value to display using the following priority hierarchy (from highest to lowest):
x-playground-exampleat the parameter levelx-playground-exampleat the schema levelexampleat the parameter level- First item in
examplesarray at the parameter level exampleat the schema level- First item in
examplesarray at the schema level
Generate custom sidebars with useSidebar
mainTheuseSidebarfunction allows you to programmatically generate custom sidebars for your VitePress site. This is useful when you want to dynamically build navigation structures based on external configurations or data rather than relying solely on static.vitepress/config.tsdefinitions.Quickstart with the vitepress-openapi starter template
mainTo jumpstart your project with a pre-configured VitePress setup and a sample OpenAPI spec, use the
degitcommand to clone the starter template.npx degit enzonotario/vitepress-openapi-starter my-api-docsUse the OAOperation component to render API operations
mainTheOAOperationcomponent is used to render a specific API operation from an OpenAPI specification. It displays all relevant details, including parameters, request bodies, responses, and an interactive playground. You can provide the specification via a direct object, a JSON string, or a URL.Use the `usePlayground` composable to manage the Playground
mainThe
usePlaygroundcomposable provides functions to manage and configure the Playground. It can be used within your.vitepress/theme/index.jsfile (typically inside theenhanceApphook) or directly within any.mdpage/file.To use it in your VitePress theme configuration, import it from
vitepress-openapi/clientand call it withinenhanceApp.import { usePlayground } from 'vitepress-openapi/client' export default { async enhanceApp({ app, router, siteData }) { const playground = usePlayground() // Use playground instance here } }Enable custom server URLs in the Playground via useOpenapi
mainIf you are configuring your OpenAPI Specification globally in your
.vitepress/theme/index.[js,ts]file using theuseOpenapicomposable, you can allow users to input their own server URLs in the Playground by settingallowCustomServer: truewithin theconfig.serverobject.useOpenapi({ spec, config: { server: { allowCustomServer: true, }, }, })Create operation links in OpenAPI or Markdown
mainOnce the
operationLinkplugin is enabled, you can create links that automatically transform into decorated operation links using the configuredlinkPrefix(default is/operations/).In OpenAPI Specifications
Use standard Markdown link syntax within the
descriptionfield of an OpenAPI operation. The plugin will intercept these links.In Markdown Files
Write links directly in your
.mdfiles using the prefix. The plugin converts them intoOAOperationLinkcomponents with the correct HTTP method badge.Example Markdown:
The [Get Artists](/operations/getAllArtists) operation retrieves all artists.Customize operation layout using Custom Slots
mainYou can override the default layout of
OAOperationandOASpeccomponents using Vue's scoped slots. This allows you to inject custom HTML or components into specific sections of the OpenAPI documentation, such as the header, parameters, or code samples.To use them, use the
#slot-namesyntax within the component. Each slot provides access to scoped props containing the parsed OpenAPI data for that specific section.<OAOperation operation-id="getUserById"> <template #header="{ operation, method, path, deprecated }"> <div class="my-header"> <span class="method">{{ method.toUpperCase() }}</span> <span class="path">{{ path }}</span> <span v-if="deprecated" class="badge">Deprecated</span> </div> </template> </OAOperation>Configure the `[tag].paths.js` loader file
mainIn your
[tag].paths.jsfile, useusePathsto iterate through the tags defined in your OpenAPI specification and return the necessary route parameters for VitePress to generate the pages.import { usePaths } from 'vitepress-openapi' import spec from '../public/openapi.json' with { type: 'json' } export default { paths() { return usePaths({ spec }) .getTags() .map(({ name }) => { return { params: { tag: name, pageTitle: `${name} - vitepress-openapi`, }, } }) }, }Hide the default branding in OASpec or OAOperation
mainTo remove the
Powered by VitePress OpenAPItext from the bottom of your components, apply thehide-brandingprop to either the<OASpec>or<OAOperation>component.<OASpec hide-branding /> <OAOperation hide-branding />