psn-api
Version:
A well-tested library that lets you get trophy, user, and game data from the PlayStation Network.
3 lines (2 loc) • 19.3 kB
JavaScript
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}Object.defineProperty(exports,"__esModule",{value:!0});var r=e(require("isomorphic-unfetch"));function t(){t=function(){return e};var e={},r=Object.prototype,n=r.hasOwnProperty,o=Object.defineProperty||function(e,r,t){e[r]=t.value},a="function"==typeof Symbol?Symbol:{},i=a.iterator||"@@iterator",s=a.asyncIterator||"@@asyncIterator",c=a.toStringTag||"@@toStringTag";function u(e,r,t){return Object.defineProperty(e,r,{value:t,enumerable:!0,configurable:!0,writable:!0}),e[r]}try{u({},"")}catch(e){u=function(e,r,t){return e[r]=t}}function p(e,r,t,n){var a=Object.create((r&&r.prototype instanceof f?r:f).prototype),i=new O(n||[]);return o(a,"_invoke",{value:k(e,t,i)}),a}function l(e,r,t){try{return{type:"normal",arg:e.call(r,t)}}catch(e){return{type:"throw",arg:e}}}e.wrap=p;var h={};function f(){}function d(){}function v(){}var y={};u(y,i,(function(){return this}));var m=Object.getPrototypeOf,g=m&&m(m(E([])));g&&g!==r&&n.call(g,i)&&(y=g);var x=v.prototype=f.prototype=Object.create(y);function w(e){["next","throw","return"].forEach((function(r){u(e,r,(function(e){return this._invoke(r,e)}))}))}function b(e,r){function t(o,a,i,s){var c=l(e[o],e,a);if("throw"!==c.type){var u=c.arg,p=u.value;return p&&"object"==typeof p&&n.call(p,"__await")?r.resolve(p.__await).then((function(e){t("next",e,i,s)}),(function(e){t("throw",e,i,s)})):r.resolve(p).then((function(e){u.value=e,i(u)}),(function(e){return t("throw",e,i,s)}))}s(c.arg)}var a;o(this,"_invoke",{value:function(e,n){function o(){return new r((function(r,o){t(e,n,r,o)}))}return a=a?a.then(o,o):o()}})}function k(e,r,t){var n="suspendedStart";return function(o,a){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw a;return{value:void 0,done:!0}}for(t.method=o,t.arg=a;;){var i=t.delegate;if(i){var s=T(i,t);if(s){if(s===h)continue;return s}}if("next"===t.method)t.sent=t._sent=t.arg;else if("throw"===t.method){if("suspendedStart"===n)throw n="completed",t.arg;t.dispatchException(t.arg)}else"return"===t.method&&t.abrupt("return",t.arg);n="executing";var c=l(e,r,t);if("normal"===c.type){if(n=t.done?"completed":"suspendedYield",c.arg===h)continue;return{value:c.arg,done:t.done}}"throw"===c.type&&(n="completed",t.method="throw",t.arg=c.arg)}}}function T(e,r){var t=r.method,n=e.iterator[t];if(void 0===n)return r.delegate=null,"throw"===t&&e.iterator.return&&(r.method="return",r.arg=void 0,T(e,r),"throw"===r.method)||"return"!==t&&(r.method="throw",r.arg=new TypeError("The iterator does not provide a '"+t+"' method")),h;var o=l(n,e.iterator,r.arg);if("throw"===o.type)return r.method="throw",r.arg=o.arg,r.delegate=null,h;var a=o.arg;return a?a.done?(r[e.resultName]=a.value,r.next=e.nextLoc,"return"!==r.method&&(r.method="next",r.arg=void 0),r.delegate=null,h):a:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,h)}function I(e){var r={tryLoc:e[0]};1 in e&&(r.catchLoc=e[1]),2 in e&&(r.finallyLoc=e[2],r.afterLoc=e[3]),this.tryEntries.push(r)}function _(e){var r=e.completion||{};r.type="normal",delete r.arg,e.completion=r}function O(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(I,this),this.reset(!0)}function E(e){if(e){var r=e[i];if(r)return r.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var t=-1,o=function r(){for(;++t<e.length;)if(n.call(e,t))return r.value=e[t],r.done=!1,r;return r.value=void 0,r.done=!0,r};return o.next=o}}return{next:S}}function S(){return{value:void 0,done:!0}}return d.prototype=v,o(x,"constructor",{value:v,configurable:!0}),o(v,"constructor",{value:d,configurable:!0}),d.displayName=u(v,c,"GeneratorFunction"),e.isGeneratorFunction=function(e){var r="function"==typeof e&&e.constructor;return!!r&&(r===d||"GeneratorFunction"===(r.displayName||r.name))},e.mark=function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,v):(e.__proto__=v,u(e,c,"GeneratorFunction")),e.prototype=Object.create(x),e},e.awrap=function(e){return{__await:e}},w(b.prototype),u(b.prototype,s,(function(){return this})),e.AsyncIterator=b,e.async=function(r,t,n,o,a){void 0===a&&(a=Promise);var i=new b(p(r,t,n,o),a);return e.isGeneratorFunction(t)?i:i.next().then((function(e){return e.done?e.value:i.next()}))},w(x),u(x,c,"Generator"),u(x,i,(function(){return this})),u(x,"toString",(function(){return"[object Generator]"})),e.keys=function(e){var r=Object(e),t=[];for(var n in r)t.push(n);return t.reverse(),function e(){for(;t.length;){var n=t.pop();if(n in r)return e.value=n,e.done=!1,e}return e.done=!0,e}},e.values=E,O.prototype={constructor:O,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(_),!e)for(var r in this)"t"===r.charAt(0)&&n.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=void 0)},stop:function(){this.done=!0;var e=this.tryEntries[0].completion;if("throw"===e.type)throw e.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var r=this;function t(t,n){return i.type="throw",i.arg=e,r.next=t,n&&(r.method="next",r.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var a=this.tryEntries[o],i=a.completion;if("root"===a.tryLoc)return t("end");if(a.tryLoc<=this.prev){var s=n.call(a,"catchLoc"),c=n.call(a,"finallyLoc");if(s&&c){if(this.prev<a.catchLoc)return t(a.catchLoc,!0);if(this.prev<a.finallyLoc)return t(a.finallyLoc)}else if(s){if(this.prev<a.catchLoc)return t(a.catchLoc,!0)}else{if(!c)throw new Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return t(a.finallyLoc)}}}},abrupt:function(e,r){for(var t=this.tryEntries.length-1;t>=0;--t){var o=this.tryEntries[t];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var a=o;break}}a&&("break"===e||"continue"===e)&&a.tryLoc<=r&&r<=a.finallyLoc&&(a=null);var i=a?a.completion:{};return i.type=e,i.arg=r,a?(this.method="next",this.next=a.finallyLoc,h):this.complete(i)},complete:function(e,r){if("throw"===e.type)throw e.arg;return"break"===e.type||"continue"===e.type?this.next=e.arg:"return"===e.type?(this.rval=this.arg=e.arg,this.method="return",this.next="end"):"normal"===e.type&&r&&(this.next=r),h},finish:function(e){for(var r=this.tryEntries.length-1;r>=0;--r){var t=this.tryEntries[r];if(t.finallyLoc===e)return this.complete(t.completion,t.afterLoc),_(t),h}},catch:function(e){for(var r=this.tryEntries.length-1;r>=0;--r){var t=this.tryEntries[r];if(t.tryLoc===e){var n=t.completion;if("throw"===n.type){var o=n.arg;_(t)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(e,r,t){return this.delegate={iterator:E(e),resultName:r,nextLoc:t},"next"===this.method&&(this.arg=void 0),h}},e}function n(e,r,t,n,o,a,i){try{var s=e[a](i),c=s.value}catch(e){return void t(e)}s.done?r(c):Promise.resolve(c).then(n,o)}function o(e){return function(){var r=this,t=arguments;return new Promise((function(o,a){var i=e.apply(r,t);function s(e){n(i,o,a,s,c,"next",e)}function c(e){n(i,o,a,s,c,"throw",e)}s(void 0)}))}}function a(){return a=Object.assign?Object.assign.bind():function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e},a.apply(this,arguments)}var i,s=function(){var e=o(t().mark((function e(n){var o,a;return t().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=3,r.default("https://ca.account.sony.com/api/authz/v3/oauth/token",{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded",Authorization:"Basic MDk1MTUxNTktNzIzNy00MzcwLTliNDAtMzgwNmU2N2MwODkxOnVjUGprYTV0bnRCMktxc1A="},body:new URLSearchParams({code:n,redirect_uri:"com.scee.psxandroid.scecompcall://redirect",grant_type:"authorization_code",token_format:"jwt"}).toString()});case 3:return o=e.sent,e.next=6,o.json();case 6:return e.abrupt("return",{accessToken:(a=e.sent).access_token,expiresIn:a.expires_in,idToken:a.id_token,refreshToken:a.refresh_token,refreshTokenExpiresIn:a.refresh_token_expires_in,scope:a.scope,tokenType:a.token_type});case 8:case"end":return e.stop()}}),e)})));return function(r){return e.apply(this,arguments)}}(),c=s,u=function(){var e=o(t().mark((function e(n){var o,a,i,s,c,u;return t().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=new URLSearchParams({access_type:"offline",client_id:"09515159-7237-4370-9b40-3806e67c0891",redirect_uri:"com.scee.psxandroid.scecompcall://redirect",response_type:"code",scope:"psn:mobile.v2.core psn:clientapp"}).toString(),i="https://ca.account.sony.com/api/authz/v3/oauth/authorize?"+a,e.next=4,r.default(i,{headers:{Cookie:"npsso="+n},redirect:"manual"});case 4:if((s=e.sent.headers).has("location")&&null!=(o=s.get("location"))&&o.includes("?code=")){e.next=8;break}throw new Error("\n There was a problem retrieving your PSN access code. Is your NPSSO code valid?\n To get a new NPSSO code, visit https://ca.account.sony.com/api/v1/ssocookie.\n ");case 8:return c=s.get("location"),u=new URLSearchParams(c.split("redirect/")[1]),e.abrupt("return",u.get("code"));case 11:case"end":return e.stop()}}),e)})));return function(r){return e.apply(this,arguments)}}(),p=u,l=function(){var e=o(t().mark((function e(n){var o,a;return t().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=3,r.default("https://ca.account.sony.com/api/authz/v3/oauth/token",{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded",Authorization:"Basic MDk1MTUxNTktNzIzNy00MzcwLTliNDAtMzgwNmU2N2MwODkxOnVjUGprYTV0bnRCMktxc1A="},body:new URLSearchParams({refresh_token:n,grant_type:"refresh_token",token_format:"jwt",scope:"psn:mobile.v2.core psn:clientapp"}).toString()});case 3:return o=e.sent,e.next=6,o.json();case 6:return e.abrupt("return",{accessToken:(a=e.sent).access_token,expiresIn:a.expires_in,idToken:a.id_token,refreshToken:a.refresh_token,refreshTokenExpiresIn:a.refresh_token_expires_in,scope:a.scope,tokenType:a.token_type});case 8:case"end":return e.stop()}}),e)})));return function(r){return e.apply(this,arguments)}}(),h=function(){var e=o(t().mark((function e(n,o,i){var s,c;return t().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,r.default(n.url,{method:null!=(s=null==n?void 0:n.method)?s:"GET",headers:a({Authorization:"Bearer "+o.accessToken,"Content-Type":"application/json"},null==n?void 0:n.headers),body:JSON.stringify(i)});case 2:return c=e.sent,e.next=5,c.json();case 5:return e.abrupt("return",e.sent);case 6:case"end":return e.stop()}}),e)})));return function(r,t,n){return e.apply(this,arguments)}}(),f=function(){var e=o(t().mark((function e(r,n){var o,a,i,s,c,u,p;return t().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return void 0===n&&(n={}),i=void 0===(a=(o=n).limit)?50:a,c=void 0===(s=o.categories)?["ps4_game","ps5_native_game"]:s,(u=new URL("https://web.np.playstation.com/api/graphql/v1/op")).searchParams.set("operationName","getUserGameList"),u.searchParams.set("variables",JSON.stringify({limit:i,categories:c.join(",")})),u.searchParams.set("extensions",JSON.stringify({persistedQuery:{version:1,sha256Hash:"e780a6d8b921ef0c59ec01ea5c5255671272ca0d819edb61320914cf7a78b3ae"}})),e.next=8,h({url:u.toString()},r);case 8:if((p=e.sent).data&&p.data.gameLibraryTitlesRetrieve){e.next=11;break}throw new Error(JSON.stringify(p));case 11:return e.abrupt("return",p);case 12:case"end":return e.stop()}}),e)})));return function(r,t){return e.apply(this,arguments)}}();exports.TrophyRarity=void 0,(i=exports.TrophyRarity||(exports.TrophyRarity={}))[i.UltraRare=0]="UltraRare",i[i.VeryRare=1]="VeryRare",i[i.Rare=2]="Rare",i[i.Common=3]="Common";var d=["headerOverrides"],v=function(e,r,t,n){void 0===t&&(t={}),void 0===n&&(n={});for(var o=function(e,r){if(null==e)return{};var t,n,o={},a=Object.keys(e);for(n=0;n<a.length;n++)r.indexOf(t=a[n])>=0||(o[t]=e[t]);return o}(t,d),i=(e+"/"+r).replace(/([^:]\/)\/+/g,"$1"),s={},c=0,u=Object.entries(a({},n,o));c<u.length;c++){var p=u[c],l=p[0],h=p[1];i.includes(":"+l)?i=i.replace(":"+l,String(h)):void 0!==h&&(s[l]=String(h))}var f=new URLSearchParams(s).toString();return f.length>0?i+"?"+f:i},y=function(){var e=o(t().mark((function e(r,n,o){var a;return t().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=v("https://m.np.playstation.com/api/search","/v1/universalSearch"),e.next=3,h({url:a,method:"POST"},r,{searchTerm:n,domainRequests:[{domain:o}]});case 3:return e.abrupt("return",e.sent);case 4:case"end":return e.stop()}}),e)})));return function(r,t,n){return e.apply(this,arguments)}}(),m="https://m.np.playstation.com/api/trophy",g=function(){var e=o(t().mark((function e(r,n,o,a){var i;return t().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return i=v(m,"/v1/npCommunicationIds/:npCommunicationId/trophyGroups/:trophyGroupId/trophies",a,{npCommunicationId:n,trophyGroupId:o}),e.next=3,h({url:i,headers:null==a?void 0:a.headerOverrides},r);case 3:return e.abrupt("return",e.sent);case 4:case"end":return e.stop()}}),e)})));return function(r,t,n,o){return e.apply(this,arguments)}}(),x=function(){var e=o(t().mark((function e(r,n,o){var a;return t().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=v(m,"/v1/npCommunicationIds/:npCommunicationId/trophyGroups",o,{npCommunicationId:n}),e.next=3,h({url:a,headers:null==o?void 0:o.headerOverrides},r);case 3:return e.abrupt("return",e.sent);case 4:case"end":return e.stop()}}),e)})));return function(r,t,n){return e.apply(this,arguments)}}(),w=function(){var e=o(t().mark((function e(r,n,o){var a;return t().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=v(m,"/v1/users/:accountId/trophyTitles",o,{accountId:n}),e.next=3,h({url:a,headers:null==o?void 0:o.headerOverrides},r);case 3:return e.abrupt("return",e.sent);case 4:case"end":return e.stop()}}),e)})));return function(r,t,n){return e.apply(this,arguments)}}(),b=function(){var e=o(t().mark((function e(r,n,o,a,i){var s,c,u,p;return t().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=v(m,"/v1/users/:accountId/npCommunicationIds/:npCommunicationId/trophyGroups/:trophyGroupId/trophies",i,{accountId:n,npCommunicationId:o,trophyGroupId:a}),e.next=3,h({url:s,headers:null==i?void 0:i.headerOverrides},r);case 3:if(null==(c=e.sent)||!c.error){e.next=6;break}throw new Error(null!=(u=null==c||null==(p=c.error)?void 0:p.message)?u:"Unexpected Error");case 6:return e.abrupt("return",c);case 7:case"end":return e.stop()}}),e)})));return function(r,t,n,o,a){return e.apply(this,arguments)}}(),k=function(){var e=o(t().mark((function e(r,n,o,a){var i,s,c,u;return t().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return i=v(m,"/v1/users/:accountId/npCommunicationIds/:npCommunicationId/trophyGroups",a,{accountId:n,npCommunicationId:o}),e.next=3,h({url:i,headers:null==a?void 0:a.headerOverrides},r);case 3:if(null==(s=e.sent)||!s.error){e.next=6;break}throw new Error(null!=(c=null==s||null==(u=s.error)?void 0:u.message)?c:"Unexpected Error");case 6:return e.abrupt("return",s);case 7:case"end":return e.stop()}}),e)})));return function(r,t,n,o){return e.apply(this,arguments)}}(),T=function(){var e=o(t().mark((function e(r,n,o){var a;return t().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=v(m,"/v1/users/:accountId/trophySummary",o,{accountId:n}),e.next=3,h({url:a,headers:null==o?void 0:o.headerOverrides},r);case 3:return e.abrupt("return",e.sent);case 4:case"end":return e.stop()}}),e)})));return function(r,t,n){return e.apply(this,arguments)}}(),I="https://m.np.playstation.com/api/userProfile/v1/internal/users",_=function(){var e=o(t().mark((function e(r,n,o){var a,i,s,c;return t().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=v(I,"/:accountId/basicPresences?type=primary",o,{accountId:n}),e.next=3,h({url:a},r);case 3:if(null==(i=e.sent)||!i.error){e.next=6;break}throw new Error(null!=(s=null==i||null==(c=i.error)?void 0:c.message)?s:"Unexpected Error");case 6:return e.abrupt("return",i);case 7:case"end":return e.stop()}}),e)})));return function(r,t,n){return e.apply(this,arguments)}}(),O=function(){var e=o(t().mark((function e(r,n,o){var a,i,s,c;return t().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=v(I,"/:accountId/profiles",o,{accountId:n}),e.next=3,h({url:a},r);case 3:if(null==(i=e.sent)||!i.error){e.next=6;break}throw new Error(null!=(s=null==i||null==(c=i.error)?void 0:c.message)?s:"Unexpected Error");case 6:return e.abrupt("return",i);case 7:case"end":return e.stop()}}),e)})));return function(r,t,n){return e.apply(this,arguments)}}(),E=function(){var e=o(t().mark((function e(r,n){var o,a,i,s;return t().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o="https://us-prof.np.community.playstation.net/userProfile/v1/users/"+n+"/profile2?fields=npId,onlineId,accountId,avatarUrls,plus,aboutMe,languagesUsed,trophySummary(@default,level,progress,earnedTrophies),isOfficiallyVerified,personalDetail(@default,profilePictureUrls),personalDetailSharing,personalDetailSharingRequestMessageFlag,primaryOnlineStatus,presences(@default,@titleInfo,platform,lastOnlineDate,hasBroadcastData),requestMessageFlag,blocking,friendRelation,following,consoleAvailability",e.next=3,h({url:o},r);case 3:if(null==(a=e.sent)||!a.error){e.next=6;break}throw new Error(null!=(i=null==a||null==(s=a.error)?void 0:s.message)?i:"Unexpected Error");case 6:return e.abrupt("return",a);case 7:case"end":return e.stop()}}),e)})));return function(r,t){return e.apply(this,arguments)}}(),S=function(){var e=o(t().mark((function e(r,n,o){var a,i,s,c;return t().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=v(I,"/:accountId/friends",o,{accountId:n}),e.next=3,h({url:a},r);case 3:if(null==(i=e.sent)||!i.error){e.next=6;break}throw new Error(null!=(s=null==i||null==(c=i.error)?void 0:c.message)?s:"Unexpected Error");case 6:return e.abrupt("return",i);case 7:case"end":return e.stop()}}),e)})));return function(r,t,n){return e.apply(this,arguments)}}();function L(){return(L=o(t().mark((function e(r,n,o){var a,i,s,c;return t().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=v("https://m.np.playstation.com/api/gamelist/v2/users","/:accountId/titles",o,{accountId:n}),e.next=3,h({url:a},r);case 3:if(null==(i=e.sent)||!i.error){e.next=6;break}throw new Error(null!=(s=null==i||null==(c=i.error)?void 0:c.message)?s:"Unexpected Error");case 6:return e.abrupt("return",i);case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}exports.call=h,exports.exchangeAccessCodeForAuthTokens=s,exports.exchangeCodeForAccessToken=c,exports.exchangeNpssoForAccessCode=u,exports.exchangeNpssoForCode=p,exports.exchangeRefreshTokenForAuthTokens=l,exports.getBasicPresence=_,exports.getProfileFromAccountId=O,exports.getProfileFromUserName=E,exports.getRecentlyPlayedGames=f,exports.getTitleTrophies=g,exports.getTitleTrophyGroups=x,exports.getUserFriendsAccountIds=S,exports.getUserPlayedGames=function(e,r,t){return L.apply(this,arguments)},exports.getUserTitles=w,exports.getUserTrophiesEarnedForTitle=b,exports.getUserTrophyGroupEarningsForTitle=k,exports.getUserTrophyProfileSummary=T,exports.makeUniversalSearch=y;
//# sourceMappingURL=psn-api.cjs.production.min.js.map