Installations
npm install hapi-swaggered
Developer Guide
Typescript
No
Module System
CommonJS
Min. Node Version
>=8.0.0
Node Version
12.13.1
NPM Version
6.13.2
Score
76.3
Supply Chain
99.5
Quality
72
Maintenance
40
Vulnerability
99.1
License
Releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (99.68%)
Makefile (0.32%)
Developer
z0mt3c
Download Statistics
Total Downloads
1,410,938
Last Day
162
Last Week
758
Last Month
3,331
Last Year
68,705
GitHub Statistics
82 Stars
322 Commits
38 Forks
4 Watching
10 Branches
19 Contributors
Bundle Size
198.15 kB
Minified
60.23 kB
Minified + Gzipped
Package Meta Information
Latest Version
3.1.0
Package Id
hapi-swaggered@3.1.0
Size
25.10 kB
NPM Version
6.13.2
Node Version
12.13.1
Publised On
24 Mar 2020
Total Downloads
Cumulative downloads
Total Downloads
1,410,938
Last day
1.9%
162
Compared to previous day
Last week
-5.1%
758
Compared to previous week
Last month
36.9%
3,331
Compared to previous month
Last year
-69.1%
68,705
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
hapi-swaggered 3.x
Yet another hapi plugin providing swagger compliant API specifications (swagger specs 2.0) based on routes and joi schemas to be used with swagger-ui.
Supports hapi 17.x and up
For earlier versions check hapi-swaggered 2.x (current default/latest npm install hapi-swaggered --save
)
Install
1npm install hapi-swaggered@next --save
Similar swagger-projects for hapi
krakenjs/swaggerize-hapi follows a design driven approach (swagger-schema first) for building APIs. In other words: it supports you to implement an api behind a specific swagger-schema while you have to create and maintain the swagger-schema yourself (or a third-party). In contrast with hapi-swaggered you will have to design your api through hapi route defintions and joi schemas (or did already) and hapi-swaggered will generate it's swagger specifications up on that (Of course not as beautiful and shiny structured as done by hand). Based on this you are able to get beautiful hands-on swagger-ui documentation (like this) for your api up and running (e.g. through hapi-swaggered-ui).
Swagger-UI
This plugin does not include the swagger-ui interface. It just serves a bare swagger 2.0 compliant json feed. If you are looking for an easy swagger-ui plugin to drop-in? You should have a look at:
Plugin Configuration
requiredTags
: an array of strings, only routes with all of the specified tags will be exposed, defaults to:['api']
produces
: an array of mime type strings, defaults to:[ 'application/json' ]
consumes
: an array of mime type strings, defaults to:[ 'application/json' ]
endpoint
: route path to the swagger specification, defaults to:'/swagger'
routeTags
: an array of strings, all routes exposed by hapi-swaggered will be tagged as specified, defaults to['swagger']
stripPrefix
: a path prefix which should be stripped from the swagger specifications. E.g. your root resource are located under/api/v12345678/resource
you might want to strip/api/v12345678
, defaults to nullbasePath
: string, optional url base path (e.g. used to fix reverse proxy routes)supportedMethods
: array of http methods, only routes with mentioned methods will be exposed, in case of a wildcard * a route will be generated for each method, defaults to['get', 'put', 'post', 'delete', 'patch']
host
: string, overwrite requests host (e.g. domain.tld:1337)schemes
: array of allowed schemes e.g.['http', 'https', 'ws', 'wss']
(optional)info
: exposed swagger api informations, defaults to null (optional)title
: string (required)description
: string (required)termsOfService
: stringcontact
: object (optional)name
: stringurl
: stringemail
: string
license
: object (optional)name
: string: stringurl
: string: string
version
: version string of your api, which will be exposed (required)
tagging
: Options used for grouping routesmode
: string, can bepath
(routes will be grouped by its path) ortags
(routes will be grouped by its tags), default ispath
pathLevel
integer, in case of modepath
it defines on which level the path grouping will take place (default is 1)stripRequiredTags
boolean, in case of modetags
it defines if therequiredTags
will not be exposed (default is true)
tags
: object (or array with objects according to the swagger specs) for defining tag / group descriptions. E.g. you two endpoints/get/this
and/get/that
and the tagging mode is set to path (with pathLevel: 1) they will be groupped unter /get and you are able to define a description through this object as{ 'get': 'get this and that' }
, defaults to nullcors
: boolean or object with cors configuration as according to the hapijs documentation (defaults to false)cache
: caching options for the swagger schema generation as specified inserver.method()
of hapi, defaults to:{ expiresIn: 15 * 60 * 1000 }
responseValidation
: boolean, turn response validation on and off for hapi-swaggered routes, defaults to falseauth
: authentication configuration hapijs documentation (default to undefined)securityDefinitions
: security definitions according to [swagger specs](https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md]
Example
Example configuration for hapi-swaggered + hapi-swaggered-ui
1const Hapi = require('hapi'); 2 3(async () => { 4 const server = await new Hapi.Server({ 5 port: 8000 6 }) 7 8 await server.register([ 9 require('inert'), 10 require('vision'), 11 { 12 plugin: require('hapi-swaggered'), 13 options: { 14 tags: { 15 'foobar/test': 'Example foobar description' 16 }, 17 info: { 18 title: 'Example API', 19 description: 'Powered by node, hapi, joi, hapi-swaggered, hapi-swaggered-ui and swagger-ui', 20 version: '1.0' 21 } 22 } 23 }, 24 { 25 plugin: require('hapi-swaggered-ui'), 26 options: { 27 title: 'Example API', 28 path: '/docs', 29 authorization: { 30 field: 'apiKey', 31 scope: 'query', // header works as well 32 // valuePrefix: 'bearer '// prefix incase 33 defaultValue: 'demoKey', 34 placeholder: 'Enter your apiKey here' 35 }, 36 swaggerOptions: { 37 validatorUrl: null 38 } 39 } 40 } 41 ]) 42 43 server.route({ 44 path: '/', 45 method: 'GET', 46 handler (request, h) { 47 return h.response().redirect('/docs') 48 } 49 }) 50 51 try { 52 await server.start() 53 console.log('Server running at:', server.info.uri) 54 } catch (err) { 55 console.log(err) 56 } 57})()
Demo Routes
1server.route({ 2 path: '/foobar/test', 3 method: 'GET', 4 options: { 5 tags: ['api'], 6 description: 'My route description', 7 notes: 'My route notes', 8 handler () { 9 return {}; 10 } 11 } 12}); 13 14server.route({ 15 path: '/foobar/{foo}/{bar}', 16 method: 'GET', 17 options: { 18 tags: ['api'], 19 validate: { 20 params: { 21 foo: Joi.string().required().description('test'), 22 bar: Joi.string().required() 23 } 24 }, 25 handler () { 26 return {}; 27 } 28 } 29});
Features
Model naming
To assign custom names to your Models use the Joi.meta() option (in previous joi versions Joi.options() may be used)
1Joi.object({}).meta({ className: 'FooBar' });
Model description
To assign a description to your Models use the Joi.meta() option like above
1Joi.object({}).meta({ description: 'A description of FooBar' });
Type naming
To override the type a Joi model should be interpreted as, use the Joi.meta() option like above. This is especially useful when utilizing the extend and coerce features of Joi schema definition
1Joi.object({}).meta({ swaggerType: string });
Document responses
There are 2 and a half different ways of documenting responses of routes:
The hapi way:
1{ 2 options: { 3 response: { 4 schema: Joi.object({ 5 bar: Joi.string().description('test').required() 6 }).description('test'), 7 status: { 8 500: Joi.object({ 9 bar: Joi.string().description('test').required() 10 }) 11 } 12 } 13 } 14}
The plugin way without schemas:
1{ 2 options: { 3 plugins: { 4 'hapi-swaggered': { 5 responses: { 6 default: {description: 'Bad Request'}, 7 500: {description: 'Internal Server Error'} 8 } 9 } 10 }, 11 response: { 12 schema: Joi.object({ 13 bar: Joi.string().required() 14 }).description('test') 15 } 16 } 17}
The plugin way with schemas:
1{ 2 options: { 3 plugins: { 4 'hapi-swaggered': { 5 responses: { 6 default: { 7 description: 'Bad Request', schema: Joi.object({ 8 bar: Joi.string().description('test').required() 9 }) 10 }, 11 500: {description: 'Internal Server Error'} 12 } 13 } 14 } 15 } 16}
Specify an operationId for a route:
1{ 2 options: { 3 plugins: { 4 'hapi-swaggered': { 5 operationId: 'testRoute' 6 } 7 } 8 } 9}
Specify an security options to a route / operation:
1{ 2 options: { 3 plugins: { 4 'hapi-swaggered': { 5 security: {} 6 } 7 } 8 } 9}
Tag filtering
Routes can be filtered for tags through the tags query parameter beside the requiredTags property which is always required to be present.
For example:
?tags=public,beta (equal to ?tags=+public,+beta)
- will only show apis and routes with tag public AND/OR beta.
?tags=public,-beta (equal to ?tags=+public,-beta)
- will only show apis and routes with tag public AND NOT beta.
Known issues
No response types
The routes response schemas which hapi-swaggered is parsing will be dropped by hapi whenever the response validation is disabled. In this case hapi-swaggered will not be able to show any response types. A very low sampling rate is sufficient to keep the repsonse types.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE.txt:0
- Info: FSF or OSI recognized license: MIT License: LICENSE.txt:0
Reason
Found 2/18 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 14 are checked with a SAST tool
Reason
27 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-mg85-8mv5-ffjr
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-ww39-953v-wcq6
- Warn: Project is vulnerable to: GHSA-765h-qjxv-5f44
- Warn: Project is vulnerable to: GHSA-f2jv-r9rf-7988
- Warn: Project is vulnerable to: GHSA-7hx8-2rxv-66xv
- Warn: Project is vulnerable to: GHSA-c429-5p7v-vgjp
- Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-2mvq-xp48-4c77
- Warn: Project is vulnerable to: GHSA-5854-jvxx-2cg9
- Warn: Project is vulnerable to: GHSA-g64q-3vg8-8f93
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
Score
1.9
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to hapi-swaggered
hapi-swaggered-ui
Easy swagger-ui drop-in plugin for hapi to be used with hapi-swaggered.
@leisurelink/hapi-swaggered-ui
Easy swagger-ui drop-in plugin for hapi to be used with hapi-swaggered.
hapi-swaggered-ui-fork
Easy swagger-ui drop-in plugin for hapi to be used with hapi-swaggered.
hapi-swaggered-fork
Yet another hapi plugin providing swagger compliant API specifications based on routes and joi schemas to be used with swagger-ui.