Skip to content

Commit

Permalink
Throw errors when invalid season IDs are passed to Client methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mkreiser committed Oct 23, 2023
1 parent 67614f8 commit 95885d0
Show file tree
Hide file tree
Showing 4 changed files with 816 additions and 468 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`2016 season integration tests getBoxscoreForWeek throws an error 1`] = `[Error: Cannot call getBoxscoreForWeek with a season ID prior to 2018 due to ESPN limitations (see README.md#espn-databases-and-data-storage for more).Call Client#getHistoricalScoreboardForWeek for historical data instead.]`;

exports[`2016 season integration tests getDraftInfo throws an error 1`] = `[Error: Cannot call getDraftInfo with a season ID prior to 2018 due to ESPN limitations (see README.md#espn-databases-and-data-storage for more).]`;

exports[`2016 season integration tests getFreeAgents throws an error 1`] = `[Error: Cannot call getFreeAgents with a season ID prior to 2018 due to ESPN limitations (see README.md#espn-databases-and-data-storage for more).]`;

exports[`2016 season integration tests getHistoricalScoreboardForWeek returns a populated array of Boxscores 1`] = `
[
Boxscore {
Expand Down Expand Up @@ -3531,3 +3537,54 @@ exports[`2016 season integration tests getHistoricalTeamsAtWeek returns a popula
},
]
`;

exports[`2016 season integration tests getLeagueInfo throws an error 1`] = `[Error: Cannot call getLeagueInfo with a season ID prior to 2018 due to ESPN limitations (see README.md#espn-databases-and-data-storage for more).]`;

exports[`2016 season integration tests getNFLGamesForPeriod returns a populated array of NFLGames 1`] = `
[
NFLGame {
"awayTeam": {
"id": 19,
"record": "2-2",
"score": 10,
"team": "New York Giants",
"teamAbbrev": "NYG",
},
"broadcaster": "ESPN",
"clock": "0:00",
"gameStatus": "Final",
"homeTeam": {
"id": 16,
"record": "4-0",
"score": 24,
"team": "Minnesota Vikings",
"teamAbbrev": "MIN",
},
"quarter": 4,
"startTime": 2016-10-04T00:30:00.000Z,
},
NFLGame {
"awayTeam": {
"id": 22,
"record": "2-3",
"score": 33,
"team": "Arizona Cardinals",
"teamAbbrev": "ARI",
},
"broadcaster": "CBS",
"clock": "0:00",
"gameStatus": "Final",
"homeTeam": {
"id": 25,
"record": "1-4",
"score": 21,
"team": "San Francisco 49ers",
"teamAbbrev": "SF",
},
"quarter": 4,
"startTime": 2016-10-07T00:25:00.000Z,
},
]
`;

exports[`2016 season integration tests getTeamsAtWeek throws an error 1`] = `[Error: Cannot call getTeamsAtWeek with a season ID prior to 2018 due to ESPN limitations (see README.md#espn-databases-and-data-storage for more).Call Client#getHistoricalTeamsAtWeek for historical data instead.]`;
84 changes: 83 additions & 1 deletion integration-tests/2016-season/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,92 @@ describe('2016 season integration tests', () => {
});
});

describe('getBoxscoreForWeek', () => {
test('throws an error', async () => {
expect.assertions(1);

try {
await client.getBoxscoreForWeek({
seasonId,
matchupPeriodId: scoringPeriodId,
scoringPeriodId
});
} catch (ex) {
// eslint-disable-next-line jest/no-conditional-expect
expect(ex).toMatchSnapshot();
}
});
});

describe('getDraftInfo', () => {
test('throws an error', async () => {
expect.assertions(1);

try {
await client.getDraftInfo({ seasonId });
} catch (ex) {
// eslint-disable-next-line jest/no-conditional-expect
expect(ex).toMatchSnapshot();
}
});
});

describe('getFreeAgents', () => {
test('throws an error', async () => {
expect.assertions(1);

try {
await client.getFreeAgents({ seasonId, scoringPeriodId });
} catch (ex) {
// eslint-disable-next-line jest/no-conditional-expect
expect(ex).toMatchSnapshot();
}
});
});

describe('getTeamsAtWeek', () => {
test('throws an error', async () => {
expect.assertions(1);

try {
await client.getTeamsAtWeek({ seasonId, scoringPeriodId });
} catch (ex) {
// eslint-disable-next-line jest/no-conditional-expect
expect(ex).toMatchSnapshot();
}
});
});

describe('getLeagueInfo', () => {
test('throws an error', async () => {
expect.assertions(1);

try {
await client.getLeagueInfo({ seasonId });
} catch (ex) {
// eslint-disable-next-line jest/no-conditional-expect
expect(ex).toMatchSnapshot();
}
});
});

describe('getNFLGamesForPeriod', () => {
test('returns a populated array of NFLGames', async () => {
const nflGames = await client.getNFLGamesForPeriod({
startDate: '20161003',
endDate: '20161008'
});

expect(nflGames).toMatchSnapshot();
});
});

describe('getHistoricalScoreboardForWeek', () => {
test('returns a populated array of Boxscores', async () => {
const scoreboards = await client.getHistoricalScoreboardForWeek({
seasonId, matchupPeriodId: scoringPeriodId, scoringPeriodId
seasonId,
matchupPeriodId: scoringPeriodId,
scoringPeriodId
});

expect(scoreboards).toMatchSnapshot();
Expand Down
39 changes: 39 additions & 0 deletions src/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ axios.defaults.baseURL = 'https://fantasy.espn.com/apis/v3/games/ffl/seasons/';
* @class
*/
class Client {
static _validateV3Params(seasonId, route, alternateRoute = '') {
if (seasonId < 2018) {
throw new Error(`Cannot call ${route} with a season ID prior to 2018 due to ESPN limitations (see README.md#espn-databases-and-data-storage for more).${alternateRoute ? `Call Client#${alternateRoute} for historical data instead.` : ''}`);
}
}

static _validateHistoricalParams(seasonId, route, alternateRoute) {
if (seasonId >= 2018) {
// Historical routes should always have a modern endpoint, so alternateRoute is required.
throw new Error(`Cannot call ${route} with a season ID after 2017 due to ESPN limitations (see README.md#espn-databases-and-data-storage for more). Call Client#${alternateRoute} for new data instead.`);
}
}

constructor(options = {}) {
this.leagueId = options.leagueId;

Expand Down Expand Up @@ -51,6 +64,12 @@ class Client {
* @returns {Boxscore[]} All boxscores for the week
*/
getBoxscoreForWeek({ seasonId, matchupPeriodId, scoringPeriodId }) {
this.constructor._validateV3Params(
seasonId,
'getBoxscoreForWeek',
'getHistoricalScoreboardForWeek'
);

const route = this.constructor._buildRoute({
base: `${seasonId}/segments/0/leagues/${this.leagueId}`,
params: `?view=mMatchup&view=mMatchupScore&scoringPeriodId=${scoringPeriodId}`
Expand All @@ -76,6 +95,8 @@ class Client {
* @returns {DraftPlayer[]} All drafted players sorted in draft order
*/
getDraftInfo({ seasonId, scoringPeriodId = 0 }) {
this.constructor._validateV3Params(seasonId, 'getDraftInfo');

const draftRoute = this.constructor._buildRoute({
base: `${seasonId}/segments/0/leagues/${this.leagueId}`,
params:
Expand Down Expand Up @@ -134,6 +155,12 @@ class Client {
* @returns {Boxscore[]} All boxscores for the week
*/
getHistoricalScoreboardForWeek({ seasonId, matchupPeriodId, scoringPeriodId }) {
this.constructor._validateHistoricalParams(
seasonId,
'getHistoricalScoreboardForWeek',
'getBoxscoreForWeek'
);

const route = this.constructor._buildRoute({
base: `${this.leagueId}`,
params: `?scoringPeriodId=${scoringPeriodId}&seasonId=${seasonId}` +
Expand Down Expand Up @@ -164,6 +191,8 @@ class Client {
* @returns {FreeAgentPlayer[]} The list of free agents.
*/
getFreeAgents({ seasonId, scoringPeriodId }) {
this.constructor._validateV3Params(seasonId, 'getFreeAgents');

const route = this.constructor._buildRoute({
base: `${seasonId}/segments/0/leagues/${this.leagueId}`,
params: `?scoringPeriodId=${scoringPeriodId}&view=kona_player_info`
Expand Down Expand Up @@ -203,6 +232,8 @@ class Client {
* @returns {Team[]} The list of teams.
*/
getTeamsAtWeek({ seasonId, scoringPeriodId }) {
this.constructor._validateV3Params(seasonId, 'getTeamsAtWeek', 'getHistoricalTeamsAtWeek');

const route = this.constructor._buildRoute({
base: `${seasonId}/segments/0/leagues/${this.leagueId}`,
params: `?scoringPeriodId=${scoringPeriodId}&view=mRoster&view=mTeam`
Expand All @@ -226,6 +257,12 @@ class Client {
* @returns {Team[]} The list of teams.
*/
getHistoricalTeamsAtWeek({ seasonId, scoringPeriodId }) {
this.constructor._validateHistoricalParams(
seasonId,
'getHistoricalTeamsAtWeek',
'getTeamsAtWeek'
);

const route = this.constructor._buildRoute({
base: `${this.leagueId}`,
params: `?scoringPeriodId=${scoringPeriodId}&seasonId=${seasonId}` +
Expand Down Expand Up @@ -287,6 +324,8 @@ class Client {
* @returns {League} The league info.
*/
getLeagueInfo({ seasonId }) {
this.constructor._validateV3Params(seasonId, 'getLeagueInfo');

const route = this.constructor._buildRoute({
base: `${seasonId}/segments/0/leagues/${this.leagueId}`,
params: '?view=mSettings'
Expand Down
Loading

0 comments on commit 95885d0

Please sign in to comment.