@nways/maps 0.0.0-stage → 1.0.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/CHANGELOG.md +21 -0
- package/LICENSE +202 -0
- package/NOTICE +29 -0
- package/README.md +78 -2
- package/autocomplete.d.ts +31 -0
- package/autocomplete.js +185 -0
- package/index.d.ts +30 -0
- package/index.js +154 -0
- package/package.json +71 -4
- package/services.d.ts +164 -0
- package/services.js +206 -0
- package/style.d.ts +27 -0
- package/style.js +228 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
`@nways/maps` follows [semantic versioning](https://semver.org): within 1.x, nothing is removed or changed in a way
|
|
4
|
+
that breaks working code. The full history is at https://nways.ng/developers/changelog.
|
|
5
|
+
|
|
6
|
+
## 1.0.0 (2026-10-15)
|
|
7
|
+
|
|
8
|
+
- Maps use N-Ways' hosted styles (`/maps/v1/styles/streets-light.json`, `streets-dark.json`) by default, so style
|
|
9
|
+
fixes reach your map without a new release. `style: 'bundled'` keeps the built-in style; the map falls back to it
|
|
10
|
+
if the hosted style can't be loaded.
|
|
11
|
+
- A note over the map (and the reason in the console) when N-Ways refuses the key; `showErrors: false` to turn it off.
|
|
12
|
+
- `createClient({ key })`: geocode, reverse, postcode, ghanapost, autocomplete, validateAddress, placesNearby,
|
|
13
|
+
placesSearch, place, route, matrix, optimize, fares, traffic and `staticMapUrl`; errors as `ApiError`.
|
|
14
|
+
- `attachAutocomplete(input, options)`: an accessible search box with N-Ways suggestions and session tokens.
|
|
15
|
+
- `styleUrl()`, `STYLE_IDS`, `sessionToken()`, `staticMapUrl()`; types for everything.
|
|
16
|
+
- Open source under the Apache License 2.0 (`LICENSE`, `NOTICE`); map data stays © OpenStreetMap contributors (ODbL).
|
|
17
|
+
- Served at `https://nways.ng/sdk/v1/` (latest 1.x) and `/sdk/v1.0.0/`, with a standalone build that includes MapLibre.
|
|
18
|
+
|
|
19
|
+
## 0.1.1 (preview)
|
|
20
|
+
|
|
21
|
+
- `createMap`, `setTheme`, `buildStyle`, `withKey`. Still served at `https://nways.ng/sdk/v0/nways-maps.js`.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
N-Ways Maps SDK
|
|
2
|
+
Copyright 2026 N-WAYS LIMITED
|
|
3
|
+
|
|
4
|
+
This product (@nways/maps) is licensed under the Apache License, Version 2.0
|
|
5
|
+
(see LICENSE).
|
|
6
|
+
|
|
7
|
+
Third-party software
|
|
8
|
+
--------------------
|
|
9
|
+
|
|
10
|
+
The npm package does not bundle third-party code. It expects these as peer
|
|
11
|
+
dependencies, each under its own licence:
|
|
12
|
+
|
|
13
|
+
MapLibre GL JS BSD 3-Clause License, Copyright (c) 2023, MapLibre contributors
|
|
14
|
+
https://github.com/maplibre/maplibre-gl-js/blob/main/LICENSE.txt
|
|
15
|
+
pmtiles BSD 3-Clause License, Copyright (c) Protomaps LLC
|
|
16
|
+
https://github.com/protomaps/PMTiles/blob/main/LICENSE
|
|
17
|
+
|
|
18
|
+
The standalone build served at https://nways.ng/sdk/v1/ bundles MapLibre GL JS,
|
|
19
|
+
pmtiles and fflate (MIT License, Copyright (c) Arjun Barrett); their full
|
|
20
|
+
licence texts are in LICENSES.txt next to it.
|
|
21
|
+
|
|
22
|
+
Map data
|
|
23
|
+
--------
|
|
24
|
+
|
|
25
|
+
The SDK is software only. The maps and data it shows come from N-Ways and are
|
|
26
|
+
built on OpenStreetMap: © OpenStreetMap contributors, available under the Open
|
|
27
|
+
Database License (ODbL) 1.0, https://www.openstreetmap.org/copyright. They are
|
|
28
|
+
not covered by the Apache License. Keep the credit "N-Ways · © OpenStreetMap
|
|
29
|
+
contributors" visible wherever the data is shown.
|
package/README.md
CHANGED
|
@@ -1,3 +1,79 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @nways/maps
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The N-Ways Maps SDK for the web: [MapLibre GL JS](https://maplibre.org) maps of Nigeria, Ghana, Benin and Togo with
|
|
4
|
+
N-Ways' hosted style, vector tiles and fonts, your API key on every request, and small helpers for the N-Ways APIs
|
|
5
|
+
(geocoding, postcodes, places, routes, fares, static maps) plus an address autocomplete box.
|
|
6
|
+
|
|
7
|
+
Docs: https://nways.ng/developers/maps-sdk · Versions: [CHANGELOG.md](CHANGELOG.md) · Licence: Apache-2.0
|
|
8
|
+
|
|
9
|
+
Every request needs an N-Ways API key: see [getting started](https://nways.ng/developers/get-started).
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npm install @nways/maps maplibre-gl@6 pmtiles@4
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Or without a build step, from N-Ways (MapLibre included):
|
|
18
|
+
|
|
19
|
+
```html
|
|
20
|
+
<link rel="stylesheet" href="https://nways.ng/sdk/v1/maplibre-gl.css" />
|
|
21
|
+
<script type="module">
|
|
22
|
+
import { createMap } from 'https://nways.ng/sdk/v1/nways-maps.standalone.js'
|
|
23
|
+
</script>
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
`/sdk/v1/` follows the latest 1.x release; `/sdk/v1.0.0/` never changes.
|
|
27
|
+
|
|
28
|
+
## A map
|
|
29
|
+
|
|
30
|
+
```js
|
|
31
|
+
import 'maplibre-gl/dist/maplibre-gl.css'
|
|
32
|
+
import { createMap, setTheme } from '@nways/maps'
|
|
33
|
+
|
|
34
|
+
const map = createMap({ container: 'map', key: 'YOUR_API_KEY', center: [3.3792, 6.5244], zoom: 12 })
|
|
35
|
+
setTheme(map, 'dark')
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
`createMap` returns a MapLibre `Map`. Options: MapLibre's, plus `key`, `theme` (`light`/`dark`), `lang` (`en`/`fr`),
|
|
39
|
+
`style` (`'hosted'` (default), `'bundled'`, or your own) and `showErrors`.
|
|
40
|
+
|
|
41
|
+
## The APIs
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
import { createClient } from '@nways/maps'
|
|
45
|
+
|
|
46
|
+
const nways = createClient({ key: 'YOUR_API_KEY' })
|
|
47
|
+
const { results } = await nways.geocode('Allen Avenue, Ikeja', { near: { lat: 6.6, lng: 3.35 } })
|
|
48
|
+
const { routes } = await nways.route({ origin: results[0].location, destination: { postcode: '101233' }, mode: 'okada' })
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Errors throw `ApiError` with `status`, `reason` (`API_KEY_HTTP_REFERRER_BLOCKED`, `RATE_LIMIT_EXCEEDED`, …) and
|
|
52
|
+
`retryAfter`.
|
|
53
|
+
|
|
54
|
+
## Autocomplete
|
|
55
|
+
|
|
56
|
+
```js
|
|
57
|
+
import { attachAutocomplete } from '@nways/maps'
|
|
58
|
+
|
|
59
|
+
attachAutocomplete(document.querySelector('#address'), {
|
|
60
|
+
key: 'YOUR_API_KEY',
|
|
61
|
+
onSelect: (place) => map.flyTo({ center: [place.location.lng, place.location.lat], zoom: 16 }),
|
|
62
|
+
})
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Attribution
|
|
66
|
+
|
|
67
|
+
Keep the map's credit visible: **N-Ways · © OpenStreetMap contributors · Protomaps**. OpenStreetMap's licence (ODbL)
|
|
68
|
+
requires it wherever its data is shown. https://nways.ng/developers/terms#attribution
|
|
69
|
+
|
|
70
|
+
## Licence
|
|
71
|
+
|
|
72
|
+
The SDK's code is licensed under the [Apache License, Version 2.0](LICENSE) (`Apache-2.0`), Copyright 2026 N-WAYS
|
|
73
|
+
LIMITED; see [NOTICE](NOTICE). MapLibre GL JS and pmtiles, its peer dependencies, are under their own BSD 3-Clause
|
|
74
|
+
licences.
|
|
75
|
+
|
|
76
|
+
The licence covers the code, not the maps and data it loads. N-Ways maps and data are built on OpenStreetMap,
|
|
77
|
+
© OpenStreetMap contributors, available under the [Open Database License (ODbL)](https://www.openstreetmap.org/copyright),
|
|
78
|
+
and keep that credit wherever they are shown. Calling the N-Ways APIs needs an API key and is governed by the
|
|
79
|
+
[N-Ways Maps Platform terms](https://nways.ng/developers/terms).
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright 2026 N-WAYS LIMITED
|
|
3
|
+
import type { Client, LatLng, Suggestion } from './services.js'
|
|
4
|
+
|
|
5
|
+
export interface AutocompleteOptions {
|
|
6
|
+
key?: string
|
|
7
|
+
origin?: string
|
|
8
|
+
lang?: 'en' | 'fr'
|
|
9
|
+
/** Rank suggestions near this point. */
|
|
10
|
+
near?: LatLng
|
|
11
|
+
/** ISO country codes: NG, GH, BJ, TG. */
|
|
12
|
+
country?: string | string[]
|
|
13
|
+
types?: string | string[]
|
|
14
|
+
/** At most this many suggestions (default 6). */
|
|
15
|
+
limit?: number
|
|
16
|
+
/** Characters typed before searching (default 2). */
|
|
17
|
+
minLength?: number
|
|
18
|
+
/** Milliseconds to wait after the last key (default 200). */
|
|
19
|
+
delay?: number
|
|
20
|
+
onSelect?: (suggestion: Suggestion) => void
|
|
21
|
+
/** Reuse a client made by createClient. */
|
|
22
|
+
client?: Client
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface AutocompleteHandle {
|
|
26
|
+
close(): void
|
|
27
|
+
destroy(): void
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function attachAutocomplete(input: HTMLInputElement, options?: AutocompleteOptions): AutocompleteHandle
|
|
31
|
+
export function suggestionsOf(data: unknown): Suggestion[]
|
package/autocomplete.js
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright 2026 N-WAYS LIMITED
|
|
3
|
+
// An address and place search box for any <input>: suggestions from N-Ways
|
|
4
|
+
// as people type (NIPOST postcodes, GhanaPost codes, streets, businesses),
|
|
5
|
+
// with the keyboard and screen readers handled (the ARIA combobox pattern).
|
|
6
|
+
//
|
|
7
|
+
// attachAutocomplete(document.querySelector('#address'), {
|
|
8
|
+
// key: 'YOUR_API_KEY',
|
|
9
|
+
// near: { lat: 6.5244, lng: 3.3792 },
|
|
10
|
+
// onSelect: (place) => console.log(place.name, place.location),
|
|
11
|
+
// })
|
|
12
|
+
//
|
|
13
|
+
// One session token per search, ended by the pick, so a whole search counts
|
|
14
|
+
// as one autocomplete session.
|
|
15
|
+
import { createClient, sessionToken } from './services.js'
|
|
16
|
+
|
|
17
|
+
const CSS = `
|
|
18
|
+
.nways-ac{position:relative}
|
|
19
|
+
.nways-ac-list{position:absolute;left:0;right:0;top:calc(100% + 4px);z-index:1000;margin:0;padding:4px;list-style:none;
|
|
20
|
+
background:var(--nways-ac-bg,#fff);color:var(--nways-ac-fg,#1d2521);border:1px solid var(--nways-ac-border,#d5dbd7);
|
|
21
|
+
border-radius:10px;box-shadow:0 8px 24px rgba(0,0,0,.14);max-height:320px;overflow:auto;font:14px/1.35 system-ui,sans-serif}
|
|
22
|
+
.nways-ac-list[hidden]{display:none}
|
|
23
|
+
.nways-ac-item{padding:8px 10px;border-radius:7px;cursor:pointer}
|
|
24
|
+
.nways-ac-item[aria-selected=true]{background:var(--nways-ac-active,#e7f2ec)}
|
|
25
|
+
.nways-ac-item small{display:block;color:var(--nways-ac-muted,#5d6761);font-size:12px}
|
|
26
|
+
.nways-ac-credit{padding:6px 10px 2px;color:var(--nways-ac-muted,#5d6761);font-size:11px}
|
|
27
|
+
@media (prefers-color-scheme:dark){.nways-ac-list{--nways-ac-bg:#1e252c;--nways-ac-fg:#e3e8ef;--nways-ac-border:#38424d;--nways-ac-active:#2b3a33;--nways-ac-muted:#a4aebb}}
|
|
28
|
+
`
|
|
29
|
+
|
|
30
|
+
let styled = false
|
|
31
|
+
function addStyle() {
|
|
32
|
+
if (styled || typeof document === 'undefined') return
|
|
33
|
+
styled = true
|
|
34
|
+
const s = document.createElement('style')
|
|
35
|
+
s.dataset.nways = 'autocomplete'
|
|
36
|
+
s.textContent = CSS
|
|
37
|
+
document.head.append(s)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** The suggestions in an autocomplete (or geocode) answer, whichever shape it has. */
|
|
41
|
+
export function suggestionsOf(data) {
|
|
42
|
+
const list = data?.suggestions ?? data?.results ?? []
|
|
43
|
+
return list.map((s) => ({ ...s, name: s.name ?? s.text ?? s.label ?? '', context: s.context ?? s.formattedAddress ?? s.secondary ?? '' }))
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
let uid = 0
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Turns an <input> into an N-Ways search box. Options: `key`, `near`, `country`,
|
|
50
|
+
* `types`, `limit` (default 6), `lang`, `minLength` (default 2), `delay` (ms,
|
|
51
|
+
* default 200), `onSelect(suggestion)`, `client` (an existing createClient()).
|
|
52
|
+
* Returns `{ close(), destroy() }`.
|
|
53
|
+
*/
|
|
54
|
+
export function attachAutocomplete(input, options = {}) {
|
|
55
|
+
if (!(input instanceof HTMLInputElement)) throw new Error('N-Ways: attachAutocomplete needs an <input>')
|
|
56
|
+
const { near, country, types, limit = 6, minLength = 2, delay = 200, onSelect } = options
|
|
57
|
+
const client = options.client ?? createClient({ key: options.key, origin: options.origin, lang: options.lang })
|
|
58
|
+
addStyle()
|
|
59
|
+
|
|
60
|
+
const id = `nways-ac-${++uid}`
|
|
61
|
+
const wrap = document.createElement('div')
|
|
62
|
+
wrap.className = 'nways-ac'
|
|
63
|
+
input.replaceWith(wrap)
|
|
64
|
+
wrap.append(input)
|
|
65
|
+
const list = document.createElement('ul')
|
|
66
|
+
list.className = 'nways-ac-list'
|
|
67
|
+
list.id = id
|
|
68
|
+
list.setAttribute('role', 'listbox')
|
|
69
|
+
list.hidden = true
|
|
70
|
+
wrap.append(list)
|
|
71
|
+
input.setAttribute('role', 'combobox')
|
|
72
|
+
input.setAttribute('aria-autocomplete', 'list')
|
|
73
|
+
input.setAttribute('aria-expanded', 'false')
|
|
74
|
+
input.setAttribute('aria-controls', id)
|
|
75
|
+
input.autocomplete = 'off'
|
|
76
|
+
|
|
77
|
+
let items = []
|
|
78
|
+
let active = -1
|
|
79
|
+
let session = sessionToken()
|
|
80
|
+
let timer = 0
|
|
81
|
+
let ctrl = null
|
|
82
|
+
|
|
83
|
+
function render() {
|
|
84
|
+
list.replaceChildren(
|
|
85
|
+
...items.map((s, i) => {
|
|
86
|
+
const li = document.createElement('li')
|
|
87
|
+
li.className = 'nways-ac-item'
|
|
88
|
+
li.id = `${id}-${i}`
|
|
89
|
+
li.setAttribute('role', 'option')
|
|
90
|
+
li.setAttribute('aria-selected', String(i === active))
|
|
91
|
+
li.textContent = s.name
|
|
92
|
+
if (s.context) {
|
|
93
|
+
const small = document.createElement('small')
|
|
94
|
+
small.textContent = s.context
|
|
95
|
+
li.append(small)
|
|
96
|
+
}
|
|
97
|
+
li.addEventListener('mousedown', (e) => {
|
|
98
|
+
e.preventDefault()
|
|
99
|
+
pick(i)
|
|
100
|
+
})
|
|
101
|
+
return li
|
|
102
|
+
}),
|
|
103
|
+
)
|
|
104
|
+
if (items.length) {
|
|
105
|
+
const credit = document.createElement('li')
|
|
106
|
+
credit.className = 'nways-ac-credit'
|
|
107
|
+
credit.setAttribute('role', 'presentation')
|
|
108
|
+
credit.textContent = 'N-Ways · © OpenStreetMap contributors'
|
|
109
|
+
list.append(credit)
|
|
110
|
+
}
|
|
111
|
+
list.hidden = !items.length
|
|
112
|
+
input.setAttribute('aria-expanded', String(!!items.length))
|
|
113
|
+
if (active >= 0) input.setAttribute('aria-activedescendant', `${id}-${active}`)
|
|
114
|
+
else input.removeAttribute('aria-activedescendant')
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function close() {
|
|
118
|
+
items = []
|
|
119
|
+
active = -1
|
|
120
|
+
render()
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function pick(i) {
|
|
124
|
+
const s = items[i]
|
|
125
|
+
if (!s) return
|
|
126
|
+
input.value = s.name
|
|
127
|
+
close()
|
|
128
|
+
session = sessionToken() // the pick ends this session
|
|
129
|
+
onSelect?.(s)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async function search() {
|
|
133
|
+
const q = input.value.trim()
|
|
134
|
+
ctrl?.abort()
|
|
135
|
+
if (q.length < minLength) return close()
|
|
136
|
+
ctrl = new AbortController()
|
|
137
|
+
try {
|
|
138
|
+
const data = await client.autocomplete(q, { session, near, country, types, limit, signal: ctrl.signal })
|
|
139
|
+
if (input.value.trim() !== q) return
|
|
140
|
+
items = suggestionsOf(data).slice(0, limit)
|
|
141
|
+
active = -1
|
|
142
|
+
render()
|
|
143
|
+
} catch (e) {
|
|
144
|
+
if (e?.name === 'AbortError') return
|
|
145
|
+
console.error('N-Ways autocomplete:', e?.reason ?? '', e?.message ?? e)
|
|
146
|
+
close()
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const onInput = () => {
|
|
151
|
+
clearTimeout(timer)
|
|
152
|
+
timer = setTimeout(search, delay)
|
|
153
|
+
}
|
|
154
|
+
const onKey = (e) => {
|
|
155
|
+
if (e.key === 'ArrowDown' && items.length) {
|
|
156
|
+
active = (active + 1) % items.length
|
|
157
|
+
render()
|
|
158
|
+
e.preventDefault()
|
|
159
|
+
} else if (e.key === 'ArrowUp' && items.length) {
|
|
160
|
+
active = (active - 1 + items.length) % items.length
|
|
161
|
+
render()
|
|
162
|
+
e.preventDefault()
|
|
163
|
+
} else if (e.key === 'Enter' && active >= 0) {
|
|
164
|
+
pick(active)
|
|
165
|
+
e.preventDefault()
|
|
166
|
+
} else if (e.key === 'Escape') close()
|
|
167
|
+
}
|
|
168
|
+
const onBlur = () => setTimeout(close, 100)
|
|
169
|
+
input.addEventListener('input', onInput)
|
|
170
|
+
input.addEventListener('keydown', onKey)
|
|
171
|
+
input.addEventListener('blur', onBlur)
|
|
172
|
+
|
|
173
|
+
return {
|
|
174
|
+
close,
|
|
175
|
+
destroy() {
|
|
176
|
+
clearTimeout(timer)
|
|
177
|
+
ctrl?.abort()
|
|
178
|
+
input.removeEventListener('input', onInput)
|
|
179
|
+
input.removeEventListener('keydown', onKey)
|
|
180
|
+
input.removeEventListener('blur', onBlur)
|
|
181
|
+
for (const a of ['role', 'aria-autocomplete', 'aria-expanded', 'aria-controls', 'aria-activedescendant']) input.removeAttribute(a)
|
|
182
|
+
wrap.replaceWith(input)
|
|
183
|
+
},
|
|
184
|
+
}
|
|
185
|
+
}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright 2026 N-WAYS LIMITED
|
|
3
|
+
import type { Map, MapOptions, StyleSpecification } from 'maplibre-gl'
|
|
4
|
+
import type { StyleOptions, Theme } from './style.js'
|
|
5
|
+
|
|
6
|
+
export { ATTRIBUTION, NWAYS_ORIGIN, STYLE_IDS, THEMES, TILESETS, buildStyle, styleUrl, withKey } from './style.js'
|
|
7
|
+
export type { StyleOptions, Theme } from './style.js'
|
|
8
|
+
export { ApiError, PATHS, createClient, sessionToken, staticMapUrl } from './services.js'
|
|
9
|
+
export type * from './services.js'
|
|
10
|
+
export { attachAutocomplete, suggestionsOf } from './autocomplete.js'
|
|
11
|
+
export type { AutocompleteOptions, AutocompleteHandle } from './autocomplete.js'
|
|
12
|
+
|
|
13
|
+
/** MapLibre GL JS itself (Marker, Popup, controls). */
|
|
14
|
+
export * as maplibregl from 'maplibre-gl'
|
|
15
|
+
|
|
16
|
+
/** The SDK's version (semver). */
|
|
17
|
+
export const VERSION: string
|
|
18
|
+
|
|
19
|
+
export type CreateMapOptions = Omit<MapOptions, 'style'> &
|
|
20
|
+
StyleOptions & {
|
|
21
|
+
/** 'hosted' (default): N-Ways' hosted style; 'bundled': the SDK's built-in style; or your own style object or URL. */
|
|
22
|
+
style?: 'hosted' | 'bundled' | StyleSpecification | string
|
|
23
|
+
/** Show a note over the map when N-Ways refuses the key (default true). */
|
|
24
|
+
showErrors?: boolean
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** A MapLibre map of Nigeria, Ghana, Benin and Togo with N-Ways' style and your key. */
|
|
28
|
+
export function createMap(options: CreateMapOptions): Map
|
|
29
|
+
/** Switches a map made by createMap between light and dark. */
|
|
30
|
+
export function setTheme(map: Map, theme: Theme): void
|