UNPKG

uefa-api

Version:

Typescript bindings and utils for the UEFA APIs

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