Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export {
RetryableSignedCommandFaultError,
} from "./signing/errors.js";
export { getTariffPeriods } from "./tariff.js";
export { Models } from "./types/vehicle.js";
export type { TariffRate, TariffPeriod, TariffResolution } from "./tariff.js";
10 changes: 10 additions & 0 deletions src/types/vehicle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,13 @@ export type VehicleResponse = {
export type GranularAccess = {
hide_private: boolean;
};

export const Models: Record<string, string> = {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep the exported model map immutable

Because this exported object is also the exact instance read by Vehicle.model(), a consumer can assign to or delete entries such as Models.S, silently changing model detection for every Vehicle instance in the process. This makes the previously private lookup table externally mutable despite the commit's intended behavior-preserving refactor; expose a runtime-frozen, read-only map or keep a separate internal copy.

Useful? React with 👍 / 👎.

S: "Model S",
"3": "Model 3",
X: "Model X",
Y: "Model Y",
C: "Cybertruck",
T: "Semi",
A: "Cybercab",
};
12 changes: 1 addition & 11 deletions src/vehicle.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
import TeslaFleetApi from "./teslafleetapi.js";
import { ClimateMode, ClimateModes, CommandResponse, FleetTelemetryConfig, Level, Seat, Seats, Trunk, VehicleDataEndpoint } from "./types/commands.js";
import { OptionsResponse } from "./types/responses.js";
import { VehicleResponse } from "./types/vehicle.js";
import { Models, VehicleResponse } from "./types/vehicle.js";
import { VehicleDataResponse } from "./types/vehicle_data.js";
import VehicleSpecific from "./vehiclespecific.js";
import VehicleSigned from "./vehiclesigned.js";

const Models: Record<string, string> = {
S: "Model S",
"3": "Model 3",
X: "Model X",
Y: "Model Y",
C: "Cybertruck",
T: "Semi",
A: "Cybercab",
};

export default class Vehicle {
parent: TeslaFleetApi;
/** Fallback private key for `signed()` when a call site doesn't pass one explicitly. */
Expand Down
15 changes: 15 additions & 0 deletions test/vehicle.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, expect, it } from "vitest";
import { Models } from "../src/index.js";
import TeslaFleetApi from "../src/teslafleetapi.js";
import Vehicle from "../src/vehicle.js";

Expand All @@ -19,3 +20,17 @@ describe("Vehicle.model", () => {
expect(vehicle.model(vin)).toBe(expected);
});
});

describe("Models public export", () => {
it.each([
["S", "Model S"],
["3", "Model 3"],
["X", "Model X"],
["Y", "Model Y"],
["C", "Cybertruck"],
["T", "Semi"],
["A", "Cybercab"],
])("maps code %s to %s", (code, expected) => {
expect(Models[code]).toBe(expected);
});
});
Loading