diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..682b58a
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,121 @@
+# Logs
+logs
+*.log
+npm-debug.log*
+yarn-debug.log*
+yarn-error.log*
+lerna-debug.log*
+
+# Diagnostic reports (https://nodejs.org/api/report.html)
+report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
+
+# test settings
+settings.json
+
+# Runtime data
+pids
+*.pid
+*.seed
+*.pid.lock
+
+# Directory for instrumented libs generated by jscoverage/JSCover
+lib-cov
+
+# Coverage directory used by tools like istanbul
+coverage
+*.lcov
+
+# nyc test coverage
+.nyc_output
+
+# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
+.grunt
+
+# Bower dependency directory (https://bower.io/)
+bower_components
+
+# node-waf configuration
+.lock-wscript
+
+# Compiled binary addons (https://nodejs.org/api/addons.html)
+build/Release
+
+# Dependency directories
+node_modules/
+jspm_packages/
+
+# Snowpack dependency directory (https://snowpack.dev/)
+web_modules/
+
+# TypeScript cache
+*.tsbuildinfo
+
+# Optional npm cache directory
+.npm
+
+# Optional eslint cache
+.eslintcache
+
+# Microbundle cache
+.rpt2_cache/
+.rts2_cache_cjs/
+.rts2_cache_es/
+.rts2_cache_umd/
+
+# Optional REPL history
+.node_repl_history
+
+# Output of 'npm pack'
+*.tgz
+
+# Yarn Integrity file
+.yarn-integrity
+
+# dotenv environment variables file
+.env
+.env.test
+
+# parcel-bundler cache (https://parceljs.org/)
+.cache
+.parcel-cache
+
+# Next.js build output
+.next
+out
+
+# Nuxt.js build / generate output
+.nuxt
+dist
+
+# Gatsby files
+.cache/
+# Comment in the public line in if your project uses Gatsby and not Next.js
+# https://nextjs.org/blog/next-9-1#public-directory-support
+# public
+
+# vuepress build output
+.vuepress/dist
+
+# Serverless directories
+.serverless/
+
+# FuseBox cache
+.fusebox/
+
+# DynamoDB Local files
+.dynamodb/
+
+# TernJS port file
+.tern-port
+
+# Stores VSCode versions used for testing VSCode extensions
+.vscode-test
+
+# yarn v2
+
+.yarn/cache
+.yarn/unplugged
+.yarn/build-state.yml
+.pnp.*
+downloads
+eslint_out.html
diff --git a/CHANGES.md b/CHANGES.md
index 44d089b..410954c 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -1,5 +1,8 @@
# ldbs-json changelog
+## 1.2.0 (May 2020)
+Replaced request with node-fetch and added some basic error trapping. Examples updated
+
## 1.1.2 (Apr 2020)
Add missing namespace attribute for filterList (PR from SimonRice)
diff --git a/README.md b/README.md
index a775f17..3b73ccb 100644
--- a/README.md
+++ b/README.md
@@ -36,6 +36,10 @@ 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.
+
+## Updates May 2020
+This update has replaced the user of the **request** module and it now uses **node-fetch**. Some simple error checks on the return status of the API are now made and the examples have also been updated.
+
## 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 :
diff --git a/examples/getArrivalBoardByCRS.js b/examples/getArrivalBoardByCRS.js
index 2696d9b..3a43953 100644
--- a/examples/getArrivalBoardByCRS.js
+++ b/examples/getArrivalBoardByCRS.js
@@ -2,8 +2,8 @@
* Simple example code for running the staff version of the DepartureBoard Info
* NB: You will need a valid Token to access the SV version of the api
*/
+const settings = require('../settings.json');
const LiveDepartureBoardService = require('../index');
-const token = "0000-0000-0000-0000"; // put a valid token here
// Set up the options for the call
const options = {
@@ -14,7 +14,9 @@ const options = {
filterType: "to"
};
-const api = new LiveDepartureBoardService(token, true);
-api.call("GetArrivalBoardByCRS", options).then(board => {
- console.log(board);
-});
\ No newline at end of file
+const api = new LiveDepartureBoardService(settings.tokens.staff, true);
+api.call("GetArrivalBoardByCRS", options)
+ .then(board => {
+ console.log(board);
+ })
+ .catch(error => console.error(error));
diff --git a/examples/getDepartureBoardWithDetails.js b/examples/getDepartureBoardWithDetails.js
index cbccfab..b9cafea 100644
--- a/examples/getDepartureBoardWithDetails.js
+++ b/examples/getDepartureBoardWithDetails.js
@@ -2,8 +2,9 @@
* Simple example code for running the user version of the DepartureBoard Info
* NB: You will need a valid Token to access the normal user version of the api
*/
+const settings = require('../settings.json');
const LiveDepartureBoardService = require('../index');
-const token = "0000-0000-0000-0000"; // put a valid token here
+
// Set up the options for the call
const options = {
@@ -11,7 +12,9 @@ const options = {
crs:"KGX",
};
-const api = new LiveDepartureBoardService(token, false);
-api.call("GetDepBoardWithDetails", options).then(board => {
- console.log(board);
-});
\ No newline at end of file
+const api = new LiveDepartureBoardService(settings.tokens.user, false);
+api.call("GetDepBoardWithDetails", options)
+ .then(board => {
+ console.log(board);
+ })
+ .catch(error => console.error(error));
diff --git a/examples/getNextDeparturesStaff.js b/examples/getNextDeparturesStaff.js
index 4314aa5..172f82a 100644
--- a/examples/getNextDeparturesStaff.js
+++ b/examples/getNextDeparturesStaff.js
@@ -2,19 +2,20 @@
* Simple example code for running the STAFF version of the DepartureBoard Info
* NB: You will need a valid Token to access the SV version of the api
*/
+const settings = require('../settings.json');
const LiveDepartureBoardService = require('../index');
-const token = "0000-0000-0000-0000"; // put a valid token here
-
// Set up the options for the call
const options = {
crs:"SVG",
filterList: ["KGX"],
- time: "2020-04-14T08:30:00",
- timeWindow:120
+ time: "2020-05-05T08:00:00",
+ timeWindow: 120
};
-const api = new LiveDepartureBoardService(token, true);
-api.call("GetNextDepartures", options).then(board => {
- console.log(board);
-});
\ No newline at end of file
+const api = new LiveDepartureBoardService(settings.tokens.staff, true);
+api.call("GetNextDepartures", options)
+ .then(board => {
+ console.log(board);
+ })
+ .catch(error => console.error(error));
diff --git a/examples/getNextDeparturesUser.js b/examples/getNextDeparturesUser.js
index f7e6aff..292bda3 100644
--- a/examples/getNextDeparturesUser.js
+++ b/examples/getNextDeparturesUser.js
@@ -2,8 +2,9 @@
* Simple example code for running the user version of the DepartureBoard Info
* NB: You will need a valid Token to access the SV version of the api
*/
+const settings = require('../settings.json');
const LiveDepartureBoardService = require('../index');
-const token = "0000-0000-0000-0000"; // put a valid token here
+
// Set up the options for the call
const options = {
@@ -14,7 +15,9 @@ const options = {
timeOffset: 0
};
-const api = new LiveDepartureBoardService(token, false);
-api.call("GetNextDepartures", options).then(board => {
- console.log(board);
-});
\ No newline at end of file
+const api = new LiveDepartureBoardService(settings.tokens.user, false);
+api.call("GetNextDepartures", options)
+ .then(board => {
+ console.log(board);
+ })
+ .catch(error => console.error(error));
diff --git a/examples/getStationList.js b/examples/getStationList.js
index 2c2226d..00c0772 100644
--- a/examples/getStationList.js
+++ b/examples/getStationList.js
@@ -2,8 +2,8 @@
* Simple example code for running the staff version of the DepartureBoard Info to access one of the Reference Data Enpoint API calls "GetStationList"
* NB: You will need a valid Token to access the SV version of the api
*/
+const settings = require('../settings.json');
const LiveDepartureBoardService = require('../index');
-const token = "0000-0000-0000-0000"; // put a valid token here
// Set up the options for the call
const options ={
@@ -11,9 +11,11 @@ const options ={
};
// create a staff version of the API
-const api = new LiveDepartureBoardService(token, true);
+const api = new LiveDepartureBoardService(settings.tokens.staff, true);
// make sure we set userRef to true
-api.call("GetStationList", options, true).then(list => {
- console.log(list);
-});
\ No newline at end of file
+api.call("GetStationList", options, true)
+ .then(list => {
+ console.log(list);
+ })
+ .catch(error => console.error(error));
diff --git a/index.js b/index.js
index ea9604d..5493720 100644
--- a/index.js
+++ b/index.js
@@ -3,11 +3,10 @@
* index.js
* Package main entry point
*/
-
-const request = require('request-promise-native'),
- parseString = require('xml2js').parseString,
- stripNS = require('xml2js').processors.stripPrefix,
- DepartureBoardSoap = require('./soap');
+const fetch = require('node-fetch');
+const parseString = require('xml2js').parseString;
+const stripNS = require('xml2js').processors.stripPrefix;
+const DepartureBoardSoap = require('./soap');
@@ -44,19 +43,29 @@ class LiveDepartureBoardService {
* @param {Bool} withAttributes - don't filter the attributes out from the result - default is 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',
- url: (useRef) ? this.refUrl: this.baseURL,
- headers: {
- 'content-type' : "text/xml"
- },
- body: soapCall
- });
- if(withAttributes) {
- return await this._parseResultWithAttributes(body, method);
+
+ try{
+ const soapCall = new DepartureBoardSoap(this.accessToken, (useRef) ? this.refTargetNamespace : this.targetNamespace, method, options).generateCall();
+ const response = await fetch((useRef) ? this.refUrl: this.baseURL, {
+ method: 'post',
+ body: soapCall,
+ headers: {'content-type' : "text/xml"}
+ });
+ const body = await response.text();
+ if(response.status !== 200){
+ throw Error(`API Error ${response.status}: ${response.statusText}`)
+ }
+
+ if(withAttributes) {
+ return await this._parseResultWithAttributes(body, method);
+ }
+ return await this._parseResult(body, method);
+
+ } catch(error) {
+ console.error(error);
+ return false;
}
- return await this._parseResult(body, method);
+
}
// Private method to parse result to JSON
@@ -99,4 +108,4 @@ class LiveDepartureBoardService {
}
-module.exports = LiveDepartureBoardService;
\ No newline at end of file
+module.exports = LiveDepartureBoardService;
diff --git a/package.json b/package.json
index 3fd8d33..737c5a4 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "ldbs-json",
- "version": "1.1.2",
+ "version": "1.2.0",
"engines": {
"node": ">=7.6"
},
@@ -16,7 +16,10 @@
"url": "https://github.com/magnatronus/ldbs-json"
},
"main": "index.js",
- "scripts": {},
+ "scripts": {
+ "lint": "./node_modules/eslint/bin/eslint.js *.js; exit 0",
+ "lint_report": "./node_modules/eslint/bin/eslint.js -f html -o eslint_out.html .;exit 0"
+ },
"keywords": [
"ldbws",
"ldbs",
@@ -26,8 +29,10 @@
"json"
],
"dependencies": {
- "request": "^2.88.0",
- "request-promise-native": "^1.0.8",
+ "node-fetch": "^2.6.0",
"xml2js": "^0.4.22"
+ },
+ "devDependencies": {
+ "eslint": "^6.8.0"
}
}
diff --git a/soap/index.js b/soap/index.js
index 223b43e..78a0321 100644
--- a/soap/index.js
+++ b/soap/index.js
@@ -7,55 +7,56 @@
class DepartureBoardSoap {
- /**
- * Create a new SOAP call
- * @param {*} token - a valid access token
- * @param {*} namespace - the target namespace - depends on staff or user version
- * @param {*} request - the operation to perform
- * @param {*} options - a JSON object of values to pass to the request
- */
- constructor(token, namespace, request, options){
- this.token = token;
- this.namespace = namespace;
- this.request = request;
- this.options = options;
- }
+ /**
+ * Create a new SOAP call
+ * @param {*} token - a valid access token
+ * @param {*} namespace - the target namespace - depends on staff or user version
+ * @param {*} request - the operation to perform
+ * @param {*} options - a JSON object of values to pass to the request
+ */
+ constructor(token, namespace, request, options){
+ this.token = token;
+ this.namespace = namespace;
+ this.request = request;
+ this.options = options;
+ }
- /**
- * Generate the complete SOAP call based on the params passed to the class constructor
- */
- generateCall() {
- return `` +
- `${this._header()}` +
- `${this._payload(this.options)}` +
- ``;
- }
+ /**
+ * Generate the complete SOAP call based on the params passed to the class constructor
+ */
+ generateCall() {
+ return `` +
+ `${this._header()}` +
+ `${this._payload(this.options)}` +
+ ``;
+ }
- // Generate the SOAP payload
- _payload(options) {
- let body = ``;
- Object.keys(options).forEach(function(key){
- let val="";
- if(key == "filterList"){
- options[key].forEach((crs) => {
- val += `${crs}`;
- });
- } else {
- val = `${options[key]}`
- }
- body += `${val}`;
- });
- return `${body}`;
- }
+ // Generate the SOAP payload
+ _payload(options) {
+ let body = ``;
+ Object.keys(options).forEach(function(key){
+ let val="";
+ if(key == "filterList"){
+ options[key].forEach((crs) => {
+ val += `${crs}`;
+ });
+ } else {
+ val = `${options[key]}`
+ }
+ body += `${val}`;
+ });
+ return `${body}`;
+ }
+
+ // generate the SOAP header
+ _header() {
+ return `` +
+ `` +
+ `${this.token}` +
+ `` +
+ ``;
+ }
- // generate the SOAP header
- _header() {
- return `` +
- `` +
- `${this.token}` +
- `` +
- ``;
- }
}
module.exports = DepartureBoardSoap;