From cee76333e392d2cbcf2056e796aa368e71daad5a Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Thu, 18 Apr 2019 11:23:18 +0000 Subject: [PATCH] Require Node.js 8, add TypeScript definition (#10) --- .editorconfig | 2 +- .gitattributes | 3 +- .gitignore | 1 + .npmrc | 1 + .travis.yml | 5 ++-- index.d.ts | 29 +++++++++++++++++++ index.js | 10 ++++++- index.test-d.ts | 6 ++++ license | 20 +++---------- package.json | 75 +++++++++++++++++++++++++------------------------ readme.md | 18 +++++++----- test.js | 8 +++--- 12 files changed, 107 insertions(+), 71 deletions(-) create mode 100644 .npmrc create mode 100644 index.d.ts create mode 100644 index.test-d.ts diff --git a/.editorconfig b/.editorconfig index 98a761d..1c6314a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -7,6 +7,6 @@ charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true -[{package.json,*.yml}] +[*.yml] indent_style = space indent_size = 2 diff --git a/.gitattributes b/.gitattributes index 391f0a4..6313b56 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1 @@ -* text=auto -*.js text eol=lf +* text=auto eol=lf diff --git a/.gitignore b/.gitignore index 3c3629e..239ecff 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +yarn.lock diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..43c97e7 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/.travis.yml b/.travis.yml index b18bae5..f3fa8cd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,4 @@ -sudo: false language: node_js node_js: - - '6' - - '4' + - '10' + - '8' diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..8214050 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,29 @@ +declare namespace ipify { + interface Options { + /** + Custom API endpoint. + + @default 'https://api.ipify.org' + */ + readonly endpoint?: string; + } +} + +/** +Get your public IP address. + +@returns The IP address. + +@example +``` +import ipify = require('ipify'); + +(async => { + console.log(await ipify()); + //=> '82.142.31.236' +})(); +``` +*/ +declare function ipify(options?: ipify.Options): Promise; + +export = ipify; diff --git a/index.js b/index.js index 90fc42a..413978c 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,12 @@ 'use strict'; const got = require('got'); -module.exports = endpoint => got(endpoint || 'https://api.ipify.org').then(res => res.body); +module.exports = async options => { + options = { + endpoint: 'https://api.ipify.org', + ...options + }; + + const {body} = await got(options.endpoint); + return body; +}; diff --git a/index.test-d.ts b/index.test-d.ts new file mode 100644 index 0000000..fedeac5 --- /dev/null +++ b/index.test-d.ts @@ -0,0 +1,6 @@ +import {expectType} from 'tsd'; +import ipify = require('.'); + +const options: ipify.Options = {}; +expectType>(ipify()); +expectType>(ipify({endpoint: 'https://example.com'})); diff --git a/license b/license index 654d0bf..e7af2f7 100644 --- a/license +++ b/license @@ -1,21 +1,9 @@ -The MIT License (MIT) +MIT License Copyright (c) Sindre Sorhus (sindresorhus.com) -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/package.json b/package.json index 0fccfcf..16887cf 100644 --- a/package.json +++ b/package.json @@ -1,39 +1,40 @@ { - "name": "ipify", - "version": "2.0.0", - "description": "Get your public IP address", - "license": "MIT", - "repository": "sindresorhus/ipify", - "author": { - "name": "Sindre Sorhus", - "email": "sindresorhus@gmail.com", - "url": "sindresorhus.com" - }, - "engines": { - "node": ">=4" - }, - "scripts": { - "test": "xo && ava" - }, - "files": [ - "index.js" - ], - "keywords": [ - "ip", - "ipv4", - "ipv6", - "address", - "public", - "external", - "own" - ], - "dependencies": { - "got": "^6.7.1", - "meow": "^3.3.0" - }, - "devDependencies": { - "ava": "*", - "is-ip": "^2.0.0", - "xo": "*" - } + "name": "ipify", + "version": "2.0.0", + "description": "Get your public IP address", + "license": "MIT", + "repository": "sindresorhus/ipify", + "author": { + "name": "Sindre Sorhus", + "email": "sindresorhus@gmail.com", + "url": "sindresorhus.com" + }, + "engines": { + "node": ">=8" + }, + "scripts": { + "test": "xo && ava && tsd" + }, + "files": [ + "index.js", + "index.d.ts" + ], + "keywords": [ + "ip", + "ipv4", + "ipv6", + "address", + "public", + "external", + "own" + ], + "dependencies": { + "got": "^9.6.0" + }, + "devDependencies": { + "ava": "^1.4.1", + "is-ip": "^3.0.0", + "tsd": "^0.7.2", + "xo": "^0.24.0" + } } diff --git a/readme.md b/readme.md index 2ec62f0..b484e97 100644 --- a/readme.md +++ b/readme.md @@ -8,7 +8,7 @@ Using the [`ipify` API](https://www.ipify.org) or a [custom `ipify` instance](ht ## Install ``` -$ npm install --save ipify +$ npm install ipify ``` @@ -17,23 +17,27 @@ $ npm install --save ipify ```js const ipify = require('ipify'); -ipify().then(ip => { - console.log(ip); +(async => { + console.log(await ipify()); //=> '82.142.31.236' -}); +})(); ``` ## API -### ipify([endpoint]) +### ipify([options]) Returns a `Promise` for the IP address. -#### endpoint +#### options + +Type: `object` + +##### endpoint Type: `string`
-Default: `https://api.ipify.org` +Default: `'https://api.ipify.org'` Custom API endpoint. diff --git a/test.js b/test.js index 0944f67..c90371d 100644 --- a/test.js +++ b/test.js @@ -1,8 +1,8 @@ import test from 'ava'; import isIp from 'is-ip'; -import m from '.'; +import ipify from '.'; -test(async t => { - t.true(isIp(await m())); - t.true(isIp(await m('https://api.ipify.org'))); +test('main', async t => { + t.true(isIp(await ipify())); + t.true(isIp(await ipify({endpoint: 'https://api.ipify.org'}))); });