google-geolocation
Advanced tools
Comparing version 1.0.2 to 2.0.0
@@ -0,1 +1,34 @@ | ||
## 2.0.0 (2021-09-22) | ||
##### Breaking Changes | ||
* Interface changed ([38765dc7](https://github.com/fvdm/nodejs-geolocation/commit/38765dc7cee426b909baa7a3320e941e36b9cc44)) | ||
##### Chores | ||
* **license:** Update old url ([5e71f9c2](https://github.com/fvdm/nodejs-geolocation/commit/5e71f9c21c5253ea96f25c96ebfcf727e08a5c19)) | ||
* Removed bitHound ([f5d75a99](https://github.com/fvdm/nodejs-geolocation/commit/f5d75a99a22bfa6aba5d41159487551a127c3918)) | ||
* **package:** Update deps and meta ([942874a1](https://github.com/fvdm/nodejs-geolocation/commit/942874a14a6101bc227bbc10a5025ed14bbb5e3a)) | ||
##### Bug Fixes | ||
* upgrade httpreq from 0.4.24 to 0.5.1 ([#9](https://github.com/fvdm/nodejs-geolocation/pull/9)) ([4f2b943e](https://github.com/fvdm/nodejs-geolocation/commit/4f2b943eca732a6ee68f8f516bbc1203e699a1ff)) | ||
##### Tests | ||
* **ci:** Ignore Coveralls error ([c4496673](https://github.com/fvdm/nodejs-geolocation/commit/c4496673c592c33b447075006826bdd527d5cf34)) | ||
* **CI:** | ||
* Clarify Coveralls job ([98b233a0](https://github.com/fvdm/nodejs-geolocation/commit/98b233a03beb94b03f979b186ee3424a0e191247)) | ||
* Removed old `git fetch` line ([73bd00bd](https://github.com/fvdm/nodejs-geolocation/commit/73bd00bd3437c1193c70d1a39520ffa3e29d347e)) | ||
* Fixed typo in env vars ([e9dc7e28](https://github.com/fvdm/nodejs-geolocation/commit/e9dc7e28aa621290960307c6b3e679f249730e1d)) | ||
* Github deps and funding configs ([3bc0aaf0](https://github.com/fvdm/nodejs-geolocation/commit/3bc0aaf099177465ef03945ce476b19503033324)) | ||
* Replaced Travis with Github action ([027f0583](https://github.com/fvdm/nodejs-geolocation/commit/027f05838516df0067bf2535c3c5ea396ec284f3)) | ||
* Fixed API error message check ([b7791263](https://github.com/fvdm/nodejs-geolocation/commit/b7791263f78ecb078c7d7ffc4949d683fe3ee1ef)) | ||
* Updated API error params ([fbf20eb1](https://github.com/fvdm/nodejs-geolocation/commit/fbf20eb1e1b3cb250e6d5d967ff2989223442ec4)) | ||
* Fixed bad refs ([529f26b9](https://github.com/fvdm/nodejs-geolocation/commit/529f26b93c22323f5dda3455e54f5b22899fe14b)) | ||
* **config:** | ||
* Allow undefined `arguments` var ([32b9f38e](https://github.com/fvdm/nodejs-geolocation/commit/32b9f38e8afc28b6da746e881a494067eb578830)) | ||
* Update ESLint and gitignore ([1ce0feb4](https://github.com/fvdm/nodejs-geolocation/commit/1ce0feb4b1d51afeca4123a1896408003f6b3040)) | ||
* Update Travis node versions and env ([c8921089](https://github.com/fvdm/nodejs-geolocation/commit/c8921089f07b98630592ec5a830b3ad88b5c9669)) | ||
#### 1.0.2 (2017-12-12) | ||
@@ -2,0 +35,0 @@ |
84
index.js
/* | ||
Name: google-geolocation - index.js | ||
Description: Google Maps Geolocation API for Node.js (unofficial) | ||
Author: Franklin van de Meent (https://frankl.in) | ||
Author: Franklin (https://fvdm.com) | ||
License: Unlicense (public domain, see LICENSE file) | ||
Source & docs: https://github.com/fvdm/nodejs-geolocation | ||
Feedback & bugs: https://github.com/fvdm/nodejs-geolocation/issues | ||
*/ | ||
'use strict'; | ||
const { doRequest } = require ('httpreq'); | ||
const httpreq = require ('httpreq'); | ||
let config = { | ||
key: null, | ||
timeout: 5000 | ||
}; | ||
/** | ||
* Perform geolocation request | ||
* | ||
* @callback callback | ||
* @param {object} param Request body parameters | ||
* @param {function} callback `(err, data)` | ||
* @param {object} params Parameters | ||
* @param {string} params.key API key | ||
* @param {number} [params.timeout=5000] Request timeout in ms | ||
* | ||
* @return {void} | ||
* @return {Promise<object>} | ||
*/ | ||
function methodGeolocation (params, callback) { | ||
module.exports = async ({ | ||
key, | ||
timeout = 5000, | ||
}) => { | ||
delete arguments[0].key; | ||
delete arguments[0].timeout; | ||
const options = { | ||
method: 'POST', | ||
url: 'https://www.googleapis.com/geolocation/v1/geolocate?key=' + config.key, | ||
json: params, | ||
timeout: config.timeout | ||
url: `https://www.googleapis.com/geolocation/v1/geolocate?key=${key}`, | ||
json: arguments[0], | ||
timeout, | ||
}; | ||
httpreq.doRequest (options, (err, res) => { | ||
let data = res && res.body || ''; | ||
let error = null; | ||
const res = await doRequest (options); | ||
const data = JSON.parse (res.body); | ||
try { | ||
data = JSON.parse (data); | ||
} catch (e) { | ||
error = new Error ('invalid response'); | ||
error.error = e; | ||
} | ||
if (data.error) { | ||
const error = new Error (data.error.message); | ||
if (err) { | ||
error = new Error ('request failed'); | ||
error.error = err; | ||
} | ||
error.code = data.error.code; | ||
error.errors = data.error.errors; | ||
if (data && data.error) { | ||
error = new Error ('api error'); | ||
error.error = data.error.errors; | ||
} | ||
throw error; | ||
} | ||
if (error) { | ||
error.statusCode = res && res.statusCode; | ||
callback (error); | ||
} else { | ||
callback (null, data); | ||
} | ||
}); | ||
} | ||
/** | ||
* Module setup | ||
* | ||
* @param {object} set Configuration settings | ||
* @param {string} set.key Google API key | ||
* @param {int} [set.timeout=5000] Request timeout in ms | ||
* | ||
* @return {function} | ||
*/ | ||
module.exports = (set) => { | ||
config.key = set && set.key || null; | ||
config.timeout = set && set.timeout || config.timeout; | ||
return methodGeolocation; | ||
return data; | ||
}; |
{ | ||
"author": { | ||
"name": "Franklin van de Meent", | ||
"email": "fr@nkl.in", | ||
"url": "https://frankl.in" | ||
"name": "Franklin", | ||
"email": "info@fvdm.com", | ||
"url": "https://fvdm.com" | ||
}, | ||
"name": "google-geolocation", | ||
"description": "Google Maps Geolocation API for Node.js (unofficial)", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"repository": { | ||
@@ -19,9 +19,9 @@ "type": "git", | ||
"dependencies": { | ||
"httpreq": "^0.4.24" | ||
"httpreq": "^0.5.1" | ||
}, | ||
"devDependencies": { | ||
"dotest": "^2.1.0" | ||
"dotest": "^2.9.0" | ||
}, | ||
"engines": { | ||
"node": ">=4.0.0" | ||
"node": ">=12" | ||
}, | ||
@@ -28,0 +28,0 @@ "keywords": [ |
101
README.md
@@ -1,3 +0,2 @@ | ||
google-geolocation | ||
================== | ||
# google-geolocation | ||
@@ -9,56 +8,49 @@ Google Maps Geolocation API for Node.js (unofficial) | ||
[![npm](https://img.shields.io/npm/v/google-geolocation.svg?maxAge=3600)](https://github.com/fvdm/nodejs-geolocation/blob/master/CHANGELOG.md) | ||
[![Build Status](https://travis-ci.org/fvdm/nodejs-geolocation.svg?branch=master)](https://travis-ci.org/fvdm/nodejs-geolocation) | ||
[![Build Status](https://github.com/fvdm/nodejs-geolocation/actions/workflows/node.js.yml/badge.svg?branch=master)](https://github.com/fvdm/nodejs-geolocation/actions/workflows/node.js.yml) | ||
[![Coverage Status](https://coveralls.io/repos/github/fvdm/nodejs-geolocation/badge.svg?branch=master)](https://coveralls.io/github/fvdm/nodejs-geolocation?branch=master) | ||
[![bitHound Dependencies](https://www.bithound.io/github/fvdm/nodejs-geolocation/badges/master/dependencies.svg)](https://www.bithound.io/github/fvdm/nodejs-geolocation/master/dependencies/npm) | ||
[![bitHound Code](https://www.bithound.io/github/fvdm/nodejs-geolocation/badges/master/code.svg)](https://www.bithound.io/github/fvdm/nodejs-geolocation/master/files) | ||
[![Greenkeeper badge](https://badges.greenkeeper.io/fvdm/nodejs-geolocation.svg)](https://greenkeeper.io/) | ||
* [Node.js](https://nodejs.org) | ||
* [API documentation](https://developers.google.com/maps/documentation/geolocation/intro) | ||
Example | ||
------- | ||
## Example | ||
```js | ||
const geolocation = require ('google-geolocation') ({ | ||
key: 'api key' | ||
}); | ||
const geo = require ('google-geolocation'); | ||
// Configure API parameters | ||
const params = { | ||
wifiAccessPoints: [ | ||
{ | ||
macAddress: '01:23:45:67:89:AB', | ||
signalStrength: -65, | ||
signalToNoiseRatio: 40 | ||
} | ||
] | ||
}; | ||
function out (obj) { | ||
console.dir (obj, { | ||
depth: null, | ||
colors: true, | ||
}); | ||
} | ||
// Get data | ||
geolocation (params, (err, data) => { | ||
if (err) { | ||
console.log (err); | ||
return; | ||
} | ||
console.log (data); | ||
}); | ||
geo ({ | ||
key: 'abc123', | ||
wifiAccessPoints: [{ | ||
macAddress: '01:23:45:67:89:AB', | ||
signalStrength: -65, | ||
signalToNoiseRatio: 40, | ||
}], | ||
}) | ||
.then (out) | ||
.catch (err => { | ||
out (err); | ||
process.exit (1); | ||
}) | ||
; | ||
``` | ||
Installation | ||
------------ | ||
## Installation | ||
`npm i google-geolocation --save` | ||
`npm i google-geolocation` | ||
Configuration | ||
------------- | ||
## Configuration | ||
param | type | required | default | description | ||
:-------|:-------|:---------|:--------|:--------------------- | ||
key | string | yes | | Google API key | ||
timeout | number | no | 5000 | Request timeout in ms | ||
param | type | default | description | ||
:---------|:-------|:--------|:----------- | ||
key | string | | Google API key | ||
[timeout] | number | `5000` | Request timeout in ms | ||
@@ -69,5 +61,7 @@ | ||
```js | ||
const geolocation = require ('google-geolocation') ({ | ||
const geo = require ('google-geolocation'); | ||
geo ({ | ||
key: 'abc123', | ||
timeout: 2000 | ||
timeout: 2000, | ||
}); | ||
@@ -77,16 +71,4 @@ ``` | ||
Errors | ||
------ | ||
## Unlicense | ||
message | description | props | ||
:----------------|:-------------------------|:-------------------------------- | ||
api error | API returned an error | `.error` (array), `.statusCode` | ||
invalid response | API returns invalid data | `.error` (string), `.statusCode` | ||
request failed | Request failed | `.error` | ||
Unlicense | ||
--------- | ||
``` | ||
This is free and unencumbered software released into the public domain. | ||
@@ -114,12 +96,9 @@ | ||
OTHER DEALINGS IN THE SOFTWARE. | ||
``` | ||
For more information, please refer to <http://unlicense.org> | ||
For more information, please refer to <https://unlicense.org> | ||
Author | ||
------ | ||
## Author | ||
[Franklin van de Meent](https://frankl.in) | ||
[![Buy me a coffee](https://frankl.in/u/kofi/kofi-readme.png)](https://ko-fi.com/franklin) | ||
[Franklin](https://fvdm.com) | ||
| [Buy me a coffee](https://fvdm.com/donating) |
129
test.js
/* | ||
Name: google-geolocation - test.js | ||
Description: Google Maps Geolocation API for Node.js (unofficial) | ||
Author: Franklin van de Meent (https://frankl.in) | ||
Author: Franklin (https://fvdm.com) | ||
License: Unlicense (public domain, see LICENSE file) | ||
Source & docs: https://github.com/fvdm/nodejs-geolocation | ||
Feedback & bugs: https://github.com/fvdm/nodejs-geolocation/issues | ||
*/ | ||
'use strict'; | ||
const dotest = require ('dotest'); | ||
const pkg = require ('./'); | ||
const app = require ('./'); | ||
const config = { | ||
key: String (process.env.KEY), | ||
timeout: parseInt (process.env.TIMEOUT, 10) | ||
key: process.env.KEY, | ||
timeout: process.env.TIMEOUT || 5000, | ||
}; | ||
const geolocation = pkg && pkg (config); | ||
dotest.add ('Package', (test) => { | ||
dotest.add ('exports', async test => { | ||
test () | ||
.isFunction ('fail', 'exports', pkg) | ||
.isFunction ('fail', 'interface', geolocation) | ||
.done (); | ||
.isFunction ('fail', 'exports', app) | ||
.done () | ||
; | ||
}); | ||
dotest.add ('Run function', (test) => { | ||
const params = { | ||
wifiAccessPoints: [ | ||
{ | ||
macAddress: '01:23:45:67:89:AB', | ||
signalStrength: -65, | ||
signalToNoiseRatio: 40 | ||
} | ||
] | ||
}; | ||
dotest.add ('Function', async test => { | ||
let error; | ||
let data; | ||
// Get data | ||
geolocation (params, (err, data) => { | ||
const location = data && data.location; | ||
try { | ||
data = await app ({ | ||
key: config.key, | ||
timeout: config.timeout, | ||
wifiAccessPoints: [ | ||
{ | ||
macAddress: '01:23:45:67:89:AB', | ||
signalStrength: -65, | ||
signalToNoiseRatio: 40, | ||
}, | ||
], | ||
}); | ||
} | ||
catch (err) { | ||
error = err; | ||
} | ||
test (err) | ||
.isObject ('fail', 'data', data) | ||
.isNotEmpty ('fail', 'data', data) | ||
.isObject ('fail', 'data.location', location) | ||
.isNumber ('fail', 'data.location.lat', location && location.lat) | ||
.isNumber ('fail', 'data.location.lng', location && location.lng) | ||
.isNumber ('fail', 'data.accuracy', data && data.accuracy) | ||
.done (); | ||
}); | ||
}); | ||
const location = data && data.location; | ||
dotest.add ('Error: api error', (test) => { | ||
const params = { | ||
considerIp: false, | ||
carrier: 0 | ||
}; | ||
geolocation (params, (err, data) => { | ||
const errors = err && err.error; | ||
const error = errors && errors [0]; | ||
test () | ||
.isError ('fail', 'err', err) | ||
.isArray ('fail', 'err.error', errors) | ||
.isObject ('fail', 'err.error[0]', error) | ||
.isExactly ('fail', 'err.error[0].reason', error && error.reason, 'notFound') | ||
.isUndefined ('fail', 'data', data) | ||
.done (); | ||
}); | ||
test (error) | ||
.isObject ('fail', 'data', data) | ||
.isNotEmpty ('fail', 'data', data) | ||
.isObject ('fail', 'data.location', location) | ||
.isNumber ('fail', 'data.location.lat', location && location.lat) | ||
.isNumber ('fail', 'data.location.lng', location && location.lng) | ||
.isNumber ('fail', 'data.accuracy', data && data.accuracy) | ||
.done () | ||
; | ||
}); | ||
dotest.add ('Error: request failed', (test) => { | ||
const cnf = { | ||
key: config.key, | ||
timeout: 1 | ||
}; | ||
dotest.add ('API error', async test => { | ||
let error; | ||
let data; | ||
const tmp = pkg (cnf); | ||
try { | ||
data = await app ({ | ||
key: 'invalid', | ||
considerIp: false, | ||
carrier: 0, | ||
}); | ||
} | ||
catch (err) { | ||
error = err; | ||
} | ||
tmp (1, (err, data) => { | ||
const error = err && err.error; | ||
test () | ||
.isError ('fail', 'err', err) | ||
.isError ('fail', 'err.error', error) | ||
.isExactly ('fail', 'err.error.code', error && error.code, 'TIMEOUT') | ||
.isUndefined ('fail', 'data', data) | ||
.done (); | ||
}); | ||
test () | ||
.isError ('fail', 'error', error) | ||
.isNotEmpty ('fail', 'error.message', error && error.message) | ||
.isNumber ('fail', 'error.code', error && error.code) | ||
.isArray ('fail', 'error.errors', error && error.errors) | ||
.isUndefined ('fail', 'data', data) | ||
.done () | ||
; | ||
}); | ||
@@ -98,0 +85,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21924
11
111
101
+ Addedhttpreq@0.5.2(transitive)
- Removedhttpreq@0.4.24(transitive)
Updatedhttpreq@^0.5.1