@nexgen-os/nominatim 0.0.0-stage → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Nexgen OS contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,15 @@
1
- # Temporary Holding Version
1
+ # `@nexgen-os/nominatim` — OpenStreetMap geocoder
2
+
3
+ An address to a point and a point to an address on [Nominatim](https://nominatim.org), worldwide, with the ISO 3166-2 region. Source `nominatim`. MIT; the data is OpenStreetMap's, under the ODbL. The public instance asks for a user agent that names the application and one request per second; an installation running its own Nominatim may give any base URL.
4
+
5
+ ```ts
6
+ import { nominatim } from '@nexgen-os/nominatim';
7
+
8
+ const geocoder = nominatim({ userAgent: 'my-app (contact@example.org)' });
9
+ const [place] = await geocoder.search('Rue de la Loi 12, Bruxelles');
10
+ ```
11
+
12
+ The place is the standard shape of `@nexgen-os/core` (address, WGS 84 point,
13
+ precision, source); `placeFields(place)` gives the place columns of `participants` it fills.
14
+ Decision [0009](../../../docs/decisions/0009-what-applies-follows-the-region.md).
2
15
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
@@ -0,0 +1,14 @@
1
+ import type { Fetch, Geocoder } from '@nexgen-os/core';
2
+ export interface NominatimOptions {
3
+ /**
4
+ * Required by the public instance's usage policy: a user agent that names
5
+ * the application. An installation running its own Nominatim may give
6
+ * anything.
7
+ */
8
+ userAgent: string;
9
+ /** Default: the public instance, one request per second. */
10
+ baseUrl?: string;
11
+ fetch?: Fetch;
12
+ }
13
+ /** A geocoder on Nominatim. `source_code` is `nominatim`. */
14
+ export declare function nominatim(options: NominatimOptions): Geocoder;
package/dist/index.js ADDED
@@ -0,0 +1,86 @@
1
+ // Nominatim — https://nominatim.org, the OpenStreetMap geocoder, as a connector.
2
+ // Source `nominatim`; the data is OpenStreetMap's (ODbL).
3
+ /** A geocoder on Nominatim. `source_code` is `nominatim`. */
4
+ export function nominatim(options) {
5
+ if (!options.userAgent || options.userAgent.trim() === '') {
6
+ throw new Error('user_agent_required: Nominatim asks every client to name itself');
7
+ }
8
+ const base = trimSlash(options.baseUrl ?? 'https://nominatim.openstreetmap.org');
9
+ const doFetch = options.fetch ?? globalThis.fetch;
10
+ const headers = { 'User-Agent': options.userAgent };
11
+ return {
12
+ source_code: 'nominatim',
13
+ async search(text, search = {}) {
14
+ const params = new URLSearchParams({ q: text, format: 'jsonv2', addressdetails: '1', limit: String(search.limit ?? 5) });
15
+ if (search.country)
16
+ params.set('countrycodes', search.country.toLowerCase());
17
+ if (search.language)
18
+ params.set('accept-language', search.language);
19
+ if (search.near)
20
+ params.set('viewbox', viewbox(search.near));
21
+ const rows = (await getJson(doFetch, `${base}/search?${params}`, headers));
22
+ return rows.map(fromNominatim).filter((p) => p !== null);
23
+ },
24
+ async reverse(lon, lat, search = {}) {
25
+ const params = new URLSearchParams({ lat: String(lat), lon: String(lon), format: 'jsonv2', addressdetails: '1', zoom: '18' });
26
+ if (search.language)
27
+ params.set('accept-language', search.language);
28
+ const row = (await getJson(doFetch, `${base}/reverse?${params}`, headers));
29
+ return row.error ? null : fromNominatim(row);
30
+ },
31
+ };
32
+ }
33
+ function fromNominatim(row) {
34
+ const a = row.address ?? {};
35
+ const country = a.country_code?.toUpperCase();
36
+ if (!country)
37
+ return null;
38
+ const road = a.road ?? a.pedestrian ?? a.footway ?? a.path;
39
+ const street = [road, a.house_number].filter(Boolean).join(' ') || null;
40
+ const city = a.city ?? a.town ?? a.village ?? a.municipality ?? a.hamlet ?? null;
41
+ const type = row.addresstype ?? '';
42
+ const precision = a.house_number
43
+ ? 'rooftop'
44
+ : type === 'building' || type === 'house' || type === 'amenity' || type === 'shop' || type === 'office'
45
+ ? 'rooftop'
46
+ : type === 'road' || type === 'street' || road
47
+ ? 'street'
48
+ : type === 'postcode'
49
+ ? 'postal_code'
50
+ : type === 'state' || type === 'county' || type === 'region' || type === 'province'
51
+ ? 'region'
52
+ : 'city';
53
+ const place = {
54
+ address: { address_line1: street, address_line2: null, postal_code: a.postcode ?? null, city, region: isoRegion(a), country },
55
+ lon: Number(row.lon),
56
+ lat: Number(row.lat),
57
+ precision,
58
+ source_code: 'nominatim',
59
+ label: row.display_name,
60
+ };
61
+ if (row.osm_type && row.osm_id !== undefined)
62
+ place.identifier = { scheme: 'osm', value: `${row.osm_type}/${row.osm_id}` };
63
+ return place;
64
+ }
65
+ /** The ISO 3166-2 code Nominatim gives per administrative level: the highest level present. */
66
+ function isoRegion(address) {
67
+ const levels = Object.keys(address)
68
+ .map((key) => /^ISO3166-2-lvl(\d+)$/.exec(key))
69
+ .filter((m) => m !== null)
70
+ .map((m) => Number(m[1]))
71
+ .sort((x, y) => x - y);
72
+ return levels.length ? address[`ISO3166-2-lvl${levels[0]}`] : null;
73
+ }
74
+ function viewbox(near) {
75
+ const d = 0.1;
76
+ return [near.lon - d, near.lat + d, near.lon + d, near.lat - d].join(',');
77
+ }
78
+ async function getJson(doFetch, url, headers) {
79
+ const response = await doFetch(url, { headers: { Accept: 'application/json', ...headers } });
80
+ if (!response.ok)
81
+ throw new Error(`geocoder_unavailable: ${response.status} from ${url}`);
82
+ return response.json();
83
+ }
84
+ function trimSlash(url) {
85
+ return url.replace(/\/+$/, '');
86
+ }
package/package.json CHANGED
@@ -1,6 +1,37 @@
1
1
  {
2
2
  "name": "@nexgen-os/nominatim",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.4.0",
4
+ "description": "Nominatim (OpenStreetMap) as a Nexgen OS geocoder.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/nexgen-os/nexgen-os.git",
9
+ "directory": "packages/connectors/nominatim"
10
+ },
11
+ "publishConfig": {
12
+ "access": "public"
13
+ },
14
+ "type": "module",
15
+ "engines": {
16
+ "node": ">=20"
17
+ },
18
+ "files": [
19
+ "dist",
20
+ "README.md",
21
+ "LICENSE"
22
+ ],
23
+ "main": "./dist/index.js",
24
+ "types": "./dist/index.d.ts",
25
+ "exports": {
26
+ ".": {
27
+ "types": "./dist/index.d.ts",
28
+ "import": "./dist/index.js"
29
+ }
30
+ },
31
+ "scripts": {
32
+ "build": "tsc -p tsconfig.build.json"
33
+ },
34
+ "dependencies": {
35
+ "@nexgen-os/core": "^0.4.0"
36
+ }
37
+ }