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

fix: cors options pass by promise #130

Merged
merged 4 commits into from
May 7, 2021
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
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ You can use it as is without passing any option or you can configure it as expla
```js
const fastify = require('fastify')()

fastify.register(require('fastify-cors'), (instance) => { (req, callback) => {
fastify.register(require('fastify-cors'), (instance) => (req, callback) => {
let corsOptions;
// do not include CORS headers for requests from localhost
if (/localhost/.test(origin)) {
Expand All @@ -77,7 +77,6 @@ fastify.register(require('fastify-cors'), (instance) => { (req, callback) => {
corsOptions = { origin: true }
}
callback(null, corsOptions) // callback expects two parameters: error and options
}
mcollina marked this conversation as resolved.
Show resolved Hide resolved
})

fastify.get('/', (req, reply) => {
Expand Down
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference types="node" />

import { FastifyInstance, FastifyPlugin, FastifyRequest } from 'fastify'
import { FastifyInstance, FastifyPluginCallback, FastifyRequest } from 'fastify';

type OriginCallback = (err: Error | null, allow: boolean) => void;
export type OriginFunction = (origin: string, callback: OriginCallback) => void;
Expand Down Expand Up @@ -74,5 +74,5 @@ export interface FastifyCorsOptionsDelegatePromise { (req: FastifyRequest): Pro
export type FastifyCorsOptionsDelegate = FastifyCorsOptionsDelegateCallback | FastifyCorsOptionsDelegatePromise
export type FastifyPluginOptionsDelegate<T> = (instance: FastifyInstance) => T;

declare const fastifyCors: FastifyPlugin<FastifyCorsOptions | FastifyPluginOptionsDelegate<FastifyCorsOptionsDelegate>>;
declare const fastifyCors: FastifyPluginCallback<FastifyCorsOptions | FastifyPluginOptionsDelegate<FastifyCorsOptionsDelegate>>;
export default fastifyCors;
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function handleCorsOptionsDelegator (optionsResolver, fastify) {
return
} else {
// handle delegator based on Promise
const ret = optionsResolver(reply)
const ret = optionsResolver(req)
if (ret && typeof ret.then === 'function') {
ret.then(options => Object.assign({}, defaultOptions, options))
.then(corsOptions => onRequest(fastify, corsOptions, req, reply, next)).catch(next)
Expand Down
12 changes: 10 additions & 2 deletions test/cors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ test('Should add cors headers (custom values)', t => {
})

test('Should support dynamic config (callback)', t => {
t.plan(10)
t.plan(16)

const configs = [{
origin: 'example.com',
Expand All @@ -109,6 +109,10 @@ test('Should support dynamic config (callback)', t => {
const fastify = Fastify()
let requestId = 0
const configDelegation = function (req, cb) {
// request should have id
t.ok(req.id)
// request should not have send
t.notOk(req.send)
const config = configs[requestId]
requestId++
if (config) {
Expand Down Expand Up @@ -178,7 +182,7 @@ test('Should support dynamic config (callback)', t => {
})

test('Should support dynamic config (Promise)', t => {
t.plan(10)
t.plan(16)

const configs = [{
origin: 'example.com',
Expand All @@ -199,6 +203,10 @@ test('Should support dynamic config (Promise)', t => {
const fastify = Fastify()
let requestId = 0
const configDelegation = function (req) {
// request should have id
t.ok(req.id)
// request should not have send
t.notOk(req.send)
const config = configs[requestId]
requestId++
if (config) {
Expand Down