@eiannone/tesla-api 1.18.0 → 1.20.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/TeslaApi.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import {request} from 'https';
2
+ import {connect} from 'http2';
2
3
 
3
4
  const BASE_URL = "https://owner-api.teslamotors.com";
4
5
 
@@ -62,7 +63,9 @@ class TeslaApi {
62
63
  const req = request(BASE_URL + "/api/1/vehicles/" + path, {
63
64
  headers: headers,
64
65
  timeout: this.timeout,
65
- method: method
66
+ method: method,
67
+ minVersion: 'TLSv1.3',
68
+ maxVersion: 'TLSv1.3'
66
69
  }, res => {
67
70
  if (res.statusCode > 199 && res.statusCode < 300) {
68
71
  res.setEncoding('utf8');
@@ -138,40 +141,52 @@ class TeslaApi {
138
141
 
139
142
  async #oauthCall(params) {
140
143
  return new Promise((resolve, reject) => {
144
+
145
+ const session = connect('https://auth.tesla.com', {
146
+ minVersion: 'TLSv1.3', maxVersion: 'TLSv1.3'
147
+ });
148
+ session.on('error', (err) => {
149
+ reject(new ApiError(err.message + " ("+err.code+")", ApiError.NETWORK));
150
+ session.destroy();
151
+ });
152
+
141
153
  const postData = JSON.stringify(params);
142
- const req = request('https://auth.tesla.com/oauth2/v3/token', {
143
- headers: {
144
- 'user-agent': "TeslaEma",
145
- 'Content-Type': 'application/json',
146
- 'Content-Length': postData.length
147
- },
148
- timeout: 30000,
149
- method: 'POST'
150
- }, res => {
151
- if (res.statusCode > 199 && res.statusCode < 300) {
152
- res.setEncoding('utf8');
154
+ const req = session.request({
155
+ ':method': 'POST',
156
+ ':path': '/oauth2/v3/token',
157
+ 'user-agent': 'TeslaEma',
158
+ 'Content-Type': 'application/json',
159
+ 'Content-Length': postData.length
160
+ }, {
161
+ timeout: 30000
162
+ });
163
+
164
+ req.on('response', (headers) => {
165
+ const statusCode = headers[':status'];
166
+ if (statusCode > 199 && statusCode < 300) {
153
167
  let rawData = '';
154
- res.on('data', chunk => { rawData += chunk; });
155
- res.on('end', () => {
168
+ req.setEncoding('utf8');
169
+ req.on('data', chunk => { rawData += chunk; });
170
+ req.on('end', () => {
156
171
  try {
157
172
  resolve(JSON.parse(rawData));
158
173
  } catch(err) {
159
174
  reject(new ApiError(err));
160
175
  }
176
+ session.close();
161
177
  });
162
178
  } else {
163
- let errMsg = res.statusMessage + " ("+res.statusCode+")";
164
- reject(new ApiError(errMsg, this.#decodeStatus(res.statusCode)));
179
+ let errMsg = headers[':status'] + "";
180
+ reject(new ApiError(errMsg, this.#decodeStatus(statusCode)));
181
+ session.close();
165
182
  }
166
183
  });
167
- req.on('error', e => {
168
- // Error code examples:
169
- // - EAI_AGAIN (DNS lookup timeout)
170
- // - ECONNRESET
171
- // - ECONNREFUSED
172
- // - ENOTFOUND
184
+
185
+ req.on('error', (e) => {
173
186
  reject(new ApiError(e.message + " ("+e.code+")", ApiError.NETWORK));
187
+ session.destroy();
174
188
  });
189
+
175
190
  req.write(postData);
176
191
  req.end();
177
192
  });
@@ -0,0 +1,7 @@
1
+ import {TeslaApi} from '../TeslaApi.js';
2
+
3
+ const ACCESS_TOKEN = '123456789abcdef'; // Replace with your actual access token
4
+ const VEHICLE_ID = '1234567890'; // Replace with your actual vehicle ID
5
+
6
+ const api = new TeslaApi(ACCESS_TOKEN, VEHICLE_ID);
7
+ api.getVehicle().then(console.log);
package/package.json CHANGED
@@ -1,12 +1,15 @@
1
1
  {
2
2
  "name": "@eiannone/tesla-api",
3
- "version": "1.18.0",
3
+ "version": "1.20.0",
4
4
  "description": "Nodejs Tesla API",
5
5
  "type": "module",
6
6
  "main": "index.js",
7
7
  "author": "Emanuele Iannone",
8
8
  "license": "MIT",
9
- "repository": "eiannone/tesla-api",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/eiannone/tesla-api.git"
12
+ },
10
13
  "dependencies": {
11
14
  "ws": "^8.2.3"
12
15
  }