fastify-swagger
Advanced tools
Comparing version 0.8.3 to 0.9.0
@@ -30,3 +30,4 @@ 'use strict' | ||
if (opts.exposeRoute === true) { | ||
fastify.register(require('./routes')) | ||
const prefix = opts.routePrefix || '/documentation' | ||
fastify.register(require('./routes'), { prefix }) | ||
} | ||
@@ -33,0 +34,0 @@ |
{ | ||
"name": "fastify-swagger", | ||
"version": "0.8.3", | ||
"version": "0.9.0", | ||
"description": "Generate Swagger files automatically for Fastify.", | ||
@@ -26,12 +26,12 @@ "main": "index.js", | ||
"devDependencies": { | ||
"fastify": "^1.2.1", | ||
"fastify": "^1.3.1", | ||
"fs-extra": "^5.0.0", | ||
"standard": "^11.0.0", | ||
"swagger-parser": "^4.1.0", | ||
"swagger-ui-dist": "3.13.3", | ||
"tap": "^11.1.3" | ||
"swagger-ui-dist": "3.14.0", | ||
"tap": "^11.1.4" | ||
}, | ||
"dependencies": { | ||
"fastify-plugin": "^0.2.2", | ||
"fastify-static": "^0.10.0", | ||
"fastify-plugin": "^1.0.0", | ||
"fastify-static": "^0.10.1", | ||
"js-yaml": "^3.11.0" | ||
@@ -38,0 +38,0 @@ }, |
@@ -6,3 +6,3 @@ # fastify-swagger | ||
[Swagger](https://swagger.io/) documentation generator for Fastify. | ||
[Swagger](https://swagger.io/) documentation generator for Fastify. | ||
It uses the schemas you declare in your routes to generate a swagger compliant doc. | ||
@@ -30,2 +30,3 @@ | ||
}, | ||
routePrefix: '/documentation', | ||
host: 'localhost', | ||
@@ -120,3 +121,3 @@ schemes: ['http'], | ||
*All the above parameters are optional.* | ||
*All the above parameters are optional.* | ||
You can use all the properties of the [swagger specification](https://swagger.io/specification/), if you find anything missing, please open an issue or a pr! | ||
@@ -148,2 +149,20 @@ | ||
##### Overwrite swagger url end-point | ||
If you would like to overwrite the `/documentation` url you can use the `routePrefix` option. | ||
```js | ||
fastify.register(require('fastify-swagger'), { | ||
swagger: { | ||
info: { | ||
title: 'Test swagger', | ||
description: 'testing the fastify swagger api', | ||
version: '0.1.0' | ||
}, | ||
... | ||
}, | ||
exposeRoute: true, | ||
routePrefix: '/documentations' | ||
} | ||
``` | ||
<a name="swagger.options"></a> | ||
@@ -165,3 +184,3 @@ ### swagger options | ||
``` | ||
npm i | ||
npm i | ||
npm run prepare:swagger-ui | ||
@@ -168,0 +187,0 @@ ``` |
'use strict' | ||
const fp = require('fastify-plugin') | ||
const path = require('path') | ||
@@ -8,5 +7,12 @@ | ||
fastify.route({ | ||
url: '/documentation/json', | ||
url: '/', | ||
method: 'GET', | ||
schema: { hide: true }, | ||
handler: (request, reply) => reply.redirect(`.${opts.prefix}/`) | ||
}) | ||
fastify.route({ | ||
url: '/json', | ||
method: 'GET', | ||
schema: { hide: true }, | ||
handler: function (req, reply) { | ||
@@ -18,3 +24,3 @@ reply.send(fastify.swagger()) | ||
fastify.route({ | ||
url: '/documentation/yaml', | ||
url: '/yaml', | ||
method: 'GET', | ||
@@ -29,13 +35,5 @@ schema: { hide: true }, | ||
fastify.route({ | ||
url: '/documentation', | ||
method: 'GET', | ||
schema: { hide: true }, | ||
handler: (request, reply) => reply.redirect('./documentation/') | ||
}) | ||
// serve swagger-ui with the help of fastify-static | ||
fastify.register(require('fastify-static'), { | ||
root: path.join(__dirname, 'static'), | ||
prefix: `/documentation/` | ||
root: path.join(__dirname, 'static') | ||
}) | ||
@@ -46,2 +44,2 @@ | ||
module.exports = fp(fastifySwagger, '>=0.14.0') | ||
module.exports = fastifySwagger |
@@ -38,3 +38,4 @@ 'use strict' | ||
if (opts.exposeRoute === true) { | ||
fastify.register(require('./routes')) | ||
const prefix = opts.routePrefix || '/documentation' | ||
fastify.register(require('./routes'), { prefix }) | ||
} | ||
@@ -41,0 +42,0 @@ |
@@ -168,6 +168,5 @@ 'use strict' | ||
test('/documenatation should redirect to /documentation/', t => { | ||
test('/documentation should redirect to /documentation/', t => { | ||
t.plan(4) | ||
const fastify = Fastify() | ||
fastify.register(fastifySwagger, swaggerInfo) | ||
@@ -193,3 +192,3 @@ | ||
test('/documenatation/:file should send back the correct file', t => { | ||
test('/documentation/:file should send back the correct file', t => { | ||
t.plan(21) | ||
@@ -289,3 +288,3 @@ const fastify = Fastify() | ||
test('/documenatation/:file 404', t => { | ||
test('/documentation/:file 404', t => { | ||
t.plan(3) | ||
@@ -317,1 +316,33 @@ const fastify = Fastify() | ||
}) | ||
test('/documentation2/json route (overwrite)', t => { | ||
t.plan(2) | ||
const fastify = Fastify() | ||
const swaggerInfoWithRouteOverwrite = JSON.parse(JSON.stringify(swaggerInfo)) | ||
swaggerInfoWithRouteOverwrite.routePrefix = '/documentation2' | ||
fastify.register(fastifySwagger, swaggerInfoWithRouteOverwrite) | ||
fastify.get('/', () => {}) | ||
fastify.post('/', () => {}) | ||
fastify.get('/example', opts1, () => {}) | ||
fastify.post('/example', opts2, () => {}) | ||
fastify.get('/parameters/:id', opts3, () => {}) | ||
fastify.get('/example1', opts4, () => {}) | ||
fastify.inject({ | ||
method: 'GET', | ||
url: '/documentation2/json' | ||
}, (err, res) => { | ||
t.error(err) | ||
var payload = JSON.parse(res.payload) | ||
Swagger.validate(payload) | ||
.then(function (api) { | ||
t.pass('valid swagger object') | ||
}) | ||
.catch(function (err) { | ||
t.fail(err) | ||
}) | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19606036
29
11175
197
- Removedfastify-plugin@0.2.2(transitive)
- Removedsemver@5.7.2(transitive)
Updatedfastify-plugin@^1.0.0
Updatedfastify-static@^0.10.1