@fastify/swagger
Advanced tools
Comparing version 8.6.0 to 8.7.0
@@ -1,2 +0,2 @@ | ||
import { FastifyPluginCallback, FastifySchema, onRequestHookHandler, preHandlerHookHandler } from 'fastify'; | ||
import { FastifyPluginCallback, FastifySchema, FastifyRequest, onRequestHookHandler, preHandlerHookHandler } from 'fastify'; | ||
import { OpenAPI, OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'; | ||
@@ -127,3 +127,9 @@ | ||
*/ | ||
transform?: <S extends FastifySchema = FastifySchema>({ schema, url }: { schema: S, url: string }) => { schema: FastifySchema, url: string }; | ||
transform?: <S extends FastifySchema = FastifySchema>({ schema, url, route, swaggerObject, openapiObject }: { | ||
schema: S, | ||
url: string, | ||
route: FastifyRequest, | ||
swaggerObject: Partial<OpenAPIV2.Document> | ||
openapiObject: Partial<OpenAPIV3.Document | OpenAPIV3_1.Document> | ||
}) => { schema: FastifySchema, url: string }; | ||
@@ -130,0 +136,0 @@ refResolver?: { |
@@ -32,3 +32,3 @@ 'use strict' | ||
const transformResult = defOpts.transform | ||
? defOpts.transform({ schema: route.schema, url: route.url }) | ||
? defOpts.transform({ schema: route.schema, url: route.url, route, openapiObject }) | ||
: {} | ||
@@ -35,0 +35,0 @@ |
@@ -30,3 +30,3 @@ 'use strict' | ||
const transformResult = defOpts.transform | ||
? defOpts.transform({ schema: route.schema, url: route.url }) | ||
? defOpts.transform({ schema: route.schema, url: route.url, route, swaggerObject }) | ||
: {} | ||
@@ -33,0 +33,0 @@ |
{ | ||
"name": "@fastify/swagger", | ||
"version": "8.6.0", | ||
"version": "8.7.0", | ||
"description": "Serve Swagger/OpenAPI documentation for Fastify, supporting dynamic generation", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -229,2 +229,4 @@ # @fastify/swagger | ||
You may also access the `openapiObject` and `swaggerObject` | ||
Some possible uses of this are: | ||
@@ -235,2 +237,3 @@ | ||
- using different schemas such as [Joi](https://github.com/hapijs/joi) and transforming them to standard JSON schemas expected by this plugin | ||
- hiding routes based on version constraints | ||
@@ -246,3 +249,3 @@ This option is available in `dynamic` mode only. | ||
swagger: { ... }, | ||
transform: ({ schema, url }) => { | ||
transform: ({ schema, url, route, swaggerObject }) => { | ||
const { | ||
@@ -272,2 +275,5 @@ params, | ||
// can add the hide tag for routes that do not match the swaggerObject version | ||
if (route?.constraints?.version !== swaggerObject.swagger) transformedSchema.hide = true | ||
return { schema: transformedSchema, url: transformedUrl } | ||
@@ -274,0 +280,0 @@ } |
@@ -59,1 +59,84 @@ 'use strict' | ||
}) | ||
test('transform can access route', async (t) => { | ||
t.plan(5) | ||
const fastify = Fastify() | ||
await fastify.register(fastifySwagger, { | ||
openapi: { info: { version: '1.0.0' } }, | ||
transform: ({ route }) => { | ||
t.ok(route) | ||
t.equal(route.method, 'GET') | ||
t.equal(route.url, '/example') | ||
t.equal(route.constraints.version, '1.0.0') | ||
return { schema: route.schema, url: route.url } | ||
} | ||
}) | ||
fastify.get('/example', { constraints: { version: '1.0.0' } }, () => {}) | ||
await fastify.ready() | ||
t.doesNotThrow(fastify.swagger) | ||
}) | ||
test('transform can access openapi object', async (t) => { | ||
t.plan(4) | ||
const fastify = Fastify() | ||
await fastify.register(fastifySwagger, { | ||
openapi: { info: { version: '1.0.0' } }, | ||
transform: ({ route, openapiObject }) => { | ||
t.ok(openapiObject) | ||
t.equal(openapiObject.openapi, '3.0.3') | ||
t.equal(openapiObject.info.version, '1.0.0') | ||
return { | ||
schema: route.schema, | ||
url: route.url | ||
} | ||
} | ||
}) | ||
fastify.get('/example', () => {}) | ||
await fastify.ready() | ||
t.doesNotThrow(fastify.swagger) | ||
}) | ||
test('transform can access swagger object', async (t) => { | ||
t.plan(4) | ||
const fastify = Fastify() | ||
await fastify.register(fastifySwagger, { | ||
swagger: { info: { version: '1.0.0' } }, | ||
transform: ({ route, swaggerObject }) => { | ||
t.ok(swaggerObject) | ||
t.equal(swaggerObject.swagger, '2.0') | ||
t.equal(swaggerObject.info.version, '1.0.0') | ||
return { | ||
schema: route.schema, | ||
url: route.url | ||
} | ||
} | ||
}) | ||
fastify.get('/example', () => {}) | ||
await fastify.ready() | ||
t.doesNotThrow(fastify.swagger) | ||
}) | ||
test('transform can hide routes based on openapi version', async (t) => { | ||
t.plan(1) | ||
const fastify = Fastify() | ||
await fastify.register(fastifySwagger, { | ||
openapi: { info: { version: '2.0.0' } }, | ||
transform: ({ schema, route, openapiObject }) => { | ||
const transformedSchema = Object.assign({}, schema) | ||
if (route?.constraints?.version !== openapiObject.info.version) transformedSchema.hide = true | ||
return { schema: transformedSchema, url: route.url } | ||
} | ||
}) | ||
fastify.get('/example', { constraints: { version: '1.0.0' } }, () => {}) | ||
await fastify.ready() | ||
const openapiObject = fastify.swagger() | ||
t.notOk(openapiObject.paths['/example']) | ||
}) |
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
248132
7115
921