|
1 | 1 | import fastify from 'fastify'
|
2 |
| -import fastifyCors from '..' |
| 2 | +import fastifyCors, {OriginFunction} from '..' |
3 | 3 |
|
4 | 4 | const app = fastify()
|
5 | 5 |
|
@@ -77,14 +77,16 @@ app.register(fastifyCors, {
|
77 | 77 | preflight: false
|
78 | 78 | })
|
79 | 79 |
|
| 80 | +const corsDelegate: OriginFunction = (origin, cb) => { |
| 81 | + if (typeof origin === 'undefined' || /localhost/.test(origin)) { |
| 82 | + cb(null, true) |
| 83 | + return |
| 84 | + } |
| 85 | + cb(new Error(), false) |
| 86 | +}; |
| 87 | + |
80 | 88 | app.register(fastifyCors, {
|
81 |
| - origin: (origin: string, cb: (err: Error | null, allow: boolean) => void) => { |
82 |
| - if (/localhost/.test(origin) || typeof origin === 'undefined') { |
83 |
| - cb(null, true) |
84 |
| - return |
85 |
| - } |
86 |
| - cb(new Error(), false) |
87 |
| - }, |
| 89 | + origin: corsDelegate, |
88 | 90 | allowedHeaders: ['authorization', 'content-type'],
|
89 | 91 | methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'],
|
90 | 92 | credentials: true,
|
@@ -172,8 +174,8 @@ appHttp2.register(fastifyCors, {
|
172 | 174 | })
|
173 | 175 |
|
174 | 176 | appHttp2.register(fastifyCors, {
|
175 |
| - origin: (origin: string, cb: (err: Error | null, allow: boolean) => void) => { |
176 |
| - if (/localhost/.test(origin) || typeof origin === 'undefined') { |
| 177 | + origin: (origin: string | undefined, cb: (err: Error | null, allow: boolean) => void) => { |
| 178 | + if (typeof origin === 'undefined' || /localhost/.test(origin)) { |
177 | 179 | cb(null, true)
|
178 | 180 | return
|
179 | 181 | }
|
|
0 commit comments