UNPKG

@geoapify/route-planner-sdk

Version:

A TypeScript SDK for the Geoapify Route Planner API that simplifies route optimization requests, and helps visualize and edit resulting routes.

157 lines (156 loc) 5.39 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { AgentSolution } from "./nested/result/agent-solution"; import { RouteActionInfo } from "./nested/result/route-action-info"; /** * Provides convenient methods for reading Route Planner API results. */ export class RoutePlannerResult { constructor(options, rawData) { this.rawData = rawData; this.options = options; } /** * Returns the raw API response. */ getRaw() { return this.rawData; } /** * Returns a list of all assigned agent solutions. */ getAgentSolutions() { return this.rawData.agents.map(agent => new AgentSolution(agent)); } /** * Finds an agent's solution by their ID. */ getAgentSolution(agentId) { let agentFound = this.rawData.agents.find(agent => agent.agentId === agentId); if (agentFound === undefined) { return undefined; } else { return new AgentSolution(agentFound); } } /** * Retrieves all waypoints of a specific agent. */ getAgentWaypoints(agentId) { const agent = this.getAgentSolution(agentId); return agent ? agent.getWaypoints() : []; } /** * Retrieves all route actions of a specific agent. */ getAgentRouteActions(agentId) { const agent = this.getAgentSolution(agentId); return agent ? agent.getActions() : []; } /** * Retrieves all route legs of a specific agent. */ getAgentRouteLegs(agentId) { const agent = this.getAgentSolution(agentId); return agent ? agent.getLegs() : []; } /** * Retrieves the options used to generate the result. */ getOptions() { return this.options; } /** * Retrieves all jobs assigned to a specific agent. */ getAgentJobs(agentId) { const agent = this.getAgentSolution(agentId); if (agent === undefined) { return []; } return agent.getActions() .filter(action => action.getJobId() !== undefined) .map(action => action.getJobId()); } /** * Retrieves all shipments assigned to a specific agent. */ getAgentShipments(agentId) { const agent = this.getAgentSolution(agentId); if (agent === undefined) { return []; } return agent.getActions() .filter(action => action.getShipmentId() !== undefined) .map(action => action.getShipmentId()); } /** * Retrieves unassigned agents. */ getUnassignedAgents() { return this.rawData.unassignedAgents ? this.rawData.unassignedAgents : []; } /** * Retrieves unassigned jobs. */ getUnassignedJobs() { return this.rawData.unassignedJobs ? this.rawData.unassignedJobs : []; } /** * Retrieves unassigned shipments. */ getUnassignedShipments() { return this.rawData.unassignedShipments ? this.rawData.unassignedShipments : []; } /** * Retrieves detailed information about a specific job. */ getJobInfo(jobId) { for (const agent of this.getAgentSolutions()) { for (const action of agent.getActions()) { if (action.getJobId() === jobId) { return new RouteActionInfo({ agentId: agent.getAgentId(), action: action, agent: agent }); } } } return undefined; // Job not found } /** * Retrieves detailed information about a specific shipment. */ getShipmentInfo(shipmentId) { for (const agent of this.getAgentSolutions()) { for (const action of agent.getActions()) { if (action.getShipmentId() === shipmentId) { return new RouteActionInfo({ agentId: agent.getAgentId(), action: action, agent: agent }); } } } return undefined; // Shipment not found } /** * Retrieves the route for a specific agent. * @param agentId - The ID of the agent. * @param mode */ getAgentRoute(agentId, mode) { return __awaiter(this, void 0, void 0, function* () { const agent = this.getAgentSolution(agentId); if (!agent) return undefined; let waypoints = agent.getWaypoints().map(waypoint => "lonlat:" + waypoint.getLocation()).join('|'); if (waypoints.length == 0) return undefined; const response = yield fetch(`${this.getOptions().baseUrl}/v1/routing?waypoints=${waypoints}&apiKey=${this.getOptions().apiKey}&mode=${mode}`); return yield response.json(); }); } }