Skip to content

Commit

Permalink
Add TypeScript definition (#21)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
Rodrigo54 and sindresorhus committed Feb 21, 2019
1 parent a8c9c56 commit 32c4266
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 7 deletions.
45 changes: 45 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
declare namespace internalIp {
/**
* @returns The IPv6 address of the internet-facing interface, as determined from the default gateway. When the address cannot be determined for any reason, `null` will be returned.
*
* @example
*
* console.log(await internalIp.v6());
* //=> 'fe80::1'
*/
export function v6(): Promise<string>;
export namespace v6 {
/**
* @returns The IPv6 address of the internet-facing interface, as determined from the default gateway. When the address cannot be determined for any reason, `null` will be returned.
*
* @example
*
* console.log(internalIp.v6.sync());
* //=> 'fe80::1'
*/
function sync(): string;
}

/**
* @returns The IPv4 address of the internet-facing interface, as determined from the default gateway. When the address cannot be determined for any reason, `null` will be returned.
*
* @example
*
* console.log(await internalIp.v4())
* //=> '10.0.0.79'
*/
export function v4(): Promise<string>;
export namespace v4 {
/**
* @returns The IPv4 address of the internet-facing interface, as determined from the default gateway. When the address cannot be determined for any reason, `null` will be returned.
*
* @example
*
* console.log(internalIp.v4.sync())
* //=> '10.0.0.79'
*/
function sync(): string;
}
}

export default internalIp;
13 changes: 8 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ function sync(family) {
}
}

module.exports.v6 = () => promise('v6');
module.exports.v4 = () => promise('v4');

module.exports.v6.sync = () => sync('v6');
module.exports.v4.sync = () => sync('v4');
const internalIp = {};
internalIp.v6 = () => promise('v6');
internalIp.v4 = () => promise('v4');
internalIp.v6.sync = () => sync('v6');
internalIp.v4.sync = () => sync('v4');

module.exports = internalIp;
module.exports.default = internalIp;
8 changes: 8 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {expectType} from 'tsd-check';
import internalIp from '.';

expectType<string>(await internalIp.v4());
expectType<string>(await internalIp.v6());

expectType<string>(internalIp.v4.sync());
expectType<string>(internalIp.v6.sync());
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"node": ">=6"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd-check"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"ip",
Expand All @@ -36,6 +37,7 @@
},
"devDependencies": {
"ava": "^1.2.1",
"tsd-check": "^0.3.0",
"xo": "^0.24.0"
}
}

0 comments on commit 32c4266

Please sign in to comment.