Gathering detailed insights and metrics for hapi-swagger-static
Gathering detailed insights and metrics for hapi-swagger-static
Gathering detailed insights and metrics for hapi-swagger-static
Gathering detailed insights and metrics for hapi-swagger-static
A companion plugin for ´hapi-swagger´ that provides a static html page as a hapi route
npm install hapi-swagger-static
Typescript
Module System
Min. Node Version
Node Version
NPM Version
60.7
Supply Chain
93.4
Quality
87.5
Maintenance
100
Vulnerability
98.9
License
JavaScript (88.34%)
HTML (11.66%)
Total Downloads
40,196
Last Day
38
Last Week
210
Last Month
1,536
Last Year
11,541
3 Stars
161 Commits
1 Forks
1 Watching
16 Branches
2 Contributors
Minified
Minified + Gzipped
Latest Version
3.1.0
Package Id
hapi-swagger-static@3.1.0
Unpacked Size
42.27 kB
Size
8.67 kB
File Count
12
NPM Version
10.9.2
Node Version
22.13.0
Publised On
16 Jan 2025
Cumulative downloads
Total Downloads
Last day
-51.9%
38
Compared to previous day
Last week
-69%
210
Compared to previous week
Last month
33.9%
1,536
Compared to previous month
Last year
128.8%
11,541
Compared to previous year
3
2
20
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.
Tested with
1npm install hapi-swagger-static
Register the plugin with Hapi server like this:
1const Hapi = require('@hapi/hapi'); 2const HapiSwagger = require('hapi-swagger'); 3const HapiSwaggerStatic = require('hapi-swagger-static'); 4const Inert = require('@hapi/inert'); 5const Vision = require('@hapi/vision'); 6 7const server = new Hapi.Server({ 8 port: 3000, 9}); 10 11const provision = async () => { 12 await server.register({ plugin: Inert }); 13 await server.register({ plugin: Vision }); 14 // first, add your api routes to hapi 15 await server.register({ // second, register `hapi-swagger` plugin 16 plugin: HapiSwagger, 17 options: { ... }, 18 }); 19 await server.register({ // last, register this plugin 20 plugin: HapiSwaggerStatic, 21 options: { ... }, 22 }); 23 await server.start(); 24}; 25 26provision();
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. |
This example assumes Handlebars or Mustache as template engine. It sets the following plugin options:
1{ template: 'api', // referring to the view file `api.html` below 2 viewOptions: { 3 layout: 'api-layout' // referring to the layout file `api-layout.html` below 4 } 5}
View example, e.g., api.html
to be placed into your view location:
1{{{api.html}}}
Layout example, e.g., api-layout.html
to be placed into your layout location:
1<html> 2 <head> 3 <meta charset="utf-8" /> 4 <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> 5 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"> 6 <link href='/public/api.css' rel='stylesheet' type='text/css' /> 7 <title>{{api.info.title}}</title> 8 </head> 9 <body> 10 <div class="container"> 11 {{{content}}} 12 <footer>Your company; for internal use only</footer> 13 </div> 14 </body> 15</html>
Style example, e.g., api.css
to be placed into /public
, containing some minor adjustments to Bootstrap 4:
1.h2, h2 { 2 margin-top: 1rem; 3} 4.h4, h4 { 5 margin-top: .5rem; 6} 7.card { 8 margin-bottom: 1rem; 9} 10.o2h-description p { 11 color: grey; 12 margin-bottom: .5rem; 13} 14.card .card-body .h4, .card .card-body h4 { 15 border-top: 1px solid #eee; 16 margin-top: 1rem; 17 padding-top: 1rem; 18} 19.card .card-body .h5, .card .card-body h5 { 20 margin-top: 1rem; 21} 22.card .card-body .o2h-description p { 23 margin-bottom: 0; 24} 25.card .card-body .o2h-example pre { 26 background-color: #eee; 27 font-size: small; 28} 29.o2h-parameter h5 .badge { 30 font-size: small; 31}
No vulnerabilities found.
No security vulnerabilities found.