Skip to content

Commit

Permalink
Add TypeScript definition (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 20, 2019
1 parent bf10000 commit db6fd61
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
40 changes: 40 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
declare namespace isReachable {
interface Options {
/**
Timeout in milliseconds after which a request is considered failed.
@default 5000
*/
readonly timeout?: number;
}
}

/**
Check if servers are reachable.
The Node.js version will do a TCP handshake with the target's port. It attempts to detect cases where a router redirects the request to itself.
The browser version is limited by the fact that browsers cannot connect to arbitrary ports. It only supports HTTP and HTTPS and the check relies on the `/favicon.ico` path being present.
@param targets - One or more targets to check. Can either be a full [URL](https://nodejs.org/api/url.html) like `https://hostname`, `hostname:port` or just `hostname`. When the protocol is missing from a target `http` is assumed. [Well-known protocols](http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml) are supported (e.g. `ftp://`, `mysql://`, `redis://` and more).
@returns Whether any of the `targets` are reachable.
@example
```
import isReachable = require('is-reachable');
(async () => {
console.log(await isReachable('sindresorhus.com'));
//=> true
console.log(await isReachable('google.com:80'));
//=> true
})();
```
*/
declare function isReachable(
targets: string | readonly string[],
options?: isReachable.Options
): Promise<boolean>;

export = isReachable;
6 changes: 6 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {expectType} from 'tsd';
import isReachable = require('.');

const options: isReachable.Options = {};
expectType<Promise<boolean>>(isReachable('sindresorhus.com'));
expectType<Promise<boolean>>(isReachable('google.com:80', {timeout: 10}));
22 changes: 12 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
"node": ">=8"
},
"scripts": {
"test": "xo && ava test.js"
"test": "xo && ava test.js && tsd"
},
"files": [
"index.js",
"index.d.ts",
"browser.js"
],
"keywords": [
Expand All @@ -41,19 +42,20 @@
"socket"
],
"dependencies": {
"arrify": "^1.0.1",
"got": "^9.3.2",
"is-port-reachable": "^2.0.0",
"p-any": "^1.1.0",
"p-timeout": "^2.0.1",
"port-numbers": "^4.0.4",
"arrify": "^2.0.1",
"got": "^9.6.0",
"is-port-reachable": "^2.0.1",
"p-any": "^2.1.0",
"p-timeout": "^3.1.0",
"port-numbers": "^4.0.7",
"prepend-http": "^2.0.0",
"router-ips": "^1.0.0",
"url-parse": "^1.4.4"
"url-parse": "^1.4.6"
},
"devDependencies": {
"ava": "^0.25.0",
"xo": "^0.23.0"
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
},
"browser": "browser.js"
}
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ One or more targets to check. Can either be a full [URL](https://nodejs.org/api/

#### options

Type: `Object`
Type: `object`

##### timeout

Expand All @@ -70,4 +70,4 @@ Timeout in milliseconds after which a request is considered failed. Default: `50

MIT © [Sindre Sorhus](https://sindresorhus.com)

[Well-known protocols]: http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml
[Well-known protocols]: https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml

0 comments on commit db6fd61

Please sign in to comment.