Skip to content

Commit 68b88a0

Browse files
committed
Add typescript defintions
1 parent 1ba557c commit 68b88a0

6 files changed

Lines changed: 78 additions & 5 deletions

File tree

.travis.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ before_install:
2929
- "test ! -d node_modules || npm rebuild"
3030
script:
3131
# Run test script, depending on istanbul install
32-
- "test ! -z $(npm -ps ls istanbul) || npm test"
33-
- "test -z $(npm -ps ls istanbul) || npm run-script test-travis"
34-
- "test -z $(npm -ps ls eslint ) || npm run-script lint"
32+
- "test ! -z $(npm -ps ls istanbul ) || npm test"
33+
- "test -z $(npm -ps ls istanbul ) || npm run-script test-travis"
34+
- "test -z $(npm -ps ls eslint ) || npm run-script lint"
35+
- "test -z $(npm -ps ls typescript) -a -z $TRAVIS_NODE_VERSION != '0.8' || npm run-script test-typings"
3536
after_script:
3637
- "test -e ./coverage/lcov.info && npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ app.use(vhost('api.example.com', function (req, res) {
151151
app.listen(3000)
152152
```
153153

154+
### Typescript support
155+
156+
Typescript defintions are included in npm package.
157+
154158
## License
155159

156160
[MIT](LICENSE)

index.d.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as http from "http"
2+
3+
declare namespace vhost {
4+
interface RequestHandler {
5+
(req: http.IncomingMessage, res: http.ServerResponse, next: Function): void
6+
}
7+
}
8+
9+
declare function vhost(host: string, handler: vhost.RequestHandler): vhost.RequestHandler
10+
11+
declare module "http" {
12+
interface IVHost {
13+
host: string,
14+
hostname: string,
15+
length: number
16+
}
17+
18+
interface IncomingMessage {
19+
vhost: IVHost | string[]
20+
}
21+
}
22+
23+
export = vhost

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
"license": "MIT",
1010
"repository": "expressjs/vhost",
1111
"devDependencies": {
12+
"@types/connect": "3.4.31",
13+
"@types/node": "9.6.5",
1214
"eslint": "3.19.0",
1315
"eslint-config-standard": "10.2.1",
1416
"eslint-plugin-import": "2.8.0",
@@ -18,19 +20,23 @@
1820
"eslint-plugin-standard": "3.0.1",
1921
"istanbul": "0.4.5",
2022
"mocha": "2.5.3",
21-
"supertest": "1.1.0"
23+
"supertest": "1.1.0",
24+
"typescript": "2.8.1"
2225
},
2326
"files": [
2427
"LICENSE",
2528
"HISTORY.md",
26-
"index.js"
29+
"index.js",
30+
"index.d.ts"
2731
],
32+
"typings": "index.d.ts",
2833
"engines": {
2934
"node": ">= 0.8.0"
3035
},
3136
"scripts": {
3237
"lint": "eslint --plugin markdown --ext js,md .",
3338
"test": "mocha --reporter spec --bail --check-leaks test/",
39+
"test-typings": "tsc -p tsconfig.json",
3440
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/",
3541
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/"
3642
}

test/test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import * as vhost from "../index"
2+
import * as connect from "connect"
3+
4+
const app = connect()
5+
6+
// Using application
7+
vhost("*.example.com", app)
8+
9+
// Using request handler
10+
vhost("*.example.com", (req, res, next) => {
11+
if (req.vhost instanceof Array) {
12+
req.vhost[0] === 'foo'
13+
req.vhost[1] === 'bar'
14+
} else {
15+
req.vhost.host === 'foo.bar.example.com:8080'
16+
req.vhost.hostname === 'foo.bar.example.com'
17+
req.vhost.length === 2
18+
}
19+
})

tsconfig.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"lib": [
5+
"es6"
6+
],
7+
"noImplicitAny": true,
8+
"noImplicitThis": true,
9+
"strictNullChecks": true,
10+
"types": [
11+
"node"
12+
],
13+
"noEmit": true,
14+
"forceConsistentCasingInFileNames": true
15+
},
16+
"files": [
17+
"index.d.ts",
18+
"test/test.ts"
19+
]
20+
}

0 commit comments

Comments
 (0)