@maplibre/maplibre-gl-directions
Advanced tools
Comparing version 0.1.5 to 0.1.6
@@ -81,2 +81,3 @@ var __defProp = Object.defineProperty; | ||
requestOptions: {}, | ||
requestTimeout: null, | ||
makePostRequest: false, | ||
@@ -671,2 +672,3 @@ sourceName: "maplibre-gl-directions", | ||
__publicField(this, "departSnappointIndex", -1); | ||
__publicField(this, "abortController"); | ||
this.map = map; | ||
@@ -708,23 +710,42 @@ this.configuration = buildConfiguration(configuration); | ||
async fetchDirections(originalEvent) { | ||
var _a; | ||
const prevInteractive = this.interactive; | ||
this.interactive = false; | ||
if (this._waypoints.length >= 2) { | ||
this.fire(new MapLibreGlDirectionsRoutingEvent("fetchroutesstart", originalEvent)); | ||
this.interactive = false; | ||
this.abortController = new AbortController(); | ||
let timer; | ||
if (this.configuration.requestTimeout !== null) { | ||
timer = setTimeout(() => { | ||
var _a2; | ||
return (_a2 = this.abortController) == null ? void 0 : _a2.abort(); | ||
}, this.configuration.requestTimeout); | ||
} | ||
const { method, url, payload } = this.buildRequest(this.configuration, this.waypointsCoordinates); | ||
let response; | ||
if (method === "get") { | ||
response = await fetch(`${url}?${payload}`).then((res) => res.json()); | ||
} else { | ||
response = await fetch(`${url}`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/x-www-form-urlencoded" | ||
}, | ||
body: payload | ||
}).then((res) => res.json()); | ||
try { | ||
if (method === "get") { | ||
response = await (await fetch(`${url}?${payload}`, { signal: this.abortController.signal })).json(); | ||
} else { | ||
response = await (await fetch(`${url}`, { | ||
signal: this.abortController.signal, | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/x-www-form-urlencoded" | ||
}, | ||
body: payload | ||
})).json(); | ||
} | ||
if (response.code !== "Ok") | ||
throw new Error((_a = response.message) != null ? _a : "An unexpected error occurred."); | ||
} finally { | ||
this.interactive = prevInteractive; | ||
this.abortController = void 0; | ||
clearTimeout(timer); | ||
this.fire(new MapLibreGlDirectionsRoutingEvent("fetchroutesend", originalEvent, response)); | ||
} | ||
this.snappoints = response.waypoints.map((snappoint, i) => { | ||
var _a; | ||
var _a2; | ||
return this.buildPoint(snappoint.location, "SNAPPOINT", { | ||
waypointProperties: (_a = this._waypoints[i].properties) != null ? _a : {} | ||
waypointProperties: (_a2 = this._waypoints[i].properties) != null ? _a2 : {} | ||
}); | ||
@@ -735,3 +756,2 @@ }); | ||
this.selectedRouteIndex = 0; | ||
this.fire(new MapLibreGlDirectionsRoutingEvent("fetchroutesend", originalEvent, response)); | ||
} else { | ||
@@ -742,3 +762,2 @@ this.snappoints = []; | ||
this.draw(false); | ||
this.interactive = prevInteractive; | ||
} | ||
@@ -1030,2 +1049,4 @@ draw(skipSelectedRouteRedraw = true) { | ||
async _addWaypoint(waypoint, index, originalEvent) { | ||
var _a; | ||
(_a = this.abortController) == null ? void 0 : _a.abort(); | ||
index = index != null ? index : this._waypoints.length; | ||
@@ -1042,2 +1063,4 @@ this._waypoints.splice(index, 0, this.buildPoint(waypoint, "WAYPOINT")); | ||
async _removeWaypoint(index, originalEvent) { | ||
var _a; | ||
(_a = this.abortController) == null ? void 0 : _a.abort(); | ||
this._waypoints.splice(index, 1); | ||
@@ -1079,2 +1102,4 @@ this.snappoints.splice(index, 1); | ||
async setWaypoints(waypoints) { | ||
var _a; | ||
(_a = this.abortController) == null ? void 0 : _a.abort(); | ||
this._waypoints = waypoints.map((waypoint) => { | ||
@@ -1081,0 +1106,0 @@ return this.buildPoint(waypoint, "WAYPOINT"); |
@@ -94,3 +94,4 @@ import type { Map, MapMouseEvent, MapTouchEvent } from "maplibre-gl"; | ||
* | ||
* Only presents when it's the {@link MapLibreGlDirectionsEventType.fetchroutesend|`fetchroutesend`} event. | ||
* Only presents when it's the {@link MapLibreGlDirectionsEventType.fetchroutesend|`fetchroutesend`} event, but might | ||
* be `undefined` in case the request to fetch directions failed. | ||
* | ||
@@ -97,0 +98,0 @@ * @see http://project-osrm.org/docs/v5.24.0/api/#responses |
@@ -95,2 +95,13 @@ import type { Map, MapMouseEvent, MapTouchEvent } from "maplibre-gl"; | ||
/** | ||
* A publicly-available abort-controller that allows to manually abort an ongoing routing-request. | ||
* | ||
* Only exists (`!== undefined`) when there's an ongoing routing-request. | ||
* | ||
* @example | ||
* ``` | ||
* direсtions.abortController.abort(); | ||
* ``` | ||
*/ | ||
abortController: AbortController | undefined; | ||
/** | ||
* Clears the map from all the instance's traces: waypoints, snappoints, routes, etc. | ||
@@ -97,0 +108,0 @@ */ |
@@ -77,2 +77,14 @@ import type { LayerSpecification } from "maplibre-gl"; | ||
/** | ||
* A timeout in ms after which a still-unresolved routing-request automatically gets aborted. | ||
* | ||
* @default `null` (no timeout) | ||
* | ||
* @example | ||
* ``` | ||
* // abort requests that take longer then 5s to complete | ||
* requestTimeout: 5000 | ||
* ``` | ||
*/ | ||
requestTimeout: number | null; | ||
/** | ||
* Whether to make a {@link https://docs.mapbox.com/api/navigation/http-post/|POST request} instead of a GET one. | ||
@@ -247,3 +259,4 @@ * | ||
export interface Directions { | ||
code: string; | ||
code: "Ok" | string; | ||
message?: string; | ||
routes: Route[]; | ||
@@ -250,0 +263,0 @@ waypoints: Snappoint[]; |
{ | ||
"name": "@maplibre/maplibre-gl-directions", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "homepage": "https://maplibre.org/maplibre-gl-directions/#/", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
298874
2193
2