UNPKG

libre-routing

Version:

This library was generated with [Nx](https://nx.dev).

85 lines 3.18 kB
import { __awaiter } from "tslib"; import { wrap } from 'comlink'; import { HereExecutor } from './here.executor'; const defaultOptions = { baseUrl: 'https://router.hereapi.com/v8/routes', transportMode: 'car', worker: true, shapePolylinePrecision: 0.0000065, generateRouteName: false, routeExcludeNotice: { critical: 'all', }, }; export class HereProvider { constructor(options) { this.options = Object.assign(Object.assign({}, defaultOptions), options); if (this.options.worker === true) { this.worker = new Worker(new URL('./here.worker', import.meta.url), { type: 'module', }); // @ts-ignore this.executorAPI = wrap(this.worker); } else { this.executorAPI = new HereExecutor(); } } destroy() { if (this.worker) { this.worker.terminate(); } } request(waypoints, opts) { const url = this.buildUrl(waypoints, opts); return this.executorAPI.request(Object.assign(Object.assign({ url }, opts), this.options)); } abortAllRequests() { return this.executorAPI.abortAllRequests(); } setOption(optionKey, value) { this.options[optionKey] = value; } hasPendingRequests() { return __awaiter(this, void 0, void 0, function* () { return this.executorAPI.hasPendingRequests(); }); } buildUrl(waypoints, opts) { var _a; const start = [...waypoints[0].originalPos].reverse(); const end = [...waypoints[waypoints.length - 1].originalPos].reverse(); const spans = this.options.spans ? this.options.spans.join(',') : null; const Return = opts.dragMode ? 'polyline' : [ ...(this.options.return || []), 'polyline', 'summary', 'routeLabels', ].join(','); const qpObj = Object.assign({ apiKey: this.options.apiKey, origin: start.toString(), destination: end.toString(), transportMode: this.options.transportMode || '', spans, return: Return, alternatives: !opts.dragMode ? (_a = opts === null || opts === void 0 ? void 0 : opts.alternatives) !== null && _a !== void 0 ? _a : 0 : 0, currency: this.options.currency || undefined }, this.options.queryParams); if (this.options.selectRouteStrategy === 'cheapest' && !qpObj.return.includes('tolls')) { qpObj.return += ',tolls'; } const queryParams = Object.keys(qpObj).reduce((acc, key) => { if (qpObj[key]) { acc[key] = qpObj[key]; } return acc; }, {}); let qp = new URLSearchParams(queryParams).toString(); if (waypoints.length > 2) { qp += `&${this.serializeWaypoints(waypoints)}`; } return `${this.options.baseUrl}?${qp}`; } serializeWaypoints(waypoints) { return waypoints .slice(1, waypoints.length - 1) .map((w) => `via=${[...w.originalPos].reverse()}`) .join('&'); } } //# sourceMappingURL=here.js.map