Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

https support and more config #3

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
50 changes: 50 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -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}}
25 changes: 18 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -18,26 +17,38 @@ 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
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);
});
```
Expand Down Expand Up @@ -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);
});
```
Expand Down Expand Up @@ -106,7 +117,7 @@ Output:
boundingbox: [ '3.8696101', '3.8697112', '11.5237394', '11.5238284' ] }
```

## License
## License

(The MIT License)

Expand Down
65 changes: 42 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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));
},
};
77 changes: 77 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,9 @@
"devDependencies": {
"debug": "^2.2.0",
"should": "^11.2.0"
},
"engines": {
"node": ">=8",
"npm": ">= 6.13.0"
}
}