@wherabouts/react 0.1.0 → 0.1.2
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 +296 -0
- package/package.json +5 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Joseph Amani
|
|
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
ADDED
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
# @wherabouts/react
|
|
2
|
+
|
|
3
|
+
React hooks for the [Wherabouts](https://wherabouts.com) location API — address autocomplete, reverse geocoding, and geofence detection with built-in debouncing, cancellation, and loading/error state.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@wherabouts/react)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## Requirements
|
|
11
|
+
|
|
12
|
+
| Peer dependency | Version |
|
|
13
|
+
|--------------------|----------|
|
|
14
|
+
| `react` | `>= 18` |
|
|
15
|
+
| `@wherabouts/sdk` | `>= 0.4.1` |
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
# npm
|
|
23
|
+
npm install @wherabouts/react @wherabouts/sdk
|
|
24
|
+
|
|
25
|
+
# pnpm
|
|
26
|
+
pnpm add @wherabouts/react @wherabouts/sdk
|
|
27
|
+
|
|
28
|
+
# yarn
|
|
29
|
+
yarn add @wherabouts/react @wherabouts/sdk
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
## Quick start
|
|
35
|
+
|
|
36
|
+
All hooks accept a `WheraboutsClient` instance as their first argument. Create one with your publishable API key from the [Wherabouts dashboard](https://wherabouts.com/dashboard).
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
import { WheraboutsClient } from "@wherabouts/sdk";
|
|
40
|
+
|
|
41
|
+
const client = new WheraboutsClient({
|
|
42
|
+
apiKey: "pk_live_...", // publishable key — safe in browser
|
|
43
|
+
});
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
> Use a **publishable key** (`pk_live_…`) in browser code. Secret keys must stay server-side only.
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## Hooks
|
|
51
|
+
|
|
52
|
+
### `useAutocomplete`
|
|
53
|
+
|
|
54
|
+
Type-ahead address search with automatic debouncing and in-flight request cancellation.
|
|
55
|
+
|
|
56
|
+
```tsx
|
|
57
|
+
import { useAutocomplete } from "@wherabouts/react";
|
|
58
|
+
|
|
59
|
+
function AddressSearch({ client }) {
|
|
60
|
+
const { query, setQuery, results, loading, error } = useAutocomplete(client, {
|
|
61
|
+
debounceMs: 300, // default — waits 300ms after last keystroke
|
|
62
|
+
limit: 5, // max results to return
|
|
63
|
+
country: "AU", // optional ISO 3166-1 alpha-2 country filter
|
|
64
|
+
state: "QLD", // optional state/region filter
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
return (
|
|
68
|
+
<div>
|
|
69
|
+
<input
|
|
70
|
+
value={query}
|
|
71
|
+
onChange={(e) => setQuery(e.target.value)}
|
|
72
|
+
placeholder="Start typing an address..."
|
|
73
|
+
/>
|
|
74
|
+
{loading && <p>Searching…</p>}
|
|
75
|
+
{error && <p>Error: {error.message}</p>}
|
|
76
|
+
<ul>
|
|
77
|
+
{results.map((r) => (
|
|
78
|
+
<li key={r.id}>{r.formattedAddress}</li>
|
|
79
|
+
))}
|
|
80
|
+
</ul>
|
|
81
|
+
</div>
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
#### Signature
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
function useAutocomplete(
|
|
90
|
+
client: WheraboutsClient,
|
|
91
|
+
options?: UseAutocompleteOptions
|
|
92
|
+
): UseAutocompleteResult
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
#### `UseAutocompleteOptions`
|
|
96
|
+
|
|
97
|
+
| Option | Type | Default | Description |
|
|
98
|
+
|---------------|----------|---------|--------------------------------------------------|
|
|
99
|
+
| `debounceMs` | `number` | `300` | Milliseconds to wait after last keystroke |
|
|
100
|
+
| `limit` | `number` | — | Maximum number of suggestions to return |
|
|
101
|
+
| `country` | `string` | — | ISO 3166-1 alpha-2 country code filter (e.g. `"AU"`) |
|
|
102
|
+
| `state` | `string` | — | State or region name filter |
|
|
103
|
+
|
|
104
|
+
#### `UseAutocompleteResult`
|
|
105
|
+
|
|
106
|
+
| Field | Type | Description |
|
|
107
|
+
|------------|------------------------|---------------------------------------------------|
|
|
108
|
+
| `query` | `string` | Current search string |
|
|
109
|
+
| `setQuery` | `(q: string) => void` | Stable setter — safe to pass directly to `onChange` |
|
|
110
|
+
| `results` | `AddressSuggestion[]` | Matching address suggestions |
|
|
111
|
+
| `loading` | `boolean` | `true` while a request is in flight |
|
|
112
|
+
| `error` | `Error \| null` | Last error, or `null` |
|
|
113
|
+
|
|
114
|
+
**Behaviour notes:**
|
|
115
|
+
- Empty or whitespace-only query immediately clears results without firing a request.
|
|
116
|
+
- Each new keystroke cancels the previous pending request via `AbortController`.
|
|
117
|
+
- `setQuery` is memoised with `useCallback` — it will not cause unnecessary re-renders.
|
|
118
|
+
|
|
119
|
+
---
|
|
120
|
+
|
|
121
|
+
### `useReverseGeocode`
|
|
122
|
+
|
|
123
|
+
Resolves a latitude/longitude coordinate to the nearest address. Pass `null` to reset.
|
|
124
|
+
|
|
125
|
+
```tsx
|
|
126
|
+
import { useReverseGeocode } from "@wherabouts/react";
|
|
127
|
+
|
|
128
|
+
function NearestAddress({ client, userLocation }) {
|
|
129
|
+
// userLocation: { lat: number; lng: number } | null
|
|
130
|
+
const { address, distance, loading, error } = useReverseGeocode(client, userLocation);
|
|
131
|
+
|
|
132
|
+
if (loading) return <p>Locating…</p>;
|
|
133
|
+
if (error) return <p>Error: {error.message}</p>;
|
|
134
|
+
if (!address) return <p>No location set</p>;
|
|
135
|
+
|
|
136
|
+
return (
|
|
137
|
+
<div>
|
|
138
|
+
<p>{address.formattedAddress}</p>
|
|
139
|
+
<p>{distance != null ? `${distance.toFixed(0)}m away` : ""}</p>
|
|
140
|
+
</div>
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
#### Signature
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
function useReverseGeocode(
|
|
149
|
+
client: WheraboutsClient,
|
|
150
|
+
coords: LatLng | null
|
|
151
|
+
): UseReverseGeocodeResult
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
#### `LatLng`
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
interface LatLng {
|
|
158
|
+
lat: number;
|
|
159
|
+
lng: number;
|
|
160
|
+
}
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
#### `UseReverseGeocodeResult`
|
|
164
|
+
|
|
165
|
+
| Field | Type | Description |
|
|
166
|
+
|------------|-------------------------------|----------------------------------------------|
|
|
167
|
+
| `address` | `ReverseGeocodeAddress \| null` | Nearest address, or `null` if not yet resolved |
|
|
168
|
+
| `distance` | `number \| null` | Distance in metres from `coords` to `address` |
|
|
169
|
+
| `loading` | `boolean` | `true` while a request is in flight |
|
|
170
|
+
| `error` | `Error \| null` | Last error, or `null` |
|
|
171
|
+
|
|
172
|
+
**Behaviour notes:**
|
|
173
|
+
- Passing `null` as `coords` immediately resets `address` and `distance` to `null` without firing a request.
|
|
174
|
+
- The effect re-runs only when `coords.lat` or `coords.lng` changes — passing a new object reference with the same values does **not** re-fetch.
|
|
175
|
+
- In-flight requests are cancelled on unmount or when `coords` changes.
|
|
176
|
+
|
|
177
|
+
---
|
|
178
|
+
|
|
179
|
+
### `useZoneContains`
|
|
180
|
+
|
|
181
|
+
Returns all geofence zones that contain a given coordinate. Useful for entry/exit detection, location-gating, and proximity-based UI.
|
|
182
|
+
|
|
183
|
+
```tsx
|
|
184
|
+
import { useZoneContains } from "@wherabouts/react";
|
|
185
|
+
|
|
186
|
+
function ZoneStatus({ client, userLocation }) {
|
|
187
|
+
const { zones, loading, error } = useZoneContains(client, userLocation);
|
|
188
|
+
|
|
189
|
+
if (loading) return <p>Checking zones…</p>;
|
|
190
|
+
if (error) return <p>Error: {error.message}</p>;
|
|
191
|
+
|
|
192
|
+
return (
|
|
193
|
+
<div>
|
|
194
|
+
{zones.length === 0 ? (
|
|
195
|
+
<p>Not inside any zone</p>
|
|
196
|
+
) : (
|
|
197
|
+
<ul>
|
|
198
|
+
{zones.map((z) => (
|
|
199
|
+
<li key={z.id}>{z.name}</li>
|
|
200
|
+
))}
|
|
201
|
+
</ul>
|
|
202
|
+
)}
|
|
203
|
+
</div>
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
#### Signature
|
|
209
|
+
|
|
210
|
+
```ts
|
|
211
|
+
function useZoneContains(
|
|
212
|
+
client: WheraboutsClient,
|
|
213
|
+
coords: LatLng | null
|
|
214
|
+
): UseZoneContainsResult
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
#### `UseZoneContainsResult`
|
|
218
|
+
|
|
219
|
+
| Field | Type | Description |
|
|
220
|
+
|-----------|-------------------|------------------------------------------|
|
|
221
|
+
| `zones` | `ZoneRecord[]` | Zones that contain `coords` |
|
|
222
|
+
| `loading` | `boolean` | `true` while a request is in flight |
|
|
223
|
+
| `error` | `Error \| null` | Last error, or `null` |
|
|
224
|
+
|
|
225
|
+
**Behaviour notes:**
|
|
226
|
+
- Passing `null` as `coords` immediately clears `zones` without firing a request.
|
|
227
|
+
- Same coordinate-stability semantics as `useReverseGeocode` — object identity is ignored, only `lat`/`lng` values matter.
|
|
228
|
+
- In-flight requests are cancelled on unmount or when `coords` changes.
|
|
229
|
+
|
|
230
|
+
---
|
|
231
|
+
|
|
232
|
+
## Using the browser Geolocation API
|
|
233
|
+
|
|
234
|
+
A common pattern — combine the browser's `navigator.geolocation` with these hooks:
|
|
235
|
+
|
|
236
|
+
```tsx
|
|
237
|
+
import { useState, useEffect } from "react";
|
|
238
|
+
import { useReverseGeocode, useZoneContains, type LatLng } from "@wherabouts/react";
|
|
239
|
+
|
|
240
|
+
function LocationAwareComponent({ client }) {
|
|
241
|
+
const [coords, setCoords] = useState<LatLng | null>(null);
|
|
242
|
+
|
|
243
|
+
useEffect(() => {
|
|
244
|
+
if (!navigator.geolocation) return;
|
|
245
|
+
const id = navigator.geolocation.watchPosition(
|
|
246
|
+
(pos) => setCoords({ lat: pos.coords.latitude, lng: pos.coords.longitude }),
|
|
247
|
+
console.error,
|
|
248
|
+
{ enableHighAccuracy: true }
|
|
249
|
+
);
|
|
250
|
+
return () => navigator.geolocation.clearWatch(id);
|
|
251
|
+
}, []);
|
|
252
|
+
|
|
253
|
+
const { address } = useReverseGeocode(client, coords);
|
|
254
|
+
const { zones } = useZoneContains(client, coords);
|
|
255
|
+
|
|
256
|
+
return (
|
|
257
|
+
<div>
|
|
258
|
+
<p>Address: {address?.formattedAddress ?? "Locating…"}</p>
|
|
259
|
+
<p>Zones: {zones.map((z) => z.name).join(", ") || "None"}</p>
|
|
260
|
+
</div>
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
---
|
|
266
|
+
|
|
267
|
+
## TypeScript
|
|
268
|
+
|
|
269
|
+
All hooks and their option/result types are exported:
|
|
270
|
+
|
|
271
|
+
```ts
|
|
272
|
+
import type {
|
|
273
|
+
LatLng,
|
|
274
|
+
UseAutocompleteOptions,
|
|
275
|
+
UseAutocompleteResult,
|
|
276
|
+
UseReverseGeocodeResult,
|
|
277
|
+
UseZoneContainsResult,
|
|
278
|
+
} from "@wherabouts/react";
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
The package ships with bundled type declarations (`.d.ts` and `.d.cts`) for both ESM and CJS consumers.
|
|
282
|
+
|
|
283
|
+
---
|
|
284
|
+
|
|
285
|
+
## Bundle formats
|
|
286
|
+
|
|
287
|
+
| Format | Entry | Use case |
|
|
288
|
+
|--------|-------------------------|-----------------------|
|
|
289
|
+
| ESM | `dist/index.js` | Bundlers (Vite, Next) |
|
|
290
|
+
| CJS | `dist/index.cjs` | Node / Jest |
|
|
291
|
+
|
|
292
|
+
---
|
|
293
|
+
|
|
294
|
+
## License
|
|
295
|
+
|
|
296
|
+
[MIT](./LICENSE) © Joseph Amani
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wherabouts/react",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "React hooks for the Wherabouts location API",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"license": "
|
|
6
|
+
"license": "MIT",
|
|
7
7
|
"sideEffects": false,
|
|
8
8
|
"engines": {
|
|
9
9
|
"node": ">=18"
|
|
@@ -28,7 +28,8 @@
|
|
|
28
28
|
},
|
|
29
29
|
"files": [
|
|
30
30
|
"dist",
|
|
31
|
-
"README.md"
|
|
31
|
+
"README.md",
|
|
32
|
+
"LICENSE"
|
|
32
33
|
],
|
|
33
34
|
"peerDependencies": {
|
|
34
35
|
"react": ">=18",
|
|
@@ -40,7 +41,7 @@
|
|
|
40
41
|
"tsup": "^8.5.1",
|
|
41
42
|
"typescript": "^5",
|
|
42
43
|
"vitest": "^4.1.4",
|
|
43
|
-
"@wherabouts/sdk": "0.4.
|
|
44
|
+
"@wherabouts/sdk": "0.4.1",
|
|
44
45
|
"@wherabouts.com/config": "0.0.0"
|
|
45
46
|
},
|
|
46
47
|
"scripts": {
|