@bn2me/client
Version:
bn2.me client library
2 lines (1 loc) • 6.4 kB
JavaScript
import{a as s,b as m,c as _,d as o}from"./chunk-OSZ7DOEH.js";var U=(a=>(a.Identify="identify",a.Email="email",a.Accounts="accounts",a.Accounts_Verified="accounts.verified",a.Accounts_DisplayName="accounts.displayName",a.BN2_Account="bn2:account",a.BN2_Collections="bn2:collections",a))(U||{});var h=class extends Error{},b=class extends h{constructor(t,n,r){super(`Received ${t}`+(n?`: ${n}`:"")+(r?` (${r})`:""));this.error=t;this.error_description=n;this.error_uri=r}};async function l(u){if(await O(u),!(u.headers.get("Content-Type")==="application/json"))throw new h("bn2.me did not return a valid JSON response");return u.json()}async function O(u){if(!u.ok){let e;throw u.headers.get("Content-Type")==="application/json"&&(e=(await u.json()).error_message),new h(`bn2.me returned an error: ${e??"Unknown error"}`)}}var f,S,w,A=class{constructor(e,t){this.access_token=e;this.options=t;m(this,f)}user(){return o(this,f,w).call(this,"api/user").then(e=>fetch(e)).then(l)}saveSettings(e){return o(this,f,w).call(this,"api/user/settings",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)}).then(t=>fetch(t)).then(O)}accounts(){return o(this,f,w).call(this,"api/accounts").then(e=>fetch(e)).then(l)}subtoken(e,t){let n=o(this,f,S).call(this,`api/accounts/${e}/subtoken`);return t?.permissions&&n.searchParams.set("permissions",t.permissions.join(",")),o(this,f,w).call(this,n).then(r=>fetch(r)).then(l)}};f=new WeakSet,S=function(e){return new URL(e,this.options?.url||"https://bn2me.vercel.app/")},w=async function(e,t){let n=e instanceof URL?e:o(this,f,S).call(this,e),r=this.options?.dpop,i=new Headers(t?.headers);return i.set("Authorization",`${r?"DPoP":"Bearer"} ${this.access_token}`),r&&i.set("DPoP",await r({htm:t?.method||"GET",htu:n.toString(),accessToken:this.access_token})),new Request(n,{cache:"no-cache",...t,headers:i})};var x,R,v=class{constructor(e,t){m(this,x);m(this,R);_(this,x,e),_(this,R,t)}isSupported(){return typeof window<"u"&&"IdentityCredential"in window}request({scopes:e,mediation:t,signal:n,mode:r,code_challenge:i,code_challenge_method:d}){if(!this.isSupported())throw new h("FedCM is not supported");return navigator.credentials.get({mediation:t,signal:n,identity:{providers:[{configURL:s(this,x),clientId:s(this,R),fields:[e.includes("identify")&&"name",e.includes("email")&&"email"].filter(Boolean),nonce:`${d}:${i}`,params:{scope:e.join(" "),code_challenge:i,code_challenge_method:d}}],mode:r}})}};x=new WeakMap,R=new WeakMap;var c,T,p,g,y,I=class{constructor(e,t){this.options=t;m(this,p);m(this,c);m(this,T);_(this,c,e),_(this,T,new v(o(this,p,g).call(this,"/fed-cm/config.json"),e.client_id))}getAuthorizationUrl(e){let t="request_uri"in e?new URLSearchParams({client_id:s(this,c).client_id,response_type:"code",request_uri:e.request_uri}):z(s(this,c).client_id,e);return o(this,p,g).call(this,`/oauth2/authorize?${t.toString()}`).toString()}async pushAuthorizationRequest(e){let t=o(this,p,g).call(this,"/oauth2/par"),n={"Content-Type":"application/x-www-form-urlencoded"};e.dpop&&(n.DPoP=await e.dpop({htm:"POST",htu:t.toString()}));let r=z(s(this,c).client_id,e);return s(this,c).client_secret&&(n.Authorization=o(this,p,y).call(this)),await fetch(t,{method:"POST",headers:n,body:r,cache:"no-store"}).then(l)}async getAccessToken({code:e,token_type:t,redirect_uri:n,code_verifier:r,dpop:i}){let d=new URLSearchParams({grant_type:"authorization_code",code:e,client_id:s(this,c).client_id,redirect_uri:n}),a={"Content-Type":"application/x-www-form-urlencoded"};s(this,c).client_secret&&(a.Authorization=o(this,p,y).call(this)),r&&d.set("code_verifier",r);let k=o(this,p,g).call(this,"/api/token");return i&&(a.DPoP=await i({htm:"POST",htu:k.toString(),accessToken:t==="DPoP"?e:void 0})),await fetch(k,{method:"POST",headers:a,body:d,cache:"no-store"}).then(l)}async refreshToken({refresh_token:e,refresh_token_type:t,dpop:n}){let r=new URLSearchParams({grant_type:"refresh_token",refresh_token:e,client_id:s(this,c).client_id}),i={"Content-Type":"application/x-www-form-urlencoded"};s(this,c).client_secret&&(i.Authorization=o(this,p,y).call(this));let d=o(this,p,g).call(this,"/api/token");return n&&(i.DPoP=await n({htm:"POST",htu:d.toString(),accessToken:t==="DPoP"?e:void 0})),await fetch(d,{method:"POST",headers:i,body:r,cache:"no-store"}).then(l)}async revokeToken({token:e}){let t=new URLSearchParams({token:e}),n={"Content-Type":"application/x-www-form-urlencoded"};s(this,c).client_secret&&(n.Authorization=o(this,p,y).call(this)),await fetch(o(this,p,g).call(this,"/api/token/revoke"),{method:"POST",cache:"no-store",headers:n,body:t}).then(l)}async introspectToken({token:e}){let t=new URLSearchParams({token:e}),n={"Content-Type":"application/x-www-form-urlencoded"};return s(this,c).client_secret&&(n.Authorization=o(this,p,y).call(this)),await fetch(o(this,p,g).call(this,"/api/token/introspect"),{method:"POST",cache:"no-store",headers:n,body:t}).then(l)}parseAuthorizationResponseSearchParams(e){let t=o(this,p,g).call(this,"/").origin,n=e.get("iss");if(!n)throw new h("Issuer Identifier verification failed: parameter `iss` is missing");if(n!==t)throw new h(`Issuer Identifier verification failed: expected "${t}", got "${n}"`);let r=e.get("error");if(r){let a=e.get("error_description")??void 0,k=e.get("error_uri")??void 0;throw new b(r,a,k)}let i=e.get("code");if(!i)throw new h("Parameter `code` is missing");let d=e.get("state")||void 0;return{code:i,state:d}}api(e,t){return new A(e,{...this.options,...t})}get fedCM(){return s(this,T)}};c=new WeakMap,T=new WeakMap,p=new WeakSet,g=function(e){return new URL(e,this.options?.url||"https://bn2me.vercel.app/")},y=function(){if(!s(this,c).client_secret)throw new h("client_secret is required");return`Basic ${btoa(`${s(this,c).client_id}:${s(this,c).client_secret}`)}`};function z(u,{redirect_uri:e,scopes:t,state:n,code_challenge:r,code_challenge_method:i,dpop_jkt:d,prompt:a,include_granted_scopes:k,verified_accounts_only:C}){let P=new URLSearchParams({client_id:u,response_type:"code",redirect_uri:e,scope:t.join(" ")});return n&&P.append("state",n),r&&i&&(P.append("code_challenge",r),P.append("code_challenge_method",i)),d&&P.append("dpop_jkt",d),a&&P.append("prompt",a),k&&P.append("include_granted_scopes","true"),C&&P.append("verified_accounts_only","true"),P}export{A as Bn2MeApi,I as Bn2MeClient,h as Bn2MeError,b as Bn2MeOAuthError,U as Scope};