UNPKG

@retroachievements/api

Version:

A well-tested library that lets you get achievement, user, and game data from RetroAchievements.

3 lines (2 loc) 14.4 kB
var e,r,t="https://retroachievements.org/API",o=function(e,r,t,o){void 0===o&&(o={});for(var n=(e+"/"+r).replaceAll(/([^:]\/)\/+/g,"$1"),s={z:t.username,y:t.webApiKey},u=0,i=Object.entries(o);u<i.length;u++){var a=i[u],c=a[0],l=a[1];n.includes(":"+c)?n=n.replace(":"+c,String(l)):void 0!==l&&(s[c]=String(l))}return n+"?"+new URLSearchParams(s).toString()},n=null!=(e=null==(r=process.env)?void 0:r.PACKAGE_VERSION)?e:"Unknown",s=function(e){try{var r=e.url,t=new Headers({"User-Agent":"RetroAchievements-api-js/"+n});return Promise.resolve(fetch(r,{headers:t})).then(function(e){if(!e.ok)throw new Error("HTTP Error: Status "+e.status+" "+e.statusText);return Promise.resolve(e.json())})}catch(e){return Promise.reject(e)}};function u(){return u=Object.assign?Object.assign.bind():function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var o in t)Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o])}return e},u.apply(this,arguments)}function i(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,o=new Array(r);t<r;t++)o[t]=e[t];return o}function a(e,r){var t="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(t)return(t=t.call(e)).next.bind(t);if(Array.isArray(e)||(t=function(e,r){if(e){if("string"==typeof e)return i(e,r);var t=Object.prototype.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?i(e,r):void 0}}(e))||r&&e&&"number"==typeof e.length){t&&(e=t);var o=0;return function(){return o>=e.length?{done:!0}:{done:!1,value:e[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var c,l,m,h=function e(r,t){void 0===t&&(t={});var o=t.shouldCastToNumbers,n=t.shouldMapToBooleans,s=r;if(Array.isArray(r)){for(var i,c=[],l=a(r);!(i=l()).done;)c.push(e(i.value,t));s=c}else if(!Array.isArray(r)&&r instanceof Object){for(var m={},h=0,v=Object.entries(r);h<v.length;h++){var f,P=v[h],p=P[0],A=P[1],y=A;null!=o&&o.includes(p)&&(y=null===A?null:Number(A)),null!=n&&n.includes(p)&&(y=null===A?null:"1"===String(A)),m=u({},m,((f={})[d(p)]=e(y,t),f))}s=m}return s},d=function(e){if(e.toUpperCase()===e)return e.toLowerCase();var r=e.charAt(0).toLowerCase()+e.slice(1);return(r=(r=(r=(r=r.replaceAll("ULID","Ulid")).replaceAll("ID","Id")).replaceAll("URL","Url")).replaceAll("rA","ra")).replaceAll("visibleUserawards","visibleUserAwards")},v=function(e,r){try{var n=r.offset,u=r.count,i={a:r.achievementId};n&&(i.o=n),u&&(i.c=u);var a=o(t,"/API_GetAchievementUnlocks.php",e,i);return Promise.resolve(s({url:a})).then(function(e){return h(e,{shouldCastToNumbers:["ID","Points","TrueRatio","RAPoints","RASoftcorePoints"],shouldMapToBooleans:["HardcoreMode"]})})}catch(e){return Promise.reject(e)}},f={game:1,achievement:2,user:3},P=function(e,r){try{var n=r.identifier,u=r.kind,i=r.offset,a=r.count,c={i:n};if(u)c.t=f[u];else if("number"==typeof n)throw new TypeError("'kind' must be specified when looking up an achievement or game.");i&&(c.o=i),a&&(c.c=a);var l=o(t,"/API_GetComments.php",e,c);return Promise.resolve(s({url:l})).then(function(e){return h(e,{shouldCastToNumbers:["Count","Total"]})})}catch(e){return Promise.reject(e)}},p=function(e,r){try{var n;null!=r&&r.shouldOnlyRetrieveActiveSystems&&(n=u({},n,{a:1})),null!=r&&r.shouldOnlyRetrieveGameSystems&&(n=u({},n,{g:1}));var i=o(t,"/API_GetConsoleIDs.php",e,n);return Promise.resolve(s({url:i})).then(function(e){return h(e,{shouldCastToNumbers:["ID"]})})}catch(e){return Promise.reject(e)}},A=function(e,r){try{var n=r.shouldOnlyRetrieveGamesWithAchievements,i=r.shouldRetrieveGameHashes,a={i:r.consoleId};void 0!==n&&(a=u({},a,{f:n?1:0})),i&&(a=u({},a,{h:i?1:0}));var c=o(t,"/API_GetGameList.php",e,a);return Promise.resolve(s({url:c})).then(function(e){return h(e,{shouldCastToNumbers:["ID","ConsoleID"]})})}catch(e){return Promise.reject(e)}},y=function(e){try{var r=o(t,"/API_GetAchievementOfTheWeek.php",e);return Promise.resolve(s({url:r})).then(function(e){return h(e,{shouldCastToNumbers:["ID","Points","TrueRatio","TotalPlayers","RAPoints","RASoftcorePoints","UnlocksCount"],shouldMapToBooleans:["HardcoreMode"]})})}catch(e){return Promise.reject(e)}},I=function(e){try{var r=o(t,"/API_GetActiveClaims.php",e);return Promise.resolve(s({url:r})).then(function(e){return h(e,{shouldMapToBooleans:["UserIsJrDev"]})})}catch(e){return Promise.reject(e)}},T=function(e,r){try{var n=o(t,"/API_GetClaims.php",e,{k:b[r.claimKind]});return Promise.resolve(s({url:n})).then(function(e){return h(e,{shouldMapToBooleans:["UserIsJrDev"]})})}catch(e){return Promise.reject(e)}},b={completed:"1",dropped:"2",expired:"3"},G=function(e,r){try{var n={};null!=r&&r.startDate&&(n.d=r.startDate),null!=r&&r.offset&&(n.o=r.offset),null!=r&&r.count&&(n.c=r.count),null!=r&&r.desiredAwardKinds&&(n.k=r.desiredAwardKinds.join(","));var u=o(t,"/API_GetRecentGameAwards.php",e,n);return Promise.resolve(s({url:u})).then(h)}catch(e){return Promise.reject(e)}},g=function(e){try{var r=o(t,"/API_GetTopTenUsers.php",e);return Promise.resolve(s({url:r})).then(function(e){for(var r,t=[],o=a(e);!(r=o()).done;){var n=r.value;t.push({username:n[1],totalPoints:Number(n[2]),totalRatioPoints:Number(n[3])})}return t})}catch(e){return Promise.reject(e)}};!function(e){e[e.NewSet=0]="NewSet",e[e.Revision=1]="Revision"}(c||(c={})),function(e){e[e.Active=0]="Active",e[e.Complete=1]="Complete",e[e.Dropped=2]="Dropped"}(l||(l={})),function(e){e[e.Primary=0]="Primary",e[e.Collaboration=1]="Collaboration"}(m||(m={}));var C,D,j=function(e,r){try{var n=o(t,"/API_GetAchievementCount.php",e,{i:r.gameId});return Promise.resolve(s({url:n})).then(h)}catch(e){return Promise.reject(e)}},N=function(e,r){try{var n=r.flags,u=r.hardcore,i={i:r.gameId};void 0!==n&&(i.f=n),void 0!==u&&(i.h=!0===u?1:0);var a=o(t,"/API_GetAchievementDistribution.php",e,i);return Promise.resolve(s({url:a}))}catch(e){return Promise.reject(e)}},S=function(e,r){try{var n=o(t,"/API_GetGame.php",e,{i:r.gameId});return Promise.resolve(s({url:n})).then(function(e){return h(e,{shouldCastToNumbers:["ID","ForumTopicID","ConsoleID","Flags"]})})}catch(e){return Promise.reject(e)}},U=function(e,r){try{var n={i:r.gameId};r.isRequestingUnofficialAchievements&&(n.f=5);var u=o(t,"/API_GetGameExtended.php",e,n);return Promise.resolve(s({url:u})).then(function(e){return h(e,{shouldCastToNumbers:["ID","NumAwarded","NumAwardedHardcore","Points","TrueRatio","DisplayOrder","NumDistinctPlayersCasual","NumDistinctPlayersHardcore"]})})}catch(e){return Promise.reject(e)}},w=function(e,r){try{var n=o(t,"/API_GetGameHashes.php",e,{i:r.gameId});return Promise.resolve(s({url:n})).then(h)}catch(e){return Promise.reject(e)}},_=function(e,r){try{var n=o(t,"/API_GetGameRankAndScore.php",e,{g:r.gameId,t:"latest-masters"===r.type?1:0});return Promise.resolve(s({url:n})).then(function(e){return h(e,{shouldCastToNumbers:["TotalScore"]})})}catch(e){return Promise.reject(e)}},R=function(e,r){try{var n=o(t,"/API_GetGameRating.php",e,{i:r.gameId});return Promise.resolve(s({url:n})).then(h)}catch(e){return Promise.reject(e)}};!function(e){e[e.CoreAchievements=3]="CoreAchievements",e[e.UnofficialAchievements=5]="UnofficialAchievements"}(C||(C={})),function(e){e.Primary="0",e.Collaboration="1"}(D||(D={}));var M=function(e,r){try{var n={};n.i=r.leaderboardId,null!=r&&r.offset&&(n.o=r.offset),null!=r&&r.count&&(n.c=r.count);var u=o(t,"/API_GetLeaderboardEntries.php",e,n);return Promise.resolve(s({url:u})).then(h)}catch(e){return Promise.reject(e)}},H=function(e,r){void 0===r&&(r={});try{var n=k(r),u=o(t,"/API_GetTicketData.php",e,n);return Promise.resolve(s({url:u})).then(function(e){return h(e,{shouldCastToNumbers:["ID","AchievementID","Points","GameID","ReportType","ReportState","OpenTickets"],shouldMapToBooleans:["Hardcore"]})})}catch(e){return Promise.reject(e)}},k=function(e){var r=e.ticketId,t=e.isGettingMostTicketedGames,o=e.username,n=e.gameId,s=e.isGettingTicketsForUnofficialAchievements,u=e.shouldReturnTicketsList,i=e.achievementId,a={};return void 0!==r?a.i=r:t?(a.f="1",a=O(a,e)):o?a.u=o:n?(a.g=n,s&&(a.f="5"),u&&(a.d="1")):i?a.a=i:a=O(a,e),a},O=function(e,r){var t=u({},e);return void 0!==r.count&&(t.c=r.count),void 0!==r.offset&&(t.o=r.offset),t},B=function(e,r){try{var n=r.toDate,u=o(t,"/API_GetAchievementsEarnedBetween.php",e,{u:r.username,f:(r.fromDate.getTime()/1e3).toFixed(0),t:(n.getTime()/1e3).toFixed(0)});return Promise.resolve(s({url:u})).then(function(e){return h(e,{shouldCastToNumbers:["AchievementID","Points","GameID"],shouldMapToBooleans:["HardcoreMode"]})})}catch(e){return Promise.reject(e)}},E=function(e,r){try{var n=r.onDate,u=o(t,"/API_GetAchievementsEarnedOnDay.php",e,{u:r.username,d:n.getFullYear()+"-"+(n.getMonth()+1)+"-"+n.getDate()});return Promise.resolve(s({url:u})).then(function(e){return h(e,{shouldCastToNumbers:["AchievementID","Points","GameID"],shouldMapToBooleans:["HardcoreMode"]})})}catch(e){return Promise.reject(e)}},L=function(e,r){try{var n={g:r.gameId,u:r.username};r.shouldIncludeHighestAwardMetadata&&(n.a=1);var u=o(t,"/API_GetGameInfoAndUserProgress.php",e,n);return Promise.resolve(s({url:u})).then(function(e){return h(e,{shouldCastToNumbers:["ID","NumAwarded","NumAwardedHardcore","Points","TrueRatio","DisplayOrder","NumDistinctPlayersCasual","NumDistinctPlayersHardcore"]})})}catch(e){return Promise.reject(e)}},x=function(e,r){try{var n=o(t,"/API_GetUserAwards.php",e,{u:r.username});return Promise.resolve(s({url:n})).then(h)}catch(e){return Promise.reject(e)}},K=function(e,r){try{var n=o(t,"/API_GetUserClaims.php",e,{u:r.username});return Promise.resolve(s({url:n})).then(function(e){return h(e,{shouldCastToNumbers:["ID","GameID","ClaimType","SetType","Status","Extension","Special","MinutesLeft"]})})}catch(e){return Promise.reject(e)}},F=function(e,r){try{var n=o(t,"/API_GetUserCompletedGames.php",e,{u:r.username});return Promise.resolve(s({url:n})).then(function(e){return h(e,{shouldCastToNumbers:["GameID","ConsoleID","MaxPossible","NumAwarded","PctWon"],shouldMapToBooleans:["HardcoreMode"]})})}catch(e){return Promise.reject(e)}},W=function(e,r){try{var n=r.offset,u=r.count,i={u:r.username};n&&(i.o=n),u&&(i.c=u);var a=o(t,"/API_GetUserCompletionProgress.php",e,i);return Promise.resolve(s({url:a})).then(h)}catch(e){return Promise.reject(e)}},z=function(e,r){try{var n=o(t,"/API_GetUserGameRankAndScore.php",e,{g:r.gameId,u:r.username});return Promise.resolve(s({url:n})).then(function(e){return h(e,{shouldCastToNumbers:["TotalScore","UserRank"]})})}catch(e){return Promise.reject(e)}},q=function(e,r){try{var n=o(t,"/API_GetUserPoints.php",e,{u:r.username});return Promise.resolve(s({url:n})).then(h)}catch(e){return Promise.reject(e)}},J=function(e,r){try{var n=o(t,"/API_GetUserProfile.php",e,{u:r.username});return Promise.resolve(s({url:n})).then(function(e){return h(e,{shouldCastToNumbers:["TotalPoints","TotalSoftcorePoints","TotalTruePoints","Permissions"],shouldMapToBooleans:["Untracked","UserWallActive"]})})}catch(e){return Promise.reject(e)}},V=function(e,r){try{var n=o(t,"/API_GetUserProgress.php",e,{u:r.username,i:r.gameIds.join(",")});return Promise.resolve(s({url:n})).then(function(e){return h(e,{shouldCastToNumbers:["NumPossibleAchievements","PossibleScore","NumAchieved","ScoreAchieved","NumAchievedHardcore","ScoreAchievedHardcore"]})})}catch(e){return Promise.reject(e)}},Y=function(e,r){try{var n=r.recentMinutes,u={u:r.username};void 0!==n&&(u.m=n);var i=o(t,"/API_GetUserRecentAchievements.php",e,u);return Promise.resolve(s({url:i})).then(function(e){return h(e,{shouldMapToBooleans:["HardcoreMode"]})})}catch(e){return Promise.reject(e)}},$=function(e,r){try{var n=r.offset,u=r.count,i={u:r.username};void 0!==n&&(i.o=n),void 0!==u&&(i.c=u);var a=o(t,"/API_GetUserRecentlyPlayedGames.php",e,i);return Promise.resolve(s({url:a})).then(function(e){return h(e,{shouldCastToNumbers:["GameID","ConsoleID","NumPossibleAchievements","PossibleScore","NumAchieved","ScoreAchieved","NumAchievedHardcore","ScoreAchievedHardcore","MyVote"]})})}catch(e){return Promise.reject(e)}},Q=function(e,r){try{var n=r.recentGamesCount,u=r.recentAchievementsCount,i={u:r.username};void 0!==n&&(i.g=n),void 0!==u&&(i.a=u);var a=o(t,"/API_GetUserSummary.php",e,i);return Promise.resolve(s({url:a})).then(function(e){return h(e,{shouldCastToNumbers:["GameID","ConsoleID","ID","LastGameID","ForumTopicID","activitytype","ContribCount","ContribYield","TotalPoints","TotalSoftcorePoints","TotalTruePoints","Permissions","NumPossibleAchievements","PossibleScore","NumAchieved","ScoreAchieved","NumAchievedHardcore","ScoreAchievedHardcore","Points","SoftcorePoints"],shouldMapToBooleans:["Untracked","UserWallActive","IsAwarded","HardcoreAchieved"]})})}catch(e){return Promise.reject(e)}},X=function(e,r){try{var n={};n.u=r.username,null!=r&&r.offset&&(n.o=r.offset),null!=r&&r.count&&(n.c=r.count);var u=o(t,"/API_GetUserWantToPlayList.php",e,n);return Promise.resolve(s({url:u})).then(h)}catch(e){return Promise.reject(e)}},Z=function(e){if(!e.username||!e.webApiKey)throw new Error('\n buildAuthorization() requires an object containing a\n username and webApiKey. eg:\n\n const authorization = buildAuthorization({\n username: "myUserName",\n webApiKey: "myWebApiKey"\n })\n ');return e};export{C as AchievementDistributionFlags,c as ClaimSetType,l as ClaimStatus,m as ClaimType,Z as buildAuthorization,j as getAchievementCount,N as getAchievementDistribution,y as getAchievementOfTheWeek,v as getAchievementUnlocks,B as getAchievementsEarnedBetween,E as getAchievementsEarnedOnDay,I as getActiveClaims,T as getClaims,P as getComments,p as getConsoleIds,S as getGame,U as getGameExtended,w as getGameHashes,L as getGameInfoAndUserProgress,A as getGameList,_ as getGameRankAndScore,R as getGameRating,M as getLeaderboardEntries,G as getRecentGameAwards,H as getTicketData,g as getTopTenUsers,x as getUserAwards,K as getUserClaims,F as getUserCompletedGames,W as getUserCompletionProgress,z as getUserGameRankAndScore,q as getUserPoints,J as getUserProfile,V as getUserProgress,Y as getUserRecentAchievements,$ as getUserRecentlyPlayedGames,Q as getUserSummary,X as getUserWantToPlayList,s as unsafe_call}; //# sourceMappingURL=api.module.js.map