@balldontlie/sdk
Version:
Official TypeScript/JavaScript SDK for the balldontlie API
119 lines (118 loc) • 5.81 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("MLB API", () => {
let api;
before(() => {
api = new src_1.BalldontlieAPI({ apiKey: API_KEY });
});
describe("Teams", () => {
it("should get all teams", () => __awaiter(void 0, void 0, void 0, function* () {
const teams = yield api.mlb.getTeams();
(0, chai_1.expect)(teams.data).to.be.an("array");
(0, chai_1.expect)(teams.data[0]).to.have.all.keys("id", "slug", "abbreviation", "display_name", "short_display_name", "name", "location", "league", "division");
}));
it("should filter teams by league", () => __awaiter(void 0, void 0, void 0, function* () {
const teams = yield api.mlb.getTeams({ league: "American" });
(0, chai_1.expect)(teams.data).to.be.an("array");
teams.data.forEach((team) => {
(0, chai_1.expect)(team.league).to.equal("American");
});
}));
it("should get a single team", () => __awaiter(void 0, void 0, void 0, function* () {
const teams = yield api.mlb.getTeams();
const teamId = teams.data[0].id;
const team = yield api.mlb.getTeam(teamId);
(0, chai_1.expect)(team.data).to.have.property("id", teamId);
}));
});
describe("Players", () => {
it("should get players with pagination", () => __awaiter(void 0, void 0, void 0, function* () {
const players = yield api.mlb.getPlayers({ per_page: 5 });
(0, chai_1.expect)(players.data).to.be.an("array");
(0, chai_1.expect)(players.data).to.have.lengthOf(5);
const player = players.data[0];
const res = yield api.mlb.getPlayer(player.id);
(0, chai_1.expect)(res.data.id).to.equal(player.id);
}));
it("should search players by name", () => __awaiter(void 0, void 0, void 0, function* () {
const players = yield api.mlb.getPlayers({ search: "Trout" });
(0, chai_1.expect)(players.data).to.be.an("array");
players.data.forEach((player) => {
(0, chai_1.expect)(player.last_name.toLowerCase()).to.include("trout");
});
}));
it("should get active players", () => __awaiter(void 0, void 0, void 0, function* () {
const players = yield api.mlb.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.mlb.getGames({
seasons: [2023],
per_page: 5,
});
(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.mlb.getGames();
const gameId = games.data[0].id;
const game = yield api.mlb.getGame(gameId);
(0, chai_1.expect)(game.data).to.have.property("id", gameId);
}));
});
describe("Standings", () => {
it("should get standings", () => __awaiter(void 0, void 0, void 0, function* () {
const res = yield api.mlb.getStandings({ season: 2023 });
(0, chai_1.expect)(res.data).to.be.an("array");
}));
});
describe("Injuries", () => {
it("should get injuries", () => __awaiter(void 0, void 0, void 0, function* () {
const injuries = yield api.mlb.getPlayerInjuries();
(0, chai_1.expect)(injuries.data).to.be.an("array");
}));
});
describe("Stats", () => {
it("should get player stats", () => __awaiter(void 0, void 0, void 0, function* () {
const stats = yield api.mlb.getStats({
per_page: 1,
});
(0, chai_1.expect)(stats.data).to.be.an("array");
(0, chai_1.expect)(stats.data[0]).to.have.property("player");
}));
it("should get season stats", () => __awaiter(void 0, void 0, void 0, function* () {
const stats = yield api.mlb.getSeasonStats({
season: 2023,
});
(0, chai_1.expect)(stats.data).to.be.an("array");
}));
it("should get team season stats", () => __awaiter(void 0, void 0, void 0, function* () {
const teams = yield api.mlb.getTeams();
const teamId = teams.data[0].id;
const stats = yield api.mlb.getTeamSeasonStats({
season: 2023,
team_id: teamId,
});
(0, chai_1.expect)(stats.data).to.be.an("array");
}));
});
});