@gw2me/client 0.8.0 → 0.8.2

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/dpop.d.ts CHANGED
@@ -1,11 +1,7 @@
1
+ import { D as DPoPParams } from './types-yBP8cksw.js';
2
+
1
3
  declare function generateDPoPKeyPair(): Promise<CryptoKeyPair>;
2
- interface DPoPOptions {
3
- htm: 'GET' | 'POST' | (string & {});
4
- htu: string;
5
- nonce?: string;
6
- accessToken?: string;
7
- }
8
- declare function createDPoPJwt({ htm, htu, nonce, accessToken }: DPoPOptions, keyPair: CryptoKeyPair): Promise<string>;
4
+ declare function createDPoPJwt({ htm, htu, nonce, accessToken }: DPoPParams, keyPair: CryptoKeyPair): Promise<string>;
9
5
  declare function jwkThumbprint(key: CryptoKey): Promise<string>;
10
6
 
11
7
  export { createDPoPJwt, generateDPoPKeyPair, jwkThumbprint };
package/dist/dpop.js CHANGED
@@ -1 +1 @@
1
- "use strict";var u=Object.defineProperty;var d=Object.getOwnPropertyDescriptor;var w=Object.getOwnPropertyNames;var l=Object.prototype.hasOwnProperty;var h=(t,r)=>{for(var e in r)u(t,e,{get:r[e],enumerable:!0})},A=(t,r,e,o)=>{if(r&&typeof r=="object"||typeof r=="function")for(let n of w(r))!l.call(t,n)&&n!==e&&u(t,n,{get:()=>r[n],enumerable:!(o=d(r,n))||o.enumerable});return t};var P=t=>A(u({},"__esModule",{value:!0}),t);var E={};h(E,{createDPoPJwt:()=>m,generateDPoPKeyPair:()=>b,jwkThumbprint:()=>C});module.exports=P(E);function a(t){let r=t instanceof ArrayBuffer?new Uint8Array(t):t;return btoa(String.fromCharCode(...r)).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}function b(){return crypto.subtle.generateKey({name:"ECDSA",namedCurve:"P-256"},!1,["sign"])}async function m({htm:t,htu:r,nonce:e,accessToken:o},n){let c=JSON.stringify({alg:"ES256",typ:"dpop+jwt",jwk:await p(n.publicKey)}),y=JSON.stringify({iat:Math.floor(Date.now()/1e3),jti:a(crypto.getRandomValues(new Uint8Array(32))),htm:t,htu:r,nonce:e,ath:o?a(await crypto.subtle.digest("SHA-256",i(o))):void 0}),s=`${a(i(c))}.${a(i(y))}`,g={name:"ECDSA",hash:"SHA-256"},f=a(await crypto.subtle.sign(g,n.privateKey,i(s)));return`${s}.${f}`}var S=new TextEncoder;function i(t){return S.encode(t)}async function p(t){let{kty:r,e,k:o,n,x:c,y,crv:s}=await crypto.subtle.exportKey("jwk",t);return{e,k:o,crv:s,kty:r,n,x:c,y}}async function C(t){let r=JSON.stringify(await p(t)),e=await crypto.subtle.digest("SHA-256",i(r));return a(e)}0&&(module.exports={createDPoPJwt,generateDPoPKeyPair,jwkThumbprint});
1
+ "use strict";var u=Object.defineProperty;var d=Object.getOwnPropertyDescriptor;var w=Object.getOwnPropertyNames;var l=Object.prototype.hasOwnProperty;var m=(t,r)=>{for(var e in r)u(t,e,{get:r[e],enumerable:!0})},P=(t,r,e,a)=>{if(r&&typeof r=="object"||typeof r=="function")for(let n of w(r))!l.call(t,n)&&n!==e&&u(t,n,{get:()=>r[n],enumerable:!(a=d(r,n))||a.enumerable});return t};var A=t=>P(u({},"__esModule",{value:!0}),t);var K={};m(K,{createDPoPJwt:()=>h,generateDPoPKeyPair:()=>b,jwkThumbprint:()=>C});module.exports=A(K);function o(t){let r=t instanceof ArrayBuffer?new Uint8Array(t):t;return btoa(String.fromCharCode(...r)).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}function b(){return crypto.subtle.generateKey({name:"ECDSA",namedCurve:"P-256"},!1,["sign"])}async function h({htm:t,htu:r,nonce:e,accessToken:a},n){let c=JSON.stringify({alg:"ES256",typ:"dpop+jwt",jwk:await p(n.publicKey)}),y=JSON.stringify({iat:Math.floor(Date.now()/1e3),jti:o(crypto.getRandomValues(new Uint8Array(32))),htm:t,htu:r,nonce:e,ath:a?o(await crypto.subtle.digest("SHA-256",i(a))):void 0}),s=`${o(i(c))}.${o(i(y))}`,f={name:"ECDSA",hash:"SHA-256"},g=o(await crypto.subtle.sign(f,n.privateKey,i(s)));return`${s}.${g}`}var S=new TextEncoder;function i(t){return S.encode(t)}async function p(t){let{kty:r,e,k:a,n,x:c,y,crv:s}=await crypto.subtle.exportKey("jwk",t);return{e,k:a,crv:s,kty:r,n,x:c,y}}async function C(t){let r=JSON.stringify(await p(t)),e=await crypto.subtle.digest("SHA-256",i(r));return o(e)}0&&(module.exports={createDPoPJwt,generateDPoPKeyPair,jwkThumbprint});
package/dist/index.d.ts CHANGED
@@ -1,28 +1,5 @@
1
- declare enum Scope {
2
- Identify = "identify",
3
- Email = "email",
4
- Accounts = "accounts",
5
- Accounts_Verified = "accounts.verified",
6
- Accounts_DisplayName = "accounts.displayName",
7
- GW2_Account = "gw2:account",
8
- GW2_Inventories = "gw2:inventories",
9
- GW2_Characters = "gw2:characters",
10
- GW2_Tradingpost = "gw2:tradingpost",
11
- GW2_Wallet = "gw2:wallet",
12
- GW2_Unlocks = "gw2:unlocks",
13
- GW2_Pvp = "gw2:pvp",
14
- GW2_Wvw = "gw2:wvw",
15
- GW2_Builds = "gw2:builds",
16
- GW2_Progression = "gw2:progression",
17
- GW2_Guilds = "gw2:guilds"
18
- }
19
- interface ClientInfo {
20
- client_id: string;
21
- client_secret?: string;
22
- }
23
- interface Options {
24
- url: string;
25
- }
1
+ import { O as Options, a as DPoPCallback, S as Scope, C as ClientInfo } from './types-yBP8cksw.js';
2
+ export { D as DPoPParams } from './types-yBP8cksw.js';
26
3
 
27
4
  interface UserResponse {
28
5
  user: {
@@ -37,6 +14,7 @@ interface AccountsResponse {
37
14
  accounts: {
38
15
  id: string;
39
16
  name: string;
17
+ shared: boolean;
40
18
  verified?: boolean;
41
19
  displayName?: string | null;
42
20
  }[];
@@ -49,7 +27,7 @@ interface SubtokenResponse {
49
27
  expiresAt: string;
50
28
  }
51
29
  interface ApiOptions extends Options {
52
- dpopKeyPair?: CryptoKeyPair;
30
+ dpop?: DPoPCallback;
53
31
  }
54
32
  declare class Gw2MeApi {
55
33
  #private;
@@ -92,27 +70,28 @@ interface AuthorizationUrlParams {
92
70
  verified_accounts_only?: boolean;
93
71
  }
94
72
  interface PushedAuthorizationRequestParams extends AuthorizationUrlParams {
95
- dpopKeyPair?: CryptoKeyPair;
73
+ dpop?: DPoPCallback;
96
74
  }
97
75
  interface AuthorizationUrlRequestUriParams {
98
76
  request_uri: string;
99
77
  }
78
+ type TokenType = 'Bearer' | 'DPoP';
100
79
  interface AuthTokenParams {
101
80
  code: string;
102
- token_type?: 'Bearer' | 'DPoP';
81
+ token_type?: TokenType;
103
82
  redirect_uri: string;
104
83
  code_verifier?: string;
105
- dpopKeyPair?: CryptoKeyPair;
84
+ dpop?: DPoPCallback;
106
85
  }
107
86
  interface RefreshTokenParams {
108
87
  refresh_token: string;
109
- refresh_token_type?: 'Bearer' | 'DPoP';
110
- dpopKeyPair?: CryptoKeyPair;
88
+ refresh_token_type?: TokenType;
89
+ dpop?: DPoPCallback;
111
90
  }
112
91
  interface TokenResponse {
113
92
  access_token: string;
114
93
  issued_token_type: 'urn:ietf:params:oauth:token-type:access_token';
115
- token_type: 'Bearer' | 'DPoP';
94
+ token_type: TokenType;
116
95
  expires_in: number;
117
96
  refresh_token?: string;
118
97
  scope: string;
@@ -157,8 +136,8 @@ declare class Gw2MeClient {
157
136
  constructor(client: ClientInfo, options?: Partial<Options> | undefined);
158
137
  getAuthorizationUrl(params: AuthorizationUrlParams | AuthorizationUrlRequestUriParams): string;
159
138
  pushAuthorizationRequest(params: PushedAuthorizationRequestParams): Promise<PushedAuthorizationRequestResponse>;
160
- getAccessToken({ code, token_type, redirect_uri, code_verifier, dpopKeyPair }: AuthTokenParams): Promise<TokenResponse>;
161
- refreshToken({ refresh_token, refresh_token_type, dpopKeyPair }: RefreshTokenParams): Promise<TokenResponse>;
139
+ getAccessToken({ code, token_type, redirect_uri, code_verifier, dpop }: AuthTokenParams): Promise<TokenResponse>;
140
+ refreshToken({ refresh_token, refresh_token_type, dpop }: RefreshTokenParams): Promise<TokenResponse>;
162
141
  revokeToken({ token }: RevokeTokenParams): Promise<void>;
163
142
  introspectToken({ token }: IntrospectTokenParams): Promise<IntrospectTokenResponse>;
164
143
  /**
@@ -184,4 +163,4 @@ declare class Gw2MeOAuthError extends Gw2MeError {
184
163
  constructor(error: string, error_description?: string | undefined, error_uri?: string | undefined);
185
164
  }
186
165
 
187
- export { type AccountsResponse, type ApiOptions, type AuthTokenParams, type AuthorizationUrlParams, type AuthorizationUrlRequestUriParams, type ClientInfo, type FedCMRequestOptions, Gw2MeApi, Gw2MeClient, Gw2MeError, Gw2MeFedCM, Gw2MeOAuthError, type IntrospectTokenParams, IntrospectTokenResponse, type Options, type PushedAuthorizationRequestParams, type PushedAuthorizationRequestResponse, type RefreshTokenParams, type RevokeTokenParams, Scope, type SubtokenOptions, type SubtokenResponse, type TokenResponse, type UserResponse };
166
+ export { type AccountsResponse, type ApiOptions, type AuthTokenParams, type AuthorizationUrlParams, type AuthorizationUrlRequestUriParams, ClientInfo, DPoPCallback, type FedCMRequestOptions, Gw2MeApi, Gw2MeClient, Gw2MeError, Gw2MeFedCM, Gw2MeOAuthError, type IntrospectTokenParams, IntrospectTokenResponse, Options, type PushedAuthorizationRequestParams, type PushedAuthorizationRequestResponse, type RefreshTokenParams, type RevokeTokenParams, Scope, type SubtokenOptions, type SubtokenResponse, type TokenResponse, type TokenType, type UserResponse };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var z=Object.defineProperty;var J=Object.getOwnPropertyDescriptor;var L=Object.getOwnPropertyNames;var M=Object.prototype.hasOwnProperty;var E=r=>{throw TypeError(r)};var N=(r,e)=>{for(var t in e)z(r,t,{get:e[t],enumerable:!0})},H=(r,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of L(e))!M.call(r,o)&&o!==t&&z(r,o,{get:()=>e[o],enumerable:!(n=J(e,o))||n.enumerable});return r};var F=r=>H(z({},"__esModule",{value:!0}),r);var D=(r,e,t)=>e.has(r)||E("Cannot "+t);var c=(r,e,t)=>(D(r,e,"read from private field"),t?t.call(r):e.get(r)),m=(r,e,t)=>e.has(r)?E("Cannot add the same private member more than once"):e instanceof WeakSet?e.add(r):e.set(r,t),k=(r,e,t,n)=>(D(r,e,"write to private field"),n?n.call(r,t):e.set(r,t),t),s=(r,e,t)=>(D(r,e,"access private method"),t);var Q={};N(Q,{Gw2MeApi:()=>v,Gw2MeClient:()=>$,Gw2MeError:()=>f,Gw2MeOAuthError:()=>T,Scope:()=>j});module.exports=F(Q);var j=(d=>(d.Identify="identify",d.Email="email",d.Accounts="accounts",d.Accounts_Verified="accounts.verified",d.Accounts_DisplayName="accounts.displayName",d.GW2_Account="gw2:account",d.GW2_Inventories="gw2:inventories",d.GW2_Characters="gw2:characters",d.GW2_Tradingpost="gw2:tradingpost",d.GW2_Wallet="gw2:wallet",d.GW2_Unlocks="gw2:unlocks",d.GW2_Pvp="gw2:pvp",d.GW2_Wvw="gw2:wvw",d.GW2_Builds="gw2:builds",d.GW2_Progression="gw2:progression",d.GW2_Guilds="gw2:guilds",d))(j||{});function _(r){let e=r instanceof ArrayBuffer?new Uint8Array(r):r;return btoa(String.fromCharCode(...e)).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}async function x({htm:r,htu:e,nonce:t,accessToken:n},o){let i=JSON.stringify({alg:"ES256",typ:"dpop+jwt",jwk:await G(o.publicKey)}),a=JSON.stringify({iat:Math.floor(Date.now()/1e3),jti:_(crypto.getRandomValues(new Uint8Array(32))),htm:r,htu:e,nonce:t,ath:n?_(await crypto.subtle.digest("SHA-256",A(n))):void 0}),h=`${_(A(i))}.${_(A(a))}`,y={name:"ECDSA",hash:"SHA-256"},U=_(await crypto.subtle.sign(y,o.privateKey,A(h)));return`${h}.${U}`}var V=new TextEncoder;function A(r){return V.encode(r)}async function G(r){let{kty:e,e:t,k:n,n:o,x:i,y:a,crv:h}=await crypto.subtle.exportKey("jwk",r);return{e:t,k:n,crv:h,kty:e,n:o,x:i,y:a}}async function W(r){let e=JSON.stringify(await G(r)),t=await crypto.subtle.digest("SHA-256",A(e));return _(t)}var f=class extends Error{},T=class extends f{constructor(t,n,o){super(`Received ${t}`+(n?`: ${n}`:"")+(o?` (${o})`:""));this.error=t;this.error_description=n;this.error_uri=o}};async function g(r){if(await q(r),!(r.headers.get("Content-Type")==="application/json"))throw new f("gw2.me did not return a valid JSON response");return r.json()}async function q(r){if(!r.ok){let e;throw r.headers.get("Content-Type")==="application/json"&&(e=(await r.json()).error_description),new f(`gw2.me returned an error: ${e??"Unknown error"}`)}}var l,K,b,v=class{constructor(e,t){this.access_token=e;this.options=t;m(this,l)}user(){return s(this,l,b).call(this,"api/user").then(e=>fetch(e)).then(g)}saveSettings(e){return s(this,l,b).call(this,"api/user/settings",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)}).then(t=>fetch(t)).then(q)}accounts(){return s(this,l,b).call(this,"api/accounts").then(e=>fetch(e)).then(g)}subtoken(e,t){let n=s(this,l,K).call(this,`api/accounts/${e}/subtoken`);return t?.permissions&&n.searchParams.set("permissions",t.permissions.join(",")),s(this,l,b).call(this,n).then(o=>fetch(o)).then(g)}};l=new WeakSet,K=function(e){return new URL(e,this.options?.url||"https://gw2.me/")},b=async function(e,t){let n=e instanceof URL?e:s(this,l,K).call(this,e),o=this.options?.dpopKeyPair,i=new Headers(t?.headers);if(i.set("Authorization",`${o?"DPoP":"Bearer"} ${this.access_token}`),o){let a=await x({htm:t?.method??"GET",htu:n.toString(),accessToken:this.access_token},o);i.set("DPoP",a)}return new Request(n,{cache:"no-cache",...t,headers:i})};var O,C,I=class{constructor(e,t){m(this,O);m(this,C);k(this,O,e),k(this,C,t)}isSupported(){return typeof window<"u"&&"IdentityCredential"in window}request({scopes:e,mediation:t,signal:n,mode:o,code_challenge:i,code_challenge_method:a}){if(!this.isSupported())throw new f("FedCM is not supported");return navigator.credentials.get({mediation:t,signal:n,identity:{providers:[{configURL:c(this,O),clientId:c(this,C),fields:[e.includes("identify")&&"name",e.includes("email")&&"email"].filter(Boolean),nonce:`${a}:${i}`,params:{scope:e.join(" "),code_challenge:i,code_challenge_method:a}}],mode:o}})}};O=new WeakMap,C=new WeakMap;var p,S,u,P,R,$=class{constructor(e,t){this.options=t;m(this,u);m(this,p);m(this,S);k(this,p,e),k(this,S,new I(s(this,u,P).call(this,"/fed-cm/config.json"),e.client_id))}getAuthorizationUrl(e){let t="request_uri"in e?new URLSearchParams({client_id:c(this,p).client_id,response_type:"code",request_uri:e.request_uri}):B(c(this,p).client_id,e);return s(this,u,P).call(this,`/oauth2/authorize?${t.toString()}`).toString()}async pushAuthorizationRequest(e){let t=s(this,u,P).call(this,"/oauth2/par"),n={"Content-Type":"application/x-www-form-urlencoded"};e.dpopKeyPair&&(n.DPoP=await x({htm:"POST",htu:t.toString()},e.dpopKeyPair),e.dpop_jkt=await W(e.dpopKeyPair.publicKey));let o=B(c(this,p).client_id,e);return c(this,p).client_secret&&(n.Authorization=s(this,u,R).call(this)),await fetch(t,{method:"POST",headers:n,body:o,cache:"no-store"}).then(g)}async getAccessToken({code:e,token_type:t,redirect_uri:n,code_verifier:o,dpopKeyPair:i}){let a=new URLSearchParams({grant_type:"authorization_code",code:e,client_id:c(this,p).client_id,redirect_uri:n}),h={"Content-Type":"application/x-www-form-urlencoded"};c(this,p).client_secret&&(h.Authorization=s(this,u,R).call(this)),o&&a.set("code_verifier",o);let y=s(this,u,P).call(this,"/api/token");return i&&(h.DPoP=await x({htm:"POST",htu:y.toString(),accessToken:t==="DPoP"?e:void 0},i)),await fetch(y,{method:"POST",headers:h,body:a,cache:"no-store"}).then(g)}async refreshToken({refresh_token:e,refresh_token_type:t,dpopKeyPair:n}){let o=new URLSearchParams({grant_type:"refresh_token",refresh_token:e,client_id:c(this,p).client_id}),i={"Content-Type":"application/x-www-form-urlencoded"};c(this,p).client_secret&&(i.Authorization=s(this,u,R).call(this));let a=s(this,u,P).call(this,"/api/token");return n&&(i.DPoP=await x({htm:"POST",htu:a.toString(),accessToken:t==="DPoP"?e:void 0},n)),await fetch(a,{method:"POST",headers:i,body:o,cache:"no-store"}).then(g)}async revokeToken({token:e}){let t=new URLSearchParams({token:e}),n={"Content-Type":"application/x-www-form-urlencoded"};c(this,p).client_secret&&(n.Authorization=s(this,u,R).call(this)),await fetch(s(this,u,P).call(this,"/api/token/revoke"),{method:"POST",cache:"no-store",headers:n,body:t}).then(g)}async introspectToken({token:e}){let t=new URLSearchParams({token:e}),n={"Content-Type":"application/x-www-form-urlencoded"};return c(this,p).client_secret&&(n.Authorization=s(this,u,R).call(this)),await fetch(s(this,u,P).call(this,"/api/token/introspect"),{method:"POST",cache:"no-store",headers:n,body:t}).then(g)}parseAuthorizationResponseSearchParams(e){let t=s(this,u,P).call(this,"/").origin,n=e.get("iss");if(!n)throw new f("Issuer Identifier verification failed: parameter `iss` is missing");if(n!==t)throw new f(`Issuer Identifier verification failed: expected "${t}", got "${n}"`);let o=e.get("error");if(o){let h=e.get("error_description")??void 0,y=e.get("error_uri")??void 0;throw new T(o,h,y)}let i=e.get("code");if(!i)throw new f("Parameter `code` is missing");let a=e.get("state")||void 0;return{code:i,state:a}}api(e,t){return new v(e,{...this.options,...t})}get fedCM(){return c(this,S)}};p=new WeakMap,S=new WeakMap,u=new WeakSet,P=function(e){return new URL(e,this.options?.url||"https://gw2.me/")},R=function(){if(!c(this,p).client_secret)throw new f("client_secret is required");return`Basic ${btoa(`${c(this,p).client_id}:${c(this,p).client_secret}`)}`};function B(r,{redirect_uri:e,scopes:t,state:n,code_challenge:o,code_challenge_method:i,dpop_jkt:a,prompt:h,include_granted_scopes:y,verified_accounts_only:U}){let w=new URLSearchParams({client_id:r,response_type:"code",redirect_uri:e,scope:t.join(" ")});return n&&w.append("state",n),o&&i&&(w.append("code_challenge",o),w.append("code_challenge_method",i)),a&&w.append("dpop_jkt",a),h&&w.append("prompt",h),y&&w.append("include_granted_scopes","true"),U&&w.append("verified_accounts_only","true"),w}0&&(module.exports={Gw2MeApi,Gw2MeClient,Gw2MeError,Gw2MeOAuthError,Scope});
1
+ "use strict";var C=Object.defineProperty;var j=Object.getOwnPropertyDescriptor;var W=Object.getOwnPropertyNames;var L=Object.prototype.hasOwnProperty;var G=n=>{throw TypeError(n)};var E=(n,e)=>{for(var t in e)C(n,t,{get:e[t],enumerable:!0})},B=(n,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of W(e))!L.call(n,o)&&o!==t&&C(n,o,{get:()=>e[o],enumerable:!(r=j(e,o))||r.enumerable});return n};var M=n=>B(C({},"__esModule",{value:!0}),n);var U=(n,e,t)=>e.has(n)||G("Cannot "+t);var a=(n,e,t)=>(U(n,e,"read from private field"),t?t.call(n):e.get(n)),m=(n,e,t)=>e.has(n)?G("Cannot add the same private member more than once"):e instanceof WeakSet?e.add(n):e.set(n,t),_=(n,e,t,r)=>(U(n,e,"write to private field"),r?r.call(n,t):e.set(n,t),t),i=(n,e,t)=>(U(n,e,"access private method"),t);var J={};E(J,{Gw2MeApi:()=>T,Gw2MeClient:()=>q,Gw2MeError:()=>d,Gw2MeOAuthError:()=>x,Scope:()=>I});module.exports=M(J);var I=(u=>(u.Identify="identify",u.Email="email",u.Accounts="accounts",u.Accounts_Verified="accounts.verified",u.Accounts_DisplayName="accounts.displayName",u.GW2_Account="gw2:account",u.GW2_Inventories="gw2:inventories",u.GW2_Characters="gw2:characters",u.GW2_Tradingpost="gw2:tradingpost",u.GW2_Wallet="gw2:wallet",u.GW2_Unlocks="gw2:unlocks",u.GW2_Pvp="gw2:pvp",u.GW2_Wvw="gw2:wvw",u.GW2_Builds="gw2:builds",u.GW2_Progression="gw2:progression",u.GW2_Guilds="gw2:guilds",u))(I||{});var d=class extends Error{},x=class extends d{constructor(t,r,o){super(`Received ${t}`+(r?`: ${r}`:"")+(o?` (${o})`:""));this.error=t;this.error_description=r;this.error_uri=o}};async function l(n){if(await z(n),!(n.headers.get("Content-Type")==="application/json"))throw new d("gw2.me did not return a valid JSON response");return n.json()}async function z(n){if(!n.ok){let e;throw n.headers.get("Content-Type")==="application/json"&&(e=(await n.json()).error_description),new d(`gw2.me returned an error: ${e??"Unknown error"}`)}}var f,S,R,T=class{constructor(e,t){this.access_token=e;this.options=t;m(this,f)}user(){return i(this,f,R).call(this,"api/user").then(e=>fetch(e)).then(l)}saveSettings(e){return i(this,f,R).call(this,"api/user/settings",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)}).then(t=>fetch(t)).then(z)}accounts(){return i(this,f,R).call(this,"api/accounts").then(e=>fetch(e)).then(l)}subtoken(e,t){let r=i(this,f,S).call(this,`api/accounts/${e}/subtoken`);return t?.permissions&&r.searchParams.set("permissions",t.permissions.join(",")),i(this,f,R).call(this,r).then(o=>fetch(o)).then(l)}};f=new WeakSet,S=function(e){return new URL(e,this.options?.url||"https://gw2.me/")},R=async function(e,t){let r=e instanceof URL?e:i(this,f,S).call(this,e),o=this.options?.dpop,s=new Headers(t?.headers);return s.set("Authorization",`${o?"DPoP":"Bearer"} ${this.access_token}`),o&&s.set("DPoP",await o({htm:t?.method??"GET",htu:r.toString(),accessToken:this.access_token})),new Request(r,{cache:"no-cache",...t,headers:s})};var A,v,O=class{constructor(e,t){m(this,A);m(this,v);_(this,A,e),_(this,v,t)}isSupported(){return typeof window<"u"&&"IdentityCredential"in window}request({scopes:e,mediation:t,signal:r,mode:o,code_challenge:s,code_challenge_method:h}){if(!this.isSupported())throw new d("FedCM is not supported");return navigator.credentials.get({mediation:t,signal:r,identity:{providers:[{configURL:a(this,A),clientId:a(this,v),fields:[e.includes("identify")&&"name",e.includes("email")&&"email"].filter(Boolean),nonce:`${h}:${s}`,params:{scope:e.join(" "),code_challenge:s,code_challenge_method:h}}],mode:o}})}};A=new WeakMap,v=new WeakMap;var c,b,p,P,y,q=class{constructor(e,t){this.options=t;m(this,p);m(this,c);m(this,b);_(this,c,e),_(this,b,new O(i(this,p,P).call(this,"/fed-cm/config.json"),e.client_id))}getAuthorizationUrl(e){let t="request_uri"in e?new URLSearchParams({client_id:a(this,c).client_id,response_type:"code",request_uri:e.request_uri}):$(a(this,c).client_id,e);return i(this,p,P).call(this,`/oauth2/authorize?${t.toString()}`).toString()}async pushAuthorizationRequest(e){let t=i(this,p,P).call(this,"/oauth2/par"),r={"Content-Type":"application/x-www-form-urlencoded"};e.dpop&&(r.DPoP=await e.dpop({htm:"POST",htu:t.toString()}));let o=$(a(this,c).client_id,e);return a(this,c).client_secret&&(r.Authorization=i(this,p,y).call(this)),await fetch(t,{method:"POST",headers:r,body:o,cache:"no-store"}).then(l)}async getAccessToken({code:e,token_type:t,redirect_uri:r,code_verifier:o,dpop:s}){let h=new URLSearchParams({grant_type:"authorization_code",code:e,client_id:a(this,c).client_id,redirect_uri:r}),g={"Content-Type":"application/x-www-form-urlencoded"};a(this,c).client_secret&&(g.Authorization=i(this,p,y).call(this)),o&&h.set("code_verifier",o);let w=i(this,p,P).call(this,"/api/token");return s&&(g.DPoP=await s({htm:"POST",htu:w.toString(),accessToken:t==="DPoP"?e:void 0})),await fetch(w,{method:"POST",headers:g,body:h,cache:"no-store"}).then(l)}async refreshToken({refresh_token:e,refresh_token_type:t,dpop:r}){let o=new URLSearchParams({grant_type:"refresh_token",refresh_token:e,client_id:a(this,c).client_id}),s={"Content-Type":"application/x-www-form-urlencoded"};a(this,c).client_secret&&(s.Authorization=i(this,p,y).call(this));let h=i(this,p,P).call(this,"/api/token");return r&&(s.DPoP=await r({htm:"POST",htu:h.toString(),accessToken:t==="DPoP"?e:void 0})),await fetch(h,{method:"POST",headers:s,body:o,cache:"no-store"}).then(l)}async revokeToken({token:e}){let t=new URLSearchParams({token:e}),r={"Content-Type":"application/x-www-form-urlencoded"};a(this,c).client_secret&&(r.Authorization=i(this,p,y).call(this)),await fetch(i(this,p,P).call(this,"/api/token/revoke"),{method:"POST",cache:"no-store",headers:r,body:t}).then(l)}async introspectToken({token:e}){let t=new URLSearchParams({token:e}),r={"Content-Type":"application/x-www-form-urlencoded"};return a(this,c).client_secret&&(r.Authorization=i(this,p,y).call(this)),await fetch(i(this,p,P).call(this,"/api/token/introspect"),{method:"POST",cache:"no-store",headers:r,body:t}).then(l)}parseAuthorizationResponseSearchParams(e){let t=i(this,p,P).call(this,"/").origin,r=e.get("iss");if(!r)throw new d("Issuer Identifier verification failed: parameter `iss` is missing");if(r!==t)throw new d(`Issuer Identifier verification failed: expected "${t}", got "${r}"`);let o=e.get("error");if(o){let g=e.get("error_description")??void 0,w=e.get("error_uri")??void 0;throw new x(o,g,w)}let s=e.get("code");if(!s)throw new d("Parameter `code` is missing");let h=e.get("state")||void 0;return{code:s,state:h}}api(e,t){return new T(e,{...this.options,...t})}get fedCM(){return a(this,b)}};c=new WeakMap,b=new WeakMap,p=new WeakSet,P=function(e){return new URL(e,this.options?.url||"https://gw2.me/")},y=function(){if(!a(this,c).client_secret)throw new d("client_secret is required");return`Basic ${btoa(`${a(this,c).client_id}:${a(this,c).client_secret}`)}`};function $(n,{redirect_uri:e,scopes:t,state:r,code_challenge:o,code_challenge_method:s,dpop_jkt:h,prompt:g,include_granted_scopes:w,verified_accounts_only:D}){let k=new URLSearchParams({client_id:n,response_type:"code",redirect_uri:e,scope:t.join(" ")});return r&&k.append("state",r),o&&s&&(k.append("code_challenge",o),k.append("code_challenge_method",s)),h&&k.append("dpop_jkt",h),g&&k.append("prompt",g),w&&k.append("include_granted_scopes","true"),D&&k.append("verified_accounts_only","true"),k}0&&(module.exports={Gw2MeApi,Gw2MeClient,Gw2MeError,Gw2MeOAuthError,Scope});
@@ -0,0 +1,34 @@
1
+ declare enum Scope {
2
+ Identify = "identify",
3
+ Email = "email",
4
+ Accounts = "accounts",
5
+ Accounts_Verified = "accounts.verified",
6
+ Accounts_DisplayName = "accounts.displayName",
7
+ GW2_Account = "gw2:account",
8
+ GW2_Inventories = "gw2:inventories",
9
+ GW2_Characters = "gw2:characters",
10
+ GW2_Tradingpost = "gw2:tradingpost",
11
+ GW2_Wallet = "gw2:wallet",
12
+ GW2_Unlocks = "gw2:unlocks",
13
+ GW2_Pvp = "gw2:pvp",
14
+ GW2_Wvw = "gw2:wvw",
15
+ GW2_Builds = "gw2:builds",
16
+ GW2_Progression = "gw2:progression",
17
+ GW2_Guilds = "gw2:guilds"
18
+ }
19
+ interface ClientInfo {
20
+ client_id: string;
21
+ client_secret?: string;
22
+ }
23
+ interface Options {
24
+ url: string;
25
+ }
26
+ interface DPoPParams {
27
+ htm: 'POST' | 'GET' | (string & {});
28
+ htu: string;
29
+ nonce?: string;
30
+ accessToken?: string;
31
+ }
32
+ type DPoPCallback = (params: DPoPParams) => string | Promise<string>;
33
+
34
+ export { type ClientInfo as C, type DPoPParams as D, type Options as O, Scope as S, type DPoPCallback as a };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gw2me/client",
3
- "version": "0.8.0",
3
+ "version": "0.8.2",
4
4
  "description": "gw2.me client library",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",