Skip to content

Commit

Permalink
hide options route from the documentation built using fastify-swagger (
Browse files Browse the repository at this point in the history
  • Loading branch information
naumf authored and mcollina committed Nov 9, 2019
1 parent 54ed7d4 commit a28da42
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ You can use it as is without passing any option, or you can configure it as expl
* `preflightContinue`: Pass the CORS preflight response to the route handler (default: `false`).
* `optionsSuccessStatus`: Provides a status code to use for successful `OPTIONS` requests, since some legacy browsers (IE11, various SmartTVs) choke on `204`.
* `preflight`: if needed you can entirely disable preflight by passing `false` here (default: `true`).
* `hideOptionsRoute`: hide options route from the documentation built using [fastify-swagger](https://github.com/fastify/fastify-swagger) (default: `true`).

## Acknowledgements

Expand Down
4 changes: 4 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ declare const fastifyCors: fastify.Plugin<Server, IncomingMessage, ServerRespons
* Pass the CORS preflight response to the route handler (default: false).
*/
preflight?: boolean;
/**
* Hide options route from the documentation built using fastify-swagger (default: true).
*/
hideOptionsRoute?: boolean;
}>

export = fastifyCors;
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ function fastifyCors (fastify, opts, next) {
maxAge,
preflightContinue,
optionsSuccessStatus,
preflight
preflight,
hideOptionsRoute
} = Object.assign({
origin: '*',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
Expand All @@ -23,15 +24,16 @@ function fastifyCors (fastify, opts, next) {
exposedHeaders: null,
allowedHeaders: null,
maxAge: null,
preflight: true
preflight: true,
hideOptionsRoute: true
}, opts)

const isOriginFalsy = !origin
const isOriginString = typeof origin === 'string'
const isOriginFunction = typeof origin === 'function'

if (preflight === true) {
fastify.options('*', (req, reply) => reply.send())
fastify.options('*', { schema: { hide: hideOptionsRoute } }, (req, reply) => reply.send())
}
fastify.addHook('onRequest', onRequest)
function onRequest (req, reply, next) {
Expand Down
17 changes: 17 additions & 0 deletions test/cors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,3 +505,20 @@ test('Disable preflight', t => {
})
})
})

test('show options route', t => {
t.plan(2)

const fastify = Fastify()
fastify.register(cors, { hideOptionsRoute: false })

fastify.addHook('onRoute', (route) => {
if (route.method === 'OPTIONS' && route.url === '*') {
t.strictEqual(route.schema.hide, false)
}
})

fastify.ready(err => {
t.error(err)
})
})

0 comments on commit a28da42

Please sign in to comment.