Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hide options route from the documentation built using fastify-swagger #42

Merged
merged 1 commit into from
Nov 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
})