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

hapi-swagger-static

v2.0.20

Published

A small companion plugin for `hapi-swagger` or `hapi-swaggered` providing a static html documentation page as hapi route

Downloads

1,579

Readme

hapi-swagger-static

Plugin for Hapi providing a static html documentation page. It's a small companion plugin for hapi-swagger or hapi-swaggered to create a static page from the /swagger.json endpoint. The static page is provided as route /documentation.html (can be renamed). It supports most of Swagger 2.0 / Open API 2.0.

main workflow Coverage Status dependencies Status Maintainability node code style License Status

Tested with

  • Node 14/16, Hapi 19, Hapi-Swagger 12, Vision 6, Inert 6
  • Node 10, Hapi 18, Hapi-Swagger 11, Vision 5, Inert 5

Install

npm install hapi-swagger-static

Usage

Register the plugin with Hapi server like this:

const Hapi = require('@hapi/hapi');
const HapiSwagger = require('hapi-swagger');
const HapiSwaggerStatic = require('hapi-swagger-static');
const Inert = require('@hapi/inert');
const Vision = require('@hapi/vision');

const server = new Hapi.Server({
  port: 3000,
});

const provision = async () => {
  await server.register({ plugin: Inert });
  await server.register({ plugin: Vision });
  // first, add your api routes to hapi
  await server.register({ // second, register `hapi-swagger` plugin
    plugin: HapiSwagger,
    options: { ... },
  });
  await server.register({ // last, register this plugin
    plugin: HapiSwaggerStatic,
    options: { ... },
  });
  await server.start();
};

provision();

Options

The plugin provides the following options:

| Option | Default | Description | |-------------|-------------|-------------| | path/documentation.html | The endpoint providing the static documentation page. | | swaggerEndpoint | /swagger.json | The endpoint to read the Swagger API specification from. | | cache | { privacy: 'public', expiresIn: 60 * 60 * 1000 } // one hour | Hapi's route.options.cache to be assigned to the static documentation endpoint. Please refer to the Hapi docs for more information. | | auth | - | Hapi's route.options.auth to be assigned to the static documentation endpoint. Please refer to the Hapi docs for more information. By default, this option is not set, i.e., inheriting auth settings from Hapi's server.options.routes.auth. | | headers | {} | The request's authorization header is automatically forwarded to the /swagger.json endpoint. If you need any additional headers, add them through the headers option. | | template | - | This plugin prefers vision to render the api into a page. template is the template filename and path, relative to the templates path configured via the server views manager. The api data is provided as view context. Assuming Handlebars or Mustache as your template engine, e.g., you can use {{api.info.title}} in the template to get hold of the api title. Use {{{api.html}}} (with three curly brackets here!) in the template for rendering the api's html content. | | viewOptions | {} | The options passed to the view via h.view(). If your default layout does not provide the Bootstrap 4 CSS resources (Bootstrap's JS is not needed), you should provide a special layout for your template. See the example below. | | o2hOptions | {} | The options passed to openapi2html. Please refer to openapi2html for more information. |

View Example

This example assumes Handlebars or Mustache as template engine. It sets the following plugin options:

{ template: 'api', // referring to the view file `api.html` below
  viewOptions: {
    layout: 'api-layout' // referring to the layout file `api-layout.html` below
  }
}

View example, e.g., api.html to be placed into your view location:

{{{api.html}}}

Layout example, e.g., api-layout.html to be placed into your layout location:

<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet">
    <link href='/public/api.css' rel='stylesheet' type='text/css' />
    <title>{{api.info.title}}</title>
  </head>
  <body>
    <div class="container">
      {{{content}}}
      <footer>Your company; for internal use only</footer>
    </div>
  </body>
</html>

Style example, e.g., api.css to be placed into /public, containing some minor adjustments to Bootstrap 4:

.h2, h2 {
  margin-top: 1rem;
}
.h4, h4 {
  margin-top: .5rem;
}
.card {
  margin-bottom: 1rem;
}
.o2h-description p {
  color: grey;
  margin-bottom: .5rem;
}
.card .card-body .h4, .card .card-body h4 {
  border-top: 1px solid #eee;
  margin-top: 1rem;
  padding-top: 1rem;
}
.card .card-body .h5, .card .card-body h5 {
  margin-top: 1rem;
}
.card .card-body .o2h-description p {
  margin-bottom: 0;
}
.card .card-body .o2h-example pre {
  background-color: #eee;
  font-size: small;
}
.o2h-parameter h5 .badge {
  font-size: small;
}