@eiannone/tesla-api 1.11.0 → 1.12.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.
Files changed (2) hide show
  1. package/TeslaApi.js +59 -63
  2. package/package.json +1 -1
package/TeslaApi.js CHANGED
@@ -129,46 +129,47 @@ class TeslaApi {
129
129
  return this.#apiCall(vid + "/command/" + command, "POST", params);
130
130
  }
131
131
 
132
- // async #oauthCall(params) {
133
- // return new Promise((resolve, reject) => {
134
- // const postData = JSON.stringify(params);
135
- // const req = request(BASE_URL + '/oauth/token', {
136
- // headers: {
137
- // 'user-agent': "TeslaEma",
138
- // 'Content-Type': 'application/json',
139
- // 'Content-Length': postData.length
140
- // },
141
- // timeout: 30000,
142
- // method: 'POST'
143
- // }, res => {
144
- // if (res.statusCode > 199 && res.statusCode < 300) {
145
- // res.setEncoding('utf8');
146
- // let rawData = '';
147
- // res.on('data', chunk => { rawData += chunk; });
148
- // res.on('end', () => {
149
- // try {
150
- // resolve(JSON.parse(rawData));
151
- // } catch(err) {
152
- // reject(new ApiError(err));
153
- // }
154
- // });
155
- // } else {
156
- // let errMsg = res.statusMessage + " ("+res.statusCode+")";
157
- // reject(new ApiError(errMsg, this.#decodeStatus(res.statusCode)));
158
- // }
159
- // });
160
- // req.on('error', e => {
161
- // // Error code examples:
162
- // // - EAI_AGAIN (DNS lookup timeout)
163
- // // - ECONNRESET
164
- // // - ECONNREFUSED
165
- // // - ENOTFOUND
166
- // reject(new ApiError(e.message + " ("+e.code+")", ApiError.NETWORK));
167
- // });
168
- // req.write(postData);
169
- // req.end();
170
- // });
171
- // }
132
+ async #oauthCall(params, bearer_token) {
133
+ return new Promise((resolve, reject) => {
134
+ const postData = JSON.stringify(params);
135
+ const req = request(BASE_URL + '/oauth/token', {
136
+ headers: {
137
+ 'user-agent': "TeslaEma",
138
+ 'Authorization': "Bearer " + bearer_token,
139
+ 'Content-Type': 'application/json',
140
+ 'Content-Length': postData.length
141
+ },
142
+ timeout: 30000,
143
+ method: 'POST'
144
+ }, res => {
145
+ if (res.statusCode > 199 && res.statusCode < 300) {
146
+ res.setEncoding('utf8');
147
+ let rawData = '';
148
+ res.on('data', chunk => { rawData += chunk; });
149
+ res.on('end', () => {
150
+ try {
151
+ resolve(JSON.parse(rawData));
152
+ } catch(err) {
153
+ reject(new ApiError(err));
154
+ }
155
+ });
156
+ } else {
157
+ let errMsg = res.statusMessage + " ("+res.statusCode+")";
158
+ reject(new ApiError(errMsg, this.#decodeStatus(res.statusCode)));
159
+ }
160
+ });
161
+ req.on('error', e => {
162
+ // Error code examples:
163
+ // - EAI_AGAIN (DNS lookup timeout)
164
+ // - ECONNRESET
165
+ // - ECONNREFUSED
166
+ // - ENOTFOUND
167
+ reject(new ApiError(e.message + " ("+e.code+")", ApiError.NETWORK));
168
+ });
169
+ req.write(postData);
170
+ req.end();
171
+ });
172
+ }
172
173
 
173
174
  async #oauthCall2(params) {
174
175
  return new Promise((resolve, reject) => {
@@ -222,30 +223,25 @@ class TeslaApi {
222
223
  refresh_token,
223
224
  scope: 'openid email offline_access'
224
225
  };
225
- return this.#oauthCall2(payLoad)
226
- .then(async (resp) => {
227
- this.token = resp.access_token;
228
- this.refresh_token = resp.refresh_token;
229
- if (typeof this.cb_refreshToken == 'function') {
230
- this.cb_refreshToken(this.token, this.refresh_token);
231
- }
232
- return resp;
233
- })
234
- .catch(error => {
235
- if (error instanceof Error) error.message += " - Unable to refresh Token";
236
- throw error;
237
- });
226
+ try {
227
+ let resp = await this.#oauthCall2(payLoad);
228
+ this.refresh_token = resp.refresh_token;
229
+ let oauth = await this.#oauthCall({
230
+ grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
231
+ client_id: '81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384',
232
+ client_secret: 'c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3'
233
+ }, resp.access_token);
234
+ this.token = oauth.access_token;
235
+ if (typeof this.cb_refreshToken == 'function') {
236
+ this.cb_refreshToken(this.token, this.refresh_token);
237
+ }
238
+ return oauth;
239
+ }
240
+ catch(error) {
241
+ if (error instanceof Error) error.message += " - Unable to refresh Token";
242
+ throw error;
243
+ }
238
244
  }
239
-
240
- // async getTokens(email, password) {
241
- // return this.#oauthCall({
242
- // 'grant_type': "password",
243
- // 'email': email,
244
- // 'password': password,
245
- // 'client_id': "81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384",
246
- // 'client_secret': "c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3"
247
- // });
248
- // }
249
245
  }
250
246
 
251
247
  export { ApiError, TeslaApi }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eiannone/tesla-api",
3
- "version": "1.11.0",
3
+ "version": "1.12.0",
4
4
  "description": "Nodejs Tesla API",
5
5
  "type": "module",
6
6
  "main": "index.js",