sleeper-api
Version:
Sleeper api from https://sleeper.com/
2 lines (1 loc) • 4.21 kB
JavaScript
class N{baseURL;defaultTimeout;constructor(B,z=1e4){this.baseURL=B,this.defaultTimeout=z}async get(B,z={}){let D=new URL(this.baseURL+B);if(z.params)Object.entries(z.params).forEach(([F,M])=>{D.searchParams.append(F,String(M))});let G=new AbortController,J=setTimeout(()=>G.abort(),z.timeout||this.defaultTimeout);try{let F=await fetch(D.toString(),{method:"GET",signal:G.signal,headers:{Accept:"application/json","Content-Type":"application/json"}});if(clearTimeout(J),!F.ok)throw new K(F.status,F.statusText,await F.text());return{data:await F.json(),status:F.status,statusText:F.statusText}}catch(F){if(clearTimeout(J),F instanceof Error&&F.name==="AbortError")throw new Error("Request timeout");throw F}}}class K extends Error{status;statusText;responseText;constructor(B,z,D){super(`HTTP ${B}: ${z}`);this.status=B;this.statusText=z;this.responseText=D;this.name="HttpError"}}var Q=(B,z=!1)=>{return z?`https://sleepercdn.com/avatars/thumbs/${B}`:`https://sleepercdn.com/avatars/${B}`};class O{apiClient;BASE_URL="https://api.sleeper.app/v1";constructor(B){this.apiClient=B||new N(this.BASE_URL,1e4)}async getUserByUsername(B){try{return(await this.apiClient.get(`/user/${B}`)).data}catch(z){this.handleError(z)}}async getUserById(B){try{return(await this.apiClient.get(`/user/${B}`)).data}catch(z){this.handleError(z)}}async getLeaguesForUser(B,z="nfl",D){try{return(await this.apiClient.get(`/user/${B}/leagues/${z}/${D}`)).data}catch(G){this.handleError(G)}}async getLeague(B){try{return(await this.apiClient.get(`/league/${B}`)).data}catch(z){this.handleError(z)}}async getRosters(B){try{return(await this.apiClient.get(`/league/${B}/rosters`)).data}catch(z){this.handleError(z)}}async getUsersInLeague(B){try{return(await this.apiClient.get(`/league/${B}/users`)).data}catch(z){this.handleError(z)}}async getMatchups(B,z){try{return(await this.apiClient.get(`/league/${B}/matchups/${z}`)).data}catch(D){this.handleError(D)}}async getWinnersBracket(B){try{return(await this.apiClient.get(`/league/${B}/winners_bracket`)).data}catch(z){this.handleError(z)}}async getLosersBracket(B){try{return(await this.apiClient.get(`/league/${B}/losers_bracket`)).data}catch(z){this.handleError(z)}}async getTransactions(B,z){try{return(await this.apiClient.get(`/league/${B}/transactions/${z}`)).data}catch(D){this.handleError(D)}}async getTradedPicks(B){try{return(await this.apiClient.get(`/league/${B}/traded_picks`)).data}catch(z){this.handleError(z)}}async getState(B){try{return(await this.apiClient.get(`/state/${B}`)).data}catch(z){this.handleError(z)}}async getDraftsForUser(B,z="nfl",D){try{return(await this.apiClient.get(`/user/${B}/drafts/${z}/${D}`)).data}catch(G){this.handleError(G)}}async getDraftsForLeague(B){try{return(await this.apiClient.get(`/league/${B}/drafts`)).data}catch(z){this.handleError(z)}}async getDraft(B){try{return(await this.apiClient.get(`/draft/${B}`)).data}catch(z){this.handleError(z)}}async getPicksInDraft(B){try{return(await this.apiClient.get(`/draft/${B}/picks`)).data}catch(z){this.handleError(z)}}async getTradedPicksInDraft(B){try{return(await this.apiClient.get(`/draft/${B}/traded_picks`)).data}catch(z){this.handleError(z)}}async getAllPlayers(B="nfl"){try{return(await this.apiClient.get(`/players/${B}`)).data}catch(z){this.handleError(z)}}async getTrendingPlayers(B,z,D=24,G=25){try{return(await this.apiClient.get(`/players/${B}/trending/${z}`,{params:{lookback_hours:D,limit:G}})).data}catch(J){this.handleError(J)}}handleError(B){if(B instanceof K){let z=B.status;switch(z){case 400:throw new Error("Bad Request: Your request is invalid.");case 404:throw new Error("Not Found: The requested resource could not be found.");case 429:throw new Error("Too Many Requests: You are being rate limited.");case 500:throw new Error("Internal Server Error: Problem with the server.");case 503:throw new Error("Service Unavailable: The service is temporarily offline.");default:throw new Error(`HTTP Error: ${z} ${B.statusText}`)}}else if(B.message==="Request timeout")throw new Error("Request timeout: No response received from the server.");else throw new Error(`Unexpected Error: ${B.message}`)}}var W=O;export{Q as getAvatarUrl,W as default,O as SleeperAPI};