Skip to content

Commit

Permalink
fix(types): Origin within OriginFunction can be undefined (#237)
Browse files Browse the repository at this point in the history
* Callback of origin should be potentially undefined

In reference to #236 origin cannot be assumed to always be a string. Propose making it `string | undefined` to ensure people handle it correctly. This would flag quickly in the sync example on the README that new URL can't work with undefined.

* Update index.d.ts

Co-authored-by: KaKa <climba03003@gmail.com>

* restructure, add typings test

Co-authored-by: KaKa <climba03003@gmail.com>
Co-authored-by: Uzlopak <aras.abbasi@googlemail.com>
  • Loading branch information
3 people authored Dec 20, 2022
1 parent 1b8950e commit 2e2f41a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 15 deletions.
13 changes: 5 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@
"version": "8.2.0",
"description": "Fastify CORS",
"main": "index.js",
"types": "index.d.ts",
"types": "types/index.d.ts",
"scripts": {
"coverage": "tap --cov --coverage-report=html test",
"lint": "npm run lint:standard && npm run lint:typescript",
"lint": "standard",
"lint:fix": "standard --fix",
"lint:standard": "standard",
"lint:typescript": "standard --parser @typescript-eslint/parser --plugin @typescript-eslint/eslint-plugin test/*.ts",
"test": "npm run unit && npm run typescript",
"test:ci": "npm run lint && tap test/*.test.js --coverage-report=lcovonly && npm run typescript",
"typescript": "tsd",
"unit": "tap test/*.test.js"
"test": "npm run test:unit && npm run test:typescript",
"test:typescript": "tsd",
"test:unit": "tap test/*.test.js"
},
"keywords": [
"fastify",
Expand Down
6 changes: 0 additions & 6 deletions test/named-import.test-d.ts

This file was deleted.

2 changes: 1 addition & 1 deletion index.d.ts → types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type FastifyCorsHook =
| 'onSend'

declare namespace fastifyCors {
export type OriginFunction = (origin: string, callback: OriginCallback) => void;
export type OriginFunction = (origin: string | undefined, callback: OriginCallback) => void;

export interface FastifyCorsOptions {
/**
Expand Down
8 changes: 8 additions & 0 deletions test/index.test-d.ts → types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fastify from 'fastify'
import { expectType } from 'tsd'
import fastifyCors, {
FastifyCorsOptionsDelegate,
FastifyCorsOptionsDelegatePromise,
Expand Down Expand Up @@ -310,3 +311,10 @@ appHttp2.register(fastifyCors, {
})

appHttp2.register(fastifyCors, delegate)

appHttp2.register(fastifyCors, {
hook: 'preParsing',
origin: function (origin) {
expectType<string|undefined>(origin)
},
})

0 comments on commit 2e2f41a

Please sign in to comment.