uefa-api
Version:
Typescript bindings and utils for the UEFA APIs
3 lines (2 loc) • 4.02 kB
JavaScript
const t="https://match.uefa.com/v5/matches",o=async(t,o)=>{const r=new URLSearchParams(o).toString(),e=await fetch(`${t}?${r}`);if(!e.ok)throw new Error(`Could not fetch data from API (HTTP ${e.status})`);const n=await e.json();if(void 0!==n.error)throw new Error(`API returned an error: ${n.error.title}, ${n.error.message}`);return n},r=t=>{if(void 0!==t)return Array.isArray(t)?t.join(","):t.toString()},e=(t,o)=>{if(t<1)throw new Error("Limit must be greater than 0");if(o<0)throw new Error("Offset must be greater than or equal to 0")},n=()=>o("https://match.uefa.com/v5/livescore",{});var i;!function(t){t.ASCENDING="ASC",t.DESCENDING="DESC"}(i||(i={}));const s=async(n,s=i.ASCENDING,a=10,d=0)=>{if(!n||!(n.competitionId||n.groupId||n.opponentTeamIds||n.matchId||n.seasonYear))throw new Error("At least one filter criteria must be provided");if(n.competitionId&&!n.seasonYear)throw new Error("Season year must be provided when filtering by competition id");e(a,d);const c={};return n.competitionId&&(c.competitionId=n.competitionId.toString()),n.matchId&&(c.matchId=r(n.matchId)),n.opponentTeamIds&&(c.opponentTeamIds=r(n.opponentTeamIds)),n.groupId&&(c.groupId=n.groupId.toString()),n.seasonYear&&(c.seasonYear=n.seasonYear.toString()),n.matchId||(c.limit=a.toString(),c.offset=d.toString()),c.order=s,o(t,c)},a=async t=>{if(!t||""===t)throw new Error("Match id must be provided");const o=await s({matchId:t},i.ASCENDING,1,0);if(0===o.length)throw new Error(`Match with id ${t} not found`);return o[0]},d=async t=>{if(!t||""===t)throw new Error("Match id must be provided");return o(`https://matchstats.uefa.com/v1/team-statistics/${t}`,{})},c=async(r,e=i.ASCENDING,n=10,s=0)=>{if(!r||""===r)throw new Error("Match id must be provided");return o(`${t}/${r}/events`,{filter:"LINEUP",order:e,limit:n.toString(),offset:s.toString()})},m=async r=>{if(!r||""===r)throw new Error("Match id must be provided");return o(`${t}/${r}/lineups`,{})},p=async(t,n=10,i=0)=>{if(!t||!t.competitionId&&!t.playerIds&&!t.seasonYear)throw new Error("At least one filter criteria must be provided");if(t.competitionId&&!t.seasonYear)throw new Error("Season year must be provided when filtering by competition id");e(n,i);const s={};return t.competitionId&&(s.competitionId=t.competitionId.toString()),t.playerIds&&(s.playerIds=r(t.playerIds)),t.seasonYear&&(s.seasonYear=t.seasonYear.toString()),t.playerIds||(s.limit=n.toString(),s.offset=i.toString()),o("https://comp.uefa.com/v2/players",s)},I=async t=>{if(!t||!(t.competitionId||t.roundId||t.groupIds||t.phase||t.seasonYear))throw new Error("At least one filter criteria must be provided");if(t.competitionId&&!t.seasonYear)throw new Error("Season year must be provided when filtering by competition id");const e={};return t.competitionId&&(e.competitionId=t.competitionId.toString()),t.roundId&&(e.roundId=r(t.roundId)),t.groupIds&&(e.groupIds=r(t.groupIds)),t.phase&&(e.phase=t.phase.toString()),t.seasonYear&&(e.seasonYear=t.seasonYear.toString()),o("https://standings.uefa.com/v1/standings",e)},u=async(t,n=10,i=0)=>{if(!t||!(t.competitionId||t.roundIds||t.teamIds||t.associationId||t.seasonYear))throw new Error("At least one filter criteria must be provided");if(t.competitionId&&!t.seasonYear)throw new Error("Season year must be provided when filtering by competition id");e(n,i);const s={};return t.competitionId&&(s.competitionId=t.competitionId.toString()),t.roundIds&&(s.roundIds=r(t.roundIds)),t.teamIds&&(s.teamIds=r(t.teamIds)),t.associationId&&(s.associationId=t.associationId.toString()),t.seasonYear&&(s.seasonYear=t.seasonYear.toString()),t.teamIds||(s.limit=n.toString(),s.offset=i.toString()),o("https://comp.uefa.com/v2/teams",s)},h=t=>{const e=r(t);return o("https://comp.uefa.com/v2/competitions",e?{competitionIds:e}:{})};export{i as SortOrder,h as getCompetitions,m as getLineups,n as getLivescore,a as getMatch,c as getMatchEvents,d as getMatchStats,s as getMatches,p as getPlayers,I as getStandings,u as getTeams};
//# sourceMappingURL=index.modern.js.map