@sports-sdk/sleeper
Version:
A package for interacting with the Sleeper public API
478 lines (475 loc) • 15.7 kB
JavaScript
import { League, nullish, SportsSdkClient } from '@sports-sdk/core';
import { z } from 'zod';
// src/client.ts
var SleeperClientLeaguesList = [
League.NBA,
League.NFL,
League.EPL,
"LALIGA"
];
var SleeperClient = class extends SportsSdkClient {
constructor(league) {
super("https://api.sleeper.app/v1");
this.league = league;
}
leagueMappings = {
[League.EPL]: "clubsoccer:epl",
[League.NBA]: "nba",
[League.NFL]: "nfl",
// will add LALIGA to core sports if/when it is more widely supported
"LALIGA": "clubsoccer:laliga"
};
/**
* Sends a GET request to the specified URL path.
* @param path - The path to append to base URL to send the request to.
* @returns The response data from the API
* @throws Will throw an error if the request fails.
*/
async request({ path }) {
const response = await this.session.get(path);
if (response.status === 200) {
return response.data;
}
throw new Error(`Failed to get a valid response: status code ${response.status}, response body ${response.data}`);
}
/**
* Lookup the current state for the sport. The state consists of the current week in the league, season start date, current season, current week, etc.
* @returns The current state of the sport.
*/
async stateLookup() {
const path = "/state/" + this.leagueMappings[this.league];
return await this.request({ path });
}
/**
* Lookup the user. This call is useful to determine the user ID for a given username. Usernames can change, but user IDs will not.
* @param user - The username to lookup.
* @returns The user details.
*/
async userLookup(user) {
const path = "/user/" + user;
return await this.request({ path });
}
/**
* Lookup the leagues a user is in.
* @param user - The user to lookup leagues for.
* @param season - The season to lookup leagues for.
* @returns The user's leagues.
*/
async getUserLeagues({ user, season }) {
const path = `/user/${user}/leagues/${this.leagueMappings[this.league]}/${season}`;
return await this.request({ path });
}
/**
* Lookup the details for a specific league.
* @param league - The league to lookup.
* @returns The league details.
*/
async getLeague(league) {
const path = `/league/${league}`;
return await this.request({ path });
}
/**
* Get rosters for a specified league.
* @param league - The league to get rosters for.
* @returns The user's roster for the specified league.
*/
async getLeagueRosters(league) {
const path = `/league/${league}/rosters`;
return await this.request({ path });
}
/**
* Get a list of users for a specified league.
* @param league - The league to get users for.
* @returns The list of users for the specified league.
*/
async getLeagueUsers(league) {
const path = `/league/${league}/users`;
return await this.request({ path });
}
/**
* Get the matchup for a specified week in a specified league.
* @param league - The league to get matchups for.
* @param week - The week to get matchups for.
* @returns The user's matchup for the specified week.
*/
async getLeagueMatchups({ league, week }) {
const path = `/league/${league}/matchups/${week}`;
return await this.request({ path });
}
/**
* Get a list describing the playoff bracket for a specified league.
* @param league - The league to get the playoff bracket for.
* @param bracket - The bracket to get the playoff bracket for.
* @returns The playoff bracket for the specified league.
*/
async getLeaguePlayoffBracket({ league, bracket }) {
const path = `/league/${league}/${bracket}_bracket`;
return await this.request({ path });
}
/**
* Get a list of all free agent transactions, waivers and trades for a specific league for a particular round.
* @param league - The league to get transactions for.
* @param week - The week to get transactions for.
* @returns The list of transactions for the specified user's roster.
*/
async getLeagueTransactions({ league, week }) {
const path = `/league/${league}/matchups/${week}`;
return await this.request({ path });
}
/**
* Get a list of traded picks in a specific league.
* @param league - The league to get traded picks for.
* @returns The list of traded picks the user currently owns in the specified league.
*/
async getLeagueTradedPicks(league) {
const path = `/league/${league}/traded_picks`;
return await this.request({ path });
}
/**
* Get a list of details for all drafts for a specific user in season, including format and status.
* @param user - The user to get drafts for.
* @param sport - The sport to get drafts for.
* @returns The list of details for all drafts for the specified user.
*/
async getUserDraft({ user, season }) {
const path = `/user/${user}/drafts/${this.leagueMappings[this.league]}/${season}`;
return await this.request({ path });
}
/**
* Get a list of details for all drafts for a specified league, including format and status.
* @param league - The league to get drafts for.
* @returns The list of details for all drafts for the specified league.
*/
async getLeagueDrafts(league) {
const path = `/league/${league}/drafts`;
return await this.request({ path });
}
/**
* Get the details of a specific draft, including format, status, and order.
* @param draft - The draft to get details for.
* @returns The details of the specified draft.
*/
async getDraft(draft) {
const path = `/draft/${draft}`;
return await this.request({ path });
}
/**
* Get picks for a specific draft.
* @param draft - The draft to get picks for.
* @returns The picks for the specified user in the specified draft.
*/
async getDraftPicks(draft) {
const path = `/draft/${draft}/picks`;
return await this.request({ path });
}
/**
* Get the traded picks in a specific draft.
* @param draft - The draft to get traded picks for.
* @returns The traded picks currently owned by the specified user in the specified draft.
*/
async getDraftTradedPicks(draft) {
const path = `/draft/${draft}/traded_picks`;
return await this.request({ path });
}
/**
* Get all players on Sleeper for the specified sport.
* @returns All players on Sleeper for the specified sport.
*/
async getAllPlayers() {
const path = `/players/${this.leagueMappings[this.league]}`;
return await this.request({ path });
}
/**
* Get list of trending players based on their add/drop frequency within the past lookback hours.
* @param type - The type of trending players to get (e.g., add, drop).
* @param lookback_hours - The lookback period in hours.
* @param limit - The maximum number of trending players to return.
* @returns The list of trending players for the specified sport.
*/
async getTrendingPlayers({ type, lookback_hours, limit }) {
const path = `/players/${this.leagueMappings[this.league]}/trending/${type}?lookback_hours=${lookback_hours}&limit=${limit}`;
return await this.request({ path });
}
};
var SeasonTypeEnum = z.enum(["pre", "post", "regular"]);
var StatusEnum = z.enum(["pre_draft", "drafting", "in_season", "complete"]);
var TransactionTypeEnum = z.enum(["trade", "free_agent", "waiver"]);
var StateSchema = nullish(
z.object({
week: z.number(),
season_type: SeasonTypeEnum,
season_start_date: z.string(),
season: z.string(),
previous_season: z.string(),
leg: z.number(),
league_season: z.string(),
league_create_season: z.string(),
display_week: z.number()
})
);
var UserSchema = nullish(
z.object({
user_id: z.string(),
username: z.string(),
display_name: z.string(),
avatar: z.string().nullable()
})
);
var UserInfoSchema = nullish(
z.object({
user_id: z.string(),
username: z.string(),
display_name: z.string(),
avatar: z.string().nullable(),
metadata: z.record(z.any()),
is_owner: z.boolean()
})
);
var LeagueInfoSchema = nullish(
z.object({
total_rosters: z.number(),
status: StatusEnum,
sport: z.string(),
settings: z.record(z.any()),
season_type: SeasonTypeEnum,
season: z.string(),
scoring_settings: z.record(z.any()),
roster_positions: z.array(z.string()),
previous_league_id: z.string().nullable(),
name: z.string(),
league_id: z.string(),
draft_id: z.string(),
avatar: z.string().nullable()
})
);
var RosterSettingsSchema = nullish(
z.object({
wins: z.number(),
waiver_position: z.number(),
waiver_budget_used: z.number(),
total_moves: z.number(),
ties: z.number(),
losses: z.number(),
fpts: z.number()
})
);
var RosterSchema = nullish(
z.object({
taxi: z.array(z.union([z.string(), z.any()])),
starters: z.array(z.union([z.string(), z.any()])),
settings: RosterSettingsSchema,
roster_id: z.number(),
reserve: z.array(z.union([z.string(), z.any()])).nullable(),
players: z.array(z.union([z.string(), z.any()])),
player_map: z.record(z.any()).nullable(),
owner_id: z.string(),
metadata: z.record(z.any()).nullable(),
league_id: z.string(),
keepers: z.array(z.string()).nullable(),
co_owners: z.array(z.string()).nullable()
})
);
var MatchupSchema = nullish(
z.object({
starters: z.array(z.union([z.string(), z.any()])),
roster_id: z.number(),
players: z.array(z.union([z.string(), z.any()])),
matchup_id: z.number(),
points: z.number(),
custom_points: z.number().nullable()
})
);
var PlayoffMatchupSchema = nullish(
z.object({
r: z.number(),
m: z.number(),
t1: z.union([z.number(), z.object({ w: z.number() })]),
t2: z.union([z.number(), z.object({ l: z.number() })]),
w: z.number().nullable(),
l: z.number().nullable(),
t1_from: z.object({ w: z.number().optional(), l: z.number().optional() }).optional(),
t2_from: z.object({ w: z.number().optional(), l: z.number().optional() }).optional(),
p: z.number().optional()
})
);
var DraftPickSchema = nullish(
z.object({
season: z.string(),
round: z.number(),
roster_id: z.number(),
previous_owner_id: z.number().nullable(),
owner_id: z.number()
})
);
var WaiverBudgetSchema = nullish(
z.object({
sender: z.number(),
receiver: z.number(),
amount: z.number()
})
);
var TransactionPlayerSchema = nullish(
z.object({
roster_id: z.number(),
player: z.any()
})
);
var TransactionSchema = nullish(
z.object({
type: TransactionTypeEnum,
transaction_id: z.string(),
status_updated: z.number(),
status: z.string(),
settings: z.record(z.any()).nullable(),
roster_ids: z.array(z.number()),
metadata: z.record(z.any()).nullable(),
leg: z.number(),
drops: z.union([
z.record(z.number()),
z.array(TransactionPlayerSchema),
z.null()
]),
draft_picks: z.array(DraftPickSchema),
creator: z.string(),
created: z.number(),
consenter_ids: z.array(z.number()),
adds: z.union([
z.record(z.number()),
z.array(TransactionPlayerSchema),
z.null()
]),
waiver_budget: z.array(WaiverBudgetSchema)
})
);
var DraftSettingsSchema = nullish(
z.object({
teams: z.number(),
slots_wr: z.number(),
slots_te: z.number(),
slots_rb: z.number(),
slots_qb: z.number(),
slots_k: z.number(),
slots_flex: z.number(),
slots_def: z.number(),
slots_bn: z.number(),
rounds: z.number(),
pick_timer: z.number()
})
);
var DraftMetadataSchema = nullish(
z.object({
scoring_type: z.string(),
name: z.string(),
description: z.string()
})
);
var DraftSchema = nullish(
z.object({
type: z.string(),
status: z.string(),
start_time: z.number(),
sport: z.string(),
settings: DraftSettingsSchema,
season_type: z.string(),
season: z.string(),
metadata: DraftMetadataSchema,
league_id: z.string(),
last_picked: z.number().nullable(),
last_message_time: z.number().nullable(),
last_message_id: z.string().nullable(),
draft_order: z.record(z.number()).optional(),
slot_to_roster_id: z.record(z.number()).optional(),
draft_id: z.string(),
creators: z.record(z.any()).nullable(),
created: z.number()
})
);
var PlayerDraftMetadataSchema = nullish(
z.object({
team: z.string(),
status: z.string(),
sport: z.string(),
position: z.string(),
player_id: z.string(),
number: z.string(),
news_updated: z.string(),
last_name: z.string(),
injury_status: z.string(),
first_name: z.string()
})
);
var PlayerDraftPickSchema = nullish(
z.object({
player_id: z.string(),
player: z.any().optional(),
picked_by: z.string(),
roster_id: z.string(),
round: z.number(),
draft_slot: z.number(),
pick_no: z.number(),
metadata: PlayerDraftMetadataSchema,
is_keeper: z.boolean().nullable(),
draft_id: z.string()
})
);
var TrendingPlayerSchema = nullish(
z.object({
player_id: z.string(),
player: z.any().optional(),
count: z.number()
})
);
var PlayerSchema = nullish(
z.object({
player_id: z.string(),
first_name: z.string(),
last_name: z.string(),
position: z.string(),
team: z.string().nullable(),
status: z.string(),
number: z.number(),
height: z.string(),
weight: z.string(),
college: z.string(),
years_exp: z.number(),
fantasy_positions: z.array(z.string()).nullable(),
depth_chart_position: z.string().nullable(),
depth_chart_order: z.number().nullable(),
metadata: z.record(z.any()).nullable(),
yahoo_id: z.union([z.string(), z.number()]).nullable(),
rotowire_id: z.union([z.string(), z.number()]).nullable(),
espn_id: z.union([z.string(), z.number()]).nullable(),
active: z.boolean(),
latest_news: z.any().nullable(),
sport: z.string(),
full_name: z.string(),
birth_date: z.string().nullable(),
search_rank: z.number(),
injury_status: z.string().nullable(),
injury_body_part: z.string().nullable(),
injury_notes: z.string().nullable(),
injury_start_date: z.string().nullable(),
practice_participation: z.string().nullable(),
practice_description: z.string().nullable(),
gsis_id: z.union([z.string(), z.number()]).nullable(),
sportradar_id: z.union([z.string(), z.number()]),
stats_id: z.union([z.string(), z.number()]).nullable(),
fantasy_data_id: z.union([z.string(), z.number()]).nullable(),
rotoworld_id: z.union([z.string(), z.number()]).nullable(),
pff_id: z.union([z.string(), z.number()]).nullable(),
swish_id: z.union([z.string(), z.number()]).nullable(),
pandascore_id: z.union([z.string(), z.number()]).nullable(),
oddsjam_id: z.union([z.string(), z.number()]).nullable(),
dl_trading_id: z.union([z.string(), z.number()]).nullable(),
search_first_name: z.string(),
search_last_name: z.string(),
search_full_name: z.string(),
hashtag: z.string(),
news_updated: z.number().nullable(),
birth_city: z.string().nullable(),
birth_state: z.string().nullable(),
birth_country: z.string().nullable(),
high_school: z.string().nullable()
})
);
export { DraftMetadataSchema, DraftPickSchema, DraftSchema, DraftSettingsSchema, LeagueInfoSchema, MatchupSchema, PlayerDraftMetadataSchema, PlayerDraftPickSchema, PlayerSchema, PlayoffMatchupSchema, RosterSchema, RosterSettingsSchema, SleeperClient, SleeperClientLeaguesList, StateSchema, TransactionPlayerSchema, TransactionSchema, TrendingPlayerSchema, UserInfoSchema, UserSchema, WaiverBudgetSchema };