diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml new file mode 100644 index 0000000..410654c --- /dev/null +++ b/.github/workflows/npm-publish.yml @@ -0,0 +1,50 @@ +# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created +# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages + +name: Node.js Package + +on: + release: + types: [created] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: 12 + - run: npm ci + - run: npm test + + publish-npm: + needs: build + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: 12 + registry-url: https://registry.npmjs.org/ + - run: npm ci + - run: npm publish + env: + NODE_AUTH_TOKEN: ${{secrets.npm_token}} + + publish-gpr: + needs: build + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: 12 + registry-url: https://npm.pkg.github.com/ + - run: npm ci + - run: npm publish + env: + NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}} diff --git a/README.md b/README.md index cfacaf9..ae3374a 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,7 @@ _nominatim-client_ is a basic node module to handle geocoding and reverse geocoding via [OpenStreetMap](http://openstreetmap.org/) (OSM). It attempts to adhere to the [Nominatim usage policy](http://wiki.openstreetmap.org/wiki/Nominatim_usage_policy). [![Build Status](https://travis-ci.org/demsking/nominatim-client.svg?branch=master)](https://travis-ci.org/demsking/nominatim-client) -[![bitHound Overall Score](https://www.bithound.io/github/demsking/nominatim-client/badges/score.svg)](https://www.bithound.io/github/demsking/nominatim-client) -[![bitHound Dependencies](https://www.bithound.io/github/demsking/nominatim-client/badges/dependencies.svg)](https://www.bithound.io/github/demsking/nominatim-client/master/dependencies/npm) + ## Installation @@ -18,9 +17,14 @@ var nominatim = require('nominatim-client'); // Set the global settings here nominatim.global({ + globalQueryElements: { useragent: "MyApp", // The name of your application referer: 'http://example.com', // The referer link email: 'user@example.com' // The valid email + }, + customPath: "/nominatim", // exemple for server + customPort: 1234, // or (search) https://localhost:1234/nominatim/?... + customHost: "localhost", // or (reverse) https://localhost:1234/nominatim/reverse?... }); // The query @@ -28,16 +32,23 @@ var query = { q: 'Avenue Monseigneur Vogt, Yaounde, Cameroon', addressdetails: '1' }; + +// The options +var options = { + headers: { + api_key: "My_Super_Api_Key_To_Access_The_Server" + } +}; ``` ### Search ```js -nominatim.search(query, function(err, data) { +nominatim.search(query, options, function(err, data) { if (err) { throw err; } - + console.log(data); }); ``` @@ -74,11 +85,11 @@ query = { lon: 11.523433 }; -nominatim.reverse(query, function (err, data) { +nominatim.reverse(query, options, function (err, data) { if (err) { throw err; } - + console.log(data); }); ``` @@ -106,7 +117,7 @@ Output: boundingbox: [ '3.8696101', '3.8697112', '11.5237394', '11.5238284' ] } ``` -## License +## License (The MIT License) diff --git a/index.js b/index.js index 8a390c8..0c75768 100644 --- a/index.js +++ b/index.js @@ -1,35 +1,40 @@ 'use strict'; -var http = require('http'); +var https = require('https'); -const API_ENDPOINT = 'nominatim.openstreetmap.org'; +var API_HOST = 'nominatim.openstreetmap.org'; var global = {}; +var API_PORT = 443; +var API_PATH = ''; + var to_encode_uri = function(params, done) { params.format = params.format || 'json'; params.useragent = params.useragent || 'NodeJS request'; - + var params_query = []; - + for (let i in global) { params_query.push(i + '=' + encodeURIComponent(global[i])); } - + for (let i in params) { params_query.push(i + '=' + encodeURIComponent(params[i])); } - + return params_query.join('&'); }; -var query = function(path, done) { - http.get({ - host: API_ENDPOINT, - path: path +var query = function(path,options, done) { + https.get({ + host: API_HOST, + port: API_PORT, + path: API_PATH + path, + headers: options.headers || {}, }, function(res) { var output = ''; - + res.setEncoding('utf8'); res.on('data', function (chunk) { @@ -49,27 +54,41 @@ var query_done = function(params, done) { if (err) { return done(err); } - + if (params.format == 'json') { - data = JSON.parse(data); + try { + data = JSON.parse(data); + } catch(e) { + console.error('ERROR parsing json : '+e); + return done(e); + } } - + done(false, data, path); }; }; module.exports = { - global: function(globals, value) { - global = globals; + global: function(globals ) { + if (typeof globals === 'undefined' || globals === null) { + // variable is undefined or null + //just do nothing + } else { + global = globals.globalQueryElements || {}; + API_HOST = globals.customHost || API_HOST; + API_PORT = globals.customPort || API_PORT; + API_PATH = globals.customPath || API_PATH; + } + }, - - search: function(params, done) { - query('/?' + to_encode_uri(params), query_done(params, done)); + + search: function(params,options, done) { + query('/search?' + to_encode_uri(params), options,query_done(params, done)); }, - - reverse: function(params, done) { + + reverse: function(params,options, done) { params.zoom = params.zoom || 18; - - query('/reverse?' + to_encode_uri(params), query_done(params, done)); + + query('/reverse?' + to_encode_uri(params), options,query_done(params, done)); }, }; diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..fd27c73 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,77 @@ +{ + "name": "nominatim-client", + "version": "1.0.1", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "debug": { + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "dev": true, + "requires": { + "ms": "2.0.0" + } + }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=", + "dev": true + }, + "should": { + "version": "11.2.1", + "resolved": "https://registry.npmjs.org/should/-/should-11.2.1.tgz", + "integrity": "sha1-kPVRRVUtAc/CAGZuToGKHJZw7aI=", + "dev": true, + "requires": { + "should-equal": "^1.0.0", + "should-format": "^3.0.2", + "should-type": "^1.4.0", + "should-type-adaptors": "^1.0.1", + "should-util": "^1.0.0" + } + }, + "should-equal": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/should-equal/-/should-equal-1.0.1.tgz", + "integrity": "sha1-C26VFvJgGp+wuy3MNpr6HH4gCvc=", + "dev": true, + "requires": { + "should-type": "^1.0.0" + } + }, + "should-format": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/should-format/-/should-format-3.0.3.tgz", + "integrity": "sha1-m/yPdPo5IFxT04w01xcwPidxJPE=", + "dev": true, + "requires": { + "should-type": "^1.3.0", + "should-type-adaptors": "^1.0.1" + } + }, + "should-type": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/should-type/-/should-type-1.4.0.tgz", + "integrity": "sha1-B1bYzoRt/QmEOmlHcZ36DUz/XPM=", + "dev": true + }, + "should-type-adaptors": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/should-type-adaptors/-/should-type-adaptors-1.1.0.tgz", + "integrity": "sha512-JA4hdoLnN+kebEp2Vs8eBe9g7uy0zbRo+RMcU0EsNy+R+k049Ki+N5tT5Jagst2g7EAja+euFuoXFCa8vIklfA==", + "dev": true, + "requires": { + "should-type": "^1.3.0", + "should-util": "^1.0.0" + } + }, + "should-util": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/should-util/-/should-util-1.0.1.tgz", + "integrity": "sha512-oXF8tfxx5cDk8r2kYqlkUJzZpDBqVY/II2WhvU0n9Y3XYvAYRmeaf1PvvIvTgPnv4KJ+ES5M0PyDq5Jp+Ygy2g==", + "dev": true + } + } +} diff --git a/package.json b/package.json index 6dde9fb..a4f0c3c 100644 --- a/package.json +++ b/package.json @@ -27,5 +27,9 @@ "devDependencies": { "debug": "^2.2.0", "should": "^11.2.0" + }, + "engines": { + "node": ">=8", + "npm": ">= 6.13.0" } }