@atomiqlab/react-native-mapbox-navigation 1.1.1 → 1.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/src/index.tsx CHANGED
@@ -2,24 +2,22 @@ import { requireNativeModule, requireNativeViewManager } from "expo-modules-core
2
2
  import { ViewProps } from "react-native";
3
3
 
4
4
  import type {
5
- ArrivalEvent,
6
- BannerInstruction,
7
- LocationUpdate,
8
- MapboxNavigationModule as MapboxNavigationModuleType,
9
- MapboxNavigationViewProps,
10
- NavigationSettings,
11
- NavigationError,
12
- NavigationOptions,
13
- RouteProgress,
14
- Subscription,
5
+ ArrivalEvent,
6
+ BannerInstruction,
7
+ LocationUpdate,
8
+ MapboxNavigationModule as MapboxNavigationModuleType,
9
+ MapboxNavigationViewProps,
10
+ NavigationSettings,
11
+ NavigationError,
12
+ NavigationOptions,
13
+ RouteProgress,
14
+ Subscription,
15
15
  } from "./MapboxNavigation.types";
16
16
 
17
- // Get the native module (use requireNativeModule instead of deprecated NativeModulesProxy)
18
17
  const MapboxNavigationModule = requireNativeModule<MapboxNavigationModuleType>(
19
18
  "MapboxNavigationModule",
20
19
  );
21
20
 
22
- // The native module already acts as an EventEmitter; don't wrap with `new EventEmitter`.
23
21
  const emitter = MapboxNavigationModule as unknown as {
24
22
  addListener: (
25
23
  eventName: string,
@@ -106,74 +104,166 @@ function normalizeNavigationOptions(options: NavigationOptions): NavigationOptio
106
104
  };
107
105
  }
108
106
 
109
- // Module API
107
+ function normalizeNativeError(error: unknown, fallbackCode = "NATIVE_ERROR"): Error {
108
+ if (error instanceof Error) {
109
+ return error;
110
+ }
111
+
112
+ const candidate = error as { code?: string; message?: string } | undefined;
113
+ const code = candidate?.code ?? fallbackCode;
114
+ const message = candidate?.message ?? "Unknown native error";
115
+ return new Error(`[${code}] ${message}`);
116
+ }
117
+
118
+ /**
119
+ * Start full-screen native turn-by-turn navigation.
120
+ *
121
+ * @param options Navigation settings such as destination, camera mode, simulation, and map UI options.
122
+ * @throws Error if options are invalid or native route/token setup fails.
123
+ */
110
124
  export async function startNavigation(
111
125
  options: NavigationOptions,
112
126
  ): Promise<void> {
113
127
  const normalizedOptions = normalizeNavigationOptions(options);
114
- return await MapboxNavigationModule.startNavigation(normalizedOptions);
128
+ try {
129
+ await MapboxNavigationModule.startNavigation(normalizedOptions);
130
+ } catch (error) {
131
+ throw normalizeNativeError(error, "START_NAVIGATION_FAILED");
132
+ }
115
133
  }
116
134
 
135
+ /**
136
+ * Stop/dismiss native navigation if active.
137
+ */
117
138
  export async function stopNavigation(): Promise<void> {
118
- return await MapboxNavigationModule.stopNavigation();
139
+ try {
140
+ await MapboxNavigationModule.stopNavigation();
141
+ } catch (error) {
142
+ throw normalizeNativeError(error, "STOP_NAVIGATION_FAILED");
143
+ }
119
144
  }
120
145
 
146
+ /**
147
+ * Enable or disable voice guidance.
148
+ *
149
+ * @param muted `true` to mute voice instructions.
150
+ */
121
151
  export async function setMuted(muted: boolean): Promise<void> {
122
- return await MapboxNavigationModule.setMuted(muted);
152
+ try {
153
+ await MapboxNavigationModule.setMuted(muted);
154
+ } catch (error) {
155
+ throw normalizeNativeError(error, "SET_MUTED_FAILED");
156
+ }
123
157
  }
124
158
 
159
+ /**
160
+ * Set voice instruction volume in range `0..1`.
161
+ */
125
162
  export async function setVoiceVolume(volume: number): Promise<void> {
126
- return await MapboxNavigationModule.setVoiceVolume(volume);
163
+ try {
164
+ await MapboxNavigationModule.setVoiceVolume(volume);
165
+ } catch (error) {
166
+ throw normalizeNativeError(error, "SET_VOICE_VOLUME_FAILED");
167
+ }
127
168
  }
128
169
 
170
+ /**
171
+ * Set spoken and displayed distance units.
172
+ */
129
173
  export async function setDistanceUnit(unit: "metric" | "imperial"): Promise<void> {
130
- return await MapboxNavigationModule.setDistanceUnit(unit);
174
+ try {
175
+ await MapboxNavigationModule.setDistanceUnit(unit);
176
+ } catch (error) {
177
+ throw normalizeNativeError(error, "SET_DISTANCE_UNIT_FAILED");
178
+ }
131
179
  }
132
180
 
181
+ /**
182
+ * Set instruction language (BCP-47-like code, for example `en`, `fr`).
183
+ */
133
184
  export async function setLanguage(language: string): Promise<void> {
134
- return await MapboxNavigationModule.setLanguage(language);
185
+ try {
186
+ await MapboxNavigationModule.setLanguage(language);
187
+ } catch (error) {
188
+ throw normalizeNativeError(error, "SET_LANGUAGE_FAILED");
189
+ }
135
190
  }
136
191
 
192
+ /**
193
+ * Check whether full-screen native navigation is currently active.
194
+ */
137
195
  export async function isNavigating(): Promise<boolean> {
138
- return await MapboxNavigationModule.isNavigating();
196
+ try {
197
+ return await MapboxNavigationModule.isNavigating();
198
+ } catch (error) {
199
+ throw normalizeNativeError(error, "IS_NAVIGATING_FAILED");
200
+ }
139
201
  }
140
202
 
203
+ /**
204
+ * Read current native navigation runtime settings.
205
+ */
141
206
  export async function getNavigationSettings(): Promise<NavigationSettings> {
142
- return await MapboxNavigationModule.getNavigationSettings();
207
+ try {
208
+ return await MapboxNavigationModule.getNavigationSettings();
209
+ } catch (error) {
210
+ throw normalizeNativeError(error, "GET_NAVIGATION_SETTINGS_FAILED");
211
+ }
143
212
  }
144
213
 
145
- // Event listeners
214
+ /**
215
+ * Subscribe to location updates from native navigation.
216
+ */
146
217
  export function addLocationChangeListener(
147
218
  listener: (location: LocationUpdate) => void,
148
219
  ): Subscription {
149
220
  return emitter.addListener("onLocationChange", listener);
150
221
  }
151
222
 
223
+ /**
224
+ * Subscribe to route progress updates.
225
+ */
152
226
  export function addRouteProgressChangeListener(
153
227
  listener: (progress: RouteProgress) => void,
154
228
  ): Subscription {
155
229
  return emitter.addListener("onRouteProgressChange", listener);
156
230
  }
157
231
 
232
+ /**
233
+ * Subscribe to arrival events.
234
+ */
158
235
  export function addArriveListener(listener: (point: ArrivalEvent) => void): Subscription {
159
236
  return emitter.addListener("onArrive", listener);
160
237
  }
161
238
 
239
+ /**
240
+ * Subscribe to cancellation events.
241
+ */
162
242
  export function addCancelNavigationListener(listener: () => void): Subscription {
163
243
  return emitter.addListener("onCancelNavigation", listener);
164
244
  }
165
245
 
246
+ /**
247
+ * Subscribe to native errors (token issues, route fetch failures, permission failures, etc.).
248
+ */
166
249
  export function addErrorListener(listener: (error: NavigationError) => void): Subscription {
167
250
  return emitter.addListener("onError", listener);
168
251
  }
169
252
 
253
+ /**
254
+ * Subscribe to banner instruction updates.
255
+ */
170
256
  export function addBannerInstructionListener(
171
257
  listener: (instruction: BannerInstruction) => void,
172
258
  ): Subscription {
173
259
  return emitter.addListener("onBannerInstruction", listener);
174
260
  }
175
261
 
176
- // React component wrapper
262
+ /**
263
+ * Embedded native navigation component.
264
+ *
265
+ * Use this when you need navigation inside your own screen layout instead of full-screen modal navigation.
266
+ */
177
267
  export function MapboxNavigationView(
178
268
  props: MapboxNavigationViewProps & ViewProps,
179
269
  ) {
@@ -181,10 +271,8 @@ export function MapboxNavigationView(
181
271
  return <NativeView {...props} />;
182
272
  }
183
273
 
184
- // Export types
185
274
  export * from "./MapboxNavigation.types";
186
275
 
187
- // Default export
188
276
  export default {
189
277
  startNavigation,
190
278
  stopNavigation,