11# hapi-swaggered
22Yet another hapi plugin providing swagger compliant API specifications (swagger specs 2.0) based on routes and joi schemas to be used with swagger-ui.
33
4- Supports hapi 7 .x and 8 .x
4+ Supports hapi 8 .x and 9 .x
55
66[ ![ Build Status] ( https://img.shields.io/travis/z0mt3c/hapi-swaggered/master.svg )] ( https://travis-ci.org/z0mt3c/hapi-swaggered )
77[ ![ Coverage Status] ( https://img.shields.io/coveralls/z0mt3c/hapi-swaggered/master.svg )] ( https://coveralls.io/r/z0mt3c/hapi-swaggered?branch=master )
@@ -13,12 +13,13 @@ Supports hapi 7.x and 8.x
1313``` bash
1414npm install hapi-swaggered --save
1515```
16+
1617## Similar swagger-projects for hapi
1718[ krakenjs/swaggerize-hapi] ( https://github.com/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] ( http://petstore.swagger.wordnik.com/ ) ) for your api up and running (e.g. through [ hapi-swaggered-ui] ( https://github.com/z0mt3c/hapi-swaggered-ui ) ).
1819
1920## Swagger-UI
2021This plugin does not include the [ swagger-ui] ( https://github.com/wordnik/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:
21- * [ hapi-swaggered-ui@>=1.3.0 ] ( https://github.com/z0mt3c/hapi-swaggered-ui )
22+ * [ hapi-swaggered-ui] ( https://github.com/z0mt3c/hapi-swaggered-ui )
2223
2324## Plugin Configuration
2425* ` requiredTags ` : an array of strings, only routes with all of the specified tags will be exposed, defaults to: ` ['api'] `
@@ -53,73 +54,74 @@ This plugin does not include the [swagger-ui](https://github.com/wordnik/swagger
5354* ` responseValidation ` : boolean, turn response validation on and off for hapi-swaggered routes, defaults to false
5455* ` auth ` : authentication configuration [ hapijs documentation] ( https://github.com/hapijs/hapi/blob/master/API.md#route-options ) (default to undefined)
5556
56- ## Example (Hapi 8 )
57+ ## Example (Hapi 9 )
5758Example configuration for hapi-swaggered + hapi-swaggered-ui
5859
5960``` js
6061var Hapi = require (' hapi' )
61- var Joi = require (' joi' )
62- var hapiSwaggered = require (' hapi-swaggered' )
63- var hapiSwaggeredUi = require (' hapi-swaggered-ui' )
6462
6563var server = new Hapi.Server ()
6664server .connection ({
6765 port: 8000 ,
6866 labels: [' api' ]
6967})
7068
71- server .register ({
72- register: hapiSwaggered,
73- options: {
74- tags: {
75- ' /foobar' : ' Example foobar description'
76- },
77- info: {
69+ server .register ([
70+ require (' inert' ),
71+ require (' vision' ),
72+ {
73+ register: require (' hapi-swaggered' ),
74+ options: {
75+ tags: {
76+ ' foobar/test' : ' Example foobar description'
77+ },
78+ info: {
79+ title: ' Example API' ,
80+ description: ' Powered by node, hapi, joi, hapi-swaggered, hapi-swaggered-ui and swagger-ui' ,
81+ version: ' 1.0'
82+ }
83+ }
84+ },
85+ {
86+ register: require (' hapi-swaggered-ui' ),
87+ options: {
7888 title: ' Example API' ,
79- description: ' Tiny hapi-swaggered example' ,
80- version: ' 1.0'
89+ path: ' /docs' ,
90+ authorization: {
91+ field: ' apiKey' ,
92+ scope: ' query' , // header works as well
93+ // valuePrefix: 'bearer '// prefix incase
94+ defaultValue: ' demoKey' ,
95+ placeholder: ' Enter your apiKey here'
96+ },
97+ swaggerOptions: {
98+ validatorUrl: null
99+ }
81100 }
82- }
83- }, {
84- select: ' api' ,
85- routes: {
86- prefix: ' /swagger'
87- }
88- }, function (err ) {
101+ }], {
102+ select: ' api'
103+ }, function (err ) {
89104 if (err) {
90105 throw err
91106 }
92- })
93107
94- server .register ({
95- register: hapiSwaggeredUi,
96- options: {
97- title: ' Example API' ,
98- authorization: {
99- field: ' apiKey' ,
100- scope: ' query' // header works as well
101- // valuePrefix: 'bearer '// prefix incase
108+ server .route ({
109+ path: ' /' ,
110+ method: ' GET' ,
111+ handler : function (request , reply ) {
112+ reply .redirect (' /docs' )
102113 }
103- }
104- }, {
105- select: ' api' ,
106- routes: {
107- prefix: ' /docs'
108- }
109- }, function (err ) {
110- if (err) {
111- throw err
112- }
113- })
114+ })
114115
115- server .route ({
116- path: ' /' ,
117- method: ' GET' ,
118- handler : function (request , reply ) {
119- reply .redirect (' /docs' )
120- }
116+ server .start (function () {
117+ console .log (' started on http://localhost:8000' )
118+ })
121119})
120+ ```
121+
122122
123+ Demo Routes
124+ ``` js
123125server .route ({
124126 path: ' /foobar/test' ,
125127 method: ' GET' ,
@@ -149,10 +151,6 @@ server.route({
149151 }
150152 }
151153})
152-
153- server .start (function () {
154- console .log (' started on http://localhost:8000' )
155- })
156154```
157155
158156## Overwriting configuration on server level (Hapi 8)
@@ -296,8 +294,8 @@ For example:
296294* ` ?tags=public,-beta (equal to ?tags=+public,-beta) `
297295 * will only show apis and routes with tag public AND NOT beta.
298296
299- ## Hapi 7 usage
300- Please have a look at a previous [ README] ( https://github.com/z0mt3c/hapi-swaggered/blob/feb699f1c2393c466ae29850733877b095673491/README.md ) .
297+ ## Hapi 8 usage
298+ Please have a look at a previous [ README] ( https://github.com/z0mt3c/hapi-swaggered/tree/v2.2.2#example-hapi-8 ) .
301299
302300## Known issues
303301### No repsonse types
0 commit comments