@ingissa/navcore-mapbox 1.0.8 → 1.0.10

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 CHANGED
@@ -1,3 +1,3 @@
1
- Copyright (c) 2026 Abdelnasser Aitabi. All rights reserved.
2
-
3
- No license is granted except by separate written agreement.
1
+ Copyright (c) 2026 Abdelnasser Aitabi. All rights reserved.
2
+
3
+ No license is granted except by separate written agreement.
package/README.md ADDED
@@ -0,0 +1,102 @@
1
+ # @ingissa/navcore-mapbox
2
+
3
+ [Official Website](https://navcore-web.vercel.app/)
4
+
5
+ > Mapbox Directions provider adapter for the NavCore navigation engine.
6
+
7
+ [![npm version](https://img.shields.io/npm/v/@ingissa/navcore-mapbox)](https://www.npmjs.com/package/@ingissa/navcore-mapbox)
8
+ ![License](https://img.shields.io/badge/license-Proprietary-red)
9
+ [![TypeScript](https://img.shields.io/badge/TypeScript-strict-blue)](https://www.typescriptlang.org/)
10
+
11
+ ---
12
+
13
+ ## Overview
14
+
15
+ `@ingissa/navcore-mapbox` provides `MapboxDirectionsProvider`, an implementation of NavCore's `DirectionsProvider` interface targeting the Mapbox Directions API.
16
+
17
+ ### Key Capabilities
18
+ - **Direct NavCore integration**: Directly compatible with `createNavigationSession` and `createRoutePlan`.
19
+ - **Automatic waypoint chunking**: Transparently splits routes with more than 25 waypoints into chunks, querying each chunk and stitching geometry with the `OVERLAP_1` strategy.
20
+ - **Configurable routing profiles**: Supports `driving`, `driving-traffic`, `cycling`, and `walking`.
21
+
22
+ > [!NOTE]
23
+ > `@ingissa/navcore-mapbox` is a routing directions provider only. It does not export a map renderer. For map rendering in React Native, use `@rnmapbox/maps` or `react-native-maps` directly in your application components by consuming state from `useNavSelector()`.
24
+
25
+ ---
26
+
27
+ ## Installation
28
+
29
+ ```bash
30
+ npm install @ingissa/navcore-mapbox @ingissa/navcore-core
31
+ ```
32
+
33
+ ### Peer Dependencies
34
+ - `@ingissa/navcore-core`: `^1.0.9` (required)
35
+ - `@rnmapbox/maps`: `>=10.0.0` (optional, for React Native applications)
36
+
37
+ ---
38
+
39
+ ## Quickstart
40
+
41
+ ```typescript
42
+ import { createNavigationSession } from '@ingissa/navcore-core';
43
+ import { MapboxDirectionsProvider } from '@ingissa/navcore-mapbox';
44
+
45
+ // Initialize Mapbox provider with your access token
46
+ const provider = new MapboxDirectionsProvider({
47
+ accessToken: 'pk.eyJ1...',
48
+ profile: 'driving-traffic', // 'driving' | 'driving-traffic' | 'cycling' | 'walking'
49
+ maxWaypointsPerChunk: 25, // Default: 25 (Mapbox API limit)
50
+ });
51
+
52
+ // Pass to a NavigationSession
53
+ const nav = createNavigationSession({
54
+ provider,
55
+ providerName: 'mapbox',
56
+ engineOptions: { isDev: true },
57
+ });
58
+
59
+ // Fetch route and start navigation
60
+ const plan = await nav.navigate({
61
+ origin: [2.3522, 48.8566],
62
+ destination: [2.3009, 48.8741],
63
+ });
64
+
65
+ console.log(`Route loaded: ${plan.distanceM}m, duration: ${plan.durationS}s`);
66
+ ```
67
+
68
+ ---
69
+
70
+ ## Standalone Usage
71
+
72
+ ```typescript
73
+ import { MapboxDirectionsProvider } from '@ingissa/navcore-mapbox';
74
+
75
+ const provider = new MapboxDirectionsProvider({
76
+ accessToken: process.env.MAPBOX_TOKEN!,
77
+ });
78
+
79
+ const result = await provider.getRoute([
80
+ [2.3522, 48.8566],
81
+ [2.3300, 48.8650],
82
+ [2.3009, 48.8741],
83
+ ]);
84
+
85
+ console.log('Coordinates:', result.geometry.length);
86
+ console.log('Distance in metres:', result.distance);
87
+ console.log('Duration in seconds:', result.duration);
88
+ ```
89
+
90
+ ---
91
+
92
+ ## Documentation
93
+
94
+ - [Directions Providers Guide](../../docs/directions-providers.md)
95
+ - [API Reference](../../docs/api-reference/navcore-mapbox.md)
96
+ - [Official Website](https://navcore-web.vercel.app/)
97
+
98
+ ---
99
+
100
+ ## Licensing
101
+
102
+ Proprietary software. Copyright (c) 2026 Abdelnasser Aitabi. All rights reserved. No license is granted except by separate written agreement.
@@ -5,7 +5,7 @@
5
5
  * Handles waypoint chunking internally for routes with >25 waypoints.
6
6
  */
7
7
  type Coordinate = [number, number];
8
- export interface MapboxDirectionsConfig {
8
+ interface MapboxDirectionsConfig {
9
9
  /** Mapbox access token */
10
10
  accessToken: string;
11
11
  /** Routing profile (default: 'driving') */
@@ -47,7 +47,7 @@ interface RouteOptions {
47
47
  overview?: string;
48
48
  language?: string;
49
49
  }
50
- export declare class MapboxDirectionsProvider {
50
+ declare class MapboxDirectionsProvider {
51
51
  private readonly config;
52
52
  constructor(config: MapboxDirectionsConfig);
53
53
  /**
@@ -64,5 +64,5 @@ export declare class MapboxDirectionsProvider {
64
64
  */
65
65
  private fetchChunkedRoute;
66
66
  }
67
- export {};
68
- //# sourceMappingURL=MapboxDirectionsProvider.d.ts.map
67
+
68
+ export { type MapboxDirectionsConfig, MapboxDirectionsProvider };
package/dist/index.d.ts CHANGED
@@ -1,9 +1,68 @@
1
1
  /**
2
- * @navcore/mapbox — Mapbox GL adapters
2
+ * MapboxDirectionsProvider
3
3
  *
4
- * Provides NavCoreRenderer and DirectionsProvider implementations
5
- * for Mapbox GL (via @rnmapbox/maps for React Native, or mapbox-gl for web).
4
+ * Implements DirectionsProvider using the Mapbox Directions API.
5
+ * Handles waypoint chunking internally for routes with >25 waypoints.
6
6
  */
7
- export { MapboxDirectionsProvider } from './MapboxDirectionsProvider';
8
- export type { MapboxDirectionsConfig } from './MapboxDirectionsProvider';
9
- //# sourceMappingURL=index.d.ts.map
7
+ type Coordinate = [number, number];
8
+ interface MapboxDirectionsConfig {
9
+ /** Mapbox access token */
10
+ accessToken: string;
11
+ /** Routing profile (default: 'driving') */
12
+ profile?: 'driving' | 'driving-traffic' | 'cycling' | 'walking';
13
+ /** API base URL (default: Mapbox production) */
14
+ baseUrl?: string;
15
+ /** Max waypoints per API call (default: 25, Mapbox limit) */
16
+ maxWaypointsPerChunk?: number;
17
+ }
18
+ interface RouteResult {
19
+ geometry: Coordinate[];
20
+ distance: number;
21
+ duration: number;
22
+ legs: Array<{
23
+ distance: number;
24
+ duration: number;
25
+ steps?: Array<{
26
+ distance: number;
27
+ duration: number;
28
+ geometry: Coordinate[];
29
+ maneuver: {
30
+ type: string;
31
+ modifier?: string;
32
+ location: Coordinate;
33
+ bearing_before: number;
34
+ bearing_after: number;
35
+ instruction?: string;
36
+ };
37
+ name?: string;
38
+ }>;
39
+ }>;
40
+ raw?: unknown;
41
+ }
42
+ interface RouteOptions {
43
+ profile?: string;
44
+ alternatives?: boolean;
45
+ steps?: boolean;
46
+ geometries?: string;
47
+ overview?: string;
48
+ language?: string;
49
+ }
50
+ declare class MapboxDirectionsProvider {
51
+ private readonly config;
52
+ constructor(config: MapboxDirectionsConfig);
53
+ /**
54
+ * Get a route through the given waypoints.
55
+ * Automatically chunks if waypoints exceed the Mapbox limit.
56
+ */
57
+ getRoute(waypoints: Coordinate[], options?: RouteOptions): Promise<RouteResult>;
58
+ /**
59
+ * Fetch a single route segment from the Mapbox API.
60
+ */
61
+ private fetchRoute;
62
+ /**
63
+ * Fetch multi-segment route, stitching chunks with OVERLAP_1 strategy.
64
+ */
65
+ private fetchChunkedRoute;
66
+ }
67
+
68
+ export { type MapboxDirectionsConfig, MapboxDirectionsProvider };
package/dist/index.js CHANGED
@@ -1,7 +1,121 @@
1
- /**
2
- * @navcore/mapbox — Mapbox GL adapters
3
- *
4
- * Provides NavCoreRenderer and DirectionsProvider implementations
5
- * for Mapbox GL (via @rnmapbox/maps for React Native, or mapbox-gl for web).
6
- */
7
- export { MapboxDirectionsProvider } from './MapboxDirectionsProvider';
1
+ 'use strict';
2
+
3
+ // src/MapboxDirectionsProvider.ts
4
+ var DEFAULT_BASE_URL = "https://api.mapbox.com/directions/v5/mapbox";
5
+ var MAX_WAYPOINTS = 25;
6
+ var MapboxDirectionsProvider = class {
7
+ constructor(config) {
8
+ this.config = {
9
+ accessToken: config.accessToken,
10
+ profile: config.profile ?? "driving",
11
+ baseUrl: config.baseUrl ?? DEFAULT_BASE_URL,
12
+ maxWaypointsPerChunk: config.maxWaypointsPerChunk ?? MAX_WAYPOINTS
13
+ };
14
+ }
15
+ /**
16
+ * Get a route through the given waypoints.
17
+ * Automatically chunks if waypoints exceed the Mapbox limit.
18
+ */
19
+ async getRoute(waypoints, options) {
20
+ if (waypoints.length < 2) {
21
+ throw new Error("At least 2 waypoints are required");
22
+ }
23
+ if (waypoints.length <= this.config.maxWaypointsPerChunk) {
24
+ return this.fetchRoute(waypoints, options);
25
+ }
26
+ return this.fetchChunkedRoute(waypoints, options);
27
+ }
28
+ /**
29
+ * Fetch a single route segment from the Mapbox API.
30
+ */
31
+ async fetchRoute(waypoints, options) {
32
+ const profile = options?.profile ?? this.config.profile;
33
+ const coords = waypoints.map((w) => `${w[0]},${w[1]}`).join(";");
34
+ const params = new URLSearchParams({
35
+ access_token: this.config.accessToken,
36
+ geometries: options?.geometries ?? "geojson",
37
+ overview: options?.overview ?? "full",
38
+ steps: String(options?.steps ?? true),
39
+ alternatives: String(options?.alternatives ?? false)
40
+ });
41
+ if (options?.language) {
42
+ params.set("language", options.language);
43
+ }
44
+ const url = `${this.config.baseUrl}/${profile}/${coords}?${params}`;
45
+ const response = await fetch(url);
46
+ if (!response.ok) {
47
+ throw new Error(`Mapbox API error: ${response.status} ${response.statusText}`);
48
+ }
49
+ const data = await response.json();
50
+ if (!data.routes || data.routes.length === 0) {
51
+ throw new Error("No route found");
52
+ }
53
+ const route = data.routes[0];
54
+ return {
55
+ geometry: route.geometry.coordinates,
56
+ distance: route.distance,
57
+ duration: route.duration,
58
+ legs: route.legs.map((leg) => ({
59
+ distance: leg.distance,
60
+ duration: leg.duration,
61
+ steps: leg.steps?.map((step) => ({
62
+ distance: step.distance,
63
+ duration: step.duration,
64
+ geometry: step.geometry?.coordinates ?? [],
65
+ maneuver: {
66
+ type: step.maneuver?.type ?? "unknown",
67
+ modifier: step.maneuver?.modifier,
68
+ location: step.maneuver?.location ?? [0, 0],
69
+ bearing_before: step.maneuver?.bearing_before ?? 0,
70
+ bearing_after: step.maneuver?.bearing_after ?? 0,
71
+ instruction: step.maneuver?.instruction
72
+ },
73
+ name: step.name
74
+ }))
75
+ })),
76
+ raw: data
77
+ };
78
+ }
79
+ /**
80
+ * Fetch multi-segment route, stitching chunks with OVERLAP_1 strategy.
81
+ */
82
+ async fetchChunkedRoute(waypoints, options) {
83
+ const chunkSize = this.config.maxWaypointsPerChunk;
84
+ const step = chunkSize - 1;
85
+ const chunks = [];
86
+ for (let i = 0; i < waypoints.length; i += step) {
87
+ const chunk = waypoints.slice(i, i + chunkSize);
88
+ if (chunk.length < 2) break;
89
+ chunks.push(chunk);
90
+ if (i + chunkSize >= waypoints.length) break;
91
+ }
92
+ const results = await Promise.all(
93
+ chunks.map((chunk) => this.fetchRoute(chunk, options))
94
+ );
95
+ const stitchedGeometry = [];
96
+ let totalDistance = 0;
97
+ let totalDuration = 0;
98
+ const allLegs = [];
99
+ for (let i = 0; i < results.length; i++) {
100
+ const result = results[i];
101
+ const geom = result.geometry;
102
+ if (i === 0) {
103
+ stitchedGeometry.push(...geom);
104
+ } else {
105
+ stitchedGeometry.push(...geom.slice(1));
106
+ }
107
+ totalDistance += result.distance;
108
+ totalDuration += result.duration;
109
+ allLegs.push(...result.legs);
110
+ }
111
+ return {
112
+ geometry: stitchedGeometry,
113
+ distance: totalDistance,
114
+ duration: totalDuration,
115
+ legs: allLegs,
116
+ raw: results.map((r) => r.raw)
117
+ };
118
+ }
119
+ };
120
+
121
+ exports.MapboxDirectionsProvider = MapboxDirectionsProvider;
package/dist/index.mjs ADDED
@@ -0,0 +1,119 @@
1
+ // src/MapboxDirectionsProvider.ts
2
+ var DEFAULT_BASE_URL = "https://api.mapbox.com/directions/v5/mapbox";
3
+ var MAX_WAYPOINTS = 25;
4
+ var MapboxDirectionsProvider = class {
5
+ constructor(config) {
6
+ this.config = {
7
+ accessToken: config.accessToken,
8
+ profile: config.profile ?? "driving",
9
+ baseUrl: config.baseUrl ?? DEFAULT_BASE_URL,
10
+ maxWaypointsPerChunk: config.maxWaypointsPerChunk ?? MAX_WAYPOINTS
11
+ };
12
+ }
13
+ /**
14
+ * Get a route through the given waypoints.
15
+ * Automatically chunks if waypoints exceed the Mapbox limit.
16
+ */
17
+ async getRoute(waypoints, options) {
18
+ if (waypoints.length < 2) {
19
+ throw new Error("At least 2 waypoints are required");
20
+ }
21
+ if (waypoints.length <= this.config.maxWaypointsPerChunk) {
22
+ return this.fetchRoute(waypoints, options);
23
+ }
24
+ return this.fetchChunkedRoute(waypoints, options);
25
+ }
26
+ /**
27
+ * Fetch a single route segment from the Mapbox API.
28
+ */
29
+ async fetchRoute(waypoints, options) {
30
+ const profile = options?.profile ?? this.config.profile;
31
+ const coords = waypoints.map((w) => `${w[0]},${w[1]}`).join(";");
32
+ const params = new URLSearchParams({
33
+ access_token: this.config.accessToken,
34
+ geometries: options?.geometries ?? "geojson",
35
+ overview: options?.overview ?? "full",
36
+ steps: String(options?.steps ?? true),
37
+ alternatives: String(options?.alternatives ?? false)
38
+ });
39
+ if (options?.language) {
40
+ params.set("language", options.language);
41
+ }
42
+ const url = `${this.config.baseUrl}/${profile}/${coords}?${params}`;
43
+ const response = await fetch(url);
44
+ if (!response.ok) {
45
+ throw new Error(`Mapbox API error: ${response.status} ${response.statusText}`);
46
+ }
47
+ const data = await response.json();
48
+ if (!data.routes || data.routes.length === 0) {
49
+ throw new Error("No route found");
50
+ }
51
+ const route = data.routes[0];
52
+ return {
53
+ geometry: route.geometry.coordinates,
54
+ distance: route.distance,
55
+ duration: route.duration,
56
+ legs: route.legs.map((leg) => ({
57
+ distance: leg.distance,
58
+ duration: leg.duration,
59
+ steps: leg.steps?.map((step) => ({
60
+ distance: step.distance,
61
+ duration: step.duration,
62
+ geometry: step.geometry?.coordinates ?? [],
63
+ maneuver: {
64
+ type: step.maneuver?.type ?? "unknown",
65
+ modifier: step.maneuver?.modifier,
66
+ location: step.maneuver?.location ?? [0, 0],
67
+ bearing_before: step.maneuver?.bearing_before ?? 0,
68
+ bearing_after: step.maneuver?.bearing_after ?? 0,
69
+ instruction: step.maneuver?.instruction
70
+ },
71
+ name: step.name
72
+ }))
73
+ })),
74
+ raw: data
75
+ };
76
+ }
77
+ /**
78
+ * Fetch multi-segment route, stitching chunks with OVERLAP_1 strategy.
79
+ */
80
+ async fetchChunkedRoute(waypoints, options) {
81
+ const chunkSize = this.config.maxWaypointsPerChunk;
82
+ const step = chunkSize - 1;
83
+ const chunks = [];
84
+ for (let i = 0; i < waypoints.length; i += step) {
85
+ const chunk = waypoints.slice(i, i + chunkSize);
86
+ if (chunk.length < 2) break;
87
+ chunks.push(chunk);
88
+ if (i + chunkSize >= waypoints.length) break;
89
+ }
90
+ const results = await Promise.all(
91
+ chunks.map((chunk) => this.fetchRoute(chunk, options))
92
+ );
93
+ const stitchedGeometry = [];
94
+ let totalDistance = 0;
95
+ let totalDuration = 0;
96
+ const allLegs = [];
97
+ for (let i = 0; i < results.length; i++) {
98
+ const result = results[i];
99
+ const geom = result.geometry;
100
+ if (i === 0) {
101
+ stitchedGeometry.push(...geom);
102
+ } else {
103
+ stitchedGeometry.push(...geom.slice(1));
104
+ }
105
+ totalDistance += result.distance;
106
+ totalDuration += result.duration;
107
+ allLegs.push(...result.legs);
108
+ }
109
+ return {
110
+ geometry: stitchedGeometry,
111
+ distance: totalDistance,
112
+ duration: totalDuration,
113
+ legs: allLegs,
114
+ raw: results.map((r) => r.raw)
115
+ };
116
+ }
117
+ };
118
+
119
+ export { MapboxDirectionsProvider };
package/package.json CHANGED
@@ -1,52 +1,44 @@
1
- {
2
- "name": "@ingissa/navcore-mapbox",
3
- "version": "1.0.8",
4
- "description": "Mapbox GL adapter for NavCore — renderer and directions provider.",
5
- "author": "Ingissa <8abnav@gmail.com>",
6
- "license": "SEE LICENSE IN LICENSE",
7
- "homepage": "https://navcore.dev",
8
- "repository": {
9
- "type": "git",
10
- "url": "git+https://github.com/ingissa/navcore-sdk.git"
11
- },
12
- "bugs": {
13
- "url": "https://github.com/ingissa/navcore-sdk/issues"
14
- },
15
- "main": "dist/index.js",
16
- "module": "dist/index.mjs",
17
- "types": "dist/index.d.ts",
18
- "exports": {
19
- ".": {
20
- "import": "./dist/index.mjs",
21
- "require": "./dist/index.js",
22
- "types": "./dist/index.d.ts"
23
- }
24
- },
25
- "files": [
26
- "dist",
27
- "src",
28
- "LICENSE"
29
- ],
30
- "sideEffects": false,
31
- "publishConfig": {
32
- "access": "public"
33
- },
34
- "engines": {
35
- "node": ">=16"
36
- },
37
- "scripts": {
38
- "build": "tsc -p tsconfig.build.json",
39
- "lint": "tsc --noEmit",
40
- "prepublishOnly": "npm run build"
41
- },
42
- "peerDependencies": {
43
- "@ingissa/navcore-core": "^1.0.8",
44
- "@rnmapbox/maps": ">=10.0.0"
45
- },
46
- "peerDependenciesMeta": {
47
- "@rnmapbox/maps": {
48
- "optional": true
49
- }
50
- },
51
- "dependencies": {}
52
- }
1
+ {
2
+ "name": "@ingissa/navcore-mapbox",
3
+ "version": "1.0.10",
4
+ "description": "Mapbox GL adapter for NavCore — renderer and directions provider.",
5
+ "author": "Ingissa <8abnav@gmail.com>",
6
+ "license": "SEE LICENSE IN LICENSE",
7
+ "homepage": "https://navcore-web.vercel.app/",
8
+ "main": "dist/index.js",
9
+ "module": "dist/index.mjs",
10
+ "types": "dist/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/index.d.ts",
14
+ "import": "./dist/index.mjs",
15
+ "require": "./dist/index.js"
16
+ }
17
+ },
18
+ "files": [
19
+ "dist",
20
+ "LICENSE"
21
+ ],
22
+ "sideEffects": false,
23
+ "publishConfig": {
24
+ "access": "public"
25
+ },
26
+ "engines": {
27
+ "node": ">=16"
28
+ },
29
+ "scripts": {
30
+ "build": "tsup",
31
+ "lint": "tsc --noEmit",
32
+ "prepublishOnly": "npm run build --prefix ../.. && node ../../scripts/check-release.mjs"
33
+ },
34
+ "peerDependencies": {
35
+ "@ingissa/navcore-core": "^1.0.10",
36
+ "@rnmapbox/maps": ">=10.0.0"
37
+ },
38
+ "peerDependenciesMeta": {
39
+ "@rnmapbox/maps": {
40
+ "optional": true
41
+ }
42
+ },
43
+ "dependencies": {}
44
+ }
@@ -1 +0,0 @@
1
- {"version":3,"file":"MapboxDirectionsProvider.d.ts","sourceRoot":"","sources":["../src/MapboxDirectionsProvider.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,KAAK,UAAU,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAEnC,MAAM,WAAW,sBAAsB;IACrC,0BAA0B;IAC1B,WAAW,EAAE,MAAM,CAAC;IACpB,2CAA2C;IAC3C,OAAO,CAAC,EAAE,SAAS,GAAG,iBAAiB,GAAG,SAAS,GAAG,SAAS,CAAC;IAChE,gDAAgD;IAChD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,6DAA6D;IAC7D,oBAAoB,CAAC,EAAE,MAAM,CAAC;CAC/B;AAED,UAAU,WAAW;IACnB,QAAQ,EAAE,UAAU,EAAE,CAAC;IACvB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,KAAK,CAAC;QACV,QAAQ,EAAE,MAAM,CAAC;QACjB,QAAQ,EAAE,MAAM,CAAC;QACjB,KAAK,CAAC,EAAE,KAAK,CAAC;YACZ,QAAQ,EAAE,MAAM,CAAC;YACjB,QAAQ,EAAE,MAAM,CAAC;YACjB,QAAQ,EAAE,UAAU,EAAE,CAAC;YACvB,QAAQ,EAAE;gBACR,IAAI,EAAE,MAAM,CAAC;gBACb,QAAQ,CAAC,EAAE,MAAM,CAAC;gBAClB,QAAQ,EAAE,UAAU,CAAC;gBACrB,cAAc,EAAE,MAAM,CAAC;gBACvB,aAAa,EAAE,MAAM,CAAC;gBACtB,WAAW,CAAC,EAAE,MAAM,CAAC;aACtB,CAAC;YACF,IAAI,CAAC,EAAE,MAAM,CAAC;SACf,CAAC,CAAC;KACJ,CAAC,CAAC;IACH,GAAG,CAAC,EAAE,OAAO,CAAC;CACf;AAED,UAAU,YAAY;IACpB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAKD,qBAAa,wBAAwB;IACnC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAmC;gBAE9C,MAAM,EAAE,sBAAsB;IAS1C;;;OAGG;IACG,QAAQ,CAAC,SAAS,EAAE,UAAU,EAAE,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,WAAW,CAAC;IAcrF;;OAEG;YACW,UAAU;IAyDxB;;OAEG;YACW,iBAAiB;CA+ChC"}
@@ -1,128 +0,0 @@
1
- /**
2
- * MapboxDirectionsProvider
3
- *
4
- * Implements DirectionsProvider using the Mapbox Directions API.
5
- * Handles waypoint chunking internally for routes with >25 waypoints.
6
- */
7
- const DEFAULT_BASE_URL = 'https://api.mapbox.com/directions/v5/mapbox';
8
- const MAX_WAYPOINTS = 25;
9
- export class MapboxDirectionsProvider {
10
- constructor(config) {
11
- this.config = {
12
- accessToken: config.accessToken,
13
- profile: config.profile ?? 'driving',
14
- baseUrl: config.baseUrl ?? DEFAULT_BASE_URL,
15
- maxWaypointsPerChunk: config.maxWaypointsPerChunk ?? MAX_WAYPOINTS,
16
- };
17
- }
18
- /**
19
- * Get a route through the given waypoints.
20
- * Automatically chunks if waypoints exceed the Mapbox limit.
21
- */
22
- async getRoute(waypoints, options) {
23
- if (waypoints.length < 2) {
24
- throw new Error('At least 2 waypoints are required');
25
- }
26
- // If within limit, single request
27
- if (waypoints.length <= this.config.maxWaypointsPerChunk) {
28
- return this.fetchRoute(waypoints, options);
29
- }
30
- // Otherwise chunk with OVERLAP_1 strategy
31
- return this.fetchChunkedRoute(waypoints, options);
32
- }
33
- /**
34
- * Fetch a single route segment from the Mapbox API.
35
- */
36
- async fetchRoute(waypoints, options) {
37
- const profile = options?.profile ?? this.config.profile;
38
- const coords = waypoints.map(w => `${w[0]},${w[1]}`).join(';');
39
- const params = new URLSearchParams({
40
- access_token: this.config.accessToken,
41
- geometries: options?.geometries ?? 'geojson',
42
- overview: options?.overview ?? 'full',
43
- steps: String(options?.steps ?? true),
44
- alternatives: String(options?.alternatives ?? false),
45
- });
46
- if (options?.language) {
47
- params.set('language', options.language);
48
- }
49
- const url = `${this.config.baseUrl}/${profile}/${coords}?${params}`;
50
- const response = await fetch(url);
51
- if (!response.ok) {
52
- throw new Error(`Mapbox API error: ${response.status} ${response.statusText}`);
53
- }
54
- const data = await response.json();
55
- if (!data.routes || data.routes.length === 0) {
56
- throw new Error('No route found');
57
- }
58
- const route = data.routes[0];
59
- return {
60
- geometry: route.geometry.coordinates,
61
- distance: route.distance,
62
- duration: route.duration,
63
- legs: route.legs.map((leg) => ({
64
- distance: leg.distance,
65
- duration: leg.duration,
66
- steps: leg.steps?.map((step) => ({
67
- distance: step.distance,
68
- duration: step.duration,
69
- geometry: step.geometry?.coordinates ?? [],
70
- maneuver: {
71
- type: step.maneuver?.type ?? 'unknown',
72
- modifier: step.maneuver?.modifier,
73
- location: step.maneuver?.location ?? [0, 0],
74
- bearing_before: step.maneuver?.bearing_before ?? 0,
75
- bearing_after: step.maneuver?.bearing_after ?? 0,
76
- instruction: step.maneuver?.instruction,
77
- },
78
- name: step.name,
79
- })),
80
- })),
81
- raw: data,
82
- };
83
- }
84
- /**
85
- * Fetch multi-segment route, stitching chunks with OVERLAP_1 strategy.
86
- */
87
- async fetchChunkedRoute(waypoints, options) {
88
- const chunkSize = this.config.maxWaypointsPerChunk;
89
- const step = chunkSize - 1; // OVERLAP_1
90
- const chunks = [];
91
- for (let i = 0; i < waypoints.length; i += step) {
92
- const chunk = waypoints.slice(i, i + chunkSize);
93
- if (chunk.length < 2)
94
- break;
95
- chunks.push(chunk);
96
- if (i + chunkSize >= waypoints.length)
97
- break;
98
- }
99
- // Fetch all chunks in parallel
100
- const results = await Promise.all(chunks.map(chunk => this.fetchRoute(chunk, options)));
101
- // Stitch geometries — drop first point of subsequent chunks (it's the overlap)
102
- const stitchedGeometry = [];
103
- let totalDistance = 0;
104
- let totalDuration = 0;
105
- const allLegs = [];
106
- for (let i = 0; i < results.length; i++) {
107
- const result = results[i];
108
- const geom = result.geometry;
109
- if (i === 0) {
110
- stitchedGeometry.push(...geom);
111
- }
112
- else {
113
- // Skip first point (overlap with previous chunk's last point)
114
- stitchedGeometry.push(...geom.slice(1));
115
- }
116
- totalDistance += result.distance;
117
- totalDuration += result.duration;
118
- allLegs.push(...result.legs);
119
- }
120
- return {
121
- geometry: stitchedGeometry,
122
- distance: totalDistance,
123
- duration: totalDuration,
124
- legs: allLegs,
125
- raw: results.map(r => r.raw),
126
- };
127
- }
128
- }
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,wBAAwB,EAAE,MAAM,4BAA4B,CAAC;AACtE,YAAY,EAAE,sBAAsB,EAAE,MAAM,4BAA4B,CAAC"}
@@ -1,198 +0,0 @@
1
- /**
2
- * MapboxDirectionsProvider
3
- *
4
- * Implements DirectionsProvider using the Mapbox Directions API.
5
- * Handles waypoint chunking internally for routes with >25 waypoints.
6
- */
7
-
8
- type Coordinate = [number, number];
9
-
10
- export interface MapboxDirectionsConfig {
11
- /** Mapbox access token */
12
- accessToken: string;
13
- /** Routing profile (default: 'driving') */
14
- profile?: 'driving' | 'driving-traffic' | 'cycling' | 'walking';
15
- /** API base URL (default: Mapbox production) */
16
- baseUrl?: string;
17
- /** Max waypoints per API call (default: 25, Mapbox limit) */
18
- maxWaypointsPerChunk?: number;
19
- }
20
-
21
- interface RouteResult {
22
- geometry: Coordinate[];
23
- distance: number;
24
- duration: number;
25
- legs: Array<{
26
- distance: number;
27
- duration: number;
28
- steps?: Array<{
29
- distance: number;
30
- duration: number;
31
- geometry: Coordinate[];
32
- maneuver: {
33
- type: string;
34
- modifier?: string;
35
- location: Coordinate;
36
- bearing_before: number;
37
- bearing_after: number;
38
- instruction?: string;
39
- };
40
- name?: string;
41
- }>;
42
- }>;
43
- raw?: unknown;
44
- }
45
-
46
- interface RouteOptions {
47
- profile?: string;
48
- alternatives?: boolean;
49
- steps?: boolean;
50
- geometries?: string;
51
- overview?: string;
52
- language?: string;
53
- }
54
-
55
- const DEFAULT_BASE_URL = 'https://api.mapbox.com/directions/v5/mapbox';
56
- const MAX_WAYPOINTS = 25;
57
-
58
- export class MapboxDirectionsProvider {
59
- private readonly config: Required<MapboxDirectionsConfig>;
60
-
61
- constructor(config: MapboxDirectionsConfig) {
62
- this.config = {
63
- accessToken: config.accessToken,
64
- profile: config.profile ?? 'driving',
65
- baseUrl: config.baseUrl ?? DEFAULT_BASE_URL,
66
- maxWaypointsPerChunk: config.maxWaypointsPerChunk ?? MAX_WAYPOINTS,
67
- };
68
- }
69
-
70
- /**
71
- * Get a route through the given waypoints.
72
- * Automatically chunks if waypoints exceed the Mapbox limit.
73
- */
74
- async getRoute(waypoints: Coordinate[], options?: RouteOptions): Promise<RouteResult> {
75
- if (waypoints.length < 2) {
76
- throw new Error('At least 2 waypoints are required');
77
- }
78
-
79
- // If within limit, single request
80
- if (waypoints.length <= this.config.maxWaypointsPerChunk) {
81
- return this.fetchRoute(waypoints, options);
82
- }
83
-
84
- // Otherwise chunk with OVERLAP_1 strategy
85
- return this.fetchChunkedRoute(waypoints, options);
86
- }
87
-
88
- /**
89
- * Fetch a single route segment from the Mapbox API.
90
- */
91
- private async fetchRoute(waypoints: Coordinate[], options?: RouteOptions): Promise<RouteResult> {
92
- const profile = options?.profile ?? this.config.profile;
93
- const coords = waypoints.map(w => `${w[0]},${w[1]}`).join(';');
94
-
95
- const params = new URLSearchParams({
96
- access_token: this.config.accessToken,
97
- geometries: options?.geometries ?? 'geojson',
98
- overview: options?.overview ?? 'full',
99
- steps: String(options?.steps ?? true),
100
- alternatives: String(options?.alternatives ?? false),
101
- });
102
-
103
- if (options?.language) {
104
- params.set('language', options.language);
105
- }
106
-
107
- const url = `${this.config.baseUrl}/${profile}/${coords}?${params}`;
108
- const response = await fetch(url);
109
-
110
- if (!response.ok) {
111
- throw new Error(`Mapbox API error: ${response.status} ${response.statusText}`);
112
- }
113
-
114
- const data = await response.json();
115
-
116
- if (!data.routes || data.routes.length === 0) {
117
- throw new Error('No route found');
118
- }
119
-
120
- const route = data.routes[0];
121
-
122
- return {
123
- geometry: route.geometry.coordinates as Coordinate[],
124
- distance: route.distance,
125
- duration: route.duration,
126
- legs: route.legs.map((leg: { distance: number; duration: number; steps?: unknown[] }) => ({
127
- distance: leg.distance,
128
- duration: leg.duration,
129
- steps: leg.steps?.map((step: any) => ({
130
- distance: step.distance,
131
- duration: step.duration,
132
- geometry: step.geometry?.coordinates ?? [],
133
- maneuver: {
134
- type: step.maneuver?.type ?? 'unknown',
135
- modifier: step.maneuver?.modifier,
136
- location: step.maneuver?.location ?? [0, 0],
137
- bearing_before: step.maneuver?.bearing_before ?? 0,
138
- bearing_after: step.maneuver?.bearing_after ?? 0,
139
- instruction: step.maneuver?.instruction,
140
- },
141
- name: step.name,
142
- })),
143
- })),
144
- raw: data,
145
- };
146
- }
147
-
148
- /**
149
- * Fetch multi-segment route, stitching chunks with OVERLAP_1 strategy.
150
- */
151
- private async fetchChunkedRoute(waypoints: Coordinate[], options?: RouteOptions): Promise<RouteResult> {
152
- const chunkSize = this.config.maxWaypointsPerChunk;
153
- const step = chunkSize - 1; // OVERLAP_1
154
- const chunks: Coordinate[][] = [];
155
-
156
- for (let i = 0; i < waypoints.length; i += step) {
157
- const chunk = waypoints.slice(i, i + chunkSize);
158
- if (chunk.length < 2) break;
159
- chunks.push(chunk);
160
- if (i + chunkSize >= waypoints.length) break;
161
- }
162
-
163
- // Fetch all chunks in parallel
164
- const results = await Promise.all(
165
- chunks.map(chunk => this.fetchRoute(chunk, options))
166
- );
167
-
168
- // Stitch geometries — drop first point of subsequent chunks (it's the overlap)
169
- const stitchedGeometry: Coordinate[] = [];
170
- let totalDistance = 0;
171
- let totalDuration = 0;
172
- const allLegs: RouteResult['legs'] = [];
173
-
174
- for (let i = 0; i < results.length; i++) {
175
- const result = results[i]!;
176
- const geom = result.geometry;
177
-
178
- if (i === 0) {
179
- stitchedGeometry.push(...geom);
180
- } else {
181
- // Skip first point (overlap with previous chunk's last point)
182
- stitchedGeometry.push(...geom.slice(1));
183
- }
184
-
185
- totalDistance += result.distance;
186
- totalDuration += result.duration;
187
- allLegs.push(...result.legs);
188
- }
189
-
190
- return {
191
- geometry: stitchedGeometry,
192
- distance: totalDistance,
193
- duration: totalDuration,
194
- legs: allLegs,
195
- raw: results.map(r => r.raw),
196
- };
197
- }
198
- }
package/src/index.ts DELETED
@@ -1,9 +0,0 @@
1
- /**
2
- * @navcore/mapbox — Mapbox GL adapters
3
- *
4
- * Provides NavCoreRenderer and DirectionsProvider implementations
5
- * for Mapbox GL (via @rnmapbox/maps for React Native, or mapbox-gl for web).
6
- */
7
-
8
- export { MapboxDirectionsProvider } from './MapboxDirectionsProvider';
9
- export type { MapboxDirectionsConfig } from './MapboxDirectionsProvider';