npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@hono/swagger-ui

v0.5.0

Published

A middleware for using SwaggerUI in Hono

Downloads

203,406

Readme

Swagger UI Middleware and Component for Hono

This library, @hono/swagger-ui, provides a middleware and a component for integrating Swagger UI with Hono applications. Swagger UI is an interactive documentation interface for APIs compliant with the OpenAPI Specification, making it easier to understand and test API endpoints.

Installation

npm install @hono/swagger-ui
# or
yarn add @hono/swagger-ui

Usage

Middleware Usage

You can use the swaggerUI middleware to serve Swagger UI on a specific route in your Hono application. Here's how you can do it:

import { Hono } from 'hono'
import { swaggerUI } from '@hono/swagger-ui'

const app = new Hono()

// Use the middleware to serve Swagger UI at /ui
app.get('/ui', swaggerUI({ url: '/doc' }))

export default app

Component Usage

If you are using hono/html, you can use the SwaggerUI component to render Swagger UI within your custom HTML structure. Here's an example:

import { Hono } from 'hono'
import { html } from 'hono/html'
import { SwaggerUI } from '@hono/swagger-ui'

const app = new Hono()

app.get('/ui', (c) => {
  return c.html(`
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <meta name="description" content="Custom Swagger" />
        <title>Custom Swagger</title>
        <script>
          // custom script
        </script>
        <style>
          /* custom style */
        </style>
      </head>
      ${SwaggerUI({ url: '/doc' })}
    </html>
  `)
})
export default app

In this example, the SwaggerUI component is used to render Swagger UI within a custom HTML structure, allowing for additional customization such as adding custom scripts and styles.

With OpenAPIHono Usage

Hono's middleware has OpenAPI integration @hono/zod-openapi, so you can use it to create an OpenAPI document and serve it easily with Swagger UI.

import { OpenAPIHono, createRoute, z } from '@hono/zod-openapi'
import { swaggerUI } from '@hono/swagger-ui'

const app = new OpenAPIHono()

app.openapi(
  createRoute({
    method: 'get',
    path: '/hello',
    responses: {
      200: {
        description: 'Respond a message',
        content: {
          'application/json': {
            schema: z.object({
              message: z.string()
            })
          }
        }
      }
    }
  }),
  (c) => {
    return c.json({
      message: 'hello'
    })
  }
)

app.get(
  '/ui',
  swaggerUI({
    url: '/doc'
  })
)

app.doc('/doc', {
  info: {
    title: 'An API',
    version: 'v1'
  },
  openapi: '3.1.0'
})

export default app

Options

Both the middleware and the component accept an options object for customization.

The following options are available:

  • version (string, optional): The version of Swagger UI to use, defaults to latest.
  • manuallySwaggerUIHtml (string, optional): If you want to use your own custom HTML, you can specify it here. If this option is specified, the all options except version will be ignored.

and most of options from Swagger UI are supported as well.

such as:

  • url (string, optional): The URL pointing to the OpenAPI definition (v2 or v3) that describes the API.
  • urls (array, optional): An array of OpenAPI definitions (v2 or v3) that describe the APIs. Each definition must have a name and url.
  • presets (array, optional): An array of presets to use for Swagger UI.
  • plugins (array, optional): An array of plugins to use for Swagger UI.

Authors

License

MIT