Source: bemap-services-v2/routing/bemap-routing-response.js

/**
 * BeNomad BeMap JavaScript API - RoutingResponse
 *
 * Container for the routes returned by `bemap.RoutingV2.calculate()`. When
 * the request was a MODE_MATRIX or MODE_ISOCHRONE call, the relevant fields
 * (`matrix`, `isochrone`) are populated and `routes` may be empty.
 *
 * @since 1.5.0
 * @public
 * @constructor
 */
bemap.RoutingResponse = function(options) {
    var opts = options || {};
    this.routes = Array.isArray(opts.routes) ? opts.routes : [];
    this.usedDestinations = Array.isArray(opts.usedDestinations) ? opts.usedDestinations : [];
    this.matrix = opts.matrix || null;
    this.isochrone = opts.isochrone || null;
    this.requestId = opts.requestId || null;
    this.warnings = Array.isArray(opts.warnings) ? opts.warnings : [];
};

bemap.RoutingResponse.prototype.getRoutes = function() { return this.routes; };
bemap.RoutingResponse.prototype.getFirstRoute = function() { return this.routes[0] || null; };
bemap.RoutingResponse.prototype.getUsedDestinations = function() { return this.usedDestinations; };
bemap.RoutingResponse.prototype.getMatrix = function() { return this.matrix; };
bemap.RoutingResponse.prototype.getIsochrone = function() { return this.isochrone; };
bemap.RoutingResponse.prototype.getRequestId = function() { return this.requestId; };
bemap.RoutingResponse.prototype.getWarnings = function() { return this.warnings; };

/**
 * Convenience accessor for the isochrone polygon. The BeMap server encodes
 * the isochrone area as the `polyline` array of the first route in
 * `routingRoutes[]` (length / duration are zero in that case). This helper
 * returns the polyline as an Array<bemap.Coordinate> ready to feed into a
 * `bemap.Polygon`, or `null` when no isochrone is present.
 *
 * Falls back to `this.isochrone` if a future server version exposes the
 * polygon under a dedicated top-level field.
 *
 * @public
 * @since 1.5.0
 * @return {Array.<bemap.Coordinate>|Object|null}
 */
bemap.RoutingResponse.prototype.getIsochronePolygon = function() {
    if (this.isochrone) return this.isochrone;
    var route = this.getFirstRoute();
    if (!route) return null;
    var coords = route.getPolyline();
    if (Array.isArray(coords) && coords.length >= 3) return coords;
    return null;
};

bemap.RoutingResponse.fromJson = function(raw) {
    var resp = new bemap.RoutingResponse();
    if (!raw) return resp;

    // BeMap server emits `routingRoutes` (per the Java DTO). Accept the
    // shorter `routes` alias for tolerance.
    var rawRoutes = raw.routingRoutes || raw.routes || [];
    for (var i = 0; i < rawRoutes.length; i++) {
        resp.routes.push(bemap.RoutingRoute.fromJson(rawRoutes[i]));
    }

    // Used-destination snap info — emitted at the response top level.
    if (Array.isArray(raw.usedDestinations)) {
        resp.usedDestinations = raw.usedDestinations.map(function(d) {
            return bemap.RoutingWaypoint.fromJson(d);
        });
    }

    if (raw.matrix) resp.matrix = raw.matrix;
    if (raw.isochrone) resp.isochrone = raw.isochrone;
    if (raw.requestId) resp.requestId = raw.requestId;
    if (Array.isArray(raw.warnings)) resp.warnings = raw.warnings.slice();

    return resp;
};