@privy-io/js-sdk-core
Advanced tools
Comparing version 0.27.1-beta-20240912214844 to 0.27.1-beta-20240913012159
@@ -1,2 +0,2 @@ | ||
import t from"fetch-retry";import{v4 as e}from"uuid";import{PrivyErrorCode as s}from"@privy-io/api-base";import{getPathWithParams as i,AppConfig as n,AnalyticsEvent as a,RefreshSession as o}from"@privy-io/public-api";import{PrivyApiError as r}from"./Error.js";import{Session as h}from"./Session.js";import c from"./toAbortSignalTimeout.js";import"eventemitter3";import"js-cookie";import"./Token.js";import"jose";const l="privy:caid";class d{baseUrl;_config;appId;appClientId;_storage;_analyticsId;_sdkVersion="js-sdk-core:0.27.1-beta-20240912214844";_fetch;nativeAppIdentifier;callbacks;_cache=new Map;session;constructor(e){this._storage=e.storage,this._analyticsId=null,this._getOrGenerateClientAnalyticsId(),this.baseUrl=e.baseUrl??"https://auth.privy.io",this.appId=e.appId,this.appClientId=e.appClientId,this._sdkVersion=e.sdkVersion??this._sdkVersion,this.callbacks=e.callbacks,"undefined"==typeof document&&(this.nativeAppIdentifier=e.nativeAppIdentifier),this.session=new h({storage:this._storage,isUsingServerCookies:!1}),this._fetch=t(fetch,{retries:3,retryDelay:500})}get isReady(){return Boolean(this._config)}get config(){return this._config}get caid(){return this._analyticsId}async _initialize(){this.isReady||(this._config=await this.getAppConfig(),this._config?.custom_api_url&&(this.baseUrl=this._config.custom_api_url,this.session.isUsingServerCookies=!0),this.callbacks?.setIsReady?.(!0),this.createAnalyticsEvent("sdk_initialize",{}))}async fetch(t,{body:e,params:s,headers:n,options:a={onRequest:this._beforeRequest.bind(this)}}){const o=`${this.baseUrl}${i(t.path,s)}`,h=new Request(o,{method:t.method,body:JSON.stringify(e),headers:n}),c=await a.onRequest(h),l=await this._fetch(h,c),d=await l.json();if(l.status>299)throw new r(d);return d}async _beforeRequestWithoutInitialize(t){const e=await this.session.getToken(),s=new Headers(t.headers);s.set("privy-app-id",this.appId),this.appClientId&&s.set("privy-client-id",this.appClientId),s.set("privy-client",this._sdkVersion),s.set("Authorization",`Bearer ${e}`),s.set("Content-Type","application/json"),s.set("Accept","application/json");const i=await this._getOrGenerateClientAnalyticsId();return i&&s.set("privy-ca-id",i),this.nativeAppIdentifier&&s.set("x-native-app-identifier",this.nativeAppIdentifier),{signal:c(2e4),headers:s,credentials:"include"}}async beforeRequestWithoutRefresh(t){return await this._initialize(),this._beforeRequestWithoutInitialize(t)}async _beforeRequest(t){return await this._initialize(),await this.getAccessToken(),this.beforeRequestWithoutRefresh(t)}async getAppConfig(){return await this.fetch(n,{params:{app_id:this.appId},options:{onRequest:this._beforeRequestWithoutInitialize.bind(this)}})}async _getOrGenerateClientAnalyticsId(){if(this._analyticsId)return this._analyticsId;try{const t=await this._storage.get(l);if("string"==typeof t&&t.length>0)return this._analyticsId=t,t}catch(t){console.error("Unable to load clientId",t)}try{this._analyticsId=e()}catch(t){console.error("Unable to generate uuidv4",t)}if(!this._analyticsId)return null;try{await this._storage.put(l,this._analyticsId)}catch(t){console.error(`Unable to store clientId: ${this._analyticsId}`,t)}return this._analyticsId}async destroyClientAnalyticsId(){try{return await this._storage.del(l)}catch(t){console.error("Unable to delete clientId",t)}}async createAnalyticsEvent(t,e){try{await this.fetch(a,{body:{event_name:t,client_id:await this._getOrGenerateClientAnalyticsId(),payload:e},options:{onRequest:this.beforeRequestWithoutRefresh.bind(this)}})}catch(t){}}async refreshSession(){const t=await this.session.getRefreshToken()??void 0,e=t??"key",s=this._cache.get(e);if(s)return await s;const i=this._refreshSession(t);this._cache.set(e,i);try{return await i}catch(t){throw t}finally{this._cache.delete(e)}}async _refreshSession(t){const e=await this.session.getToken();if(!this.session.hasRefreshCredentials(e,t??null))throw await this._initialize(),new Error("missing_or_invalid_token");try{const{session_update_action:e,...s}=await this.fetch(o,{body:{refresh_token:t},options:{onRequest:this.beforeRequestWithoutRefresh.bind(this)}});return this.callbacks?.setUser?.(s.user),"set"===e&&await Promise.all([this.session.storeToken(s.token),this.session.storeRefreshToken(s.refresh_token),this.session.storeIdentityToken(s.identity_token)]),"clear"===e&&(await this.session.destroyLocalState(),this.callbacks?.setUser?.(null)),"ignore"===e&&s.token&&(await this.session.storeToken(s.token),s.identity_token&&await this.session.storeIdentityToken(s.identity_token)),s}catch(t){throw(t instanceof r&&t.code===s.MISSING_OR_INVALID_TOKEN||t instanceof Error&&"missing_or_invalid_token"===t.message)&&(await this.session.destroyLocalState(),this.callbacks?.setUser?.(null)),t}}async getAccessToken(){const[t,e]=await Promise.all([this.session.getToken(),this.session.getRefreshToken()]);if(!this.session.tokenIsActive(t)&&this.session.hasRefreshCredentials(t,e))try{return await this.refreshSession(),await this.session.getToken()}catch(t){return null}return t}async getIdentityToken(){return await this.session.getIdentityToken()}}export{d as PrivyInternal}; | ||
import t from"fetch-retry";import{v4 as e}from"uuid";import{PrivyErrorCode as s}from"@privy-io/api-base";import{getPathWithParams as i,AppConfig as n,AnalyticsEvent as a,RefreshSession as o}from"@privy-io/public-api";import{PrivyApiError as r}from"./Error.js";import{Session as h}from"./Session.js";import c from"./toAbortSignalTimeout.js";import"eventemitter3";import"js-cookie";import"./Token.js";import"jose";const l="privy:caid";class d{baseUrl;_config;appId;appClientId;_storage;_analyticsId;_sdkVersion="js-sdk-core:0.27.1-beta-20240913012159";_fetch;nativeAppIdentifier;callbacks;_cache=new Map;session;constructor(e){this._storage=e.storage,this._analyticsId=null,this._getOrGenerateClientAnalyticsId(),this.baseUrl=e.baseUrl??"https://auth.privy.io",this.appId=e.appId,this.appClientId=e.appClientId,this._sdkVersion=e.sdkVersion??this._sdkVersion,this.callbacks=e.callbacks,"undefined"==typeof document&&(this.nativeAppIdentifier=e.nativeAppIdentifier),this.session=new h({storage:this._storage,isUsingServerCookies:!1}),this._fetch=t(fetch,{retries:3,retryDelay:500})}get isReady(){return Boolean(this._config)}get config(){return this._config}get caid(){return this._analyticsId}async _initialize(){this.isReady||(this._config=await this.getAppConfig(),this._config?.custom_api_url&&(this.baseUrl=this._config.custom_api_url,this.session.isUsingServerCookies=!0),this.callbacks?.setIsReady?.(!0),this.createAnalyticsEvent("sdk_initialize",{}))}async fetch(t,{body:e,params:s,headers:n,options:a={onRequest:this._beforeRequest.bind(this)}}){const o=`${this.baseUrl}${i(t.path,s)}`,h=new Request(o,{method:t.method,body:JSON.stringify(e),headers:n}),c=await a.onRequest(h),l=await this._fetch(h,c),d=await l.json();if(l.status>299)throw new r(d);return d}async _beforeRequestWithoutInitialize(t){const e=await this.session.getToken(),s=new Headers(t.headers);s.set("privy-app-id",this.appId),this.appClientId&&s.set("privy-client-id",this.appClientId),s.set("privy-client",this._sdkVersion),s.set("Authorization",`Bearer ${e}`),s.set("Content-Type","application/json"),s.set("Accept","application/json");const i=await this._getOrGenerateClientAnalyticsId();return i&&s.set("privy-ca-id",i),this.nativeAppIdentifier&&s.set("x-native-app-identifier",this.nativeAppIdentifier),{signal:c(2e4),headers:s,credentials:"include"}}async beforeRequestWithoutRefresh(t){return await this._initialize(),this._beforeRequestWithoutInitialize(t)}async _beforeRequest(t){return await this._initialize(),await this.getAccessToken(),this.beforeRequestWithoutRefresh(t)}async getAppConfig(){return await this.fetch(n,{params:{app_id:this.appId},options:{onRequest:this._beforeRequestWithoutInitialize.bind(this)}})}async _getOrGenerateClientAnalyticsId(){if(this._analyticsId)return this._analyticsId;try{const t=await this._storage.get(l);if("string"==typeof t&&t.length>0)return this._analyticsId=t,t}catch(t){console.error("Unable to load clientId",t)}try{this._analyticsId=e()}catch(t){console.error("Unable to generate uuidv4",t)}if(!this._analyticsId)return null;try{await this._storage.put(l,this._analyticsId)}catch(t){console.error(`Unable to store clientId: ${this._analyticsId}`,t)}return this._analyticsId}async destroyClientAnalyticsId(){try{return await this._storage.del(l)}catch(t){console.error("Unable to delete clientId",t)}}async createAnalyticsEvent(t,e){try{await this.fetch(a,{body:{event_name:t,client_id:await this._getOrGenerateClientAnalyticsId(),payload:e},options:{onRequest:this.beforeRequestWithoutRefresh.bind(this)}})}catch(t){}}async refreshSession(){const t=await this.session.getRefreshToken()??void 0,e=t??"key",s=this._cache.get(e);if(s)return await s;const i=this._refreshSession(t);this._cache.set(e,i);try{return await i}catch(t){throw t}finally{this._cache.delete(e)}}async _refreshSession(t){const e=await this.session.getToken();if(!this.session.hasRefreshCredentials(e,t??null))throw await this._initialize(),new Error("missing_or_invalid_token");try{const{session_update_action:e,...s}=await this.fetch(o,{body:{refresh_token:t},options:{onRequest:this.beforeRequestWithoutRefresh.bind(this)}});return this.callbacks?.setUser?.(s.user),"set"===e&&await Promise.all([this.session.storeToken(s.token),this.session.storeRefreshToken(s.refresh_token),this.session.storeIdentityToken(s.identity_token)]),"clear"===e&&(await this.session.destroyLocalState(),this.callbacks?.setUser?.(null)),"ignore"===e&&s.token&&(await this.session.storeToken(s.token),s.identity_token&&await this.session.storeIdentityToken(s.identity_token)),s}catch(t){throw(t instanceof r&&t.code===s.MISSING_OR_INVALID_TOKEN||t instanceof Error&&"missing_or_invalid_token"===t.message)&&(await this.session.destroyLocalState(),this.callbacks?.setUser?.(null)),t}}async getAccessToken(){const[t,e]=await Promise.all([this.session.getToken(),this.session.getRefreshToken()]);if(!this.session.tokenIsActive(t)&&this.session.hasRefreshCredentials(t,e))try{return await this.refreshSession(),await this.session.getToken()}catch(t){return null}return t}async getIdentityToken(){return await this.session.getIdentityToken()}}export{d as PrivyInternal}; | ||
//# sourceMappingURL=PrivyInternal.js.map |
{ | ||
"name": "@privy-io/js-sdk-core", | ||
"version": "0.27.1-beta-20240912214844", | ||
"version": "0.27.1-beta-20240913012159", | ||
"description": "Vanilla JS client for the Privy Auth API", | ||
@@ -69,3 +69,3 @@ "sideEffects": false, | ||
"@privy-io/api-base": "^1.2.3", | ||
"@privy-io/public-api": "2.9.5-beta-20240912214844", | ||
"@privy-io/public-api": "2.9.5-beta-20240913012159", | ||
"eventemitter3": "^5.0.1", | ||
@@ -72,0 +72,0 @@ "fetch-retry": "^5.0.6", |
Sorry, the diff of this file is too big to display
+ Added@privy-io/public-api@2.9.5-beta-20240913012159(transitive)
- Removed@privy-io/public-api@2.9.5-beta-20240912214844(transitive)