Skip to content

Commit

Permalink
fix: cors options pass by promise (#130)
Browse files Browse the repository at this point in the history
* fix: cors options delegator parameter

* refactor: update plugin types

* docs: fix wrong example

* test: check function params
  • Loading branch information
climba03003 authored May 7, 2021
1 parent fe1b50b commit 28133a8
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
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
}
})
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

0 comments on commit 28133a8

Please sign in to comment.