@balldontlie/sdk
Version:
Official TypeScript/JavaScript SDK for the balldontlie API
158 lines (157 loc) • 7.64 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const src_1 = require("../src");
const chai_1 = require("chai");
const API_KEY = process.env.BALLDONTLIE_API_KEY;
if (!API_KEY) {
throw new Error("BALLDONTLIE_API_KEY environment variable is required");
}
describe("NBA API", () => {
let api;
before(() => {
api = new src_1.BalldontlieAPI({ apiKey: API_KEY });
});
describe("Leaders", () => {
it("should get leaders", () => __awaiter(void 0, void 0, void 0, function* () {
const leaders = yield api.nba.getLeaders({
stat_type: "ast",
season: 2023,
});
(0, chai_1.expect)(leaders.data.length).to.be.greaterThan(0);
}));
});
describe("Odds", () => {
it("should get odds", () => __awaiter(void 0, void 0, void 0, function* () {
const odds = yield api.nba.getOdds({ date: "2024-04-01" });
(0, chai_1.expect)(odds.data.length).to.be.greaterThan(0);
}));
});
describe("Teams", () => {
it("should get all teams", () => __awaiter(void 0, void 0, void 0, function* () {
const teams = yield api.nba.getTeams();
(0, chai_1.expect)(teams.data).to.be.an("array");
(0, chai_1.expect)(teams.data[0]).to.have.all.keys("id", "conference", "division", "city", "name", "full_name", "abbreviation");
}));
it("should get teams filtered by conference", () => __awaiter(void 0, void 0, void 0, function* () {
const teams = yield api.nba.getTeams({ conference: "East" });
(0, chai_1.expect)(teams.data).to.be.an("array");
teams.data.forEach((team) => {
(0, chai_1.expect)(team.conference).to.equal("East");
});
}));
it("should get a single team", () => __awaiter(void 0, void 0, void 0, function* () {
const team = yield api.nba.getTeam(1);
(0, chai_1.expect)(team.data).to.have.property("id", 1);
}));
});
describe("Players", () => {
it("should get players with pagination", () => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b;
const players = yield api.nba.getPlayers({ per_page: 5 });
(0, chai_1.expect)(players.data).to.be.an("array");
(0, chai_1.expect)(players.data).to.have.lengthOf(5);
(0, chai_1.expect)((_a = players.meta) === null || _a === void 0 ? void 0 : _a.per_page).to.equal(5);
(0, chai_1.expect)((_b = players.meta) === null || _b === void 0 ? void 0 : _b.next_cursor).to.be.a("number");
}));
it("should search players by name", () => __awaiter(void 0, void 0, void 0, function* () {
const players = yield api.nba.getPlayers({ search: "lebron" });
(0, chai_1.expect)(players.data).to.be.an("array");
players.data.forEach((player) => {
(0, chai_1.expect)(player.first_name.toLowerCase()).to.include("lebron");
});
}));
it("should get active players", () => __awaiter(void 0, void 0, void 0, function* () {
const players = yield api.nba.getActivePlayers();
(0, chai_1.expect)(players.data).to.be.an("array");
}));
});
describe("Games", () => {
it("should get games with filters", () => __awaiter(void 0, void 0, void 0, function* () {
const games = yield api.nba.getGames({
seasons: [2023],
dates: ["2024-04-01", "2024-04-02"],
});
(0, chai_1.expect)(games.data).to.be.an("array");
games.data.forEach((game) => {
(0, chai_1.expect)(game.season).to.equal(2023);
});
}));
it("should get a specific game", () => __awaiter(void 0, void 0, void 0, function* () {
const games = yield api.nba.getGames();
const gameId = games.data[0].id;
const game = yield api.nba.getGame(gameId);
(0, chai_1.expect)(game.data).to.have.property("id", gameId);
}));
it("should get live box scores", () => __awaiter(void 0, void 0, void 0, function* () {
const boxScores = yield api.nba.getLiveBoxScores();
(0, chai_1.expect)(boxScores.data).to.be.an("array");
}));
});
describe("Stats", () => {
it("should get player stats", () => __awaiter(void 0, void 0, void 0, function* () {
const stats = yield api.nba.getStats({
per_page: 1,
});
(0, chai_1.expect)(stats.data).to.be.an("array");
stats.data.forEach((stat) => {
(0, chai_1.expect)(stat).to.have.property("pts");
(0, chai_1.expect)(stat).to.have.property("ast");
(0, chai_1.expect)(stat).to.have.property("reb");
});
}));
it("should get season averages", () => __awaiter(void 0, void 0, void 0, function* () {
const players = yield api.nba.getPlayers();
const playerId = players.data[0].id;
const stats = yield api.nba.getSeasonAverages({
season: 2023,
player_id: playerId,
});
(0, chai_1.expect)(stats.data).to.be.an("array");
}));
it("should get advanced stats", () => __awaiter(void 0, void 0, void 0, function* () {
const stats = yield api.nba.getAdvancedStats({
seasons: [2023],
per_page: 5,
});
(0, chai_1.expect)(stats.data).to.be.an("array");
}));
});
describe("Injuries", () => {
it("should get injuries", () => __awaiter(void 0, void 0, void 0, function* () {
const res = yield api.nba.getPlayerInjuries();
(0, chai_1.expect)(res.data).to.be.an("array");
const injury = res.data[0];
(0, chai_1.expect)(injury).to.have.property("player");
(0, chai_1.expect)(injury).to.have.property("status");
}));
});
describe("Standings", () => {
it("should get standings", () => __awaiter(void 0, void 0, void 0, function* () {
const standings = yield api.nba.getStandings({ season: 2023 });
(0, chai_1.expect)(standings.data).to.be.an("array");
const s = standings.data[0];
(0, chai_1.expect)(s).to.have.property("team");
}));
});
describe("Error Handling", () => {
it("should handle 404 errors", () => __awaiter(void 0, void 0, void 0, function* () {
try {
yield api.nba.getTeam(999999);
chai_1.expect.fail("Should have thrown error");
}
catch (e) {
(0, chai_1.expect)(e).to.be.instanceOf(src_1.APIError);
(0, chai_1.expect)(e.statusCode).to.equal(404);
}
}));
});
});