mobility-toolbox-js
Version:
Toolbox for JavaScript applications in the domains of mobility and logistics.
86 lines (85 loc) • 3.83 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 HttpAPI from './HttpAPI';
/**
* This class provides convenience methods to use the [geOps Mapset API](https://geops.com/de/solution/mapset).
*
* @example
* import { MapsetAPI } from 'mobility-toolbox-js/api';
*
* const api = new MapsetAPI({
* apiKey: 'yourApiKey',
* // tenants: ['geopstest'],
* // url: 'https://editor.mapset.io/api/v1',
* });
*
* const plans = await api.getPlans({
* bbox: [8.5, 47.3, 8.6, 47.4],
* zoom: 10,
* // timestamp: (new Date()).toISOString(),
* });
*
* console.log('Log route:', JSON.stringify(plans));
*
* @public
*/
class MapsetAPI extends HttpAPI {
/**
* Constructor
*
* @param {Object} options Options.
* @param {string} options.apiKey Access key for [geOps APIs](https://developer.geops.io/).
* @param {string[]} [options.tags] Array of tags to filter plans.
* @param {string[]} [options.tenants=["geopstest"]] Array of tenants to filter plans.
* @param {string} [options.url='https://editor.mapset.io/api/v1/'] Url of the [geOps Mapset API](https://geops.com/de/solution/mapset).
*/
constructor(options) {
var _a, _b;
super(Object.assign(Object.assign({}, options), {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
url: options.url || 'https://editor.mapset.io/api/v1/' }));
this.tags = [];
this.tenants = [];
this.tags = (_a = options.tags) !== null && _a !== void 0 ? _a : [];
this.tenants = (_b = options.tenants) !== null && _b !== void 0 ? _b : ['geopstest'];
}
/**
* Get single mapset plan by ID.
*
* @param {string} id Mapset Plan identifier.
* @param {FetchOptions} config Options for the fetch request.
* @return {Promise<MapsetPlan>} A mapset plan.
* @public
*/
getPlanById(id_1) {
return __awaiter(this, arguments, void 0, function* (id, config = {}) {
return yield this.fetch(`meta/kml/${id}/`, {}, Object.assign({ method: 'GET' }, config));
});
}
/**
* Get a list of mapset plans.
*
* @param {MapsetGetPlansParameters} params Request parameters.
* @param {FetchOptions} config Options for the fetch request.
* @return {Promise<MapsetPlan[]>} An array of mapset plan objects with kml strings in data attribute.
* @public
*/
getPlans(params_1) {
return __awaiter(this, arguments, void 0, function* (params, config = {}) {
var _a, _b, _c;
const res = yield this.fetch('export/kml/', Object.assign({ defaultplans: ((_a = this.tags) === null || _a === void 0 ? void 0 : _a.toString()) ? 'false' : 'true', key: this.apiKey, tags: (_b = this.tags) === null || _b === void 0 ? void 0 : _b.toString(), tenants: (_c = this.tenants) === null || _c === void 0 ? void 0 : _c.toString() }, (params || {})), config);
if (res === null || res === void 0 ? void 0 : res.detail) {
throw new Error(res.detail);
}
return (res === null || res === void 0 ? void 0 : res.results) || [];
});
}
}
export default MapsetAPI;