Skip to content

Commit 0b9c8d3

Browse files
R-CampbellFdawgs
andauthored
feat: Add custom decorator support (#837)
* add the ability to customize the decorator * Update README.md fix inverted transform examples Signed-off-by: Robert Campbell <[email protected]> * remove extra ready * Update README.md Signed-off-by: Frazer Smith <[email protected]> * Update README.md Signed-off-by: Frazer Smith <[email protected]> * Update README.md Signed-off-by: Frazer Smith <[email protected]> --------- Signed-off-by: Robert Campbell <[email protected]> Signed-off-by: Frazer Smith <[email protected]> Co-authored-by: Frazer Smith <[email protected]>
1 parent a04ae41 commit 0b9c8d3

File tree

4 files changed

+83
-14
lines changed

4 files changed

+83
-14
lines changed

README.md

+70-12
Original file line numberDiff line numberDiff line change
@@ -230,18 +230,19 @@ An example of using `@fastify/swagger` with `static` mode enabled can be found [
230230

231231
#### Options
232232

233-
| Option | Default | Description |
234-
| ---------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------- |
235-
| hiddenTag | X-HIDDEN | Tag to control hiding of routes. |
236-
| hideUntagged | false | If `true` remove routes without tags from resulting Swagger/OpenAPI schema file. |
237-
| initOAuth | {} | Configuration options for [Swagger UI initOAuth](https://swagger.io/docs/open-source-tools/swagger-ui/usage/oauth2/). |
238-
| openapi | {} | [OpenAPI configuration](https://swagger.io/specification/#oasObject). |
239-
| stripBasePath | true | Strips base path from routes in docs. |
240-
| swagger | {} | [Swagger configuration](https://swagger.io/specification/v2/#swaggerObject). |
241-
| transform | null | Transform method for the route's schema and url. [documentation](#register.options.transform). | |
242-
| transformObject | null | Transform method for the swagger or openapi object before it is rendered. [documentation](#register.options.transformObject). | |
243-
| refResolver | {} | Option to manage the `$ref`s of your application's schemas. Read the [`$ref` documentation](#register.options.refResolver) |
244-
| exposeHeadRoutes | false | Include HEAD routes in the definitions |
233+
| Option | Default | Description |
234+
| ---------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------- |
235+
| hiddenTag | X-HIDDEN | Tag to control hiding of routes. |
236+
| hideUntagged | false | If `true` remove routes without tags from resulting Swagger/OpenAPI schema file. |
237+
| initOAuth | {} | Configuration options for [Swagger UI initOAuth](https://swagger.io/docs/open-source-tools/swagger-ui/usage/oauth2/). |
238+
| openapi | {} | [OpenAPI configuration](https://swagger.io/specification/#oasObject). |
239+
| stripBasePath | true | Strips base path from routes in docs. |
240+
| swagger | {} | [Swagger configuration](https://swagger.io/specification/v2/#swaggerObject). |
241+
| transform | null | Transform method for the route's schema and url. [documentation](#register.options.transform). |
242+
| transformObject | null | Transform method for the swagger or openapi object before it is rendered. [documentation](#register.options.transformObject). |
243+
| refResolver | {} | Option to manage the `$ref`s of your application's schemas. Read the [`$ref` documentation](#register.options.refResolver) |
244+
| exposeHeadRoutes | false | Include HEAD routes in the definitions |
245+
| decorator | 'swagger' | Overrides the Fastify decorator. [documentation](#register.options.decorator). |
245246

246247
<a name="register.options.transform"></a>
247248
#### Transform
@@ -362,6 +363,63 @@ await fastify.register(require('@fastify/swagger'), {
362363
363364
To deep down the `buildLocalReference` arguments, you may read the [documentation](https://github.com/Eomm/json-schema-resolver#usage-resolve-one-schema-against-external-schemas).
364365
366+
<a name="register.options.decorator"></a>
367+
#### Decorator
368+
369+
By passing a string to the `decorator` option, you can override the default decorator function (`fastify.swagger()`) with a custom one. This allows you to create multiple documents by registering `@fastify/swagger` multiple times with different `transform` functions:
370+
371+
```js
372+
// Create an internal Swagger doc
373+
await fastify.register(require('@fastify/swagger'), {
374+
swagger: { ... },
375+
transform: ({ schema, url, route, swaggerObject }) => {
376+
const {
377+
params,
378+
body,
379+
querystring,
380+
headers,
381+
response,
382+
...transformedSchema
383+
} = schema
384+
let transformedUrl = URL
385+
386+
// Hide external URLs
387+
if (url.startsWith('/external')) transformedSchema.hide = true
388+
389+
return { schema: transformedSchema, url: transformedUrl }
390+
},
391+
decorator: 'internalSwagger'
392+
})
393+
394+
// Create an external Swagger doc
395+
await fastify.register(require('@fastify/swagger'), {
396+
swagger: { ... },
397+
transform: ({ schema, url, route, swaggerObject }) => {
398+
const {
399+
params,
400+
body,
401+
querystring,
402+
headers,
403+
response,
404+
...transformedSchema
405+
} = schema
406+
let transformedUrl = URL
407+
408+
// Hide internal URLs
409+
if (url.startsWith('/internal')) transformedSchema.hide = true
410+
411+
return { schema: transformedSchema, url: transformedUrl }
412+
},
413+
decorator: 'externalSwagger'
414+
})
415+
```
416+
417+
You can then call those decorators individually to retrieve them:
418+
```
419+
fastify.internalSwagger()
420+
fastify.externalSwagger()
421+
```
422+
365423
<a name="route.options"></a>
366424
### Route options
367425

lib/mode/dynamic.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module.exports = function (fastify, opts, done) {
1313
swagger: {},
1414
transform: null,
1515
transformObject: null,
16+
decorator: 'swagger',
1617
refResolver: {
1718
buildLocalReference (json, baseUri, fragment, i) {
1819
if (!json.title && json.$id) {
@@ -31,7 +32,7 @@ module.exports = function (fastify, opts, done) {
3132
}
3233

3334
const swagger = resolveSwaggerFunction(opts, cache, routes, Ref, done)
34-
fastify.decorate('swagger', swagger)
35+
fastify.decorate(opts.decorator, swagger)
3536

3637
done()
3738
}

lib/mode/static.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ module.exports = function (fastify, opts, done) {
5656
swaggerObject = opts.specification.document
5757
}
5858

59-
fastify.decorate('swagger', swagger)
59+
fastify.decorate(opts.decorator || 'swagger', swagger)
6060

6161
const cache = {
6262
swaggerObject: null,

test/decorator.js

+10
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,13 @@ test('fastify.swagger should throw if called before ready (openapi)', async (t)
3333

3434
t.throws(fastify.swagger.bind(fastify))
3535
})
36+
37+
test('decorator can be overridden', async (t) => {
38+
t.plan(1)
39+
const fastify = Fastify()
40+
41+
await fastify.register(fastifySwagger, { decorator: 'customSwaggerDecorator' })
42+
43+
await fastify.ready()
44+
t.ok(fastify.customSwaggerDecorator())
45+
})

0 commit comments

Comments
 (0)