@nexgen-os/mapbox 0.0.0-stage → 0.4.1
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 +21 -0
- package/README.md +70 -2
- package/dist/index.d.ts +22 -0
- package/dist/index.js +116 -0
- package/package.json +36 -4
- package/source.json +14 -0
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,71 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@nexgen-os/mapbox`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[Mapbox](https://www.mapbox.com) as a geocoder of Nexgen OS, on its
|
|
4
|
+
[Geocoding API v6](https://docs.mapbox.com/api/search/geocoding/): the
|
|
5
|
+
commercial service an installation pays for, asked after the register of the
|
|
6
|
+
country and the open fallback. MIT; the data is Mapbox's.
|
|
7
|
+
|
|
8
|
+
```ts
|
|
9
|
+
import { mapbox } from '@nexgen-os/mapbox';
|
|
10
|
+
|
|
11
|
+
const geocoder = mapbox({ accessToken }); // the token the connectors row names
|
|
12
|
+
await geocoder.search('rue de la loi 16', { country: 'be' }); // search as you type, best first
|
|
13
|
+
await geocoder.forward('Rue de la Loi 16, 1000 Bruxelles'); // the best place for a full address
|
|
14
|
+
await geocoder.reverse(4.3521, 50.8467); // the full address at a point
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Every answer is a place in the standard shape of
|
|
18
|
+
[`@nexgen-os/core`](../../core/src/address.ts): the address in the fields
|
|
19
|
+
every country has, the region in ISO 3166-2 (`region_code_full`), the point,
|
|
20
|
+
its precision and the source `mapbox`.
|
|
21
|
+
|
|
22
|
+
| Mapbox | `location_precision` |
|
|
23
|
+
|---|---|
|
|
24
|
+
| an address at `rooftop` or `point` accuracy | `rooftop` |
|
|
25
|
+
| an address at `parcel` accuracy | `parcel` |
|
|
26
|
+
| an address `interpolated` or `approximate`, a street | `street` |
|
|
27
|
+
| a postcode | `postal_code` |
|
|
28
|
+
| a place, a locality | `city` |
|
|
29
|
+
| a region, a country | `region` |
|
|
30
|
+
|
|
31
|
+
Mapbox does not publish the country's own municipality code: a place it
|
|
32
|
+
gives keeps `municipality_code` empty, and the country's register (`ban`,
|
|
33
|
+
`geopunt`, `urbis`, `icar`), asked first, is the one that fills it.
|
|
34
|
+
|
|
35
|
+
## In the chain
|
|
36
|
+
|
|
37
|
+
`geocoderChain()` of the core keeps the order the
|
|
38
|
+
[decision 0009](../../../docs/decisions/0009-what-applies-follows-the-region.md)
|
|
39
|
+
gives: the register of the country or its regions, then the open fallback
|
|
40
|
+
(`nominatim`, `photon`), then the commercial service. Which worldwide
|
|
41
|
+
geocoder is the commercial one is read from its `sources` row
|
|
42
|
+
(`redistributable = false`), not from the order it is named in.
|
|
43
|
+
|
|
44
|
+
## Terms, licence and retention
|
|
45
|
+
|
|
46
|
+
The package ships its `sources` row, [`source.json`](source.json)
|
|
47
|
+
(`mapboxSource`; write it with `registerSource()` of the core): kind `api`,
|
|
48
|
+
`auth = 'api_key'`, worldwide, not redistributable. Mapbox's terms tell two
|
|
49
|
+
modes apart:
|
|
50
|
+
|
|
51
|
+
- **temporary geocoding**, the default of the API: results may be shown, not
|
|
52
|
+
stored;
|
|
53
|
+
- **permanent geocoding** (`permanent=true`, billed per request): results may
|
|
54
|
+
be stored by the customer, never redistributed as a dataset.
|
|
55
|
+
|
|
56
|
+
A place a participant keeps is stored, so the connector asks the permanent
|
|
57
|
+
mode by default; `permanent: false` is for a lookup shown and thrown away.
|
|
58
|
+
`retention_days` is empty: in the permanent mode Mapbox sets no limit; check
|
|
59
|
+
the [product terms](https://www.mapbox.com/legal/service-terms) in force
|
|
60
|
+
for the installation's plan before relying on it.
|
|
61
|
+
|
|
62
|
+
## Configuration
|
|
63
|
+
|
|
64
|
+
An organisation's `connectors` row names the source `mapbox` and its access
|
|
65
|
+
token by `secret_ref`; the vault holds it, the caller resolves it and passes
|
|
66
|
+
it as `accessToken`. The token travels in the query string of each request
|
|
67
|
+
and is never written in an error.
|
|
68
|
+
|
|
69
|
+
## Licence
|
|
70
|
+
|
|
71
|
+
MIT.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Fetch, Geocoder, Place, SearchOptions, SourceRow } from '@nexgen-os/core';
|
|
2
|
+
export interface MapboxOptions {
|
|
3
|
+
/** The access token, resolved by the caller from the `secret_ref` of the organisation's `connectors` row; never stored. */
|
|
4
|
+
accessToken: string;
|
|
5
|
+
/**
|
|
6
|
+
* Permanent geocoding, the mode whose results may be stored (billed as
|
|
7
|
+
* such). Default true: a place is kept. False only for a lookup shown and
|
|
8
|
+
* thrown away.
|
|
9
|
+
*/
|
|
10
|
+
permanent?: boolean;
|
|
11
|
+
/** Default `https://api.mapbox.com`. */
|
|
12
|
+
baseUrl?: string;
|
|
13
|
+
fetch?: Fetch;
|
|
14
|
+
}
|
|
15
|
+
export interface MapboxGeocoder extends Geocoder {
|
|
16
|
+
/** The best place for an address typed in full (`autocomplete=false`), null when none. */
|
|
17
|
+
forward(text: string, options?: SearchOptions): Promise<Place | null>;
|
|
18
|
+
}
|
|
19
|
+
/** A geocoder on Mapbox. `source_code` is `mapbox`. */
|
|
20
|
+
export declare function mapbox(options: MapboxOptions): MapboxGeocoder;
|
|
21
|
+
/** The `sources` row this package ships (`source.json`): write it with `registerSource()` of the core. */
|
|
22
|
+
export declare const mapboxSource: SourceRow;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// Mapbox — https://www.mapbox.com, a commercial geocoder an installation pays
|
|
2
|
+
// for, as a connector, on the Geocoding API v6
|
|
3
|
+
// (https://docs.mapbox.com/api/search/geocoding/). Source `mapbox`, shipped
|
|
4
|
+
// with this package (`source.json`).
|
|
5
|
+
//
|
|
6
|
+
// In the chain of `geocoderFor()` it is the commercial service: asked after
|
|
7
|
+
// the register of the country and the open fallback found nothing. Its
|
|
8
|
+
// answers are kept only in the permanent mode its terms require for a
|
|
9
|
+
// result that is stored (`permanent=true`, the default here): a participant
|
|
10
|
+
// placed by it keeps the point.
|
|
11
|
+
import { readFileSync } from 'node:fs';
|
|
12
|
+
/** A geocoder on Mapbox. `source_code` is `mapbox`. */
|
|
13
|
+
export function mapbox(options) {
|
|
14
|
+
if (!options?.accessToken)
|
|
15
|
+
throw new Error('secret_not_found: mapbox needs the access token its connectors row names');
|
|
16
|
+
const base = (options.baseUrl ?? 'https://api.mapbox.com').replace(/\/+$/, '');
|
|
17
|
+
const doFetch = options.fetch ?? globalThis.fetch;
|
|
18
|
+
const common = () => {
|
|
19
|
+
const params = new URLSearchParams({ access_token: options.accessToken });
|
|
20
|
+
if (options.permanent ?? true)
|
|
21
|
+
params.set('permanent', 'true');
|
|
22
|
+
return params;
|
|
23
|
+
};
|
|
24
|
+
const forward = async (text, search, autocomplete, limit) => {
|
|
25
|
+
const params = common();
|
|
26
|
+
params.set('q', text);
|
|
27
|
+
params.set('limit', String(limit));
|
|
28
|
+
params.set('autocomplete', String(autocomplete));
|
|
29
|
+
params.set('types', 'address,street,postcode,place,locality,region');
|
|
30
|
+
if (search.country)
|
|
31
|
+
params.set('country', search.country.toLowerCase());
|
|
32
|
+
if (search.language)
|
|
33
|
+
params.set('language', search.language);
|
|
34
|
+
if (search.near)
|
|
35
|
+
params.set('proximity', `${search.near.lon},${search.near.lat}`);
|
|
36
|
+
const body = (await getJson(doFetch, `${base}/search/geocode/v6/forward?${params}`));
|
|
37
|
+
return (body.features ?? []).map(fromMapbox).filter((p) => p !== null);
|
|
38
|
+
};
|
|
39
|
+
return {
|
|
40
|
+
source_code: 'mapbox',
|
|
41
|
+
async search(text, search = {}) {
|
|
42
|
+
return forward(text, search, true, search.limit ?? 5);
|
|
43
|
+
},
|
|
44
|
+
async forward(text, search = {}) {
|
|
45
|
+
return (await forward(text, search, false, 1))[0] ?? null;
|
|
46
|
+
},
|
|
47
|
+
async reverse(lon, lat, search = {}) {
|
|
48
|
+
const params = common();
|
|
49
|
+
params.set('longitude', String(lon));
|
|
50
|
+
params.set('latitude', String(lat));
|
|
51
|
+
params.set('types', 'address');
|
|
52
|
+
params.set('limit', '1');
|
|
53
|
+
if (search.language)
|
|
54
|
+
params.set('language', search.language);
|
|
55
|
+
const body = (await getJson(doFetch, `${base}/search/geocode/v6/reverse?${params}`));
|
|
56
|
+
const first = body.features?.[0];
|
|
57
|
+
return first ? fromMapbox(first) : null;
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/** The `sources` row this package ships (`source.json`): write it with `registerSource()` of the core. */
|
|
62
|
+
export const mapboxSource = JSON.parse(readFileSync(new URL('../source.json', import.meta.url), 'utf8'));
|
|
63
|
+
function fromMapbox(feature) {
|
|
64
|
+
const p = feature.properties;
|
|
65
|
+
const c = p.context ?? {};
|
|
66
|
+
const country = c.country?.country_code?.toUpperCase();
|
|
67
|
+
if (!country)
|
|
68
|
+
return null;
|
|
69
|
+
const street = c.address?.name
|
|
70
|
+
?? ([c.address?.address_number, c.address?.street_name].filter(Boolean).join(' ') || null)
|
|
71
|
+
?? null;
|
|
72
|
+
const line1 = street ?? (p.feature_type === 'street' ? (p.name ?? null) : (c.street?.name ?? null));
|
|
73
|
+
const [lon, lat] = p.coordinates ? [p.coordinates.longitude, p.coordinates.latitude] : feature.geometry.coordinates;
|
|
74
|
+
const place = {
|
|
75
|
+
address: {
|
|
76
|
+
address_line1: line1,
|
|
77
|
+
address_line2: null,
|
|
78
|
+
postal_code: c.postcode?.name ?? null,
|
|
79
|
+
city: c.place?.name ?? c.locality?.name ?? null,
|
|
80
|
+
region: c.region?.region_code_full ?? null,
|
|
81
|
+
// Mapbox does not publish the country's own municipality code.
|
|
82
|
+
municipality_code: null,
|
|
83
|
+
country,
|
|
84
|
+
},
|
|
85
|
+
lon,
|
|
86
|
+
lat,
|
|
87
|
+
precision: precisionOf(p.feature_type, p.coordinates?.accuracy),
|
|
88
|
+
source_code: 'mapbox',
|
|
89
|
+
label: p.full_address ?? [p.name, p.place_formatted].filter(Boolean).join(', '),
|
|
90
|
+
};
|
|
91
|
+
return place;
|
|
92
|
+
}
|
|
93
|
+
/** How close the point is: the accuracy of an address point, else the kind of the feature. */
|
|
94
|
+
function precisionOf(type, accuracy) {
|
|
95
|
+
if (type === 'address') {
|
|
96
|
+
if (accuracy === 'rooftop' || accuracy === 'point')
|
|
97
|
+
return 'rooftop';
|
|
98
|
+
if (accuracy === 'parcel')
|
|
99
|
+
return 'parcel';
|
|
100
|
+
return 'street';
|
|
101
|
+
}
|
|
102
|
+
if (type === 'street')
|
|
103
|
+
return 'street';
|
|
104
|
+
if (type === 'postcode')
|
|
105
|
+
return 'postal_code';
|
|
106
|
+
if (type === 'region' || type === 'country')
|
|
107
|
+
return 'region';
|
|
108
|
+
return 'city';
|
|
109
|
+
}
|
|
110
|
+
async function getJson(doFetch, url) {
|
|
111
|
+
const response = await doFetch(url, { headers: { Accept: 'application/json' } });
|
|
112
|
+
// The token travels in the query: never in a message.
|
|
113
|
+
if (!response.ok)
|
|
114
|
+
throw new Error(`geocoder_unavailable: ${response.status} from ${url.replace(/access_token=[^&]*/, 'access_token=…')}`);
|
|
115
|
+
return response.json();
|
|
116
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nexgen-os/mapbox",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.4.1",
|
|
4
|
+
"description": "Mapbox (Geocoding API v6) as a Nexgen OS geocoder: forward, search as you type and reverse, the commercial service of the chain, in the permanent mode.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/nexgen-os/nexgen-os.git",
|
|
9
|
+
"directory": "packages/connectors/mapbox"
|
|
10
|
+
},
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public"
|
|
13
|
+
},
|
|
14
|
+
"type": "module",
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=20"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist",
|
|
20
|
+
"source.json",
|
|
21
|
+
"README.md",
|
|
22
|
+
"LICENSE"
|
|
23
|
+
],
|
|
24
|
+
"main": "./dist/index.js",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"import": "./dist/index.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "tsc -p tsconfig.build.json"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@nexgen-os/core": "^0.4.0"
|
|
37
|
+
}
|
|
38
|
+
}
|
package/source.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"code": "mapbox",
|
|
3
|
+
"name": "Mapbox Geocoding",
|
|
4
|
+
"description": "A commercial geocoder, worldwide: an address to a point and a point to its address, with house-number precision where Mapbox has it. Read by @nexgen-os/mapbox, in the permanent mode, the one whose results Mapbox's terms let a customer store; asked after the country's register and the open fallback.",
|
|
5
|
+
"kind": "api",
|
|
6
|
+
"base_url": "https://api.mapbox.com/search/geocode/v6",
|
|
7
|
+
"docs_url": "https://docs.mapbox.com/api/search/geocoding/",
|
|
8
|
+
"auth": "api_key",
|
|
9
|
+
"license": "Mapbox Terms of Service and product terms: results of temporary geocoding may not be stored; permanent geocoding (permanent=true, billed per request) may be stored by the customer; neither may be redistributed as a dataset",
|
|
10
|
+
"attribution": "© Mapbox",
|
|
11
|
+
"retention_days": null,
|
|
12
|
+
"redistributable": false,
|
|
13
|
+
"published_by": "@nexgen-os/mapbox"
|
|
14
|
+
}
|