Skip to content

Commit

Permalink
Remove swagger-ui (#672)
Browse files Browse the repository at this point in the history
* remove cspNonce decorator

* remove swagger-ui dependency

* remove csp reference

* remove more of the swagger-ui code

* fix linting error

* increase test coverage again

* remove @fastify/static as dependency

* increase test coverage

* remove garbage

* 100%

* remove .npmignore

* remove unused packges

* add Migration.md

* improve README.md

* Update MIGRATION.md

* example for dynamic mode

* use import for top level await
  • Loading branch information
Uzlopak committed Oct 10, 2022
1 parent bc6dd2f commit f15bebd
Show file tree
Hide file tree
Showing 26 changed files with 396 additions and 2,093 deletions.
50 changes: 0 additions & 50 deletions .npmignore

This file was deleted.

177 changes: 177 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
# Migration

## Migrating from version 7 to 8

As of version 8 `@fastify/swagger` is only responsible for generating valid
swagger/openapi-specifications. The new `@fastify/swagger-ui` plugin is
responsible for serving the swagger-ui frontend.

Options in version 7 of `@fastify/swagger` related to the configuration
of the swagger-ui frontend are now options of `@fastify/swagger-ui`.

The `exposeRoute` option is removed.

Following are the `@fastify/swagger-ui` options:

| Option | Default | Description |
| ------------------ | ---------------- | ------------------------------------------------------------------------------------------------------------------------- |
| baseDir | undefined | Only relevant if `@fastify/swagger` used in static-mode and additional schema-files contain referenced schemas. Specify the directory where all spec files that are included in the main one using $ref will be located. By default, this is the directory where the main spec file is located. Provided value should be an absolute path without trailing slash. |
| initOAuth | {} | Configuration options for [Swagger UI initOAuth](https://swagger.io/docs/open-source-tools/swagger-ui/usage/oauth2/). |
| routePrefix | '/documentation' | Overwrite the default Swagger UI route prefix. |
| staticCSP | false | Enable CSP header for static resources. |
| transformStaticCSP | undefined | Synchronous function to transform CSP header for static resources if the header has been previously set. |
| uiConfig | {} | Configuration options for [Swagger UI](https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/configuration.md). Must be literal values, see [#5710](https://github.com/swagger-api/swagger-ui/issues/5710).|
| uiHooks | {} | Additional hooks for the documentation's routes. You can provide the `onRequest` and `preHandler` hooks with the same [route's options](https://www.fastify.io/docs/latest/Routes/#options) interface.|

The `baseDir` option is new and is only needed if external spec files should be
exposed. `baseDir` option of `@fastify/swagger-ui` should be set to the same
value as the `specification.baseDir` option of `@fastify/swagger`.

### Example (static-mode):

before:
```js
import Fastify from 'fastify'
import fastifySwagger from '@fastify/swagger'

const fastify = new Fastify()

await fastify.register(fastifySwagger, {
mode: 'static',
specification: {
path: './examples/example-static-specification.yaml',
postProcessor: function(swaggerObject) {
return swaggerObject
},
baseDir: '/path/to/external/spec/files/location',
},
exposeRoute: true,
routePrefix: '/documentation',
initOAuth: { },
uiConfig: {
docExpansion: 'full',
deepLinking: false
},
uiHooks: {
onRequest: function (request, reply, next) { next() },
preHandler: function (request, reply, next) { next() }
},
staticCSP: true,
transformStaticCSP: (header) => header
})
```

after:
```js
import Fastify from 'fastify'
import fastifySwagger from '@fastify/swagger'
import fastifySwaggerUi from '@fastify/swagger-ui'

const fastify = new Fastify()

await fastify.register(fastifySwagger, {
mode: 'static',
specification: {
path: './examples/example-static-specification.yaml',
postProcessor: function(swaggerObject) {
return swaggerObject
},
baseDir: '/path/to/external/spec/files/location'
}
})

await fastify.register(fastifySwaggerUi, {
baseDir: '/path/to/external/spec/files/location',
routePrefix: '/documentation',
initOAuth: { },
uiConfig: {
docExpansion: 'full',
deepLinking: false
},
uiHooks: {
onRequest: function (request, reply, next) { next() },
preHandler: function (request, reply, next) { next() }
},
staticCSP: true,
transformStaticCSP: (header) => header
})
```

### Example (dynamic-mode):

before:
```js
import Fastify from 'fastify'
import fastifySwagger from '@fastify/swagger'

const fastify = new Fastify()

await fastify.register(fastifySwagger, {
mode: 'dynamic',
openapi: {
info: {
title: String,
description: String,
version: String,
},
externalDocs: Object,
servers: [ Object ],
components: Object,
security: [ Object ],
tags: [ Object ]
},
exposeRoute: true,
routePrefix: '/documentation',
initOAuth: { },
uiConfig: {
docExpansion: 'full',
deepLinking: false
},
uiHooks: {
onRequest: function (request, reply, next) { next() },
preHandler: function (request, reply, next) { next() }
},
staticCSP: true,
transformStaticCSP: (header) => header
})
```

after:
```js
import Fastify from 'fastify'
import fastifySwagger from '@fastify/swagger'
import fastifySwaggerUi from '@fastify/swagger-ui'

const fastify = new Fastify()

await fastify.register(fastifySwagger, {
mode: 'dynamic',
openapi: {
info: {
title: String,
description: String,
version: String,
},
externalDocs: Object,
servers: [ Object ],
components: Object,
security: [ Object ],
tags: [ Object ]
}
})

await fastify.register(fastifySwaggerUi, {
routePrefix: '/documentation',
initOAuth: { },
uiConfig: {
docExpansion: 'full',
deepLinking: false
},
uiHooks: {
onRequest: function (request, reply, next) { next() },
preHandler: function (request, reply, next) { next() }
},
staticCSP: true,
transformStaticCSP: (header) => header
})
```
63 changes: 8 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
![CI workflow](https://github.com/fastify/fastify-swagger/workflows/CI%20workflow/badge.svg)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)

A Fastify plugin for serving a [Swagger UI](https://swagger.io/tools/swagger-ui/), using [Swagger (OpenAPI v2)](https://swagger.io/specification/v2/) or [OpenAPI v3](https://swagger.io/specification) schemas automatically generated from your route schemas, or from an existing Swagger/OpenAPI schema.
A Fastify plugin for serving [Swagger (OpenAPI v2)](https://swagger.io/specification/v2/) or [OpenAPI v3](https://swagger.io/specification) schemas, which are automatically generated from your route schemas, or from an existing Swagger/OpenAPI schema.

Supports Fastify versions `4.x`.

Expand All @@ -14,6 +14,12 @@ Supports Fastify versions `4.x`.

If you are looking for a plugin to generate routes from an existing OpenAPI schema, check out [fastify-openapi-glue](https://github.com/seriousme/fastify-openapi-glue).

Following plugins serve swagger/openapi front-ends based on the swagger definitions generated by this plugin:

- [@fastify/swagger-ui](https://github.com/fastify/fastify-swagger-ui)

See also [the migration guide](MIGRATION.md) for migrating from `@fastify/swagger` version <= `<=7.x` to version `>=8.x`.

<a name="install"></a>
## Install
```
Expand All @@ -28,7 +34,6 @@ Add it to your project with `register`, pass it some options, call the `swagger`
const fastify = require('fastify')()

await fastify.register(require('@fastify/swagger'), {
routePrefix: '/documentation',
swagger: {
info: {
title: 'Test swagger',
Expand Down Expand Up @@ -66,18 +71,7 @@ await fastify.register(require('@fastify/swagger'), {
in: 'header'
}
}
},
uiConfig: {
docExpansion: 'full',
deepLinking: false
},
uiHooks: {
onRequest: function (request, reply, next) { next() },
preHandler: function (request, reply, next) { next() }
},
staticCSP: true,
transformStaticCSP: (header) => header,
exposeRoute: true
}
})

fastify.put('/some-route/:id', {
Expand Down Expand Up @@ -217,31 +211,16 @@ An example of using `@fastify/swagger` with `static` mode enabled can be found [

| Option | Default | Description |
| ------------------ | ---------------- | ------------------------------------------------------------------------------------------------------------------------- |
| exposeRoute | false | Exposes documentation route. |
| hiddenTag | X-HIDDEN | Tag to control hiding of routes. |
| hideUntagged | false | If `true` remove routes without tags from resulting Swagger/OpenAPI schema file. |
| initOAuth | {} | Configuration options for [Swagger UI initOAuth](https://swagger.io/docs/open-source-tools/swagger-ui/usage/oauth2/). |
| openapi | {} | [OpenAPI configuration](https://swagger.io/specification/#oasObject). |
| routePrefix | '/documentation' | Overwrite the default Swagger UI route prefix. |
| staticCSP | false | Enable CSP header for static resources. |
| stripBasePath | true | Strips base path from routes in docs. |
| swagger | {} | [Swagger configuration](https://swagger.io/specification/v2/#swaggerObject). |
| transform | null | Transform method for the route's schema and url. [documentation](#register.options.transform). |
| transformStaticCSP | undefined | Synchronous function to transform CSP header for static resources if the header has been previously set. |
| uiConfig | {} | Configuration options for [Swagger UI](https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/configuration.md). Must be literal values, see [#5710](https://github.com/swagger-api/swagger-ui/issues/5710).|
| uiHooks | {} | Additional hooks for the documentation's routes. You can provide the `onRequest` and `preHandler` hooks with the same [route's options](https://www.fastify.io/docs/latest/Routes/#options) interface.|
| refResolver | {} | Option to manage the `$ref`s of your application's schemas. Read the [`$ref` documentation](#register.options.refResolver) |
| logLevel | info | Allow to define route log level. |

If you set `exposeRoute` to `true` the plugin will expose the documentation with the following APIs:

| URL | Description |
| ----------------------- | ------------------------------------------ |
| `'/documentation/json'` | The JSON object representing the API |
| `'/documentation/yaml'` | The YAML object representing the API |
| `'/documentation/'` | The swagger UI |
| `'/documentation/*'` | External files that you may use in `$ref` |

<a name="register.options.transform"></a>
#### Transform

Expand Down Expand Up @@ -685,32 +664,6 @@ There are two ways to hide a route from the Swagger UI:
- Pass `{ hide: true }` to the schema object inside the route declaration.
- Use the tag declared in `hiddenTag` options property inside the route declaration. Default is `X-HIDDEN`.
<a name="route.uiHooks"></a>
#### Protect your documentation routes
You can protect your documentation by configuring an authentication hook.
Here is an example using the [`@fastify/basic-auth`](https://github.com/fastify/fastify-basic-auth) plugin:
```js
await fastify.register(require('@fastify/basic-auth'), {
validate (username, password, req, reply, done) {
if (username === 'admin' && password === 'admin') {
done()
} else {
done(new Error('You can not access'))
}
},
authenticate: true
})

await fastify.register(fastifySwagger, {
exposeRoute: true,
uiHooks: {
onRequest: fastify.basicAuth
}
})
```
<a name="function.options"></a>
### Swagger function options
Expand Down
20 changes: 10 additions & 10 deletions examples/example-static-specification.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
openapi: 3.0.0
info:
description: "Test swagger specification"
version: "1.0.0"
title: "Test swagger specification"
description: Test swagger specification
version: 1.0.0
title: Test swagger specification
contact:
email: "super.developer@gmail.com"
email: super.developer@gmail.com
servers:
- url: http://localhost:3000/
description: Localhost (uses test data)
Expand All @@ -15,17 +15,17 @@ paths:
tags:
- Status
responses:
200:
description: 'Server is alive'
"200":
description: Server is alive
content:
application/json:
schema:
type: object
properties:
health:
type: boolean
type: boolean
date:
type: string
type: string
example:
health: true
date: "2018-02-19T15:36:46.758Z"
health: true
date: 2018-02-19T15:36:46.758Z
4 changes: 3 additions & 1 deletion examples/test-package.json
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{}
{
"name": "test"
}
Loading

0 comments on commit f15bebd

Please sign in to comment.