UNPKG

@spasea/uz-booking-client

Version:
70 lines (69 loc) 3.13 kB
"use strict"; 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const requestable_1 = require("../lib/requestable"); const stations_1 = require("../assets/stations/stations"); class Station extends requestable_1.default { /** * Construct station class. * @constructor * @param {string} lang - language * @param {string} auth * @param {string} apiBase - the base UzBooking API URL * @param userId */ constructor(lang, auth, apiBase, userId) { super(lang, auth, apiBase, false, userId); this.lang = lang; } /** * Find station by name * @param {string} stationName - the name of station * @param {string} [stationFromId] * @param {Function} [callback] - callback function * @returns {Promise} - the promise for the http request */ find(stationName, stationFromId, callback) { return __awaiter(this, void 0, void 0, function* () { const response = yield this.request('GET', '/api/stations', Object.assign({ search: stationName }, stationFromId ? { station_from_id: stationFromId } : {}), 'json', false, callback); return response.data; }); } /** * Find station by name from assets * @param {string} stationName - the name of station * @param {Function} [callback] - callback function * @returns {Promise} - the promise for the http request */ findFromAssets(stationName, callback) { return __awaiter(this, void 0, void 0, function* () { const filteredStations = []; for (const property in stations_1.default) { if (Object.prototype.hasOwnProperty.call(stations_1.default, property)) { if (stations_1.default[property][this.lang] && stations_1.default[property][this.lang].title .toLowerCase() .startsWith(stationName.toLowerCase())) { filteredStations.push({ title: stations_1.default[property][this.lang].title, value: property, }); } } } if (callback) { return callback(null, { data: filteredStations }); } return Promise.resolve({ data: filteredStations }); }); } } exports.default = Station;