@chaosity/location-client-react 0.1.1 → 0.1.3
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/README.md +246 -0
- package/package.json +5 -6
package/README.md
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
# @chaosity/location-client-react
|
|
2
|
+
|
|
3
|
+
React bindings for [@chaosity/location-client](https://www.npmjs.com/package/@chaosity/location-client) - AWS Location Service compatible client.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @chaosity/location-client-react @chaosity/location-client
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```tsx
|
|
14
|
+
import { LocationClientProvider, useLocationClient } from '@chaosity/location-client-react'
|
|
15
|
+
import { SuggestCommand } from '@chaosity/location-client'
|
|
16
|
+
|
|
17
|
+
// 1. Wrap your app with the provider
|
|
18
|
+
function App() {
|
|
19
|
+
return (
|
|
20
|
+
<LocationClientProvider getConfig={getLocationConfig}>
|
|
21
|
+
<MapComponent />
|
|
22
|
+
</LocationClientProvider>
|
|
23
|
+
)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// 2. Use the client in any component
|
|
27
|
+
function MapComponent() {
|
|
28
|
+
const { client, config, loading, error } = useLocationClient()
|
|
29
|
+
|
|
30
|
+
const searchPlaces = async (query: string) => {
|
|
31
|
+
if (!client) return
|
|
32
|
+
|
|
33
|
+
const command = new SuggestCommand({
|
|
34
|
+
QueryText: query,
|
|
35
|
+
MaxResults: 5
|
|
36
|
+
})
|
|
37
|
+
const response = await client.send(command)
|
|
38
|
+
return response.ResultItems
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return <div>...</div>
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## API Reference
|
|
46
|
+
|
|
47
|
+
### LocationClientProvider
|
|
48
|
+
|
|
49
|
+
Provides the location client to all child components.
|
|
50
|
+
|
|
51
|
+
```tsx
|
|
52
|
+
<LocationClientProvider getConfig={getLocationConfig}>
|
|
53
|
+
{children}
|
|
54
|
+
</LocationClientProvider>
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
**Props:**
|
|
58
|
+
- `getConfig` (function, required) - Async function that returns `{ apiUrl: string, token: string }`
|
|
59
|
+
- `children` (ReactNode, required) - Child components
|
|
60
|
+
|
|
61
|
+
### useLocationClient
|
|
62
|
+
|
|
63
|
+
Hook to access the location client in any component.
|
|
64
|
+
|
|
65
|
+
```tsx
|
|
66
|
+
const { client, config, loading, error } = useLocationClient()
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
**Returns:**
|
|
70
|
+
- `client` (GeoPlacesClient | null) - The location client instance
|
|
71
|
+
- `config` (ClientConfig | null) - The client configuration (apiUrl, token)
|
|
72
|
+
- `loading` (boolean) - Whether the client is initializing
|
|
73
|
+
- `error` (string | null) - Error message if initialization failed
|
|
74
|
+
|
|
75
|
+
**Throws:** Error if used outside `LocationClientProvider`
|
|
76
|
+
|
|
77
|
+
## Usage with Next.js Server Actions
|
|
78
|
+
|
|
79
|
+
```tsx
|
|
80
|
+
// app/actions/location.ts (Server-side)
|
|
81
|
+
'use server'
|
|
82
|
+
|
|
83
|
+
export async function getLocationConfig() {
|
|
84
|
+
const response = await fetch('https://api.example.com/auth/token', {
|
|
85
|
+
method: 'POST',
|
|
86
|
+
headers: { 'Content-Type': 'application/json' },
|
|
87
|
+
body: JSON.stringify({
|
|
88
|
+
grant_type: 'client_credentials',
|
|
89
|
+
client_id: process.env.LOCATION_CLIENT_ID!,
|
|
90
|
+
client_secret: process.env.LOCATION_CLIENT_SECRET!
|
|
91
|
+
})
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
const data = await response.json()
|
|
95
|
+
return {
|
|
96
|
+
apiUrl: 'https://api.example.com',
|
|
97
|
+
token: data.access_token
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// app/layout.tsx (Client-side)
|
|
102
|
+
'use client'
|
|
103
|
+
|
|
104
|
+
import { LocationClientProvider } from '@chaosity/location-client-react'
|
|
105
|
+
import { getLocationConfig } from './actions/location'
|
|
106
|
+
|
|
107
|
+
export default function RootLayout({ children }) {
|
|
108
|
+
return (
|
|
109
|
+
<LocationClientProvider getConfig={getLocationConfig}>
|
|
110
|
+
{children}
|
|
111
|
+
</LocationClientProvider>
|
|
112
|
+
)
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Complete Example with MapLibre
|
|
117
|
+
|
|
118
|
+
```tsx
|
|
119
|
+
'use client'
|
|
120
|
+
|
|
121
|
+
import { useLocationClient } from '@chaosity/location-client-react'
|
|
122
|
+
import { GeoPlaces } from '@chaosity/location-client'
|
|
123
|
+
import maplibregl from 'maplibre-gl'
|
|
124
|
+
import MaplibreGeocoder from '@maplibre/maplibre-gl-geocoder'
|
|
125
|
+
import { useEffect, useRef } from 'react'
|
|
126
|
+
|
|
127
|
+
export default function MapComponent() {
|
|
128
|
+
const mapContainer = useRef<HTMLDivElement>(null)
|
|
129
|
+
const map = useRef<maplibregl.Map | null>(null)
|
|
130
|
+
const { config, client, loading, error } = useLocationClient()
|
|
131
|
+
|
|
132
|
+
useEffect(() => {
|
|
133
|
+
if (!mapContainer.current || map.current || loading || !config || !client) return
|
|
134
|
+
|
|
135
|
+
// Initialize map
|
|
136
|
+
const mapInstance = new maplibregl.Map({
|
|
137
|
+
container: mapContainer.current,
|
|
138
|
+
style: `${config.apiUrl}/maps/Standard/descriptor`,
|
|
139
|
+
center: [-123.12, 49.28],
|
|
140
|
+
zoom: 10,
|
|
141
|
+
transformRequest: (url) => {
|
|
142
|
+
if (url.startsWith(config.apiUrl)) {
|
|
143
|
+
return {
|
|
144
|
+
url,
|
|
145
|
+
headers: { 'Authorization': `Bearer ${config.token}` }
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return { url }
|
|
149
|
+
}
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
// Add navigation controls
|
|
153
|
+
mapInstance.addControl(new maplibregl.NavigationControl(), 'top-right')
|
|
154
|
+
|
|
155
|
+
// Add geocoder
|
|
156
|
+
const geoPlaces = new GeoPlaces(config.apiUrl, config.token, mapInstance)
|
|
157
|
+
const geocoder = new MaplibreGeocoder(geoPlaces, {
|
|
158
|
+
maplibregl,
|
|
159
|
+
showResultsWhileTyping: true,
|
|
160
|
+
limit: 30
|
|
161
|
+
})
|
|
162
|
+
mapInstance.addControl(geocoder, 'top-left')
|
|
163
|
+
|
|
164
|
+
// Handle result selection
|
|
165
|
+
geocoder.on('result', async (event) => {
|
|
166
|
+
const { id, result_type } = event.result
|
|
167
|
+
if (result_type === 'Place') {
|
|
168
|
+
const details = await geoPlaces.searchByPlaceId(id)
|
|
169
|
+
console.log('Place details:', details)
|
|
170
|
+
}
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
map.current = mapInstance
|
|
174
|
+
|
|
175
|
+
return () => {
|
|
176
|
+
if (map.current) {
|
|
177
|
+
map.current.remove()
|
|
178
|
+
map.current = null
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}, [config, client, loading])
|
|
182
|
+
|
|
183
|
+
if (error) {
|
|
184
|
+
return <div>Error: {error}</div>
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (loading) {
|
|
188
|
+
return <div>Loading map...</div>
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return <div ref={mapContainer} style={{ width: '100%', height: '600px' }} />
|
|
192
|
+
}
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
## Available Commands
|
|
196
|
+
|
|
197
|
+
All AWS Location Service commands are available through the client:
|
|
198
|
+
|
|
199
|
+
```tsx
|
|
200
|
+
import {
|
|
201
|
+
SuggestCommand,
|
|
202
|
+
GeocodeCommand,
|
|
203
|
+
ReverseGeocodeCommand,
|
|
204
|
+
GetPlaceCommand,
|
|
205
|
+
SearchTextCommand,
|
|
206
|
+
SearchNearbyCommand
|
|
207
|
+
} from '@chaosity/location-client'
|
|
208
|
+
|
|
209
|
+
function MyComponent() {
|
|
210
|
+
const { client } = useLocationClient()
|
|
211
|
+
|
|
212
|
+
const searchPlaces = async () => {
|
|
213
|
+
const response = await client.send(
|
|
214
|
+
new SuggestCommand({ QueryText: 'Vancouver', MaxResults: 5 })
|
|
215
|
+
)
|
|
216
|
+
return response.ResultItems
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
## TypeScript Support
|
|
222
|
+
|
|
223
|
+
Full TypeScript support with types from AWS SDK:
|
|
224
|
+
|
|
225
|
+
```tsx
|
|
226
|
+
import type { SuggestCommandOutput } from '@aws-sdk/client-geo-places'
|
|
227
|
+
|
|
228
|
+
const { client } = useLocationClient()
|
|
229
|
+
|
|
230
|
+
const response: SuggestCommandOutput = await client.send(
|
|
231
|
+
new SuggestCommand({ QueryText: 'Vancouver' })
|
|
232
|
+
)
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
## Security Best Practices
|
|
236
|
+
|
|
237
|
+
⚠️ **NEVER expose client credentials in browser code!**
|
|
238
|
+
|
|
239
|
+
- The `getConfig` function should call a server-side API or Server Action
|
|
240
|
+
- Store `client_id` and `client_secret` in server environment variables only
|
|
241
|
+
- Only the JWT token should be sent to the browser
|
|
242
|
+
- Tokens should be short-lived and refreshed as needed
|
|
243
|
+
|
|
244
|
+
## License
|
|
245
|
+
|
|
246
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chaosity/location-client-react",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "React bindings for Chaosity Location Service client",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -20,15 +20,14 @@
|
|
|
20
20
|
"license": "MIT",
|
|
21
21
|
"repository": {
|
|
22
22
|
"type": "git",
|
|
23
|
-
"url": "https://github.com/chaosity-io/location-service-
|
|
24
|
-
"directory": "location-service-client-react"
|
|
23
|
+
"url": "https://github.com/chaosity-io/location-service-client-react.git"
|
|
25
24
|
},
|
|
26
|
-
"homepage": "https://github.com/chaosity-io/location-service-
|
|
25
|
+
"homepage": "https://github.com/chaosity-io/location-service-client-react",
|
|
27
26
|
"bugs": {
|
|
28
|
-
"url": "https://github.com/chaosity-io/location-service-
|
|
27
|
+
"url": "https://github.com/chaosity-io/location-service-client-react/issues"
|
|
29
28
|
},
|
|
30
29
|
"dependencies": {
|
|
31
|
-
"@chaosity/location-client": "^0.1.
|
|
30
|
+
"@chaosity/location-client": "^0.1.3"
|
|
32
31
|
},
|
|
33
32
|
"peerDependencies": {
|
|
34
33
|
"react": "^18.0.0 || ^19.0.0"
|