mobility-toolbox-js
Version:
Toolbox for JavaScript applications in the domains of mobility and logistics.
77 lines (76 loc) • 3.66 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 { isMocoNotificationNotOutOfDate } from '../common/utils/mocoUtils';
import HttpAPI from './HttpAPI';
/**
* This class provides convenience methods to use to the [geOps MOCO API](https://geops.com/de/solution/disruption-information).
*
* @example
* import { MocoAPI } from 'mobility-toolbox-js/api';
*
* const api = new MocoAPI({
* // graph: 'osm',
* // url: 'https://moco.geops.io/api/v1',
* // ssoConfig: "geopstest",
* });
*
* const notifications = await api.getNotifications();
*
* console.log('Log route:', JSON.stringify(notifications));
*
* @private
*/
class MocoAPI extends HttpAPI {
/**
* Constructor
*
* @param {Object} options Options.
*
* @param {string} options.apiKey Access key for [geOps APIs](https://developer.geops.io/).
* @param {string} [options.url='https://moco.geops.io/api/v1'] Service url.
* @param {string} [options.ssoConfig='geopstest'] SSO config to get notifications from.
* @param {string} [options.graph='osm'] Graph to use for geometries.
*/
constructor(options) {
super(Object.assign(Object.assign({}, options), { apiKey: options.apiKey, url: options.url || 'https://moco.geops.io/api/v1/' }));
this.graph = 'osm';
this.simplify = 0; // The backend has 100 as default value, but we use 0 to get the full geometries.
this.ssoConfig = 'geopstest';
this.ssoConfig = options.ssoConfig || 'geopstest';
this.graph = options.graph || 'osm';
this.simplify = options.simplify || 0;
}
/**
* Get notifications from the MOCO API.
* Notifications are returned as an array of GeoJSON feature collections.
*
* @param {MocoParameters} params Request parameters.
* @param {FetchOptions} config Options for the fetch request.
* @return {Promise<MocoNotification[]>} An array of GeoJSON feature collections with coordinates in [EPSG:4326](http://epsg.io/4326).
* @public
*/
getNotifications() {
return __awaiter(this, arguments, void 0, function* (params = {}, config = {}) {
const apiParams = Object.assign({}, params);
delete apiParams.date; // Not used in this method
let notifications = yield this.fetch('export/publication/', Object.assign({ graph: this.graph || 'osm', simplify: this.simplify || 0, sso_config: this.ssoConfig || 'geopstest' }, apiParams), config);
// TODO in the future we hope that the date parameter will be used by the API to filter out-of-date notifications.
// For now we filter them out manually.
const date = params === null || params === void 0 ? void 0 : params.date;
if (date) {
notifications = notifications.filter((notification) => {
return isMocoNotificationNotOutOfDate(notification, date);
});
}
return notifications;
});
}
}
export default MocoAPI;