@happycoderse/js-sl-api
Version:
Javscript client for SL API
87 lines (76 loc) • 2.53 kB
JavaScript
const axios = require("axios");
class SlApiClient {
constructor({ apiKey_realtimeInfo, apiKey_nearMe, apiKey_location }) {
this.APIKEY_REALTIMEINFO = apiKey_realtimeInfo;
this.APIKEY_NEARME = apiKey_nearMe;
this.APIKEY_LOCATION = apiKey_location;
}
GetNextDeparture(SiteId) {
if (!this.APIKEY_REALTIMEINFO)
throw new Error("Missing APIKEY_REALTIMEINFO");
if (SiteId === null || SiteId === undefined) return null;
return axios({
method: "GET",
url:
"/api2/realtimedeparturesV4.json?key=" +
this.APIKEY_REALTIMEINFO +
"&siteid=" +
SiteId +
"&timewindow=60",
})
.then(function (response) {
if (response.data.StatusCode && response.data.StatusCode !== "200")
throw new Error(response.data.Message);
return response.data.ResponseData.Buses;
})
.catch(function (error) {
console.log("Error in API client", error);
throw new Error(error.message);
});
}
UpdateStopInfo(position) {
if (!this.APIKEY_NEARME) throw new Error("Missing APIKEY_NEARME");
return axios({
method: "GET",
url:
"/api2/nearbystopsv2.json?key=" +
this.APIKEY_NEARME +
"&originCoordLat=" +
position.latitude +
"&originCoordLong=" +
position.longitude +
"&maxNo=5&r=900",
})
.then(function (response) {
if (response.data.StatusCode && response.data.StatusCode !== "200")
throw new Error(response.data.Message);
return response.data.stopLocationOrCoordLocation;
})
.catch(function (error) {
console.log("Error in API client", error);
throw new Error(error.message);
});
}
LookupPlaces(address) {
if (!this.APIKEY_LOCATION) throw new Error("Missing APIKEY_LOCATION");
return axios({
method: "GET",
url:
"/api2/typeahead.json?key=" +
this.APIKEY_LOCATION +
"&searchstring=" +
address +
"&stationsonly=false",
})
.then(function (response) {
if (response.data.StatusCode && response.data.StatusCode !== "200")
throw new Error(response.data.Message);
return response.data.ResponseData;
})
.catch(function (error) {
console.log("Error in API client", error);
throw new Error(error.message);
});
}
}
module.exports.SlApiClient = SlApiClient;