UNPKG

@retroachievements/api

Version:

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

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