@gw2me/client 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -102,8 +102,27 @@ declare class Gw2MeClient {
102
102
  getAuthorizationUrl({ redirect_uri, scopes, state, code_challenge, code_challenge_method, prompt, include_granted_scopes, verified_accounts_only, }: AuthorizationUrlParams): string;
103
103
  getAccessToken({ code, redirect_uri, code_verifier }: AuthTokenParams): Promise<TokenResponse>;
104
104
  refreshToken({ refresh_token }: RefreshTokenParams): Promise<TokenResponse>;
105
+ /**
106
+ * Parses the search params received from gw2.me on the redirect url (code and state).
107
+ * If gw2.me returned an error response, this will throw an error.
108
+ *
109
+ * @returns The code and optional state.
110
+ */
111
+ parseAuthorizationResponseSearchParams(searchParams: URLSearchParams): {
112
+ code: string;
113
+ state: string | undefined;
114
+ };
105
115
  api(access_token: string): Gw2MeApi;
106
116
  get fedCM(): Gw2MeFedCM;
107
117
  }
108
118
 
109
- export { type AccountsResponse, type AuthTokenParams, type AuthorizationUrlParams, type ClientInfo, Gw2MeApi, Gw2MeClient, type Options, type RefreshTokenParams, Scope, type SubtokenResponse, type TokenResponse, type UserResponse };
119
+ declare class Gw2MeError extends Error {
120
+ }
121
+ declare class Gw2MeOAuthError extends Gw2MeError {
122
+ error: string;
123
+ error_description?: string | undefined;
124
+ error_uri?: string | undefined;
125
+ constructor(error: string, error_description?: string | undefined, error_uri?: string | undefined);
126
+ }
127
+
128
+ export { type AccountsResponse, type AuthTokenParams, type AuthorizationUrlParams, type ClientInfo, Gw2MeApi, Gw2MeClient, Gw2MeError, Gw2MeOAuthError, type Options, type RefreshTokenParams, Scope, type SubtokenResponse, type TokenResponse, type UserResponse };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var A=Object.defineProperty;var $=Object.getOwnPropertyDescriptor;var G=Object.getOwnPropertyNames;var z=Object.prototype.hasOwnProperty;var C=t=>{throw TypeError(t)};var j=(t,e)=>{for(var r in e)A(t,r,{get:e[r],enumerable:!0})},E=(t,e,r,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of G(e))!z.call(t,s)&&s!==r&&A(t,s,{get:()=>e[s],enumerable:!(n=$(e,s))||n.enumerable});return t};var I=t=>E(A({},"__esModule",{value:!0}),t);var v=(t,e,r)=>e.has(t)||C("Cannot "+r);var o=(t,e,r)=>(v(t,e,"read from private field"),r?r.call(t):e.get(t)),a=(t,e,r)=>e.has(t)?C("Cannot add the same private member more than once"):e instanceof WeakSet?e.add(t):e.set(t,r),h=(t,e,r,n)=>(v(t,e,"write to private field"),n?n.call(t,r):e.set(t,r),r),p=(t,e,r)=>(v(t,e,"access private method"),r);var B={};j(B,{Gw2MeApi:()=>_,Gw2MeClient:()=>U,Scope:()=>T});module.exports=I(B);var T=(i=>(i.Identify="identify",i.Email="email",i.Accounts="accounts",i.Accounts_Verified="accounts.verified",i.Accounts_DisplayName="accounts.displayName",i.GW2_Account="gw2:account",i.GW2_Inventories="gw2:inventories",i.GW2_Characters="gw2:characters",i.GW2_Tradingpost="gw2:tradingpost",i.GW2_Wallet="gw2:wallet",i.GW2_Unlocks="gw2:unlocks",i.GW2_Pvp="gw2:pvp",i.GW2_Wvw="gw2:wvw",i.GW2_Builds="gw2:builds",i.GW2_Progression="gw2:progression",i.GW2_Guilds="gw2:guilds",i))(T||{});async function g(t){let e=t.headers.get("Content-Type")==="application/json";if(!t.ok){let r;throw e&&(r=(await t.json()).error_description),new Error(`gw2.me returned an error: ${r??"Unknown error"}`)}if(!e)throw new Error("gw2.me did not return a valid JSON response");return t.json()}var f,x,_=class{constructor(e,r){this.access_token=e;this.options=r;a(this,f)}user(){return fetch(p(this,f,x).call(this,"api/user"),{headers:{Authorization:`Bearer ${this.access_token}`},cache:"no-store"}).then(g)}accounts(){return fetch(p(this,f,x).call(this,"api/accounts"),{headers:{Authorization:`Bearer ${this.access_token}`},cache:"no-store"}).then(g)}subtoken(e){return fetch(p(this,f,x).call(this,`api/accounts/${e}/subtoken`),{headers:{Authorization:`Bearer ${this.access_token}`},cache:"no-store"}).then(g)}};f=new WeakSet,x=function(e){return new URL(e,this.options?.url||"https://gw2.me/")};var w,k,P=class{constructor(e,r){a(this,w);a(this,k);h(this,w,e),h(this,k,r)}isSupported(){return typeof window<"u"&&"IdentityCredential"in window}request({mediation:e,signal:r,mode:n}){if(!this.isSupported())throw new Error("FedCM is not supported");return navigator.credentials.get({mediation:e,signal:r,identity:{providers:[{configURL:o(this,w),clientId:o(this,k)}],mode:n}})}};w=new WeakMap,k=new WeakMap;var c,d,R,l,y,U=class{constructor({client_id:e,client_secret:r},n){this.options=n;a(this,l);a(this,c);a(this,d);a(this,R);h(this,c,e),h(this,d,r),h(this,R,new P(p(this,l,y).call(this,"/fed-cm/config.json"),o(this,c)))}getAuthorizationUrl({redirect_uri:e,scopes:r,state:n,code_challenge:s,code_challenge_method:m,prompt:b,include_granted_scopes:O,verified_accounts_only:W}){let u=new URLSearchParams({client_id:o(this,c),response_type:"code",redirect_uri:e,scope:r.join(" ")});return n&&u.append("state",n),s&&m&&(u.append("code_challenge",s),u.append("code_challenge_method",m)),b&&u.append("prompt",b),O&&u.append("include_granted_scopes","true"),W&&u.append("verified_accounts_only","true"),p(this,l,y).call(this,`/oauth2/authorize?${u.toString()}`).toString()}async getAccessToken({code:e,redirect_uri:r,code_verifier:n}){let s=new URLSearchParams({grant_type:"authorization_code",code:e,client_id:o(this,c),redirect_uri:r}),m={"Content-Type":"application/x-www-form-urlencoded"};return o(this,d)&&(m.Authorization=`Basic ${btoa(`${o(this,c)}:${o(this,d)}`)}`),n&&s.set("code_verifier",n),await fetch(p(this,l,y).call(this,"/api/token"),{method:"POST",headers:m,body:s,cache:"no-store"}).then(g)}async refreshToken({refresh_token:e}){if(!o(this,d))throw new Error("client_secret required");let r=new URLSearchParams({grant_type:"refresh_token",refresh_token:e,client_id:o(this,c)}),n={"Content-Type":"application/x-www-form-urlencoded",Authorization:`Basic ${btoa(`${o(this,c)}:${o(this,d)}`)}`};return await fetch(p(this,l,y).call(this,"/api/token"),{method:"POST",headers:n,body:r,cache:"no-store"}).then(g)}api(e){return new _(e,this.options)}get fedCM(){return o(this,R)}};c=new WeakMap,d=new WeakMap,R=new WeakMap,l=new WeakSet,y=function(e){return new URL(e,this.options?.url||"https://gw2.me/")};0&&(module.exports={Gw2MeApi,Gw2MeClient,Scope});
1
+ "use strict";var C=Object.defineProperty;var W=Object.getOwnPropertyDescriptor;var z=Object.getOwnPropertyNames;var j=Object.prototype.hasOwnProperty;var O=r=>{throw TypeError(r)};var L=(r,e)=>{for(var t in e)C(r,t,{get:e[t],enumerable:!0})},M=(r,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of z(e))!j.call(r,i)&&i!==t&&C(r,i,{get:()=>e[i],enumerable:!(n=W(e,i))||n.enumerable});return r};var B=r=>M(C({},"__esModule",{value:!0}),r);var T=(r,e,t)=>e.has(r)||O("Cannot "+t);var s=(r,e,t)=>(T(r,e,"read from private field"),t?t.call(r):e.get(r)),c=(r,e,t)=>e.has(r)?O("Cannot add the same private member more than once"):e instanceof WeakSet?e.add(r):e.set(r,t),l=(r,e,t,n)=>(T(r,e,"write to private field"),n?n.call(r,t):e.set(r,t),t),p=(r,e,t)=>(T(r,e,"access private method"),t);var E={};L(E,{Gw2MeApi:()=>x,Gw2MeClient:()=>I,Gw2MeError:()=>a,Gw2MeOAuthError:()=>y,Scope:()=>G});module.exports=B(E);var G=(o=>(o.Identify="identify",o.Email="email",o.Accounts="accounts",o.Accounts_Verified="accounts.verified",o.Accounts_DisplayName="accounts.displayName",o.GW2_Account="gw2:account",o.GW2_Inventories="gw2:inventories",o.GW2_Characters="gw2:characters",o.GW2_Tradingpost="gw2:tradingpost",o.GW2_Wallet="gw2:wallet",o.GW2_Unlocks="gw2:unlocks",o.GW2_Pvp="gw2:pvp",o.GW2_Wvw="gw2:wvw",o.GW2_Builds="gw2:builds",o.GW2_Progression="gw2:progression",o.GW2_Guilds="gw2:guilds",o))(G||{});var a=class extends Error{},y=class extends a{constructor(t,n,i){super(`Received ${t}`+(n?`: ${n}`:"")+(i?` (${i})`:""));this.error=t;this.error_description=n;this.error_uri=i}};async function m(r){let e=r.headers.get("Content-Type")==="application/json";if(!r.ok){let t;throw e&&(t=(await r.json()).error_description),new a(`gw2.me returned an error: ${t??"Unknown error"}`)}if(!e)throw new a("gw2.me did not return a valid JSON response");return r.json()}var w,A,x=class{constructor(e,t){this.access_token=e;this.options=t;c(this,w)}user(){return fetch(p(this,w,A).call(this,"api/user"),{headers:{Authorization:`Bearer ${this.access_token}`},cache:"no-store"}).then(m)}accounts(){return fetch(p(this,w,A).call(this,"api/accounts"),{headers:{Authorization:`Bearer ${this.access_token}`},cache:"no-store"}).then(m)}subtoken(e){return fetch(p(this,w,A).call(this,`api/accounts/${e}/subtoken`),{headers:{Authorization:`Bearer ${this.access_token}`},cache:"no-store"}).then(m)}};w=new WeakSet,A=function(e){return new URL(e,this.options?.url||"https://gw2.me/")};var R,b,P=class{constructor(e,t){c(this,R);c(this,b);l(this,R,e),l(this,b,t)}isSupported(){return typeof window<"u"&&"IdentityCredential"in window}request({mediation:e,signal:t,mode:n}){if(!this.isSupported())throw new a("FedCM is not supported");return navigator.credentials.get({mediation:e,signal:t,identity:{providers:[{configURL:s(this,R),clientId:s(this,b)}],mode:n}})}};R=new WeakMap,b=new WeakMap;var d,g,v,h,_,I=class{constructor({client_id:e,client_secret:t},n){this.options=n;c(this,h);c(this,d);c(this,g);c(this,v);l(this,d,e),l(this,g,t),l(this,v,new P(p(this,h,_).call(this,"/fed-cm/config.json"),s(this,d)))}getAuthorizationUrl({redirect_uri:e,scopes:t,state:n,code_challenge:i,code_challenge_method:u,prompt:k,include_granted_scopes:U,verified_accounts_only:$}){let f=new URLSearchParams({client_id:s(this,d),response_type:"code",redirect_uri:e,scope:t.join(" ")});return n&&f.append("state",n),i&&u&&(f.append("code_challenge",i),f.append("code_challenge_method",u)),k&&f.append("prompt",k),U&&f.append("include_granted_scopes","true"),$&&f.append("verified_accounts_only","true"),p(this,h,_).call(this,`/oauth2/authorize?${f.toString()}`).toString()}async getAccessToken({code:e,redirect_uri:t,code_verifier:n}){let i=new URLSearchParams({grant_type:"authorization_code",code:e,client_id:s(this,d),redirect_uri:t}),u={"Content-Type":"application/x-www-form-urlencoded"};return s(this,g)&&(u.Authorization=`Basic ${btoa(`${s(this,d)}:${s(this,g)}`)}`),n&&i.set("code_verifier",n),await fetch(p(this,h,_).call(this,"/api/token"),{method:"POST",headers:u,body:i,cache:"no-store"}).then(m)}async refreshToken({refresh_token:e}){if(!s(this,g))throw new a("client_secret required");let t=new URLSearchParams({grant_type:"refresh_token",refresh_token:e,client_id:s(this,d)}),n={"Content-Type":"application/x-www-form-urlencoded",Authorization:`Basic ${btoa(`${s(this,d)}:${s(this,g)}`)}`};return await fetch(p(this,h,_).call(this,"/api/token"),{method:"POST",headers:n,body:t,cache:"no-store"}).then(m)}parseAuthorizationResponseSearchParams(e){let t=p(this,h,_).call(this,"/").origin,n=e.get("iss");if(!n)throw new a("Issuer Identifier verification failed: parameter `iss` is missing");if(n!==t)throw new a(`Issuer Identifier verification failed: expected "${t}", got "${n}"`);let i=e.get("error");if(i){let U=e.get("error_description")??void 0,$=e.get("error_uri")??void 0;throw new y(i,U,$)}let u=e.get("code");if(!u)throw new a("Parameter `code` is missing");let k=e.get("state")||void 0;return{code:u,state:k}}api(e){return new x(e,this.options)}get fedCM(){return s(this,v)}};d=new WeakMap,g=new WeakMap,v=new WeakMap,h=new WeakSet,_=function(e){return new URL(e,this.options?.url||"https://gw2.me/")};0&&(module.exports={Gw2MeApi,Gw2MeClient,Gw2MeError,Gw2MeOAuthError,Scope});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gw2me/client",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "gw2.me client library",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -30,7 +30,7 @@
30
30
  "@gw2treasures/tsconfig": "0.0.1",
31
31
  "eslint": "8.57.1",
32
32
  "tsup": "8.3.5",
33
- "typescript": "5.6.3",
33
+ "typescript": "5.7.2",
34
34
  "undici-types": "6.21.0"
35
35
  },
36
36
  "scripts": {