jmri-client 2.0.1 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,15 +1,15 @@
1
- declare type Protocol = "http" | "https";
1
+ declare type Protocol = 'http' | 'https';
2
2
  declare class JmriClient {
3
3
  protected readonly _protocol: Protocol;
4
4
  protected readonly _host: string;
5
5
  protected readonly _port: number;
6
6
  constructor(protocol: Protocol, host: string, port?: number);
7
7
  protected _validate: () => void;
8
- protected _getValue: (type: string) => Promise<unknown>;
9
- protected _setValue: (type: string, data: object) => Promise<unknown>;
10
- protected _send: (method: string, type: string, data?: object) => Promise<unknown>;
11
- getPower: () => Promise<unknown>;
12
- setPower: (isOn: boolean) => Promise<unknown>;
13
- getRoster: () => Promise<unknown>;
8
+ protected _getValue: (type: string) => Promise<import("axios").AxiosResponse<any, any>>;
9
+ protected _setValue: (type: string, data: object) => Promise<import("axios").AxiosResponse<any, any>>;
10
+ protected _send: (method: string, type: string, data?: object) => Promise<import("axios").AxiosResponse<any, any>>;
11
+ getPower: () => Promise<import("axios").AxiosResponse<any, any>>;
12
+ setPower: (isOn: boolean) => Promise<import("axios").AxiosResponse<any, any>>;
13
+ getRoster: () => Promise<import("axios").AxiosResponse<any, any>>;
14
14
  }
15
15
  export { JmriClient };
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- "use strict";
1
+ 'use strict';
2
2
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
3
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
4
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -35,23 +35,28 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
35
35
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
36
  }
37
37
  };
38
- import fetch from "node-fetch";
38
+ var __importDefault = (this && this.__importDefault) || function (mod) {
39
+ return (mod && mod.__esModule) ? mod : { "default": mod };
40
+ };
41
+ exports.__esModule = true;
42
+ exports.JmriClient = void 0;
43
+ var axios_1 = __importDefault(require("axios"));
39
44
  var JmriClient = /** @class */ (function () {
40
45
  function JmriClient(protocol, host, port) {
41
46
  var _this = this;
42
47
  this._port = 80;
43
48
  this._validate = function () {
44
49
  if (!_this._protocol)
45
- throw new Error("protocol is empty");
50
+ throw new Error('protocol is empty');
46
51
  if (!_this._host)
47
- throw new Error("host is empty");
52
+ throw new Error('host is empty');
48
53
  if (_this._port < 0 || _this._port > 65535)
49
- throw new Error("port is out of range");
54
+ throw new Error('port is out of range');
50
55
  };
51
56
  this._getValue = function (type) { return __awaiter(_this, void 0, void 0, function () {
52
57
  return __generator(this, function (_a) {
53
58
  switch (_a.label) {
54
- case 0: return [4 /*yield*/, this._send("GET", type)];
59
+ case 0: return [4 /*yield*/, this._send('GET', type)];
55
60
  case 1: return [2 /*return*/, _a.sent()];
56
61
  }
57
62
  });
@@ -59,38 +64,35 @@ var JmriClient = /** @class */ (function () {
59
64
  this._setValue = function (type, data) { return __awaiter(_this, void 0, void 0, function () {
60
65
  return __generator(this, function (_a) {
61
66
  switch (_a.label) {
62
- case 0: return [4 /*yield*/, this._send("POST", type, data)];
67
+ case 0: return [4 /*yield*/, this._send('POST', type, data)];
63
68
  case 1: return [2 /*return*/, _a.sent()];
64
69
  }
65
70
  });
66
71
  }); };
67
72
  this._send = function (method, type, data) { return __awaiter(_this, void 0, void 0, function () {
68
- var uri, options, response;
73
+ var config;
69
74
  return __generator(this, function (_a) {
70
75
  switch (_a.label) {
71
76
  case 0:
72
- uri = "".concat(this._protocol, "://").concat(this._host, ":").concat(this._port, "/json/").concat(type);
73
- options = {
74
- method: method,
77
+ config = {
78
+ url: "".concat(this._protocol, "://").concat(this._host, ":").concat(this._port, "/json/").concat(type)
75
79
  };
80
+ config.method = method;
76
81
  if (data !== undefined) {
77
- (options.body = JSON.stringify(data)),
78
- (options.headers = {
79
- "Content-Type": "application/json; charset=utf-8",
80
- });
82
+ config.data = data;
83
+ config.headers = {
84
+ 'Content-type': 'application/json; charset=utf-8'
85
+ };
81
86
  }
82
- return [4 /*yield*/, fetch(uri, options)];
83
- case 1:
84
- response = _a.sent();
85
- return [4 /*yield*/, response.json()];
86
- case 2: return [2 /*return*/, _a.sent()];
87
+ return [4 /*yield*/, axios_1["default"].request(config)];
88
+ case 1: return [2 /*return*/, _a.sent()];
87
89
  }
88
90
  });
89
91
  }); };
90
92
  this.getPower = function () { return __awaiter(_this, void 0, void 0, function () {
91
93
  return __generator(this, function (_a) {
92
94
  switch (_a.label) {
93
- case 0: return [4 /*yield*/, this._getValue("power")];
95
+ case 0: return [4 /*yield*/, this._getValue('power')];
94
96
  case 1: return [2 /*return*/, _a.sent()];
95
97
  }
96
98
  });
@@ -98,8 +100,8 @@ var JmriClient = /** @class */ (function () {
98
100
  this.setPower = function (isOn) { return __awaiter(_this, void 0, void 0, function () {
99
101
  return __generator(this, function (_a) {
100
102
  switch (_a.label) {
101
- case 0: return [4 /*yield*/, this._setValue("power", {
102
- state: isOn ? 2 : 4,
103
+ case 0: return [4 /*yield*/, this._setValue('power', {
104
+ state: isOn ? 2 : 4
103
105
  })];
104
106
  case 1: return [2 /*return*/, _a.sent()];
105
107
  }
@@ -108,7 +110,7 @@ var JmriClient = /** @class */ (function () {
108
110
  this.getRoster = function () { return __awaiter(_this, void 0, void 0, function () {
109
111
  return __generator(this, function (_a) {
110
112
  switch (_a.label) {
111
- case 0: return [4 /*yield*/, this._getValue("roster")];
113
+ case 0: return [4 /*yield*/, this._getValue('roster')];
112
114
  case 1: return [2 /*return*/, _a.sent()];
113
115
  }
114
116
  });
@@ -121,4 +123,4 @@ var JmriClient = /** @class */ (function () {
121
123
  }
122
124
  return JmriClient;
123
125
  }());
124
- export { JmriClient };
126
+ exports.JmriClient = JmriClient;
package/index.js CHANGED
@@ -1 +1 @@
1
- module.exports = require('./lib/jmriClient');
1
+ module.exports = require("./dist");
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "jmri-client",
3
- "version": "2.0.1",
3
+ "version": "2.2.0",
4
4
  "description": "node client to connect to a JMRI xmlio webservice",
5
5
  "main": "dist/index.js",
6
- "type": "module",
7
6
  "types": "dist/index.d.ts",
8
7
  "scripts": {
9
8
  "prepublish": "npm run build",
10
9
  "build": "tsc",
11
- "test": "jest --no-cache --coverage"
10
+ "test": "jest --no-cache --coverage",
11
+ "functional": "node ./tests/functional/demo.mjs"
12
12
  },
13
13
  "keywords": [
14
14
  "jmri",
@@ -37,14 +37,15 @@
37
37
  "node": ">=14"
38
38
  },
39
39
  "devDependencies": {
40
- "@types/jest": "^29.0.0",
41
- "@types/node": "^18.7.14",
42
- "jest": "^28.1.3",
43
- "ts-jest": "^28.0.8",
44
- "typescript": "^4.8.2"
40
+ "@types/jest": "^29.0.3",
41
+ "@types/node": "^18.7.18",
42
+ "jest": "^29.0.3",
43
+ "prompt": "^1.3.0",
44
+ "ts-jest": "^29.0.1",
45
+ "typescript": "^4.8.3"
45
46
  },
46
47
  "dependencies": {
47
- "node-fetch": "^3.2.10",
48
+ "axios": "^0.27.2",
48
49
  "xml2json": "^0.12.0"
49
50
  },
50
51
  "files": [