UNPKG

trento-parking

Version:

A TypeScript library providing easy access to parking and bike slot availability, location, and details in Trento, Italy. Fetches data from the official Comune di Trento parking services.

154 lines (148 loc) 5.25 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); // src/index.ts var index_exports = {}; __export(index_exports, { ParkingData: () => ParkingData, ParkingDataCollection: () => parking_service_default, ParkingType: () => ParkingType }); module.exports = __toCommonJS(index_exports); // src/services/parking-service.ts var import_axios = __toESM(require("axios")); // src/models/parking-data.ts var ParkingData = class { constructor(data) { __publicField(this, "id"); __publicField(this, "driver"); __publicField(this, "geom"); __publicField(this, "name"); __publicField(this, "type"); __publicField(this, "city"); __publicField(this, "color"); __publicField(this, "capacity"); __publicField(this, "freeslots"); __publicField(this, "diag"); __publicField(this, "threshold"); __publicField(this, "timestamp"); __publicField(this, "distances"); __publicField(this, "currentTimestamp"); __publicField(this, "address"); __publicField(this, "gmaps"); __publicField(this, "link"); __publicField(this, "offline"); __publicField(this, "opening"); __publicField(this, "open"); __publicField(this, "phone"); __publicField(this, "website"); __publicField(this, "openingFlag"); Object.assign(this, data); } getDistances() { try { return JSON.parse(this.distances); } catch (error) { console.error("Error parsing distances JSON:", error); return []; } } getDistancesCompleted(parkings) { const distances = this.getDistances(); const distancesCompleted = distances.map((distance) => { const parking = parkings.find((p) => p.id === distance.id); if (!parking) { return null; } return { ...distance, ...parking }; }); return distancesCompleted.filter((distance) => distance !== null); } isAvailable() { return this.freeslots > 0; } isFull() { return this.freeslots === 0 && this.capacity > 0; } }; // src/models/parking-types.ts var ParkingType = /* @__PURE__ */ ((ParkingType2) => { ParkingType2["BIKE"] = "bike"; ParkingType2["PARK"] = "park"; return ParkingType2; })(ParkingType || {}); // src/services/parking-service.ts var ParkingDataCollection = class { constructor(baseUrl = "https://parcheggi.comune.trento.it/static/services") { __publicField(this, "axiosInstance"); __publicField(this, "baseUrl"); __publicField(this, "parkingData", []); __publicField(this, "lastRefresh", null); this.baseUrl = baseUrl; this.axiosInstance = import_axios.default.create({ baseURL: this.baseUrl }); } async refresh() { try { const response = await this.axiosInstance.get("/registry_parks.json"); this.parkingData = response.data.map((data) => new ParkingData(data)); this.lastRefresh = Date.now(); } catch (error) { console.error("Error refreshing parking data:", error); throw error; } } all() { return this.parkingData; } byId(id) { return this.parkingData.find((parking) => parking.id === id) ?? null; } byType(type) { return this.parkingData.filter((parking) => parking.type === type); } getAvailable() { return this.parkingData.filter((parking) => parking.isAvailable()); } getFull() { return this.parkingData.filter((parking) => parking.isFull()); } getLastRefresh() { return this.lastRefresh; } }; var parking_service_default = ParkingDataCollection; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ParkingData, ParkingDataCollection, ParkingType });