UNPKG

@sports-sdk/sleeper

Version:

A package for interacting with the Sleeper public API

500 lines (496 loc) 17.5 kB
'use strict'; var core = require('@sports-sdk/core'); var zod = require('zod'); // src/client.ts var SleeperClientLeaguesList = [ core.League.NBA, core.League.NFL, core.League.EPL, "LALIGA" ]; var SleeperClient = class extends core.SportsSdkClient { constructor(league) { super("https://api.sleeper.app/v1"); this.league = league; } leagueMappings = { [core.League.EPL]: "clubsoccer:epl", [core.League.NBA]: "nba", [core.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 = zod.z.enum(["pre", "post", "regular"]); var StatusEnum = zod.z.enum(["pre_draft", "drafting", "in_season", "complete"]); var TransactionTypeEnum = zod.z.enum(["trade", "free_agent", "waiver"]); var StateSchema = core.nullish( zod.z.object({ week: zod.z.number(), season_type: SeasonTypeEnum, season_start_date: zod.z.string(), season: zod.z.string(), previous_season: zod.z.string(), leg: zod.z.number(), league_season: zod.z.string(), league_create_season: zod.z.string(), display_week: zod.z.number() }) ); var UserSchema = core.nullish( zod.z.object({ user_id: zod.z.string(), username: zod.z.string(), display_name: zod.z.string(), avatar: zod.z.string().nullable() }) ); var UserInfoSchema = core.nullish( zod.z.object({ user_id: zod.z.string(), username: zod.z.string(), display_name: zod.z.string(), avatar: zod.z.string().nullable(), metadata: zod.z.record(zod.z.any()), is_owner: zod.z.boolean() }) ); var LeagueInfoSchema = core.nullish( zod.z.object({ total_rosters: zod.z.number(), status: StatusEnum, sport: zod.z.string(), settings: zod.z.record(zod.z.any()), season_type: SeasonTypeEnum, season: zod.z.string(), scoring_settings: zod.z.record(zod.z.any()), roster_positions: zod.z.array(zod.z.string()), previous_league_id: zod.z.string().nullable(), name: zod.z.string(), league_id: zod.z.string(), draft_id: zod.z.string(), avatar: zod.z.string().nullable() }) ); var RosterSettingsSchema = core.nullish( zod.z.object({ wins: zod.z.number(), waiver_position: zod.z.number(), waiver_budget_used: zod.z.number(), total_moves: zod.z.number(), ties: zod.z.number(), losses: zod.z.number(), fpts: zod.z.number() }) ); var RosterSchema = core.nullish( zod.z.object({ taxi: zod.z.array(zod.z.union([zod.z.string(), zod.z.any()])), starters: zod.z.array(zod.z.union([zod.z.string(), zod.z.any()])), settings: RosterSettingsSchema, roster_id: zod.z.number(), reserve: zod.z.array(zod.z.union([zod.z.string(), zod.z.any()])).nullable(), players: zod.z.array(zod.z.union([zod.z.string(), zod.z.any()])), player_map: zod.z.record(zod.z.any()).nullable(), owner_id: zod.z.string(), metadata: zod.z.record(zod.z.any()).nullable(), league_id: zod.z.string(), keepers: zod.z.array(zod.z.string()).nullable(), co_owners: zod.z.array(zod.z.string()).nullable() }) ); var MatchupSchema = core.nullish( zod.z.object({ starters: zod.z.array(zod.z.union([zod.z.string(), zod.z.any()])), roster_id: zod.z.number(), players: zod.z.array(zod.z.union([zod.z.string(), zod.z.any()])), matchup_id: zod.z.number(), points: zod.z.number(), custom_points: zod.z.number().nullable() }) ); var PlayoffMatchupSchema = core.nullish( zod.z.object({ r: zod.z.number(), m: zod.z.number(), t1: zod.z.union([zod.z.number(), zod.z.object({ w: zod.z.number() })]), t2: zod.z.union([zod.z.number(), zod.z.object({ l: zod.z.number() })]), w: zod.z.number().nullable(), l: zod.z.number().nullable(), t1_from: zod.z.object({ w: zod.z.number().optional(), l: zod.z.number().optional() }).optional(), t2_from: zod.z.object({ w: zod.z.number().optional(), l: zod.z.number().optional() }).optional(), p: zod.z.number().optional() }) ); var DraftPickSchema = core.nullish( zod.z.object({ season: zod.z.string(), round: zod.z.number(), roster_id: zod.z.number(), previous_owner_id: zod.z.number().nullable(), owner_id: zod.z.number() }) ); var WaiverBudgetSchema = core.nullish( zod.z.object({ sender: zod.z.number(), receiver: zod.z.number(), amount: zod.z.number() }) ); var TransactionPlayerSchema = core.nullish( zod.z.object({ roster_id: zod.z.number(), player: zod.z.any() }) ); var TransactionSchema = core.nullish( zod.z.object({ type: TransactionTypeEnum, transaction_id: zod.z.string(), status_updated: zod.z.number(), status: zod.z.string(), settings: zod.z.record(zod.z.any()).nullable(), roster_ids: zod.z.array(zod.z.number()), metadata: zod.z.record(zod.z.any()).nullable(), leg: zod.z.number(), drops: zod.z.union([ zod.z.record(zod.z.number()), zod.z.array(TransactionPlayerSchema), zod.z.null() ]), draft_picks: zod.z.array(DraftPickSchema), creator: zod.z.string(), created: zod.z.number(), consenter_ids: zod.z.array(zod.z.number()), adds: zod.z.union([ zod.z.record(zod.z.number()), zod.z.array(TransactionPlayerSchema), zod.z.null() ]), waiver_budget: zod.z.array(WaiverBudgetSchema) }) ); var DraftSettingsSchema = core.nullish( zod.z.object({ teams: zod.z.number(), slots_wr: zod.z.number(), slots_te: zod.z.number(), slots_rb: zod.z.number(), slots_qb: zod.z.number(), slots_k: zod.z.number(), slots_flex: zod.z.number(), slots_def: zod.z.number(), slots_bn: zod.z.number(), rounds: zod.z.number(), pick_timer: zod.z.number() }) ); var DraftMetadataSchema = core.nullish( zod.z.object({ scoring_type: zod.z.string(), name: zod.z.string(), description: zod.z.string() }) ); var DraftSchema = core.nullish( zod.z.object({ type: zod.z.string(), status: zod.z.string(), start_time: zod.z.number(), sport: zod.z.string(), settings: DraftSettingsSchema, season_type: zod.z.string(), season: zod.z.string(), metadata: DraftMetadataSchema, league_id: zod.z.string(), last_picked: zod.z.number().nullable(), last_message_time: zod.z.number().nullable(), last_message_id: zod.z.string().nullable(), draft_order: zod.z.record(zod.z.number()).optional(), slot_to_roster_id: zod.z.record(zod.z.number()).optional(), draft_id: zod.z.string(), creators: zod.z.record(zod.z.any()).nullable(), created: zod.z.number() }) ); var PlayerDraftMetadataSchema = core.nullish( zod.z.object({ team: zod.z.string(), status: zod.z.string(), sport: zod.z.string(), position: zod.z.string(), player_id: zod.z.string(), number: zod.z.string(), news_updated: zod.z.string(), last_name: zod.z.string(), injury_status: zod.z.string(), first_name: zod.z.string() }) ); var PlayerDraftPickSchema = core.nullish( zod.z.object({ player_id: zod.z.string(), player: zod.z.any().optional(), picked_by: zod.z.string(), roster_id: zod.z.string(), round: zod.z.number(), draft_slot: zod.z.number(), pick_no: zod.z.number(), metadata: PlayerDraftMetadataSchema, is_keeper: zod.z.boolean().nullable(), draft_id: zod.z.string() }) ); var TrendingPlayerSchema = core.nullish( zod.z.object({ player_id: zod.z.string(), player: zod.z.any().optional(), count: zod.z.number() }) ); var PlayerSchema = core.nullish( zod.z.object({ player_id: zod.z.string(), first_name: zod.z.string(), last_name: zod.z.string(), position: zod.z.string(), team: zod.z.string().nullable(), status: zod.z.string(), number: zod.z.number(), height: zod.z.string(), weight: zod.z.string(), college: zod.z.string(), years_exp: zod.z.number(), fantasy_positions: zod.z.array(zod.z.string()).nullable(), depth_chart_position: zod.z.string().nullable(), depth_chart_order: zod.z.number().nullable(), metadata: zod.z.record(zod.z.any()).nullable(), yahoo_id: zod.z.union([zod.z.string(), zod.z.number()]).nullable(), rotowire_id: zod.z.union([zod.z.string(), zod.z.number()]).nullable(), espn_id: zod.z.union([zod.z.string(), zod.z.number()]).nullable(), active: zod.z.boolean(), latest_news: zod.z.any().nullable(), sport: zod.z.string(), full_name: zod.z.string(), birth_date: zod.z.string().nullable(), search_rank: zod.z.number(), injury_status: zod.z.string().nullable(), injury_body_part: zod.z.string().nullable(), injury_notes: zod.z.string().nullable(), injury_start_date: zod.z.string().nullable(), practice_participation: zod.z.string().nullable(), practice_description: zod.z.string().nullable(), gsis_id: zod.z.union([zod.z.string(), zod.z.number()]).nullable(), sportradar_id: zod.z.union([zod.z.string(), zod.z.number()]), stats_id: zod.z.union([zod.z.string(), zod.z.number()]).nullable(), fantasy_data_id: zod.z.union([zod.z.string(), zod.z.number()]).nullable(), rotoworld_id: zod.z.union([zod.z.string(), zod.z.number()]).nullable(), pff_id: zod.z.union([zod.z.string(), zod.z.number()]).nullable(), swish_id: zod.z.union([zod.z.string(), zod.z.number()]).nullable(), pandascore_id: zod.z.union([zod.z.string(), zod.z.number()]).nullable(), oddsjam_id: zod.z.union([zod.z.string(), zod.z.number()]).nullable(), dl_trading_id: zod.z.union([zod.z.string(), zod.z.number()]).nullable(), search_first_name: zod.z.string(), search_last_name: zod.z.string(), search_full_name: zod.z.string(), hashtag: zod.z.string(), news_updated: zod.z.number().nullable(), birth_city: zod.z.string().nullable(), birth_state: zod.z.string().nullable(), birth_country: zod.z.string().nullable(), high_school: zod.z.string().nullable() }) ); exports.DraftMetadataSchema = DraftMetadataSchema; exports.DraftPickSchema = DraftPickSchema; exports.DraftSchema = DraftSchema; exports.DraftSettingsSchema = DraftSettingsSchema; exports.LeagueInfoSchema = LeagueInfoSchema; exports.MatchupSchema = MatchupSchema; exports.PlayerDraftMetadataSchema = PlayerDraftMetadataSchema; exports.PlayerDraftPickSchema = PlayerDraftPickSchema; exports.PlayerSchema = PlayerSchema; exports.PlayoffMatchupSchema = PlayoffMatchupSchema; exports.RosterSchema = RosterSchema; exports.RosterSettingsSchema = RosterSettingsSchema; exports.SleeperClient = SleeperClient; exports.SleeperClientLeaguesList = SleeperClientLeaguesList; exports.StateSchema = StateSchema; exports.TransactionPlayerSchema = TransactionPlayerSchema; exports.TransactionSchema = TransactionSchema; exports.TrendingPlayerSchema = TrendingPlayerSchema; exports.UserInfoSchema = UserInfoSchema; exports.UserSchema = UserSchema; exports.WaiverBudgetSchema = WaiverBudgetSchema;