Skip to content

Commit 29000a9

Browse files
authored
Export originCallback and originFunction (#64)
Fixes #62 #63
1 parent 1724292 commit 29000a9

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { FastifyPlugin } from 'fastify'
44

55
type OriginCallback = (err: Error | null, allow: boolean) => void;
6-
type OriginFunction = (origin: string, callback: OriginCallback) => void;
6+
export type OriginFunction = (origin: string, callback: OriginCallback) => void;
77
type OriginType = string | boolean | RegExp;
88
type ValueOrArray<T> = T | ArrayOfValueOrArray<T>;
99
interface ArrayOfValueOrArray<T> extends Array<ValueOrArray<T>> { }

test/index.test-d.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fastify from 'fastify'
2-
import fastifyCors from '..'
2+
import fastifyCors, {OriginFunction} from '..'
33

44
const app = fastify()
55

@@ -77,14 +77,16 @@ app.register(fastifyCors, {
7777
preflight: false
7878
})
7979

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+
8088
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,
8890
allowedHeaders: ['authorization', 'content-type'],
8991
methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'],
9092
credentials: true,
@@ -172,8 +174,8 @@ appHttp2.register(fastifyCors, {
172174
})
173175

174176
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)) {
177179
cb(null, true)
178180
return
179181
}

0 commit comments

Comments
 (0)