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 | const soap_1 = require("soap");
|
13 | const config_1 = require("./config");
|
14 | require("./types/index");
|
15 | class NationalRailWrapper {
|
16 | constructor(token) {
|
17 | this.apiToken = token;
|
18 | this.wsdlUrl = config_1.wsdlUrl;
|
19 | this.soapHeader = config_1.getSoapHeader(this.apiToken);
|
20 | }
|
21 | initClient() {
|
22 | return __awaiter(this, void 0, void 0, function* () {
|
23 | try {
|
24 | const client = yield soap_1.createClientAsync(this.wsdlUrl);
|
25 | client.addSoapHeader(this.soapHeader);
|
26 | this.soapClient = client;
|
27 | }
|
28 | catch (err) {
|
29 | throw new Error('An error occured trying to retrieve Soap Client');
|
30 | }
|
31 | });
|
32 | }
|
33 | getDepartures(options) {
|
34 | return __awaiter(this, void 0, void 0, function* () {
|
35 | const methodName = config_1.wsdlMethodMap.get('getDepartures');
|
36 | const filter = Object.assign(Object.assign({}, this.parseStationOptions(options)), { filterType: 'to' });
|
37 | return this.invoke({ methodName, filter });
|
38 | });
|
39 | }
|
40 | getArrivals(options) {
|
41 | const methodName = config_1.wsdlMethodMap.get('getArrivals');
|
42 | const filter = Object.assign(Object.assign({}, this.parseStationOptions(options)), { filterType: 'from' });
|
43 | return this.invoke({ methodName, filter });
|
44 | }
|
45 | getAll(options) {
|
46 | const methodName = config_1.wsdlMethodMap.get('getAll');
|
47 | const filter = this.parseStationOptions(options);
|
48 | return this.invoke({ methodName, filter });
|
49 | }
|
50 | getServiceDetails({ serviceId }) {
|
51 | const methodName = config_1.wsdlMethodMap.get('getServiceDetails');
|
52 | const filter = { serviceID: serviceId };
|
53 | return this.invoke({ methodName, filter });
|
54 | }
|
55 | parseStationOptions({ station, count = 10 }) {
|
56 | const filter = {
|
57 | crs: station,
|
58 | };
|
59 | if (count) {
|
60 | filter.numRows = count;
|
61 | }
|
62 | return filter;
|
63 | }
|
64 | invoke({ methodName, filter }) {
|
65 | return __awaiter(this, void 0, void 0, function* () {
|
66 | if (!this.soapClient) {
|
67 | yield this.initClient();
|
68 | }
|
69 | if (!methodName) {
|
70 | throw new Error(`Method with name '${methodName}' not found in WsdlMap`);
|
71 | }
|
72 | const response = this.soapClient && (yield this.soapClient[methodName](filter));
|
73 | return this.formatResult(response);
|
74 | });
|
75 | }
|
76 | formatResult(response) {
|
77 | const [data] = response;
|
78 | let res = null;
|
79 | if (Object.prototype.hasOwnProperty.call(data, 'GetServiceDetailsResult')) {
|
80 | const { GetServiceDetailsResult: serviceData } = data;
|
81 | res = serviceData;
|
82 | }
|
83 | if (Object.prototype.hasOwnProperty.call(data, 'GetStationBoardResult')) {
|
84 | const { GetStationBoardResult: stationData } = data;
|
85 | res = stationData.trainServices.service;
|
86 | }
|
87 | return {
|
88 | success: res !== null,
|
89 | data: res !== null ? res : [],
|
90 | };
|
91 | }
|
92 | }
|
93 | exports.NationalRailWrapper = NationalRailWrapper;
|
94 |
|
\ | No newline at end of file |