@teslemetry/api 0.0.1 → 0.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.
package/dist/index.mjs CHANGED
@@ -622,6 +622,13 @@ const client = createClient(createConfig({
622
622
  //#endregion
623
623
  //#region src/client/sdk.gen.ts
624
624
  /**
625
+ * Streaming field parameters
626
+ */
627
+ const getFieldsJson = (options) => (options?.client ?? client).get({
628
+ url: "/fields.json",
629
+ ...options
630
+ });
631
+ /**
625
632
  * Products
626
633
  *
627
634
  * Returns products mapped to user.
@@ -3080,9 +3087,6 @@ var TeslemetryVehicleStream = class {
3080
3087
  onConnectivity(callback) {
3081
3088
  return this.stream.onConnectivity(callback, { vin: this.vin });
3082
3089
  }
3083
- onCredits(callback) {
3084
- return this.stream.onCredits(callback, { vin: this.vin });
3085
- }
3086
3090
  onVehicleData(callback) {
3087
3091
  return this.stream.onVehicleData(callback, { vin: this.vin });
3088
3092
  }
@@ -3113,15 +3117,17 @@ var TeslemetryVehicleStream = class {
3113
3117
  var TeslemetryStream = class {
3114
3118
  root;
3115
3119
  active = false;
3120
+ connected = false;
3116
3121
  vin;
3122
+ cache;
3117
3123
  logger;
3118
3124
  listeners = /* @__PURE__ */ new Map();
3119
3125
  _connectionListeners = /* @__PURE__ */ new Map();
3120
3126
  vehicles = /* @__PURE__ */ new Map();
3121
- controller = null;
3122
- constructor(root, vin) {
3127
+ constructor(root, options) {
3123
3128
  this.root = root;
3124
- this.vin = vin;
3129
+ this.vin = options?.vin;
3130
+ this.cache = options?.cache;
3125
3131
  this.logger = root.logger;
3126
3132
  if (this.vin) this.getVehicle(this.vin);
3127
3133
  }
@@ -3129,9 +3135,6 @@ var TeslemetryStream = class {
3129
3135
  if (!this.vehicles.has(vin)) this.vehicles.set(vin, new TeslemetryVehicleStream(this.root, this, vin));
3130
3136
  return this.vehicles.get(vin);
3131
3137
  }
3132
- get connected() {
3133
- return this.active;
3134
- }
3135
3138
  async connect() {
3136
3139
  if (this.active) return;
3137
3140
  this.active = true;
@@ -3142,11 +3145,12 @@ var TeslemetryStream = class {
3142
3145
  while (this.active) try {
3143
3146
  const sse = await getSseByVin_({
3144
3147
  client: this.root.client,
3145
- path: { vin: this.vin || "" }
3148
+ path: { vin: this.vin || "" },
3149
+ query: { cache: this.cache }
3146
3150
  });
3147
- if (sse.controller) this.controller = sse.controller;
3148
3151
  this.logger.info(`Connected to stream`);
3149
3152
  retries = 0;
3153
+ this.connected = true;
3150
3154
  this._updateConnectionListeners(true);
3151
3155
  if (sse.stream) for await (const event of sse.stream) {
3152
3156
  if (!this.active) break;
@@ -3155,6 +3159,7 @@ var TeslemetryStream = class {
3155
3159
  } catch (error) {
3156
3160
  if (!this.active) break;
3157
3161
  this.logger.error("SSE error:", error);
3162
+ this.connected = false;
3158
3163
  this._updateConnectionListeners(false);
3159
3164
  retries++;
3160
3165
  const delay = Math.min(2 ** retries, 600) * 1e3;
@@ -3169,10 +3174,6 @@ var TeslemetryStream = class {
3169
3174
  }
3170
3175
  close() {
3171
3176
  this.active = false;
3172
- if (this.controller) {
3173
- this.controller.abort();
3174
- this.controller = null;
3175
- }
3176
3177
  this.logger.info(`Disconnecting from stream`);
3177
3178
  this._updateConnectionListeners(false);
3178
3179
  }
@@ -4905,6 +4906,10 @@ var TeslemetryApi = class {
4905
4906
  energySites: this._energySites
4906
4907
  };
4907
4908
  }
4909
+ async fields() {
4910
+ const { data } = await getFieldsJson({ client: this.root.client });
4911
+ return data;
4912
+ }
4908
4913
  /**
4909
4914
  * Returns a list of products (vehicles and energy sites) associated with the user account.
4910
4915
  * @returns A promise that resolves to an object containing a `response` array and count.
@@ -4938,10 +4943,11 @@ var TeslemetryApi = class {
4938
4943
  * and detailed info for each vehicle.
4939
4944
  */
4940
4945
  async fleetStatus(vins) {
4941
- return postApi1VehiclesFleetStatus({
4946
+ const { data } = await postApi1VehiclesFleetStatus({
4942
4947
  body: { vins },
4943
4948
  client: this.root.client
4944
4949
  });
4950
+ return data;
4945
4951
  }
4946
4952
  /**
4947
4953
  * Returns vehicles belonging to the account. This endpoint is paginated.
@@ -4949,7 +4955,8 @@ var TeslemetryApi = class {
4949
4955
  * pagination details, and a total count.
4950
4956
  */
4951
4957
  async vehicles() {
4952
- return getApi1Vehicles({ client: this.root.client });
4958
+ const { data } = await getApi1Vehicles({ client: this.root.client });
4959
+ return data;
4953
4960
  }
4954
4961
  /**
4955
4962
  * Redeems a share invite. Once redeemed, the account will gain access to the vehicle within the Tesla app.
@@ -4957,10 +4964,11 @@ var TeslemetryApi = class {
4957
4964
  * @returns Promise to an object containing the `vehicle_id_s` and `vin` of the shared vehicle.
4958
4965
  */
4959
4966
  async redeemInvitation(code) {
4960
- return postApi1VehiclesInvitationsRedeem({
4967
+ const { data } = await postApi1VehiclesInvitationsRedeem({
4961
4968
  body: { code },
4962
4969
  client: this.root.client
4963
4970
  });
4971
+ return data;
4964
4972
  }
4965
4973
  };
4966
4974
 
@@ -4988,9 +4996,9 @@ var Teslemetry = class {
4988
4996
  logger;
4989
4997
  _user = null;
4990
4998
  _charging = null;
4991
- constructor(access_token, region, logger = consoleLogger) {
4992
- this.logger = logger;
4993
- if (region) this.region = region;
4999
+ constructor(access_token, options) {
5000
+ this.logger = options?.logger || consoleLogger;
5001
+ if (options?.region) this.region = options.region;
4994
5002
  this.client = createClient({
4995
5003
  auth: access_token,
4996
5004
  baseUrl: `https://${this.region || "api"}.teslemetry.com`,
@@ -5008,7 +5016,7 @@ var Teslemetry = class {
5008
5016
  }
5009
5017
  return response;
5010
5018
  });
5011
- this.sse = new TeslemetryStream(this);
5019
+ this.sse = new TeslemetryStream(this, options?.stream);
5012
5020
  this.api = new TeslemetryApi(this);
5013
5021
  }
5014
5022
  /**
@@ -5032,6 +5040,31 @@ var Teslemetry = class {
5032
5040
  return response.headers.get("x-region");
5033
5041
  }
5034
5042
  /**
5043
+ * Creates API instances for all products (vehicles and energy sites) associated with the account.
5044
+ * @returns A promise that resolves to an object containing vehicle and energy site names, API, and SSE instances.
5045
+ */
5046
+ async createProducts() {
5047
+ const { data } = await getApi1Products({ client: this.client });
5048
+ const result = {
5049
+ vehicles: {},
5050
+ energySites: {}
5051
+ };
5052
+ data.response?.forEach((product) => {
5053
+ if (product.device_type === "vehicle") result.vehicles[product.vin] = {
5054
+ name: product.display_name ?? useTeslaModel(product.vin),
5055
+ vin: product.vin,
5056
+ api: this.api.getVehicle(product.vin),
5057
+ sse: this.sse.getVehicle(product.vin)
5058
+ };
5059
+ if (product.device_type === "energy") result.energySites[product.energy_site_id] = {
5060
+ name: product.site_name ?? "Unnamed",
5061
+ site: product.energy_site_id,
5062
+ api: this.api.getEnergySite(product.energy_site_id)
5063
+ };
5064
+ });
5065
+ return result;
5066
+ }
5067
+ /**
5035
5068
  * Get a vehicle API instance for the specified VIN.
5036
5069
  * @param vin Vehicle Identification Number
5037
5070
  * @returns TeslemetryVehicleApi instance
@@ -5064,6 +5097,15 @@ var Teslemetry = class {
5064
5097
  return this._charging;
5065
5098
  }
5066
5099
  };
5100
+ const model_names = {
5101
+ "3": "Model 3",
5102
+ S: "Model S",
5103
+ X: "Model X",
5104
+ Y: "Model Y",
5105
+ C: "Cybertruck",
5106
+ T: "Semi"
5107
+ };
5108
+ const useTeslaModel = (vin) => model_names?.[vin[3]] ?? "Unknown";
5067
5109
 
5068
5110
  //#endregion
5069
5111
  //#region src/exceptions.ts
@@ -5099,4 +5141,4 @@ var ValueError = class extends Error {
5099
5141
  };
5100
5142
 
5101
5143
  //#endregion
5102
- export { Teslemetry, TeslemetryApi, TeslemetryStream, TeslemetryStreamConnectionError, TeslemetryStreamEnded, TeslemetryStreamError, TeslemetryVehicleApi, TeslemetryVehicleStream, TeslemetryVehicleStreamNotConfigured, ValueError };
5144
+ export { Teslemetry, TeslemetryApi, TeslemetryEnergyApi, TeslemetryStream, TeslemetryStreamConnectionError, TeslemetryStreamEnded, TeslemetryStreamError, TeslemetryVehicleApi, TeslemetryVehicleStream, TeslemetryVehicleStreamNotConfigured, ValueError };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teslemetry/api",
3
- "version": "0.0.1",
3
+ "version": "0.1.0",
4
4
  "description": "API client for Teslemetry",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",