diff --git a/.travis.yml b/.travis.yml index c98f746..4cbb9c4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,6 +22,7 @@ before_install: # Setup Node.js version-specific dependencies - "test $TRAVIS_NODE_VERSION != '0.8' || npm rm --save-dev istanbul" + - "test $TRAVIS_NODE_VERSION = '0.8' || npm install --save-dev @types/connect @types/node typescript" - "test $(echo $TRAVIS_NODE_VERSION | cut -d. -f1) -ge 4 || npm rm --save-dev $(grep -E '\"eslint\\S*\"' package.json | cut -d'\"' -f2)" # Update Node.js modules @@ -29,8 +30,9 @@ before_install: - "test ! -d node_modules || npm rebuild" script: # Run test script, depending on istanbul install - - "test ! -z $(npm -ps ls istanbul) || npm test" - - "test -z $(npm -ps ls istanbul) || npm run-script test-travis" - - "test -z $(npm -ps ls eslint ) || npm run-script lint" + - "test ! -z $(npm -ps ls istanbul ) || npm test" + - "test -z $(npm -ps ls istanbul ) || npm run-script test-travis" + - "test -z $(npm -ps ls eslint ) || npm run-script lint" + - "test -z $(npm -ps ls typescript) || npm run-script test-typings" after_script: - "test -e ./coverage/lcov.info && npm install coveralls@2 && cat ./coverage/lcov.info | coveralls" diff --git a/README.md b/README.md index f6289dc..4c89aa3 100644 --- a/README.md +++ b/README.md @@ -151,6 +151,10 @@ app.use(vhost('api.example.com', function (req, res) { app.listen(3000) ``` +### Typescript support + +Typescript defintions are included in npm package. + ## License [MIT](LICENSE) diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..cac7c10 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,24 @@ +import * as http from "http" + +declare namespace vhost { + interface RequestHandler { + (req: http.IncomingMessage, res: http.ServerResponse, next: Function): void + } +} + +declare function vhost(hostname: string | RegExp, handle: vhost.RequestHandler): vhost.RequestHandler + +declare module "http" { + interface IVHost { + [key: number]: string + host: string, + hostname: string, + length: number + } + + interface IncomingMessage { + vhost: IVHost + } +} + +export = vhost diff --git a/package.json b/package.json index a137963..cde3898 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,8 @@ "files": [ "LICENSE", "HISTORY.md", - "index.js" + "index.js", + "index.d.ts" ], "engines": { "node": ">= 0.8.0" @@ -31,6 +32,7 @@ "scripts": { "lint": "eslint --plugin markdown --ext js,md .", "test": "mocha --reporter spec --bail --check-leaks test/", + "test-typings": "tsc -p tsconfig.json", "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/", "test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/" } diff --git a/test/test.ts b/test/test.ts new file mode 100644 index 0000000..2140fe0 --- /dev/null +++ b/test/test.ts @@ -0,0 +1,19 @@ +import * as vhost from "../index" +import * as connect from "connect" + +const app = connect() + +// Using string hostname +vhost("*.example.com", app) + +// Using regex hostname +vhost(/(.*)\.example.com/, app) + +// Using request handler +vhost("*.example.com", (req, res, next) => { + req.vhost[0] === 'foo' + req.vhost[1] === 'bar' + req.vhost.host === 'foo.bar.example.com:8080' + req.vhost.hostname === 'foo.bar.example.com' + req.vhost.length === 2 +}) \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..5a18eb0 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,20 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "types": [ + "node" + ], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "test/test.ts" + ] +}