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