@geoapify/route-planner-sdk
Version:
TypeScript SDK for the Geoapify Route Planner API. Supports route optimization, delivery planning, and timeline visualization in browser and Node.js
99 lines (98 loc) • 3.21 kB
JavaScript
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 { universalFetch } from './tools/fetch';
import { RoutePlannerError } from "./models";
import { Utils } from "./tools/utils";
import { RoutePlannerResult } from "./models/entities/route-planner-result";
export class RoutePlanner {
constructor(options, raw) {
this.options = options;
if (!this.options.baseUrl) {
this.options.baseUrl = 'https://api.geoapify.com';
}
if (raw) {
this.raw = raw;
}
else {
this.raw = {
agents: [],
jobs: [],
shipments: [],
locations: [],
avoid: []
};
}
}
getRaw() {
return this.raw;
}
setRaw(value) {
this.raw = value;
return this;
}
setMode(mode) {
this.raw.mode = mode;
return this;
}
addAgent(agent) {
this.raw.agents.push(agent.getRaw());
return this;
}
addJob(job) {
this.raw.jobs.push(job.getRaw());
return this;
}
addLocation(location) {
this.raw.locations.push(location.getRaw());
return this;
}
addShipment(shipment) {
this.raw.shipments.push(shipment.getRaw());
return this;
}
addAvoid(avoid) {
this.raw.avoid.push(avoid.getRaw());
return this;
}
setTraffic(traffic) {
this.raw.traffic = traffic;
return this;
}
setType(type) {
this.raw.type = type;
return this;
}
setMaxSpeed(max_speed) {
this.raw.max_speed = max_speed;
return this;
}
setUnits(units) {
this.raw.units = units;
return this;
}
plan() {
return __awaiter(this, void 0, void 0, function* () {
const requestBody = Utils.cleanObject(this.raw);
const response = yield universalFetch(`${this.options.baseUrl}/v1/routeplanner?apiKey=${this.options.apiKey}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(requestBody)
});
if (!response.ok) {
let errorResponse = yield response.json();
throw new RoutePlannerError(errorResponse.error, errorResponse.message, errorResponse);
}
let responseData = yield response.json();
return new RoutePlannerResult(this.options, responseData);
});
}
}