Skip to content

Commit e434dcf

Browse files
fix: allow originCallback null param in TypeScript (#58)
1 parent 015954b commit e434dcf

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,17 @@ You can use it as is without passing any option, or you can configure it as expl
3434
- `String` - set `origin` to a specific origin. For example if you set it to `"http://example.com"` only requests from "http://example.com" will be allowed.
3535
- `RegExp` - set `origin` to a regular expression pattern which will be used to test the request origin. If it's a match, the request origin will be reflected. For example the pattern `/example\.com$/` will reflect any request that is coming from an origin ending with "example.com".
3636
- `Array` - set `origin` to an array of valid origins. Each origin can be a `String` or a `RegExp`. For example `["http://example1.com", /\.example2\.com$/]` will accept any request from "http://example1.com" or from a subdomain of "example2.com".
37-
- `Function` - set `origin` to a function implementing some custom logic. The function takes the request origin as the first parameter and a callback as a second (which expects the signature `err [object], allow [bool]`), *async-await* and promises are supported as well. Fastify instance is bound to function call and you may access via `this`.
37+
- `Function` - set `origin` to a function implementing some custom logic. The function takes the request origin as the first parameter and a callback as a second (which expects the signature `err [Error | null], allow [bool]`), *async-await* and promises are supported as well. Fastify instance is bound to function call and you may access via `this`. For example:
38+
```js
39+
origin: (origin, cb) => {
40+
if(/localhost/.test(origin)){
41+
// Request from localhost will pass
42+
cb(null, true)
43+
return
44+
}
45+
cb(new Error("Not allowed"), false)
46+
}
47+
```
3848
* `methods`: Configures the **Access-Control-Allow-Methods** CORS header. Expects a comma-delimited string (ex: 'GET,PUT,POST') or an array (ex: `['GET', 'PUT', 'POST']`).
3949
* `allowedHeaders`: Configures the **Access-Control-Allow-Headers** CORS header. Expects a comma-delimited string (ex: `'Content-Type,Authorization'`) or an array (ex: `['Content-Type', 'Authorization']`). If not specified, defaults to reflecting the headers specified in the request's **Access-Control-Request-Headers** header.
4050
* `exposedHeaders`: Configures the **Access-Control-Expose-Headers** CORS header. Expects a comma-delimited string (ex: `'Content-Range,X-Content-Range'`) or an array (ex: `['Content-Range', 'X-Content-Range']`). If not specified, no custom headers are exposed.

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Server, IncomingMessage, ServerResponse } from 'http'
44

55
import fastify = require('fastify');
66

7-
type originCallback = (err: Error, allow: boolean) => void;
7+
type originCallback = (err: Error | null, allow: boolean) => void;
88

99
type originFunction = (origin: string, callback: originCallback) => void;
1010

test/types/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ app.register(fastifyCors, {
7676
})
7777

7878
app.register(fastifyCors, {
79-
origin: (origin: string, cb: Function) => {
80-
if (/localhost/.test(origin)) {
79+
origin: (origin: string, cb: (err: Error | null, allow: boolean) => void) => {
80+
if (/localhost/.test(origin) || typeof origin === 'undefined') {
8181
cb(null, true)
8282
return
8383
}

0 commit comments

Comments
 (0)