myenergi-api 2.0.0 → 2.1.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.
@@ -62,7 +62,7 @@ class AuthDigest {
62
62
  get nc() {
63
63
  var _a;
64
64
  this._nc++;
65
- var myHex = ("0000000" + ((_a = this._nc) === null || _a === void 0 ? void 0 : _a.toString(16))).substr(-8);
65
+ const myHex = ("0000000" + ((_a = this._nc) === null || _a === void 0 ? void 0 : _a.toString(16))).substr(-8);
66
66
  return myHex;
67
67
  }
68
68
  get cnonce() {
@@ -70,35 +70,37 @@ class AuthDigest {
70
70
  return this._cnonce;
71
71
  }
72
72
  md5(data) {
73
- let md5 = crypto.createHash("md5");
74
- let result = md5.update(data).digest("hex");
73
+ const md5 = crypto.createHash("md5");
74
+ const result = md5.update(data).digest("hex");
75
75
  return result;
76
76
  }
77
77
  init(wwwAuthHeader) {
78
- let authSplit = wwwAuthHeader.split(",");
79
- for (let item of authSplit) {
80
- if (item.indexOf("realm=") >= 0) {
81
- let realmSplit = item.split('="');
78
+ if (!wwwAuthHeader)
79
+ return;
80
+ const authSplit = wwwAuthHeader === null || wwwAuthHeader === void 0 ? void 0 : wwwAuthHeader.split(",");
81
+ for (const item of authSplit) {
82
+ if (item && item.indexOf("realm=") >= 0) {
83
+ const realmSplit = item.split('="');
82
84
  this._realm = realmSplit[realmSplit.length - 1];
83
85
  this._realm = this._realm.substring(0, this._realm.length - 1);
84
86
  }
85
- if (item.indexOf("nonce=") >= 0) {
86
- let nonceSplit = item.split('="');
87
+ if (item && item.indexOf("nonce=") >= 0) {
88
+ const nonceSplit = item.split('="');
87
89
  this._nonce = nonceSplit[nonceSplit.length - 1];
88
90
  this._nonce = this._nonce.substring(0, this._nonce.length - 1);
89
91
  }
90
- if (item.indexOf("qop=") >= 0) {
91
- let qopSplit = item.split('="');
92
+ if (item && item.indexOf("qop=") >= 0) {
93
+ const qopSplit = item.split('="');
92
94
  this._qop = qopSplit[qopSplit.length - 1];
93
95
  this._qop = this._qop.substring(0, this._qop.length - 1);
94
96
  }
95
- if (item.indexOf("opaque=") >= 0) {
96
- let opaqueSplit = item.split('="');
97
+ if (item && item.indexOf("opaque=") >= 0) {
98
+ const opaqueSplit = item.split('="');
97
99
  this._opaque = opaqueSplit[opaqueSplit.length - 1];
98
100
  this._opaque = this._opaque.substring(0, this._opaque.length - 1);
99
101
  }
100
- if (item.indexOf("algorithm=") >= 0) {
101
- let algorithmSplit = item.split("=");
102
+ if (item && item.indexOf("algorithm=") >= 0) {
103
+ const algorithmSplit = item.split("=");
102
104
  this._algorithm = algorithmSplit[algorithmSplit.length - 1];
103
105
  this._algorithm = this._algorithm.substring(0, this._algorithm.length);
104
106
  if (this._onError && this.algorithm !== "MD5") {
@@ -113,9 +115,9 @@ class AuthDigest {
113
115
  return "";
114
116
  const nc = this.nc;
115
117
  const cnonce = this.cnonce;
116
- let HA1 = this.md5(this.username + ":" + this.realm + ":" + this._password);
117
- let HA2 = this.md5(httpMethod + ":" + path);
118
- let response = this.md5(HA1 + ":" + this.nonce + ":" + nc + ":" + cnonce + ":" + this.qop + ":" + HA2);
118
+ const HA1 = this.md5(this.username + ":" + this.realm + ":" + this._password);
119
+ const HA2 = this.md5(httpMethod + ":" + path);
120
+ const response = this.md5(HA1 + ":" + this.nonce + ":" + nc + ":" + cnonce + ":" + this.qop + ":" + HA2);
119
121
  let res = `Digest username="${this.username}",`;
120
122
  res += `realm="${this.realm}",`;
121
123
  res += `nonce="${this.nonce}",`;
package/dist/Digest.d.ts CHANGED
@@ -5,5 +5,5 @@ export declare class Digest {
5
5
  private _maxRetryCount;
6
6
  constructor(baseUrl: string, username: string, password: string);
7
7
  private request;
8
- get(requestUrl: string, data?: any): Promise<any>;
8
+ get(requestUrl: URL, data?: unknown): Promise<string>;
9
9
  }
package/dist/Digest.js CHANGED
@@ -25,13 +25,12 @@ var __importStar = (this && this.__importStar) || function (mod) {
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.Digest = void 0;
27
27
  const https = __importStar(require("https"));
28
- const url = __importStar(require("url"));
29
28
  const AuthDigest_1 = require("./AuthDigest");
30
29
  class Digest {
31
30
  constructor(baseUrl, username, password) {
32
31
  this._maxRedirectCount = 3;
33
32
  this._maxRetryCount = 2;
34
- this._baseUrl = url.parse(baseUrl, true);
33
+ this._baseUrl = new URL(baseUrl);
35
34
  this._authDigest = new AuthDigest_1.AuthDigest(username, password, (err) => {
36
35
  console.error(err);
37
36
  });
@@ -87,7 +86,7 @@ class Digest {
87
86
  reject(resaon);
88
87
  });
89
88
  }
90
- else if (res.statusCode >= 200 && res.statusCode < 300) {
89
+ else if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) {
91
90
  const myenergiAsn = res.headers["x_myenergi-asn"];
92
91
  if (myenergiAsn && myenergiAsn !== "undefined" && myenergiAsn !== this._baseUrl.host) {
93
92
  if (redirectCount > this._maxRedirectCount) {
@@ -115,13 +114,13 @@ class Digest {
115
114
  resolve(resData);
116
115
  });
117
116
  }
118
- else if (res.statusCode >= 300 && res.statusCode < 400) {
117
+ else if (res.statusCode && res.statusCode >= 300 && res.statusCode < 400) {
119
118
  if (redirectCount > this._maxRedirectCount) {
120
119
  reject(`Too many redirects: ${res.headers["location"]}`);
121
120
  return;
122
121
  }
123
122
  const location = res.headers["location"];
124
- const uri = url.parse(location, true);
123
+ const uri = new URL(location);
125
124
  if (uri.host !== this._baseUrl.host) {
126
125
  this._baseUrl.host = uri.host;
127
126
  }
@@ -151,18 +150,17 @@ class Digest {
151
150
  });
152
151
  }
153
152
  get(requestUrl, data) {
154
- const uri = url.parse(requestUrl, true);
155
153
  const options = {
156
154
  hostname: this._baseUrl.hostname,
157
155
  host: this._baseUrl.host,
158
156
  port: this._baseUrl.port,
159
- path: uri.path,
157
+ path: requestUrl.pathname,
160
158
  method: "GET",
161
159
  headers: {
162
160
  Connection: "Keep-Alive",
163
161
  "Content-Type": "application/json",
164
162
  Accept: "application/json",
165
- Host: uri.hostname,
163
+ Host: requestUrl.hostname,
166
164
  },
167
165
  };
168
166
  return this.request(options, data);
@@ -6,16 +6,16 @@ export declare class MyEnergi {
6
6
  private _config;
7
7
  private _digest;
8
8
  constructor(username: string, password: string, apiBaseUrl?: string);
9
- getStatusAll(): Promise<any>;
9
+ getStatusAll(): Promise<unknown>;
10
10
  getStatusZappiAll(): Promise<Zappi[]>;
11
11
  getStatusZappi(serialNumber: string): Promise<Zappi | null>;
12
- setZappiChargeMode(serialNo: string, chargeMode: ZappiChargeMode): Promise<any>;
13
- setZappiBoostMode(serialNo: string, boostMode: ZappiBoostMode, kwh?: number, completeTime?: string): Promise<any>;
14
- setZappiGreenLevel(serialNo: string, percentage: number): Promise<any>;
12
+ setZappiChargeMode(serialNo: string, chargeMode: ZappiChargeMode): Promise<unknown>;
13
+ setZappiBoostMode(serialNo: string, boostMode: ZappiBoostMode, kwh?: number, completeTime?: string): Promise<unknown>;
14
+ setZappiGreenLevel(serialNo: string, percentage: number): Promise<unknown>;
15
15
  getStatusEddiAll(): Promise<Eddi[]>;
16
16
  getStatusEddi(serialNumber: string): Promise<Eddi | null>;
17
- setEddiMode(serialNo: string, mode: EddiMode): Promise<any>;
18
- setEddiBoost(serialNo: string, boost: EddiBoost, minutes?: number): Promise<any>;
17
+ setEddiMode(serialNo: string, mode: EddiMode): Promise<unknown>;
18
+ setEddiBoost(serialNo: string, boost: EddiBoost, minutes?: number): Promise<unknown>;
19
19
  getStatusHarviAll(): Promise<Harvi[]>;
20
20
  getStatusHarvi(serialNumber: string): Promise<Harvi | null>;
21
21
  }
package/dist/MyEnergi.js CHANGED
@@ -37,7 +37,7 @@ class MyEnergi {
37
37
  getStatusAll() {
38
38
  return __awaiter(this, void 0, void 0, function* () {
39
39
  try {
40
- const data = yield this._digest.get(this._config.status_url);
40
+ const data = yield this._digest.get(new URL(this._config.status_url, this._config.base_url));
41
41
  const jsonData = JSON.parse(data);
42
42
  return jsonData;
43
43
  }
@@ -49,7 +49,7 @@ class MyEnergi {
49
49
  getStatusZappiAll() {
50
50
  return __awaiter(this, void 0, void 0, function* () {
51
51
  try {
52
- const data = yield this._digest.get(this._config.zappi_url);
52
+ const data = yield this._digest.get(new URL(this._config.zappi_url, this._config.base_url));
53
53
  const jsonData = JSON.parse(data);
54
54
  if (jsonData.zappi)
55
55
  return Object.assign([], jsonData.zappi);
@@ -64,7 +64,7 @@ class MyEnergi {
64
64
  getStatusZappi(serialNumber) {
65
65
  return __awaiter(this, void 0, void 0, function* () {
66
66
  try {
67
- const data = yield this._digest.get(this._config.zappi_url);
67
+ const data = yield this._digest.get(new URL(this._config.zappi_url, this._config.base_url));
68
68
  const jsonData = JSON.parse(data);
69
69
  if (jsonData.zappi) {
70
70
  const zappi = Object.assign([], jsonData.zappi).find((zappi) => {
@@ -86,7 +86,7 @@ class MyEnergi {
86
86
  setZappiChargeMode(serialNo, chargeMode) {
87
87
  return __awaiter(this, void 0, void 0, function* () {
88
88
  try {
89
- const url = `${this._config.zappi_mode_url}${serialNo}-${chargeMode}-0-0-0000`;
89
+ const url = new URL(`${this._config.zappi_mode_url}${serialNo}-${chargeMode}-0-0-0000`, this._config.base_url);
90
90
  const data = yield this._digest.get(url);
91
91
  const jsonData = JSON.parse(data);
92
92
  return jsonData;
@@ -103,7 +103,7 @@ class MyEnergi {
103
103
  kwh = 0;
104
104
  completeTime = "0000";
105
105
  }
106
- const url = `${this._config.zappi_mode_url}${serialNo}-0-${boostMode}-${kwh}-${completeTime}`;
106
+ const url = new URL(`${this._config.zappi_mode_url}${serialNo}-0-${boostMode}-${kwh}-${completeTime}`, this._config.base_url);
107
107
  const data = yield this._digest.get(url);
108
108
  const jsonData = JSON.parse(data);
109
109
  return jsonData;
@@ -116,7 +116,7 @@ class MyEnergi {
116
116
  setZappiGreenLevel(serialNo, percentage) {
117
117
  return __awaiter(this, void 0, void 0, function* () {
118
118
  try {
119
- const url = `${this._config.zappi_min_green_url}${serialNo}-${percentage}`;
119
+ const url = new URL(`${this._config.zappi_min_green_url}${serialNo}-${percentage}`, this._config.base_url);
120
120
  const data = yield this._digest.get(url);
121
121
  const jsonData = JSON.parse(data);
122
122
  return jsonData;
@@ -129,7 +129,7 @@ class MyEnergi {
129
129
  getStatusEddiAll() {
130
130
  return __awaiter(this, void 0, void 0, function* () {
131
131
  try {
132
- const data = yield this._digest.get(this._config.eddi_url);
132
+ const data = yield this._digest.get(new URL(this._config.eddi_url, this._config.base_url));
133
133
  const jsonData = JSON.parse(data);
134
134
  if (jsonData.eddi)
135
135
  return Object.assign([], jsonData.eddi);
@@ -144,7 +144,7 @@ class MyEnergi {
144
144
  getStatusEddi(serialNumber) {
145
145
  return __awaiter(this, void 0, void 0, function* () {
146
146
  try {
147
- const data = yield this._digest.get(this._config.eddi_url);
147
+ const data = yield this._digest.get(new URL(this._config.eddi_url, this._config.base_url));
148
148
  const jsonData = JSON.parse(data);
149
149
  if (jsonData.eddi) {
150
150
  const eddi = Object.assign([], jsonData.eddi).find((eddi) => {
@@ -166,7 +166,7 @@ class MyEnergi {
166
166
  setEddiMode(serialNo, mode) {
167
167
  return __awaiter(this, void 0, void 0, function* () {
168
168
  try {
169
- const url = `${this._config.eddi_mode_url}${serialNo}-${mode}`;
169
+ const url = new URL(`${this._config.eddi_mode_url}${serialNo}-${mode}`, this._config.base_url);
170
170
  const data = yield this._digest.get(url);
171
171
  const jsonData = JSON.parse(data);
172
172
  return jsonData;
@@ -179,7 +179,7 @@ class MyEnergi {
179
179
  setEddiBoost(serialNo, boost, minutes = 0) {
180
180
  return __awaiter(this, void 0, void 0, function* () {
181
181
  try {
182
- const url = `${this._config.eddi_boost_url}${serialNo}-${boost}-${minutes}`;
182
+ const url = new URL(`${this._config.eddi_boost_url}${serialNo}-${boost}-${minutes}`, this._config.base_url);
183
183
  const data = yield this._digest.get(url);
184
184
  const jsonData = JSON.parse(data);
185
185
  return jsonData;
@@ -192,7 +192,7 @@ class MyEnergi {
192
192
  getStatusHarviAll() {
193
193
  return __awaiter(this, void 0, void 0, function* () {
194
194
  try {
195
- const data = yield this._digest.get(this._config.harvi_url);
195
+ const data = yield this._digest.get(new URL(this._config.harvi_url, this._config.base_url));
196
196
  const jsonData = JSON.parse(data);
197
197
  if (jsonData.harvi)
198
198
  return Object.assign([], jsonData.harvi);
@@ -207,7 +207,7 @@ class MyEnergi {
207
207
  getStatusHarvi(serialNumber) {
208
208
  return __awaiter(this, void 0, void 0, function* () {
209
209
  try {
210
- const data = yield this._digest.get(this._config.harvi_url);
210
+ const data = yield this._digest.get(new URL(this._config.harvi_url, this._config.base_url));
211
211
  const jsonData = JSON.parse(data);
212
212
  if (jsonData.harvi) {
213
213
  const harvi = Object.assign([], jsonData.harvi).find((harvi) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "myenergi-api",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "NodeJS implementation of MyEnergi API for controlling Zappi, Eddi and other MyEnergi products",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -11,7 +11,7 @@
11
11
  "build": "tsc",
12
12
  "test": "jest --runInBand --config jestconfig.json",
13
13
  "format": "prettier --write \"src/**/*.ts\" \"tests/**/*.ts\"",
14
- "lint": "tslint -p tsconfig.json",
14
+ "lint": "npx eslint -c .eslintrc.json '**/*.ts'",
15
15
  "prepare": "tsc",
16
16
  "preversion": "npm test",
17
17
  "version": "",
@@ -35,16 +35,19 @@
35
35
  "homepage": "https://github.com/bisand/myenergi-api#readme",
36
36
  "dependencies": {},
37
37
  "devDependencies": {
38
- "@jest/test-sequencer": "^28.1.1",
38
+ "@tsconfig/node12": "^1.0.11",
39
+ "@jest/test-sequencer": "^28.1.3",
39
40
  "@types/jest": "^28.1.3",
40
- "@types/node": "^16.11.12",
41
- "dotenv": "^16.0.2",
41
+ "@types/node": "^12",
42
+ "@typescript-eslint/eslint-plugin": "^5.39.0",
43
+ "@typescript-eslint/parser": "^5.39.0",
44
+ "dotenv": "^16.0.3",
42
45
  "jest": "^28.1.3",
43
46
  "nock": "^13.2.9",
44
47
  "prettier": "^2.7.1",
45
48
  "ts-jest": "^28.0.8",
46
49
  "ts-node": "^10.9.1",
47
- "tslint": "^6.1.3",
48
- "typescript": "^4.8.3"
50
+ "typescript": "^4.8.4",
51
+ "eslint": "^8.24.0"
49
52
  }
50
53
  }