1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9 | });
|
10 | };
|
11 | Object.defineProperty(exports, "__esModule", { value: true });
|
12 | exports.SlowZone = void 0;
|
13 | const node_https_1 = require("node:https");
|
14 | const train_1 = require("./parsers/train");
|
15 | const API_BASE_URL = "lapi.transitchicago.com";
|
16 | const API_BASE_PATH = "/api/1.0";
|
17 | const PKG_VERSION = "3.0.4";
|
18 | class SlowZone {
|
19 | constructor(options) {
|
20 | this.apiKey = options.apiKey;
|
21 | }
|
22 | getArrivalsForStation(stationId, options = {}) {
|
23 | return this.getArrivals(Object.assign(Object.assign({}, options), { mapid: stationId }));
|
24 | }
|
25 | getArrivalsForStop(stopId, options = {}) {
|
26 | return this.getArrivals(Object.assign(Object.assign({}, options), { stpid: stopId }));
|
27 | }
|
28 | followTrain(runId) {
|
29 | return this.fetch("ttfollow.aspx", { runnumber: runId });
|
30 | }
|
31 | getArrivals(options = {}) {
|
32 | return this.fetch("ttarrivals.aspx", options);
|
33 | }
|
34 | fetch(endpoint, queryParams) {
|
35 | return __awaiter(this, void 0, void 0, function* () {
|
36 | return this.makeRequest(endpoint, queryParams).then((resp) => {
|
37 | return new Promise((resolve, reject) => {
|
38 | if (!resp) {
|
39 | reject(new Error("invalid response"));
|
40 | }
|
41 | if (resp.ctatt.errCd != "0") {
|
42 | return reject(new Error(`[${resp.ctatt.errCd}] ${resp.ctatt.errNm}`));
|
43 | }
|
44 | resolve(resp.ctatt.eta.map((trainData) => new train_1.Train(trainData).toJSON()));
|
45 | });
|
46 | });
|
47 | });
|
48 | }
|
49 | makeRequest(endpoint, queryParams = {}) {
|
50 | return __awaiter(this, void 0, void 0, function* () {
|
51 | const query = new URLSearchParams(Object.assign(Object.assign({}, queryParams), { key: this.apiKey, outputType: "json" }));
|
52 | const options = {
|
53 | hostname: API_BASE_URL,
|
54 | port: 443,
|
55 | path: `${API_BASE_PATH}/${endpoint}?${query.toString()}`,
|
56 | method: "GET",
|
57 | headers: {
|
58 | Accept: "application/json",
|
59 | "User-Agent": `slow-zone/${PKG_VERSION}`,
|
60 | },
|
61 | };
|
62 | return new Promise(function (resolve, reject) {
|
63 | const req = (0, node_https_1.request)(options, (res) => {
|
64 | if (res.statusCode && (res.statusCode < 200 || res.statusCode >= 300)) {
|
65 | return reject(new Error(res.statusCode.toString()));
|
66 | }
|
67 | let body = [], json;
|
68 | res.on("data", (chunk) => {
|
69 | body.push(chunk);
|
70 | });
|
71 | res.on("end", () => {
|
72 | try {
|
73 | json = JSON.parse(Buffer.concat(body).toString());
|
74 | }
|
75 | catch (e) {
|
76 | return reject(e);
|
77 | }
|
78 | return resolve(json);
|
79 | });
|
80 | });
|
81 | req.on("error", (err) => {
|
82 | return reject(err);
|
83 | });
|
84 | req.end();
|
85 | });
|
86 | });
|
87 | }
|
88 | }
|
89 | exports.SlowZone = SlowZone;
|
90 | module.exports = SlowZone;
|