Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fastify-swagger

Package Overview
Dependencies
Maintainers
16
Versions
100
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-swagger - npm Package Compare versions

Comparing version 4.17.1 to 5.0.0

8

index.d.ts

@@ -1,2 +0,2 @@

import { FastifyPluginCallback, onRequestHookHandler, preHandlerHookHandler } from 'fastify';
import {FastifyPluginCallback, FastifySchema, onRequestHookHandler, preHandlerHookHandler} from 'fastify';
import { OpenAPI, OpenAPIV2, OpenAPIV3 } from 'openapi-types';

@@ -114,3 +114,3 @@

interface JSONObject {
export interface JSONObject {
[key: string]: JSONValue;

@@ -131,5 +131,5 @@ }

/**
* Overwrite the route schema
* custom function to transform the route's schema and url
*/
transform?: Function;
transform?: <S extends FastifySchema = FastifySchema>({schema, url}: {schema: S, url: string}) => { schema: JSONObject, url: string };

@@ -136,0 +136,0 @@ refResolver?: {

@@ -29,6 +29,7 @@ 'use strict'

for (const route of routes) {
const schema = defOpts.transform
? defOpts.transform(route.schema)
: route.schema
const transformResult = defOpts.transform
? defOpts.transform({ schema: route.schema, url: route.url })
: {}
const schema = transformResult.schema || route.schema
const shouldRouteHideOpts = {

@@ -41,3 +42,4 @@ hiddenTag: defOpts.hiddenTag,

const url = normalizeUrl(route.url, defOpts.servers, defOpts.stripBasePath)
let url = transformResult.url || route.url
url = normalizeUrl(url, defOpts.servers, defOpts.stripBasePath)

@@ -44,0 +46,0 @@ const openapiRoute = Object.assign({}, openapiObject.paths[url])

@@ -29,6 +29,7 @@ 'use strict'

for (const route of routes) {
const schema = defOpts.transform
? defOpts.transform(route.schema)
: route.schema
const transformResult = defOpts.transform
? defOpts.transform({ schema: route.schema, url: route.url })
: {}
const schema = transformResult.schema || route.schema
const shouldRouteHideOpts = {

@@ -41,3 +42,4 @@ hiddenTag: defOpts.hiddenTag,

const url = normalizeUrl(route.url, defOpts.basePath, defOpts.stripBasePath)
let url = transformResult.url || route.url
url = normalizeUrl(url, defOpts.basePath, defOpts.stripBasePath)

@@ -44,0 +46,0 @@ const swaggerRoute = Object.assign({}, swaggerObject.paths[url])

{
"name": "fastify-swagger",
"version": "4.17.1",
"version": "5.0.0",
"description": "Serve Swagger/OpenAPI documentation for Fastify, supporting dynamic generation",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -228,3 +228,3 @@ # fastify-swagger

| swagger | {} | [Swagger configuration](https://swagger.io/specification/v2/#swaggerObject). |
| transform | null | Transform method for schema. |
| 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. |

@@ -245,6 +245,16 @@ | 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).|

<a name="register.options.transform"></a>
#### Transforms
#### Transform
To use different schemas such as [Joi](https://github.com/hapijs/joi) you can pass a synchronous `transform` method in the options to convert them back to standard JSON schemas expected by this plugin to generate the documentation (`dynamic` mode only).
By passing a synchronous `transform` function you can modify the route's url and schema.
Some possible uses of this are:
- add the `hide` flag on schema according to your own logic based on url & schema
- altering the route url into something that's more suitable for the api spec
- using different schemas such as [Joi](https://github.com/hapijs/joi) and transforming them to standard JSON schemas expected by this plugin
This option is available in `dynamic` mode only.
Examples of all the possible uses mentioned:
```js

@@ -255,17 +265,30 @@ const convert = require('joi-to-json')

swagger: { ... },
...
transform: schema => {
transform: ({ schema, url }) => {
const {
params = undefined,
body = undefined,
querystring = undefined,
...others
params,
body,
querystring,
headers,
response,
} = schema
const transformed = { ...others }
if (params) transformed.params = convert(params)
if (body) transformed.body = convert(body)
if (querystring) transformed.querystring = convert(querystring)
return transformed
const transformedSchema = Object.assign({}, schema) //shallow copy of schema
let transformedUrl = url
// Transform the schema as you wish with your own custom logic.
// In this example convert is from 'joi-to-json' lib and converts a Joi based schema to json schema
if (params) transformedSchema.params = convert(params)
if (body) transformedSchema.body = convert(body)
if (querystring) transformedSchema.querystring = convert(querystring)
if (headers) transformedSchema.headers = convert(headers)
if (response) transformedSchema.response = convert(response)
// can add the hide tag if needed
if (url.startsWith('/internal')) transformedSchema.hide = true
// can transform the url
if (url.startsWith('/latest_version/endpoint')) transformedUrl = url.replace('latest_version', 'v3')
return { schema: transformedSchema, url: transformedUrl }
}
}
})
```

@@ -272,0 +295,0 @@

@@ -116,4 +116,4 @@ 'use strict'

...openapiOption,
transform: schema => {
return { ...schema, hide: true }
transform: ({ schema, url }) => {
return { schema: { ...schema, hide: true }, url }
}

@@ -120,0 +120,0 @@ })

@@ -289,4 +289,4 @@ 'use strict'

...swaggerOption,
transform: schema => {
return { ...schema, hide: true }
transform: ({ schema, url }) => {
return { schema: { ...schema, hide: true }, url }
}

@@ -293,0 +293,0 @@ })

@@ -19,9 +19,12 @@ 'use strict'

const valid = {
transform: schema => Object.keys(schema).reduce((transformed, key) => {
transformed[key] = convertible.includes(key)
? Convert(schema[key])
: schema[key]
return transformed
},
{})
transform: ({ schema, url }) => {
const newSchema = Object.keys(schema).reduce((transformed, key) => {
transformed[key] = convertible.includes(key)
? Convert(schema[key])
: schema[key]
return transformed
},
{})
return { schema: newSchema, url }
}
}

@@ -28,0 +31,0 @@ const invalid = {

import fastify from 'fastify';
import fastifySwagger from '../..';
import fastifySwagger, {JSONObject} from '../..';
import { minimalOpenApiV3Document } from './minimal-openapiV3-document';

@@ -11,3 +11,6 @@

app.register(fastifySwagger, {});
app.register(fastifySwagger, { transform: (schema : any) => schema });
app.register(fastifySwagger, { transform: ({schema, url}) => ({
schema: schema as unknown as JSONObject,
url: url,
})});
app.register(fastifySwagger, {

@@ -14,0 +17,0 @@ mode: 'static',

@@ -6,3 +6,3 @@ import fastify from 'fastify';

FastifySwaggerUiConfigOptions,
FastifySwaggerUiHooksOptions,
FastifySwaggerUiHooksOptions, JSONObject,
} from "../.."

@@ -29,3 +29,6 @@ import { minimalOpenApiV3Document } from './minimal-openapiV3-document';

app.register(fastifySwagger, {});
app.register(fastifySwagger, { transform: (schema : any) => schema });
app.register(fastifySwagger, { transform: ({schema, url}) => ({
schema: schema as unknown as JSONObject,
url: url,
})});
app.register(fastifySwagger, {

@@ -32,0 +35,0 @@ mode: 'static',

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc