Skip to content

Commit

Permalink
Merge pull request #4 from spurreiter/break-remove-teleport
Browse files Browse the repository at this point in the history
Breaking Change: Remove teleport geocoder
  • Loading branch information
spurreiter authored Jul 13, 2024
2 parents 9011dcb + 34d4f7b commit 7f1a478
Show file tree
Hide file tree
Showing 60 changed files with 568 additions and 644 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

strategy:
matrix:
node-version: [18.x]
node-version: [20.x, 22.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
Expand Down
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ Features:
| [OsmGeocoder](https://nominatim.org/release-docs/develop/) |||| Searches nominatim.org |
| [PeliasGeocoder](https://github.com/pelias/documentation/blob/master/README.md) |||| Local or [Geocode.earth](https://geocode.earth/docs) |
| [PickpointGeocoder](https://pickpoint.io/api-reference) |||| Search Results based on OSM |
| [TeleportGeocoder](https://developers.teleport.org/api/resources/) |||| Searches only by city names, no addresses |
| [TomTomGeocoder](https://developer.tomtom.com/) |||| |
| [YandexGeocoder](https://yandex.com/dev/maps/geocoder/) |||| |

Expand All @@ -52,7 +51,7 @@ Features:
import { fetchAdapter, OsmGeocoder } from '@spurreiter/geocoder'

const adapter = fetchAdapter()
const geocoder = new OsmGeocoder(adapter, { language: 'en', limit: 5 })
const geocoder = new OsmGeocoder(adapter, { language: 'en', limit: 5, referer: 'https://mysite' })

const results = await geocoder.forward('135 pilkington avenue, birmingham')
// [
Expand Down Expand Up @@ -119,7 +118,7 @@ import { Cascade, fetchAdapter, HereGeocoder, OsmGeocoder } from '@spurreiter/ge
const language = "es"
const geocoders = [
new HereGeocoder(adapter, { apiKey: 'your-api-key', language }),
new OsmGeocoder(adapter, { language })
new OsmGeocoder(adapter, { language, referer: 'https://mysite' })
]
const cascade = new Cascade(geocoders)

Expand All @@ -139,7 +138,7 @@ import { Combine, fetchAdapter, HereGeocoder, OsmGeocoder } from '@spurreiter/ge

const geocoders = [
new HereGeocoder(adapter, { apiKey: 'your-api-key' }),
new OsmGeocoder(adapter)
new OsmGeocoder(adapter, { referer: 'https://mysite' })
]
const combine = new Combine(geocoders)

Expand Down
37 changes: 18 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@spurreiter/geocoder",
"version": "1.5.5",
"version": "2.0.0-0",
"description": "Node geocoding library, google maps, bing maps, mapquest, mapbox, here maps, arcgis, nominatim, ...",
"keywords": [
"geocoder",
Expand All @@ -17,7 +17,6 @@
"osm",
"pelias",
"pickpoint-api",
"teleport",
"yandex-api"
],
"homepage": "https://github.com/spurreiter/geocoder#readme",
Expand Down Expand Up @@ -50,9 +49,9 @@
"all": "npm-run-all clean test build lint types",
"clean": "rimraf types coverage lib",
"lint": "eslint --fix --ext js .",
"build": "rollup -c",
"build": "./scripts/version.cjs && rollup -c",
"test": "c8 mocha",
"types": "rimraf types; tsc --emitDeclarationOnly true --noEmitOnError && cp src/*.d.ts types"
"types": "rimraf types; tsc && cp src/*.d.ts types"
},
"mocha": {
"exit": true,
Expand All @@ -61,25 +60,25 @@
},
"dependencies": {
"agentkeepalive": "^4.5.0",
"got": "^13.0.0",
"i18n-iso-countries": "^7.7.0"
"got": "^14.4.1",
"i18n-iso-countries": "^7.11.2"
},
"devDependencies": {
"@types/node": "^20.8.7",
"c8": "^8.0.1",
"dotenv": "^16.3.1",
"eslint": "^8.52.0",
"@types/node": "^20.14.10",
"c8": "^9.1.0",
"dotenv": "^16.4.5",
"eslint": "^8.57.0",
"eslint-config-standard": "^17.1.0",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-n": "^16.2.0",
"eslint-plugin-promise": "^6.1.1",
"mocha": "^10.2.0",
"npm-run-all2": "^6.1.1",
"rimraf": "^5.0.5",
"rollup": "^4.1.4",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-n": "^16.6.2",
"eslint-plugin-promise": "^6.4.0",
"mocha": "^10.6.0",
"npm-run-all2": "^6.2.2",
"rimraf": "^5.0.9",
"rollup": "^4.18.1",
"serialize-to-module": "^1.1.0",
"sinon": "^17.0.0",
"typescript": "^5.2.2"
"sinon": "^17.0.2",
"typescript": "^5.5.3"
},
"engines": {
"node": ">=14"
Expand Down
13 changes: 13 additions & 0 deletions scripts/version.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env node

const fs = require('node:fs')
const path = require('node:path')
const { version } = require('../package.json')

const content = `export const version = '${version}'\n`

fs.writeFileSync(
path.resolve(__dirname, '..', 'src/version.js'),
content,
'utf-8'
)
4 changes: 2 additions & 2 deletions src/adapter.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// @ts-expect-error
import got from 'got'
// @ts-expect-error
import HttpAgent from 'agentkeepalive'
import { isNumber } from './utils/index.js'
import { version } from './version.js'
const { HttpsAgent } = HttpAgent

/** @typedef {import('./types').AdapterOptions} AdapterOptions */
Expand Down Expand Up @@ -35,7 +35,7 @@ export function fetchAdapter (opts) {
}

if (!globalOpts.headers[USER_AGENT]) {
globalOpts.headers[USER_AGENT] = 'Mozilla/5.0 (X11; Linux x86_64; rv:88.0) Gecko/20100101 Firefox/88.0'
globalOpts.headers[USER_AGENT] = `geocoder/${version}`
}

return (url, options = {}) => {
Expand Down
1 change: 0 additions & 1 deletion src/geocoder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ export * from './openmapquest.js'
export * from './osm.js'
export * from './pelias.js'
export * from './pickpoint.js'
export * from './teleport.js'
export * from './tomtom.js'
export * from './yandex.js'
2 changes: 1 addition & 1 deletion src/geocoder/mapquest.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class MapQuestGeocoder extends AbstractGeocoder {
// @ts-ignore
super(adapter, options)

const { apiKey, licensed, limit: maxResults, ...params } = options
const { apiKey, licensed = true, limit: maxResults, ...params } = options

if (!apiKey) {
throw new Error(`You must specify apiKey to use ${this.constructor.name}`)
Expand Down
2 changes: 1 addition & 1 deletion src/geocoder/openmapquest.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class OpenMapQuestGeocoder extends OsmGeocoder {
* @param {string} [options.language]
*/
constructor (adapter, options = { apiKey: '' }) {
super(adapter, options)
super(adapter, { ...options, needsReferer: false })

const { apiKey, ...params } = options

Expand Down
27 changes: 21 additions & 6 deletions src/geocoder/osm.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ function mapParams (params) {
}

function hasResult (result) {
return (Array.isArray(result) && result.length && result[0].osm_type) || (result && result.osm_type)
return (
(Array.isArray(result) && result.length && result[0].osm_type) ||
(result && result.osm_type)
)
}

function mapToNumber (arr) {
return Array.isArray(arr) ? arr.map(n => +n) : undefined
return Array.isArray(arr) ? arr.map((n) => +n) : undefined
}

function toBbox (boundingbox) {
Expand Down Expand Up @@ -61,6 +64,7 @@ export class OsmGeocoder extends AbstractGeocoder {
* @see https://nominatim.org/release-docs/develop/api/Search/
* @param {fetchAdapterFn} adapter
* @param {object} options
* @param {string} [options.referer] referer header
* @param {number} [options.limit=10]
* @param {string} [options.language]
* @param {number} [options.addressdetails]
Expand All @@ -70,8 +74,9 @@ export class OsmGeocoder extends AbstractGeocoder {
* @param {number} [options.dedupe]
* @param {string} [options.endpoint] custom endpoint
* @param {string} [options.revEndpoint] custom reverse endpoint
* @param {boolean} [options.needsReferer]
*/
constructor (adapter, options = {}) {
constructor (adapter, options) {
// @ts-ignore
super(adapter, options)

Expand All @@ -80,11 +85,21 @@ export class OsmGeocoder extends AbstractGeocoder {
revEndpoint = 'https://nominatim.openstreetmap.org/reverse',
// @ts-ignore
apiKey,
referer,
needsReferer = true,
...params
} = options
} = options || {}

if (needsReferer && !referer) {
throw new Error(
'Nominatim Usage Policy requires Referer header; ' +
'https://operations.osmfoundation.org/policies/nominatim/'
)
}

this.endpoint = endpoint
this.revEndpoint = revEndpoint
this.fetchopts = referer ? { headers: { referer } } : undefined

this.params = {
...params,
Expand All @@ -108,7 +123,7 @@ export class OsmGeocoder extends AbstractGeocoder {
params = mapParams(params)
const url = this.createUrl(this.endpoint, params)

const res = await this.adapter(url)
const res = await this.adapter(url, this.fetchopts)
if (res.status !== 200) {
throw HttpError(res)
}
Expand All @@ -130,7 +145,7 @@ export class OsmGeocoder extends AbstractGeocoder {

const url = this.createUrl(this.revEndpoint, params)

const res = await this.adapter(url)
const res = await this.adapter(url, this.fetchopts)
if (res.status !== 200) {
throw HttpError(res)
}
Expand Down
9 changes: 4 additions & 5 deletions src/geocoder/pickpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,12 @@ export class PickpointGeocoder extends OsmGeocoder {
* @param {number} [options.limit=10]
* @param {string} [options.language]
*/
constructor (adapter, options = { apiKey: '' }) {
super(adapter, options)

const { apiKey, ...params } = options
constructor (adapter, options) {
super(adapter, { ...options, needsReferer: false })
const { apiKey, ...params } = options || {}

if (!apiKey) {
throw new Error(`You must specify apiKey to use ${this.constructor.name}`)
throw new Error('You must specify apiKey to use PickpointGeocoder')
}

this.endpoint = 'https://api.pickpoint.io/v1/forward'
Expand Down
Loading

0 comments on commit 7f1a478

Please sign in to comment.