Source: bemap-services-v2/charging-stations/bemap-charging-stations.js

/**
 * BeNomad BeMap JavaScript API - ChargingStations service (v1.5)
 *
 * Promise-based wrapper around `POST service/chargingstation/search/1.0`.
 *
 * **What it does.** Fetches pools of charging stations around a coordinate,
 * along a corridor, or inside a bounding box. The user's ACL filters the
 * providers exposed via `bemap.AclService.getCurrentUserDetails()` — pass
 * one of those `key` values into `request.providers`.
 *
 * Auth: requires `ROLE_CHARGINGSTATION` or `ROLE_EVSMARTROUTING`.
 *
 * Typical usage:
 *
 *     var cs = new bemap.ChargingStations(ctx);
 *     var req = new bemap.ChargingStationSearchRequest({
 *         providers: ['ecoMovement'],
 *         coordinate: new bemap.Coordinate(2.35, 48.85),
 *         radius: 2000,
 *         maxPoolResult: 25,
 *         options: [bemap.ChargingStationOption.PATH_POOL_MAP]
 *     });
 *     cs.search(req).then(function(response) {
 *         response.getPools().forEach(function(pool) {
 *             addMarker(pool.getCoordinate(), pool.getName());
 *         });
 *     });
 *
 * @since 1.5.0
 * @public
 * @constructor
 * @extends {bemap.ServiceV2}
 * @param {bemap.Context} context BeMap-JS-API Context. Mandatory.
 * @param {Object} [options]
 */
bemap.ChargingStations = function(context, options) {
    bemap.ServiceV2.call(this, context, options);
};
bemap.inherits(bemap.ChargingStations, bemap.ServiceV2);

/**
 * Search for charging-station pools matching the supplied filters.
 *
 * @public
 * @param {bemap.ChargingStationSearchRequest} request
 * @param {Object} [options]
 * @param {AbortSignal} [options.signal] AbortSignal for cancellation.
 * @return {Promise<bemap.ChargingStationSearchResponse>}
 */
bemap.ChargingStations.prototype.search = function(request, options) {
    if (!(request instanceof bemap.ChargingStationSearchRequest)) {
        return Promise.reject(new bemap.Error({
            code: bemap.Error.INVALID_ARGUMENT,
            message: 'ChargingStations.search requires a bemap.ChargingStationSearchRequest'
        }));
    }
    var opts = options || {};
    var body = request.toJson();
    if (!body.geoserver) body.geoserver = this.ctx.getGeoserver();
    // Default to the Context's chargingStationProvider when the request did not
    // specify one explicitly — same opt-in convenience as Context.geoserver.
    if ((!body.providers || !body.providers.length) && this.ctx.chargingStationProvider) {
        body.providers = [this.ctx.chargingStationProvider];
    }
    // Server rejects a request without `providers` with an opaque 4xx. Trap
    // it client-side with a typed error so consumers can branch on it.
    if (!body.providers || !body.providers.length) {
        return Promise.reject(new bemap.Error({
            code: bemap.Error.INVALID_ARGUMENT,
            message: 'ChargingStations.search: at least one provider is required ' +
                     '(set request.providers or ctx.chargingStationProvider)'
        }));
    }
    return this._post('service/chargingstation/search/1.0', body, { signal: opts.signal })
        .then(function(json) {
            return bemap.ChargingStationSearchResponse.fromJson(json);
        });
};

/**
 * Fetch the list of connector types known by the server. The result is the
 * `types[]` array from `GET service/chargingstation/connector/list/1.0`.
 *
 * @public
 * @param {Object} [options]
 * @param {String} [options.geoserver] Override the Context geoserver.
 * @param {AbortSignal} [options.signal]
 * @return {Promise<Array.<Object>>} The raw `types[]` array from the server.
 */
bemap.ChargingStations.prototype.getConnectorTypes = function(options) {
    var opts = options || {};
    var gs = opts.geoserver || this.ctx.getGeoserver();
    var path = 'service/chargingstation/connector/list/1.0';
    if (gs) path += '?geoserver=' + encodeURIComponent(gs);
    return this._get(path, { signal: opts.signal })
        .then(function(json) {
            return (json && Array.isArray(json.types)) ? json.types : [];
        });
};