Skip to content

Commit

Permalink
Use IPv6 by default, add useIPv6 option (#12)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
Yanis Benson and sindresorhus committed Jun 15, 2019
1 parent e9f4118 commit 894100d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 11 deletions.
16 changes: 14 additions & 2 deletions index.d.ts
@@ -1,9 +1,18 @@
declare namespace ipify {
interface Options {
/**
Use the IPv6 API endpoint. The IPv6 endpoint will return an IPv6 address if available, IPv4 address otherwise.
Setting the `endpoint` option will override this.
@default true
*/
readonly useIPv6?: boolean;

/**
Custom API endpoint.
@default 'https://api.ipify.org'
@default 'https://api6.ipify.org'
*/
readonly endpoint?: string;
}
Expand All @@ -18,8 +27,11 @@ Get your public IP address.
```
import ipify = require('ipify');
(async => {
(async () => {
console.log(await ipify());
//=> '2001:0db8:85a3:0000:0000:8a2e:0370:7334'
console.log(await ipify({useIPv6: false});
//=> '82.142.31.236'
})();
```
Expand Down
14 changes: 8 additions & 6 deletions index.js
@@ -1,12 +1,14 @@
'use strict';
const got = require('got');

module.exports = async options => {
options = {
endpoint: 'https://api.ipify.org',
...options
};
const IPIFY_ENDPOINT_IPV4 = 'https://api.ipify.org';
const IPIFY_ENDPOINT_IPV6 = 'https://api6.ipify.org';

const {body} = await got(options.endpoint);
module.exports = async ({useIPv6 = true, endpoint} = {}) => {
if (endpoint === undefined) {
endpoint = useIPv6 ? IPIFY_ENDPOINT_IPV6 : IPIFY_ENDPOINT_IPV4;
}

const {body} = await got(endpoint);
return body;
};
2 changes: 2 additions & 0 deletions index.test-d.ts
Expand Up @@ -3,4 +3,6 @@ import ipify = require('.');

const options: ipify.Options = {};
expectType<Promise<string>>(ipify());
expectType<Promise<string>>(ipify({useIPv6: true}));
expectType<Promise<string>>(ipify({useIPv6: false}));
expectType<Promise<string>>(ipify({endpoint: 'https://example.com'}));
16 changes: 14 additions & 2 deletions readme.md
Expand Up @@ -17,8 +17,11 @@ $ npm install ipify
```js
const ipify = require('ipify');

(async => {
(async () => {
console.log(await ipify());
//=> '2001:0db8:85a3:0000:0000:8a2e:0370:7334'

console.log(await ipify({useIPv6: false});
//=> '82.142.31.236'
})();
```
Expand All @@ -34,10 +37,19 @@ Returns a `Promise<string>` with an IP address.
Type: `object`
##### useIPv6
Type: `boolean`<br>
Default: `true`
Use the IPv6 API endpoint. The IPv6 endpoint will return an IPv6 address if available, IPv4 address otherwise.
Setting the `endpoint` option will override this.
##### endpoint
Type: `string`<br>
Default: `'https://api.ipify.org'`
Default: `'https://api6.ipify.org'`
Custom API endpoint.
Expand Down
9 changes: 8 additions & 1 deletion test.js
Expand Up @@ -4,5 +4,12 @@ import ipify from '.';

test('main', async t => {
t.true(isIp(await ipify()));
t.true(isIp(await ipify({endpoint: 'https://api.ipify.org'})));
});

test('useIPv6:false', async t => {
t.true(isIp.v4(await ipify({useIPv6: false})));
});

test('endpoint:custom', async t => {
t.true(isIp.v4(await ipify({endpoint: 'https://api.ipify.org'})));
});

0 comments on commit 894100d

Please sign in to comment.