From 7b6b80573c896768612b2bf7dbd44aec62e6d6cb Mon Sep 17 00:00:00 2001 From: Steve Rogers Date: Sat, 14 Dec 2019 13:49:06 +0000 Subject: [PATCH] added ability to stop attribute removal from result data --- CHANGES.md | 3 +++ README.md | 11 +++++++++++ index.js | 28 ++++++++++++++++++++++++++-- package.json | 2 +- 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index f44a1fe..3b8875f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,8 @@ # ldbs-json changelog +## 1.1.1 (Dec 2019) +By default attributes are removed from the returned data Now they can be included. + ## 1.1.0 (Dec 2019) Updated to allow for access to Staff Reference Data Endpoint calls diff --git a/README.md b/README.md index cf6e58b..a775f17 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,17 @@ There are 3 examples ( two STAFF versions and one USER version that can be found This module is a continuation from my other module https://www.npmjs.com/package/ldbws-json which was only for use with the USER version of the OpenLDBWS web service. +## New Options for the api.call() method (Dec 2019) +The API will still work as it did but there are 2 new (optional) flags that can be past in when making an API call. The call now : + +- api.call(*method*, *options*, *useRef*, *withAttributes*) + +The parameters **method** and **options** are the same as they were but there are 2 new options **useRef** and **withAttributes** (both default to false so have no impact if not specified). + + - **useRef** can be set to true so that a Staff API call can access the Reference Data Endpoint + - **withAttributes** can be set to true to stop the attributes being stripped from the result. **NB: setting this to true will change the JSON data structure returned**. + + # Acknowledgements - The LDBS-JSON module is powered by National Rail Enquiries. diff --git a/index.js b/index.js index 9c320c9..ea9604d 100644 --- a/index.js +++ b/index.js @@ -40,9 +40,10 @@ class LiveDepartureBoardService { * Query the LiveDepartureBoardService for the requested data * @param {String} method - the LDBWS or LDBSVWS to perform * @param {Object} options - a JSON object consisting of the key/value pairs for the requested method - * @param {Bool} userRef - only applies for the staff version of the API and allows access to those calls that use the reference data endpoint + * @param {Bool} useRef - only applies for the staff version of the API and allows access to those calls that use the reference data endpoint - default is false + * @param {Bool} withAttributes - don't filter the attributes out from the result - default is false */ - async call(method, options, useRef = false) { + async call(method, options, useRef = false, withAttributes = false, ) { const soapCall = new DepartureBoardSoap(this.accessToken, (useRef) ? this.refTargetNamespace : this.targetNamespace, method, options).generateCall(); const body = await request({ method: 'POST', @@ -52,6 +53,9 @@ class LiveDepartureBoardService { }, body: soapCall }); + if(withAttributes) { + return await this._parseResultWithAttributes(body, method); + } return await this._parseResult(body, method); } @@ -73,6 +77,26 @@ class LiveDepartureBoardService { }); } + // Private method to parse result to JSON and return all data including attributes + // this version will return the the content text as "_" and all associated attribute in "$" + // for more information see https://www.npmjs.com/package/xml2js + _parseResultWithAttributes(body, method) { + return new Promise((resolve, reject) => { + parseString(body, { + tagNameProcessors: [stripNS], + explicitArray : false, + ignoreAttrs : false + }, function(err, result){ + if(!err){ + const data = result.Envelope.Body[`${method}Response`]; + resolve(data); + } else { + reject(err); + } + }); + }); + } + } module.exports = LiveDepartureBoardService; \ No newline at end of file diff --git a/package.json b/package.json index c02c56c..8d6097d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ldbs-json", - "version": "1.1.0", + "version": "1.1.1", "engines": { "node": ">=7.6" },