@privy-io/js-sdk-core 0.56.3 → 0.56.4
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e=require("fetch-retry"),t=require("uuid"),s=require("@privy-io/api-base"),i=require("@privy-io/public-api"),r=require("../Error.js"),a=require("../Session.js"),n=require("../toAbortSignalTimeout.js"),o=require("../utils/toSearchParams.js"),h=require("./logger.js");function c(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}require("eventemitter3"),require("jose"),require("js-cookie"),require("../Token.js"),require("../utils/allSettled.js"),require("../utils/noop.js");var l=/*#__PURE__*/c(e);let g="privy:caid";exports.PrivyInternal=class{setCallbacks(e){this.callbacks={...this.callbacks,...e}}get isReady(){return!!this._config}get config(){return this._config}get caid(){return this._analyticsId}async _initialize(){if(this.isReady)this.callbacks?.setIsReady?.(!0);else{if(!await this.isStorageAccessible())throw new r.PrivyClientError({code:"storage_error",error:"Unable to access storage"});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._sdkVersion.startsWith("react-auth:")||this.createAnalyticsEvent("sdk_initialize",{})}}getPath(e,{params:t,query:s}){return`${this.baseUrl}${i.getPathWithParams(e.path,t)}${o.toSearchParams(s)}`}async fetch(e,{body:t,params:s,query:i,headers:a,onRequest:n=this._beforeRequest.bind(this)}){let o=new Request(this.getPath(e,{params:s,query:i}),{method:e.method,body:JSON.stringify(t),headers:a}),h=await n(o),c=await this._fetch(o,h),l=await c.json();if(c.status>299)throw new r.PrivyApiError(l);return l}async _beforeRequestWithoutInitialize(e){let t=await this.session.getPrivyAccessToken()??await this.session.getCustomerAccessToken(),s=new Headers(e.headers);s.set("privy-app-id",this.appId),this.appClientId&&s.set("privy-client-id",this.appClientId),s.set("privy-client",this._sdkVersion),t&&s.set("Authorization",`Bearer ${t}`),s.set("Content-Type","application/json"),s.set("Accept","application/json");let i=await this._getOrGenerateClientAnalyticsId();return i&&s.set("privy-ca-id",i),this.nativeAppIdentifier&&s.set("x-native-app-identifier",this.nativeAppIdentifier),{signal:n.default(2e4),headers:s,credentials:"include"}}async beforeRequestWithoutRefresh(e){return await this._initialize(),this._beforeRequestWithoutInitialize(e)}async _beforeRequest(e){return await this._initialize(),await this.getAccessTokenInternal(),this.beforeRequestWithoutRefresh(e)}async getAppConfig(){return await this.fetch(i.AppConfig,{params:{app_id:this.appId},onRequest:this._beforeRequestWithoutInitialize.bind(this)})}async _getOrGenerateClientAnalyticsId(){if(this._analyticsId)return this._analyticsId;try{let e=await this._storage.get(g);if("string"==typeof e&&e.length>0)return this._analyticsId=e,e}catch(e){this.logger.error("Unable to load clientId",e)}try{this._analyticsId=t.v4()}catch(e){this.logger.error("Unable to generate uuidv4",e)}if(!this._analyticsId)return null;try{await this._storage.put(g,this._analyticsId)}catch(e){this.logger.error(`Unable to store clientId: ${this._analyticsId}`,e)}return this._analyticsId}async destroyClientAnalyticsId(){try{return await this._storage.del(g)}catch(e){this.logger.error("Unable to delete clientId",e)}}async createAnalyticsEvent(e,t){try{await this.fetch(i.AnalyticsEvent,{body:{event_name:e,client_id:await this._getOrGenerateClientAnalyticsId(),payload:t},onRequest:this.beforeRequestWithoutRefresh.bind(this)})}catch(e){}}async refreshSession(e=!1){if(!await this.isStorageAccessible())throw new r.PrivyClientError({code:"storage_error",error:"Unable to access storage"});let t=await this.session.getRefreshToken()??void 0,s=t??"key",i=this._cache.get(s);if(i)return this.logger.debug("[privy:refresh] found in-flight session refresh request, deduping"),await i;let a=this._refreshSession(t,e);this._cache.set(s,a);try{return await a}finally{this._cache.delete(s)}}async _refreshSession(e,t){let a=await this.session.getCustomerAccessToken();if(!this.session.hasRefreshCredentials(a,e??null))throw this.logger.debug("[privy:refresh] missing tokens, skipping request"),await this._initialize(),new r.PrivyApiError({code:s.PrivyErrorCode.MISSING_OR_INVALID_TOKEN,error:"No tokens found in storage"});try{this.logger.debug(`[privy:refresh] fetching: ${i.RefreshSession.path}`);let s=await this.fetch(i.RefreshSession,{body:{refresh_token:e},onRequest:this.beforeRequestWithoutRefresh.bind(this)}),r=s.session_update_action;return this.logger.debug(`[privy:refresh] response: ${r}`),t||this.callbacks?.setUser?.(s.user),"set"===r&&(await this.session.updateWithTokensResponse(s),this.logger.debug("[privy:refresh] tokens stored")),"clear"===r&&(await this.session.destroyLocalState(),this.logger.debug("[privy:refresh] tokens cleared"),t||this.callbacks?.setUser?.(null)),"ignore"===r&&s.token&&(await this.session.storeCustomerAccessToken(s.token),await this.session.storePrivyAccessToken(s.privy_access_token),this.logger.debug("[privy:refresh] access token stored"),s.identity_token&&(this.logger.debug("[privy:refresh] identity token stored"),await this.session.storeIdentityToken(s.identity_token))),this.logger.debug("[privy:refresh] returning response"),s}catch(e){throw this.logger.debug(`[privy:refresh] error: ${e.message??"unknown error"}`),e instanceof r.PrivyApiError&&e.code===s.PrivyErrorCode.MISSING_OR_INVALID_TOKEN&&(await this.session.destroyLocalState(),t||this.callbacks?.setUser?.(null)),e}}async getAccessToken(){let[e,t]=await Promise.all([this.session.getCustomerAccessToken(),this.session.getRefreshToken()]);if(!this.session.tokenIsActive(e)&&this.session.hasRefreshCredentials(e,t)){let t=await this.refreshSession(),s=await this.session.getCustomerAccessToken();return t.token||this.logger.debug("[privy:getAccessToken] expected token received null"),t.token===e&&this.logger.debug("[privy:getAccessToken] expected new token in response received existing"),s===e&&this.logger.debug("[privy:getAccessToken] expected new token in storage received existing"),t.token??s}return e}async getAccessTokenInternal(){let e=await this.getAccessToken();return await this.session.getPrivyAccessToken()??e}async getIdentityToken(){return await this.session.getIdentityToken()}async isStorageAccessible(){try{let e=`privy:__storage__test-${t.v4()}`,s="blobby";await this._storage.put(e,s);let i=await this._storage.get(e);return await this._storage.del(e),i===s}catch(e){return this.logger.error(e),!1}}constructor(e){this._sdkVersion="js-sdk-core:0.56.
|
|
1
|
+
"use strict";var e=require("fetch-retry"),t=require("uuid"),s=require("@privy-io/api-base"),i=require("@privy-io/public-api"),r=require("../Error.js"),a=require("../Session.js"),n=require("../toAbortSignalTimeout.js"),o=require("../utils/toSearchParams.js"),h=require("./logger.js");function c(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}require("eventemitter3"),require("jose"),require("js-cookie"),require("../Token.js"),require("../utils/allSettled.js"),require("../utils/noop.js");var l=/*#__PURE__*/c(e);let g="privy:caid";exports.PrivyInternal=class{setCallbacks(e){this.callbacks={...this.callbacks,...e}}get isReady(){return!!this._config}get config(){return this._config}get caid(){return this._analyticsId}async _initialize(){if(this.isReady)this.callbacks?.setIsReady?.(!0);else{if(!await this.isStorageAccessible())throw new r.PrivyClientError({code:"storage_error",error:"Unable to access storage"});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._sdkVersion.startsWith("react-auth:")||this.createAnalyticsEvent("sdk_initialize",{})}}getPath(e,{params:t,query:s}){return`${this.baseUrl}${i.getPathWithParams(e.path,t)}${o.toSearchParams(s)}`}async fetch(e,{body:t,params:s,query:i,headers:a,onRequest:n=this._beforeRequest.bind(this)}){let o=new Request(this.getPath(e,{params:s,query:i}),{method:e.method,body:JSON.stringify(t),headers:a}),h=await n(o),c=await this._fetch(o,h),l=await c.json();if(c.status>299)throw new r.PrivyApiError(l);return l}async _beforeRequestWithoutInitialize(e){let t=await this.session.getPrivyAccessToken()??await this.session.getCustomerAccessToken(),s=new Headers(e.headers);s.set("privy-app-id",this.appId),this.appClientId&&s.set("privy-client-id",this.appClientId),s.set("privy-client",this._sdkVersion),t&&s.set("Authorization",`Bearer ${t}`),s.set("Content-Type","application/json"),s.set("Accept","application/json");let i=await this._getOrGenerateClientAnalyticsId();return i&&s.set("privy-ca-id",i),this.nativeAppIdentifier&&s.set("x-native-app-identifier",this.nativeAppIdentifier),{signal:n.default(2e4),headers:s,credentials:"include"}}async beforeRequestWithoutRefresh(e){return await this._initialize(),this._beforeRequestWithoutInitialize(e)}async _beforeRequest(e){return await this._initialize(),await this.getAccessTokenInternal(),this.beforeRequestWithoutRefresh(e)}async getAppConfig(){return await this.fetch(i.AppConfig,{params:{app_id:this.appId},onRequest:this._beforeRequestWithoutInitialize.bind(this)})}async _getOrGenerateClientAnalyticsId(){if(this._analyticsId)return this._analyticsId;try{let e=await this._storage.get(g);if("string"==typeof e&&e.length>0)return this._analyticsId=e,e}catch(e){this.logger.error("Unable to load clientId",e)}try{this._analyticsId=t.v4()}catch(e){this.logger.error("Unable to generate uuidv4",e)}if(!this._analyticsId)return null;try{await this._storage.put(g,this._analyticsId)}catch(e){this.logger.error(`Unable to store clientId: ${this._analyticsId}`,e)}return this._analyticsId}async destroyClientAnalyticsId(){try{return await this._storage.del(g)}catch(e){this.logger.error("Unable to delete clientId",e)}}async createAnalyticsEvent(e,t){try{await this.fetch(i.AnalyticsEvent,{body:{event_name:e,client_id:await this._getOrGenerateClientAnalyticsId(),payload:t},onRequest:this.beforeRequestWithoutRefresh.bind(this)})}catch(e){}}async refreshSession(e=!1){if(!await this.isStorageAccessible())throw new r.PrivyClientError({code:"storage_error",error:"Unable to access storage"});let t=await this.session.getRefreshToken()??void 0,s=t??"key",i=this._cache.get(s);if(i)return this.logger.debug("[privy:refresh] found in-flight session refresh request, deduping"),await i;let a=this._refreshSession(t,e);this._cache.set(s,a);try{return await a}finally{this._cache.delete(s)}}async _refreshSession(e,t){let a=await this.session.getCustomerAccessToken();if(!this.session.hasRefreshCredentials(a,e??null))throw this.logger.debug("[privy:refresh] missing tokens, skipping request"),await this._initialize(),new r.PrivyApiError({code:s.PrivyErrorCode.MISSING_OR_INVALID_TOKEN,error:"No tokens found in storage"});try{this.logger.debug(`[privy:refresh] fetching: ${i.RefreshSession.path}`);let s=await this.fetch(i.RefreshSession,{body:{refresh_token:e},onRequest:this.beforeRequestWithoutRefresh.bind(this)}),r=s.session_update_action;return this.logger.debug(`[privy:refresh] response: ${r}`),t||this.callbacks?.setUser?.(s.user),"set"===r&&(await this.session.updateWithTokensResponse(s),this.logger.debug("[privy:refresh] tokens stored")),"clear"===r&&(await this.session.destroyLocalState(),this.logger.debug("[privy:refresh] tokens cleared"),t||this.callbacks?.setUser?.(null)),"ignore"===r&&s.token&&(await this.session.storeCustomerAccessToken(s.token),await this.session.storePrivyAccessToken(s.privy_access_token),this.logger.debug("[privy:refresh] access token stored"),s.identity_token&&(this.logger.debug("[privy:refresh] identity token stored"),await this.session.storeIdentityToken(s.identity_token))),this.logger.debug("[privy:refresh] returning response"),s}catch(e){throw this.logger.debug(`[privy:refresh] error: ${e.message??"unknown error"}`),e instanceof r.PrivyApiError&&e.code===s.PrivyErrorCode.MISSING_OR_INVALID_TOKEN&&(await this.session.destroyLocalState(),t||this.callbacks?.setUser?.(null)),e}}async getAccessToken(){let[e,t]=await Promise.all([this.session.getCustomerAccessToken(),this.session.getRefreshToken()]);if(!this.session.tokenIsActive(e)&&this.session.hasRefreshCredentials(e,t)){let t=await this.refreshSession(),s=await this.session.getCustomerAccessToken();return t.token||this.logger.debug("[privy:getAccessToken] expected token received null"),t.token===e&&this.logger.debug("[privy:getAccessToken] expected new token in response received existing"),s===e&&this.logger.debug("[privy:getAccessToken] expected new token in storage received existing"),t.token??s}return e}async getAccessTokenInternal(){let e=await this.getAccessToken();return await this.session.getPrivyAccessToken()??e}async getIdentityToken(){return await this.session.getIdentityToken()}async isStorageAccessible(){try{let e=`privy:__storage__test-${t.v4()}`,s="blobby";await this._storage.put(e,s);let i=await this._storage.get(e);return await this._storage.del(e),i===s}catch(e){return this.logger.error(e),!1}}constructor(e){this._sdkVersion="js-sdk-core:0.56.4",this._cache=new Map,this.logger=h.toConsoleLogger({level:e.logLevel??"ERROR"}),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,this.nativeAppIdentifier=e.nativeAppIdentifier,this.session=new a.Session({storage:this._storage,isUsingServerCookies:!1,appId:e.appId}),this._fetch=l.default(globalThis.fetch,{retries:3,retryDelay:e=>3**e*500,retryOn:[408,409,425,500,502,503,504]}),this.session.on("error_storing_tokens",(e=>{this.createAnalyticsEvent("error_updating_tokens_in_storage",{reason:e})}))}};
|
package/dist/dts/index.d.mts
CHANGED
|
@@ -1095,8 +1095,8 @@ declare class EmbeddedWalletApi {
|
|
|
1095
1095
|
created_with_device?: string | undefined;
|
|
1096
1096
|
authenticator_name?: string | undefined;
|
|
1097
1097
|
} | {
|
|
1098
|
+
firstName?: string | null | undefined;
|
|
1098
1099
|
telegramUserId: string;
|
|
1099
|
-
firstName: string | null | undefined;
|
|
1100
1100
|
type: "telegram";
|
|
1101
1101
|
verified_at: number;
|
|
1102
1102
|
first_verified_at: number | null;
|
|
@@ -1553,8 +1553,8 @@ declare class UserApi {
|
|
|
1553
1553
|
created_with_device?: string | undefined;
|
|
1554
1554
|
authenticator_name?: string | undefined;
|
|
1555
1555
|
} | {
|
|
1556
|
+
firstName?: string | null | undefined;
|
|
1556
1557
|
telegramUserId: string;
|
|
1557
|
-
firstName: string | null | undefined;
|
|
1558
1558
|
type: "telegram";
|
|
1559
1559
|
verified_at: number;
|
|
1560
1560
|
first_verified_at: number | null;
|
|
@@ -1908,8 +1908,8 @@ declare class EmailApi {
|
|
|
1908
1908
|
created_with_device?: string | undefined;
|
|
1909
1909
|
authenticator_name?: string | undefined;
|
|
1910
1910
|
} | {
|
|
1911
|
+
firstName?: string | null | undefined;
|
|
1911
1912
|
telegramUserId: string;
|
|
1912
|
-
firstName: string | null | undefined;
|
|
1913
1913
|
type: "telegram";
|
|
1914
1914
|
verified_at: number;
|
|
1915
1915
|
first_verified_at: number | null;
|
|
@@ -2228,8 +2228,8 @@ declare class EmailApi {
|
|
|
2228
2228
|
created_with_device?: string | undefined;
|
|
2229
2229
|
authenticator_name?: string | undefined;
|
|
2230
2230
|
} | {
|
|
2231
|
+
firstName?: string | null | undefined;
|
|
2231
2232
|
telegramUserId: string;
|
|
2232
|
-
firstName: string | null | undefined;
|
|
2233
2233
|
type: "telegram";
|
|
2234
2234
|
verified_at: number;
|
|
2235
2235
|
first_verified_at: number | null;
|
|
@@ -2537,8 +2537,8 @@ declare class EmailApi {
|
|
|
2537
2537
|
created_with_device?: string | undefined;
|
|
2538
2538
|
authenticator_name?: string | undefined;
|
|
2539
2539
|
} | {
|
|
2540
|
+
firstName?: string | null | undefined;
|
|
2540
2541
|
telegramUserId: string;
|
|
2541
|
-
firstName: string | null | undefined;
|
|
2542
2542
|
type: "telegram";
|
|
2543
2543
|
verified_at: number;
|
|
2544
2544
|
first_verified_at: number | null;
|
|
@@ -2881,8 +2881,8 @@ declare class FarcasterApi {
|
|
|
2881
2881
|
created_with_device?: string | undefined;
|
|
2882
2882
|
authenticator_name?: string | undefined;
|
|
2883
2883
|
} | {
|
|
2884
|
+
firstName?: string | null | undefined;
|
|
2884
2885
|
telegramUserId: string;
|
|
2885
|
-
firstName: string | null | undefined;
|
|
2886
2886
|
type: "telegram";
|
|
2887
2887
|
verified_at: number;
|
|
2888
2888
|
first_verified_at: number | null;
|
|
@@ -3195,8 +3195,8 @@ declare class FarcasterApi {
|
|
|
3195
3195
|
created_with_device?: string | undefined;
|
|
3196
3196
|
authenticator_name?: string | undefined;
|
|
3197
3197
|
} | {
|
|
3198
|
+
firstName?: string | null | undefined;
|
|
3198
3199
|
telegramUserId: string;
|
|
3199
|
-
firstName: string | null | undefined;
|
|
3200
3200
|
type: "telegram";
|
|
3201
3201
|
verified_at: number;
|
|
3202
3202
|
first_verified_at: number | null;
|
|
@@ -3557,8 +3557,8 @@ declare class OAuthApi {
|
|
|
3557
3557
|
created_with_device?: string | undefined;
|
|
3558
3558
|
authenticator_name?: string | undefined;
|
|
3559
3559
|
} | {
|
|
3560
|
+
firstName?: string | null | undefined;
|
|
3560
3561
|
telegramUserId: string;
|
|
3561
|
-
firstName: string | null | undefined;
|
|
3562
3562
|
type: "telegram";
|
|
3563
3563
|
verified_at: number;
|
|
3564
3564
|
first_verified_at: number | null;
|
|
@@ -3872,8 +3872,8 @@ declare class OAuthApi {
|
|
|
3872
3872
|
created_with_device?: string | undefined;
|
|
3873
3873
|
authenticator_name?: string | undefined;
|
|
3874
3874
|
} | {
|
|
3875
|
+
firstName?: string | null | undefined;
|
|
3875
3876
|
telegramUserId: string;
|
|
3876
|
-
firstName: string | null | undefined;
|
|
3877
3877
|
type: "telegram";
|
|
3878
3878
|
verified_at: number;
|
|
3879
3879
|
first_verified_at: number | null;
|
|
@@ -4307,8 +4307,8 @@ declare class PasskeyApi {
|
|
|
4307
4307
|
created_with_device?: string | undefined;
|
|
4308
4308
|
authenticator_name?: string | undefined;
|
|
4309
4309
|
} | {
|
|
4310
|
+
firstName?: string | null | undefined;
|
|
4310
4311
|
telegramUserId: string;
|
|
4311
|
-
firstName: string | null | undefined;
|
|
4312
4312
|
type: "telegram";
|
|
4313
4313
|
verified_at: number;
|
|
4314
4314
|
first_verified_at: number | null;
|
|
@@ -4642,8 +4642,8 @@ declare class PhoneApi {
|
|
|
4642
4642
|
created_with_device?: string | undefined;
|
|
4643
4643
|
authenticator_name?: string | undefined;
|
|
4644
4644
|
} | {
|
|
4645
|
+
firstName?: string | null | undefined;
|
|
4645
4646
|
telegramUserId: string;
|
|
4646
|
-
firstName: string | null | undefined;
|
|
4647
4647
|
type: "telegram";
|
|
4648
4648
|
verified_at: number;
|
|
4649
4649
|
first_verified_at: number | null;
|
|
@@ -4962,8 +4962,8 @@ declare class PhoneApi {
|
|
|
4962
4962
|
created_with_device?: string | undefined;
|
|
4963
4963
|
authenticator_name?: string | undefined;
|
|
4964
4964
|
} | {
|
|
4965
|
+
firstName?: string | null | undefined;
|
|
4965
4966
|
telegramUserId: string;
|
|
4966
|
-
firstName: string | null | undefined;
|
|
4967
4967
|
type: "telegram";
|
|
4968
4968
|
verified_at: number;
|
|
4969
4969
|
first_verified_at: number | null;
|
|
@@ -5271,8 +5271,8 @@ declare class PhoneApi {
|
|
|
5271
5271
|
created_with_device?: string | undefined;
|
|
5272
5272
|
authenticator_name?: string | undefined;
|
|
5273
5273
|
} | {
|
|
5274
|
+
firstName?: string | null | undefined;
|
|
5274
5275
|
telegramUserId: string;
|
|
5275
|
-
firstName: string | null | undefined;
|
|
5276
5276
|
type: "telegram";
|
|
5277
5277
|
verified_at: number;
|
|
5278
5278
|
first_verified_at: number | null;
|
|
@@ -5651,8 +5651,8 @@ declare class SiweApi {
|
|
|
5651
5651
|
created_with_device?: string | undefined;
|
|
5652
5652
|
authenticator_name?: string | undefined;
|
|
5653
5653
|
} | {
|
|
5654
|
+
firstName?: string | null | undefined;
|
|
5654
5655
|
telegramUserId: string;
|
|
5655
|
-
firstName: string | null | undefined;
|
|
5656
5656
|
type: "telegram";
|
|
5657
5657
|
verified_at: number;
|
|
5658
5658
|
first_verified_at: number | null;
|
|
@@ -5977,8 +5977,8 @@ declare class SiweApi {
|
|
|
5977
5977
|
created_with_device?: string | undefined;
|
|
5978
5978
|
authenticator_name?: string | undefined;
|
|
5979
5979
|
} | {
|
|
5980
|
+
firstName?: string | null | undefined;
|
|
5980
5981
|
telegramUserId: string;
|
|
5981
|
-
firstName: string | null | undefined;
|
|
5982
5982
|
type: "telegram";
|
|
5983
5983
|
verified_at: number;
|
|
5984
5984
|
first_verified_at: number | null;
|
|
@@ -6336,8 +6336,8 @@ declare class SiwsApi {
|
|
|
6336
6336
|
created_with_device?: string | undefined;
|
|
6337
6337
|
authenticator_name?: string | undefined;
|
|
6338
6338
|
} | {
|
|
6339
|
+
firstName?: string | null | undefined;
|
|
6339
6340
|
telegramUserId: string;
|
|
6340
|
-
firstName: string | null | undefined;
|
|
6341
6341
|
type: "telegram";
|
|
6342
6342
|
verified_at: number;
|
|
6343
6343
|
first_verified_at: number | null;
|
|
@@ -6667,8 +6667,8 @@ declare class SiwsApi {
|
|
|
6667
6667
|
created_with_device?: string | undefined;
|
|
6668
6668
|
authenticator_name?: string | undefined;
|
|
6669
6669
|
} | {
|
|
6670
|
+
firstName?: string | null | undefined;
|
|
6670
6671
|
telegramUserId: string;
|
|
6671
|
-
firstName: string | null | undefined;
|
|
6672
6672
|
type: "telegram";
|
|
6673
6673
|
verified_at: number;
|
|
6674
6674
|
first_verified_at: number | null;
|
|
@@ -7033,8 +7033,8 @@ declare class SmartWalletApi {
|
|
|
7033
7033
|
created_with_device?: string | undefined;
|
|
7034
7034
|
authenticator_name?: string | undefined;
|
|
7035
7035
|
} | {
|
|
7036
|
+
firstName?: string | null | undefined;
|
|
7036
7037
|
telegramUserId: string;
|
|
7037
|
-
firstName: string | null | undefined;
|
|
7038
7038
|
type: "telegram";
|
|
7039
7039
|
verified_at: number;
|
|
7040
7040
|
first_verified_at: number | null;
|
|
@@ -8139,8 +8139,8 @@ declare const delegateWallet: (client: Privy) => ({ address, chainType }: Delega
|
|
|
8139
8139
|
created_with_device?: string | undefined;
|
|
8140
8140
|
authenticator_name?: string | undefined;
|
|
8141
8141
|
} | {
|
|
8142
|
+
firstName?: string | null | undefined;
|
|
8142
8143
|
telegramUserId: string;
|
|
8143
|
-
firstName: string | null | undefined;
|
|
8144
8144
|
type: "telegram";
|
|
8145
8145
|
verified_at: number;
|
|
8146
8146
|
first_verified_at: number | null;
|
|
@@ -8456,8 +8456,8 @@ declare const revokeWallets: (client: Privy) => () => Promise<{
|
|
|
8456
8456
|
created_with_device?: string | undefined;
|
|
8457
8457
|
authenticator_name?: string | undefined;
|
|
8458
8458
|
} | {
|
|
8459
|
+
firstName?: string | null | undefined;
|
|
8459
8460
|
telegramUserId: string;
|
|
8460
|
-
firstName: string | null | undefined;
|
|
8461
8461
|
type: "telegram";
|
|
8462
8462
|
verified_at: number;
|
|
8463
8463
|
first_verified_at: number | null;
|
package/dist/dts/index.d.ts
CHANGED
|
@@ -1095,8 +1095,8 @@ declare class EmbeddedWalletApi {
|
|
|
1095
1095
|
created_with_device?: string | undefined;
|
|
1096
1096
|
authenticator_name?: string | undefined;
|
|
1097
1097
|
} | {
|
|
1098
|
+
firstName?: string | null | undefined;
|
|
1098
1099
|
telegramUserId: string;
|
|
1099
|
-
firstName: string | null | undefined;
|
|
1100
1100
|
type: "telegram";
|
|
1101
1101
|
verified_at: number;
|
|
1102
1102
|
first_verified_at: number | null;
|
|
@@ -1553,8 +1553,8 @@ declare class UserApi {
|
|
|
1553
1553
|
created_with_device?: string | undefined;
|
|
1554
1554
|
authenticator_name?: string | undefined;
|
|
1555
1555
|
} | {
|
|
1556
|
+
firstName?: string | null | undefined;
|
|
1556
1557
|
telegramUserId: string;
|
|
1557
|
-
firstName: string | null | undefined;
|
|
1558
1558
|
type: "telegram";
|
|
1559
1559
|
verified_at: number;
|
|
1560
1560
|
first_verified_at: number | null;
|
|
@@ -1908,8 +1908,8 @@ declare class EmailApi {
|
|
|
1908
1908
|
created_with_device?: string | undefined;
|
|
1909
1909
|
authenticator_name?: string | undefined;
|
|
1910
1910
|
} | {
|
|
1911
|
+
firstName?: string | null | undefined;
|
|
1911
1912
|
telegramUserId: string;
|
|
1912
|
-
firstName: string | null | undefined;
|
|
1913
1913
|
type: "telegram";
|
|
1914
1914
|
verified_at: number;
|
|
1915
1915
|
first_verified_at: number | null;
|
|
@@ -2228,8 +2228,8 @@ declare class EmailApi {
|
|
|
2228
2228
|
created_with_device?: string | undefined;
|
|
2229
2229
|
authenticator_name?: string | undefined;
|
|
2230
2230
|
} | {
|
|
2231
|
+
firstName?: string | null | undefined;
|
|
2231
2232
|
telegramUserId: string;
|
|
2232
|
-
firstName: string | null | undefined;
|
|
2233
2233
|
type: "telegram";
|
|
2234
2234
|
verified_at: number;
|
|
2235
2235
|
first_verified_at: number | null;
|
|
@@ -2537,8 +2537,8 @@ declare class EmailApi {
|
|
|
2537
2537
|
created_with_device?: string | undefined;
|
|
2538
2538
|
authenticator_name?: string | undefined;
|
|
2539
2539
|
} | {
|
|
2540
|
+
firstName?: string | null | undefined;
|
|
2540
2541
|
telegramUserId: string;
|
|
2541
|
-
firstName: string | null | undefined;
|
|
2542
2542
|
type: "telegram";
|
|
2543
2543
|
verified_at: number;
|
|
2544
2544
|
first_verified_at: number | null;
|
|
@@ -2881,8 +2881,8 @@ declare class FarcasterApi {
|
|
|
2881
2881
|
created_with_device?: string | undefined;
|
|
2882
2882
|
authenticator_name?: string | undefined;
|
|
2883
2883
|
} | {
|
|
2884
|
+
firstName?: string | null | undefined;
|
|
2884
2885
|
telegramUserId: string;
|
|
2885
|
-
firstName: string | null | undefined;
|
|
2886
2886
|
type: "telegram";
|
|
2887
2887
|
verified_at: number;
|
|
2888
2888
|
first_verified_at: number | null;
|
|
@@ -3195,8 +3195,8 @@ declare class FarcasterApi {
|
|
|
3195
3195
|
created_with_device?: string | undefined;
|
|
3196
3196
|
authenticator_name?: string | undefined;
|
|
3197
3197
|
} | {
|
|
3198
|
+
firstName?: string | null | undefined;
|
|
3198
3199
|
telegramUserId: string;
|
|
3199
|
-
firstName: string | null | undefined;
|
|
3200
3200
|
type: "telegram";
|
|
3201
3201
|
verified_at: number;
|
|
3202
3202
|
first_verified_at: number | null;
|
|
@@ -3557,8 +3557,8 @@ declare class OAuthApi {
|
|
|
3557
3557
|
created_with_device?: string | undefined;
|
|
3558
3558
|
authenticator_name?: string | undefined;
|
|
3559
3559
|
} | {
|
|
3560
|
+
firstName?: string | null | undefined;
|
|
3560
3561
|
telegramUserId: string;
|
|
3561
|
-
firstName: string | null | undefined;
|
|
3562
3562
|
type: "telegram";
|
|
3563
3563
|
verified_at: number;
|
|
3564
3564
|
first_verified_at: number | null;
|
|
@@ -3872,8 +3872,8 @@ declare class OAuthApi {
|
|
|
3872
3872
|
created_with_device?: string | undefined;
|
|
3873
3873
|
authenticator_name?: string | undefined;
|
|
3874
3874
|
} | {
|
|
3875
|
+
firstName?: string | null | undefined;
|
|
3875
3876
|
telegramUserId: string;
|
|
3876
|
-
firstName: string | null | undefined;
|
|
3877
3877
|
type: "telegram";
|
|
3878
3878
|
verified_at: number;
|
|
3879
3879
|
first_verified_at: number | null;
|
|
@@ -4307,8 +4307,8 @@ declare class PasskeyApi {
|
|
|
4307
4307
|
created_with_device?: string | undefined;
|
|
4308
4308
|
authenticator_name?: string | undefined;
|
|
4309
4309
|
} | {
|
|
4310
|
+
firstName?: string | null | undefined;
|
|
4310
4311
|
telegramUserId: string;
|
|
4311
|
-
firstName: string | null | undefined;
|
|
4312
4312
|
type: "telegram";
|
|
4313
4313
|
verified_at: number;
|
|
4314
4314
|
first_verified_at: number | null;
|
|
@@ -4642,8 +4642,8 @@ declare class PhoneApi {
|
|
|
4642
4642
|
created_with_device?: string | undefined;
|
|
4643
4643
|
authenticator_name?: string | undefined;
|
|
4644
4644
|
} | {
|
|
4645
|
+
firstName?: string | null | undefined;
|
|
4645
4646
|
telegramUserId: string;
|
|
4646
|
-
firstName: string | null | undefined;
|
|
4647
4647
|
type: "telegram";
|
|
4648
4648
|
verified_at: number;
|
|
4649
4649
|
first_verified_at: number | null;
|
|
@@ -4962,8 +4962,8 @@ declare class PhoneApi {
|
|
|
4962
4962
|
created_with_device?: string | undefined;
|
|
4963
4963
|
authenticator_name?: string | undefined;
|
|
4964
4964
|
} | {
|
|
4965
|
+
firstName?: string | null | undefined;
|
|
4965
4966
|
telegramUserId: string;
|
|
4966
|
-
firstName: string | null | undefined;
|
|
4967
4967
|
type: "telegram";
|
|
4968
4968
|
verified_at: number;
|
|
4969
4969
|
first_verified_at: number | null;
|
|
@@ -5271,8 +5271,8 @@ declare class PhoneApi {
|
|
|
5271
5271
|
created_with_device?: string | undefined;
|
|
5272
5272
|
authenticator_name?: string | undefined;
|
|
5273
5273
|
} | {
|
|
5274
|
+
firstName?: string | null | undefined;
|
|
5274
5275
|
telegramUserId: string;
|
|
5275
|
-
firstName: string | null | undefined;
|
|
5276
5276
|
type: "telegram";
|
|
5277
5277
|
verified_at: number;
|
|
5278
5278
|
first_verified_at: number | null;
|
|
@@ -5651,8 +5651,8 @@ declare class SiweApi {
|
|
|
5651
5651
|
created_with_device?: string | undefined;
|
|
5652
5652
|
authenticator_name?: string | undefined;
|
|
5653
5653
|
} | {
|
|
5654
|
+
firstName?: string | null | undefined;
|
|
5654
5655
|
telegramUserId: string;
|
|
5655
|
-
firstName: string | null | undefined;
|
|
5656
5656
|
type: "telegram";
|
|
5657
5657
|
verified_at: number;
|
|
5658
5658
|
first_verified_at: number | null;
|
|
@@ -5977,8 +5977,8 @@ declare class SiweApi {
|
|
|
5977
5977
|
created_with_device?: string | undefined;
|
|
5978
5978
|
authenticator_name?: string | undefined;
|
|
5979
5979
|
} | {
|
|
5980
|
+
firstName?: string | null | undefined;
|
|
5980
5981
|
telegramUserId: string;
|
|
5981
|
-
firstName: string | null | undefined;
|
|
5982
5982
|
type: "telegram";
|
|
5983
5983
|
verified_at: number;
|
|
5984
5984
|
first_verified_at: number | null;
|
|
@@ -6336,8 +6336,8 @@ declare class SiwsApi {
|
|
|
6336
6336
|
created_with_device?: string | undefined;
|
|
6337
6337
|
authenticator_name?: string | undefined;
|
|
6338
6338
|
} | {
|
|
6339
|
+
firstName?: string | null | undefined;
|
|
6339
6340
|
telegramUserId: string;
|
|
6340
|
-
firstName: string | null | undefined;
|
|
6341
6341
|
type: "telegram";
|
|
6342
6342
|
verified_at: number;
|
|
6343
6343
|
first_verified_at: number | null;
|
|
@@ -6667,8 +6667,8 @@ declare class SiwsApi {
|
|
|
6667
6667
|
created_with_device?: string | undefined;
|
|
6668
6668
|
authenticator_name?: string | undefined;
|
|
6669
6669
|
} | {
|
|
6670
|
+
firstName?: string | null | undefined;
|
|
6670
6671
|
telegramUserId: string;
|
|
6671
|
-
firstName: string | null | undefined;
|
|
6672
6672
|
type: "telegram";
|
|
6673
6673
|
verified_at: number;
|
|
6674
6674
|
first_verified_at: number | null;
|
|
@@ -7033,8 +7033,8 @@ declare class SmartWalletApi {
|
|
|
7033
7033
|
created_with_device?: string | undefined;
|
|
7034
7034
|
authenticator_name?: string | undefined;
|
|
7035
7035
|
} | {
|
|
7036
|
+
firstName?: string | null | undefined;
|
|
7036
7037
|
telegramUserId: string;
|
|
7037
|
-
firstName: string | null | undefined;
|
|
7038
7038
|
type: "telegram";
|
|
7039
7039
|
verified_at: number;
|
|
7040
7040
|
first_verified_at: number | null;
|
|
@@ -8139,8 +8139,8 @@ declare const delegateWallet: (client: Privy) => ({ address, chainType }: Delega
|
|
|
8139
8139
|
created_with_device?: string | undefined;
|
|
8140
8140
|
authenticator_name?: string | undefined;
|
|
8141
8141
|
} | {
|
|
8142
|
+
firstName?: string | null | undefined;
|
|
8142
8143
|
telegramUserId: string;
|
|
8143
|
-
firstName: string | null | undefined;
|
|
8144
8144
|
type: "telegram";
|
|
8145
8145
|
verified_at: number;
|
|
8146
8146
|
first_verified_at: number | null;
|
|
@@ -8456,8 +8456,8 @@ declare const revokeWallets: (client: Privy) => () => Promise<{
|
|
|
8456
8456
|
created_with_device?: string | undefined;
|
|
8457
8457
|
authenticator_name?: string | undefined;
|
|
8458
8458
|
} | {
|
|
8459
|
+
firstName?: string | null | undefined;
|
|
8459
8460
|
telegramUserId: string;
|
|
8460
|
-
firstName: string | null | undefined;
|
|
8461
8461
|
type: "telegram";
|
|
8462
8462
|
verified_at: number;
|
|
8463
8463
|
first_verified_at: number | null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import e from"fetch-retry";import{v4 as t}from"uuid";import{PrivyErrorCode as s}from"@privy-io/api-base";import{getPathWithParams as i,AppConfig as r,AnalyticsEvent as a,RefreshSession as n}from"@privy-io/public-api";import{PrivyClientError as o,PrivyApiError as h}from"../Error.mjs";import{Session as c}from"../Session.mjs";import l from"../toAbortSignalTimeout.mjs";import{toSearchParams as g}from"../utils/toSearchParams.mjs";import{toConsoleLogger as d}from"./logger.mjs";import"eventemitter3";import"jose";import"js-cookie";import"../Token.mjs";import"../utils/allSettled.mjs";import"../utils/noop.mjs";let p="privy:caid";class u{setCallbacks(e){this.callbacks={...this.callbacks,...e}}get isReady(){return!!this._config}get config(){return this._config}get caid(){return this._analyticsId}async _initialize(){if(this.isReady)this.callbacks?.setIsReady?.(!0);else{if(!await this.isStorageAccessible())throw new o({code:"storage_error",error:"Unable to access storage"});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._sdkVersion.startsWith("react-auth:")||this.createAnalyticsEvent("sdk_initialize",{})}}getPath(e,{params:t,query:s}){return`${this.baseUrl}${i(e.path,t)}${g(s)}`}async fetch(e,{body:t,params:s,query:i,headers:r,onRequest:a=this._beforeRequest.bind(this)}){let n=new Request(this.getPath(e,{params:s,query:i}),{method:e.method,body:JSON.stringify(t),headers:r}),o=await a(n),c=await this._fetch(n,o),l=await c.json();if(c.status>299)throw new h(l);return l}async _beforeRequestWithoutInitialize(e){let t=await this.session.getPrivyAccessToken()??await this.session.getCustomerAccessToken(),s=new Headers(e.headers);s.set("privy-app-id",this.appId),this.appClientId&&s.set("privy-client-id",this.appClientId),s.set("privy-client",this._sdkVersion),t&&s.set("Authorization",`Bearer ${t}`),s.set("Content-Type","application/json"),s.set("Accept","application/json");let i=await this._getOrGenerateClientAnalyticsId();return i&&s.set("privy-ca-id",i),this.nativeAppIdentifier&&s.set("x-native-app-identifier",this.nativeAppIdentifier),{signal:l(2e4),headers:s,credentials:"include"}}async beforeRequestWithoutRefresh(e){return await this._initialize(),this._beforeRequestWithoutInitialize(e)}async _beforeRequest(e){return await this._initialize(),await this.getAccessTokenInternal(),this.beforeRequestWithoutRefresh(e)}async getAppConfig(){return await this.fetch(r,{params:{app_id:this.appId},onRequest:this._beforeRequestWithoutInitialize.bind(this)})}async _getOrGenerateClientAnalyticsId(){if(this._analyticsId)return this._analyticsId;try{let e=await this._storage.get(p);if("string"==typeof e&&e.length>0)return this._analyticsId=e,e}catch(e){this.logger.error("Unable to load clientId",e)}try{this._analyticsId=t()}catch(e){this.logger.error("Unable to generate uuidv4",e)}if(!this._analyticsId)return null;try{await this._storage.put(p,this._analyticsId)}catch(e){this.logger.error(`Unable to store clientId: ${this._analyticsId}`,e)}return this._analyticsId}async destroyClientAnalyticsId(){try{return await this._storage.del(p)}catch(e){this.logger.error("Unable to delete clientId",e)}}async createAnalyticsEvent(e,t){try{await this.fetch(a,{body:{event_name:e,client_id:await this._getOrGenerateClientAnalyticsId(),payload:t},onRequest:this.beforeRequestWithoutRefresh.bind(this)})}catch(e){}}async refreshSession(e=!1){if(!await this.isStorageAccessible())throw new o({code:"storage_error",error:"Unable to access storage"});let t=await this.session.getRefreshToken()??void 0,s=t??"key",i=this._cache.get(s);if(i)return this.logger.debug("[privy:refresh] found in-flight session refresh request, deduping"),await i;let r=this._refreshSession(t,e);this._cache.set(s,r);try{return await r}finally{this._cache.delete(s)}}async _refreshSession(e,t){let i=await this.session.getCustomerAccessToken();if(!this.session.hasRefreshCredentials(i,e??null))throw this.logger.debug("[privy:refresh] missing tokens, skipping request"),await this._initialize(),new h({code:s.MISSING_OR_INVALID_TOKEN,error:"No tokens found in storage"});try{this.logger.debug(`[privy:refresh] fetching: ${n.path}`);let s=await this.fetch(n,{body:{refresh_token:e},onRequest:this.beforeRequestWithoutRefresh.bind(this)}),i=s.session_update_action;return this.logger.debug(`[privy:refresh] response: ${i}`),t||this.callbacks?.setUser?.(s.user),"set"===i&&(await this.session.updateWithTokensResponse(s),this.logger.debug("[privy:refresh] tokens stored")),"clear"===i&&(await this.session.destroyLocalState(),this.logger.debug("[privy:refresh] tokens cleared"),t||this.callbacks?.setUser?.(null)),"ignore"===i&&s.token&&(await this.session.storeCustomerAccessToken(s.token),await this.session.storePrivyAccessToken(s.privy_access_token),this.logger.debug("[privy:refresh] access token stored"),s.identity_token&&(this.logger.debug("[privy:refresh] identity token stored"),await this.session.storeIdentityToken(s.identity_token))),this.logger.debug("[privy:refresh] returning response"),s}catch(e){throw this.logger.debug(`[privy:refresh] error: ${e.message??"unknown error"}`),e instanceof h&&e.code===s.MISSING_OR_INVALID_TOKEN&&(await this.session.destroyLocalState(),t||this.callbacks?.setUser?.(null)),e}}async getAccessToken(){let[e,t]=await Promise.all([this.session.getCustomerAccessToken(),this.session.getRefreshToken()]);if(!this.session.tokenIsActive(e)&&this.session.hasRefreshCredentials(e,t)){let t=await this.refreshSession(),s=await this.session.getCustomerAccessToken();return t.token||this.logger.debug("[privy:getAccessToken] expected token received null"),t.token===e&&this.logger.debug("[privy:getAccessToken] expected new token in response received existing"),s===e&&this.logger.debug("[privy:getAccessToken] expected new token in storage received existing"),t.token??s}return e}async getAccessTokenInternal(){let e=await this.getAccessToken();return await this.session.getPrivyAccessToken()??e}async getIdentityToken(){return await this.session.getIdentityToken()}async isStorageAccessible(){try{let e=`privy:__storage__test-${t()}`,s="blobby";await this._storage.put(e,s);let i=await this._storage.get(e);return await this._storage.del(e),i===s}catch(e){return this.logger.error(e),!1}}constructor(t){this._sdkVersion="js-sdk-core:0.56.
|
|
1
|
+
import e from"fetch-retry";import{v4 as t}from"uuid";import{PrivyErrorCode as s}from"@privy-io/api-base";import{getPathWithParams as i,AppConfig as r,AnalyticsEvent as a,RefreshSession as n}from"@privy-io/public-api";import{PrivyClientError as o,PrivyApiError as h}from"../Error.mjs";import{Session as c}from"../Session.mjs";import l from"../toAbortSignalTimeout.mjs";import{toSearchParams as g}from"../utils/toSearchParams.mjs";import{toConsoleLogger as d}from"./logger.mjs";import"eventemitter3";import"jose";import"js-cookie";import"../Token.mjs";import"../utils/allSettled.mjs";import"../utils/noop.mjs";let p="privy:caid";class u{setCallbacks(e){this.callbacks={...this.callbacks,...e}}get isReady(){return!!this._config}get config(){return this._config}get caid(){return this._analyticsId}async _initialize(){if(this.isReady)this.callbacks?.setIsReady?.(!0);else{if(!await this.isStorageAccessible())throw new o({code:"storage_error",error:"Unable to access storage"});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._sdkVersion.startsWith("react-auth:")||this.createAnalyticsEvent("sdk_initialize",{})}}getPath(e,{params:t,query:s}){return`${this.baseUrl}${i(e.path,t)}${g(s)}`}async fetch(e,{body:t,params:s,query:i,headers:r,onRequest:a=this._beforeRequest.bind(this)}){let n=new Request(this.getPath(e,{params:s,query:i}),{method:e.method,body:JSON.stringify(t),headers:r}),o=await a(n),c=await this._fetch(n,o),l=await c.json();if(c.status>299)throw new h(l);return l}async _beforeRequestWithoutInitialize(e){let t=await this.session.getPrivyAccessToken()??await this.session.getCustomerAccessToken(),s=new Headers(e.headers);s.set("privy-app-id",this.appId),this.appClientId&&s.set("privy-client-id",this.appClientId),s.set("privy-client",this._sdkVersion),t&&s.set("Authorization",`Bearer ${t}`),s.set("Content-Type","application/json"),s.set("Accept","application/json");let i=await this._getOrGenerateClientAnalyticsId();return i&&s.set("privy-ca-id",i),this.nativeAppIdentifier&&s.set("x-native-app-identifier",this.nativeAppIdentifier),{signal:l(2e4),headers:s,credentials:"include"}}async beforeRequestWithoutRefresh(e){return await this._initialize(),this._beforeRequestWithoutInitialize(e)}async _beforeRequest(e){return await this._initialize(),await this.getAccessTokenInternal(),this.beforeRequestWithoutRefresh(e)}async getAppConfig(){return await this.fetch(r,{params:{app_id:this.appId},onRequest:this._beforeRequestWithoutInitialize.bind(this)})}async _getOrGenerateClientAnalyticsId(){if(this._analyticsId)return this._analyticsId;try{let e=await this._storage.get(p);if("string"==typeof e&&e.length>0)return this._analyticsId=e,e}catch(e){this.logger.error("Unable to load clientId",e)}try{this._analyticsId=t()}catch(e){this.logger.error("Unable to generate uuidv4",e)}if(!this._analyticsId)return null;try{await this._storage.put(p,this._analyticsId)}catch(e){this.logger.error(`Unable to store clientId: ${this._analyticsId}`,e)}return this._analyticsId}async destroyClientAnalyticsId(){try{return await this._storage.del(p)}catch(e){this.logger.error("Unable to delete clientId",e)}}async createAnalyticsEvent(e,t){try{await this.fetch(a,{body:{event_name:e,client_id:await this._getOrGenerateClientAnalyticsId(),payload:t},onRequest:this.beforeRequestWithoutRefresh.bind(this)})}catch(e){}}async refreshSession(e=!1){if(!await this.isStorageAccessible())throw new o({code:"storage_error",error:"Unable to access storage"});let t=await this.session.getRefreshToken()??void 0,s=t??"key",i=this._cache.get(s);if(i)return this.logger.debug("[privy:refresh] found in-flight session refresh request, deduping"),await i;let r=this._refreshSession(t,e);this._cache.set(s,r);try{return await r}finally{this._cache.delete(s)}}async _refreshSession(e,t){let i=await this.session.getCustomerAccessToken();if(!this.session.hasRefreshCredentials(i,e??null))throw this.logger.debug("[privy:refresh] missing tokens, skipping request"),await this._initialize(),new h({code:s.MISSING_OR_INVALID_TOKEN,error:"No tokens found in storage"});try{this.logger.debug(`[privy:refresh] fetching: ${n.path}`);let s=await this.fetch(n,{body:{refresh_token:e},onRequest:this.beforeRequestWithoutRefresh.bind(this)}),i=s.session_update_action;return this.logger.debug(`[privy:refresh] response: ${i}`),t||this.callbacks?.setUser?.(s.user),"set"===i&&(await this.session.updateWithTokensResponse(s),this.logger.debug("[privy:refresh] tokens stored")),"clear"===i&&(await this.session.destroyLocalState(),this.logger.debug("[privy:refresh] tokens cleared"),t||this.callbacks?.setUser?.(null)),"ignore"===i&&s.token&&(await this.session.storeCustomerAccessToken(s.token),await this.session.storePrivyAccessToken(s.privy_access_token),this.logger.debug("[privy:refresh] access token stored"),s.identity_token&&(this.logger.debug("[privy:refresh] identity token stored"),await this.session.storeIdentityToken(s.identity_token))),this.logger.debug("[privy:refresh] returning response"),s}catch(e){throw this.logger.debug(`[privy:refresh] error: ${e.message??"unknown error"}`),e instanceof h&&e.code===s.MISSING_OR_INVALID_TOKEN&&(await this.session.destroyLocalState(),t||this.callbacks?.setUser?.(null)),e}}async getAccessToken(){let[e,t]=await Promise.all([this.session.getCustomerAccessToken(),this.session.getRefreshToken()]);if(!this.session.tokenIsActive(e)&&this.session.hasRefreshCredentials(e,t)){let t=await this.refreshSession(),s=await this.session.getCustomerAccessToken();return t.token||this.logger.debug("[privy:getAccessToken] expected token received null"),t.token===e&&this.logger.debug("[privy:getAccessToken] expected new token in response received existing"),s===e&&this.logger.debug("[privy:getAccessToken] expected new token in storage received existing"),t.token??s}return e}async getAccessTokenInternal(){let e=await this.getAccessToken();return await this.session.getPrivyAccessToken()??e}async getIdentityToken(){return await this.session.getIdentityToken()}async isStorageAccessible(){try{let e=`privy:__storage__test-${t()}`,s="blobby";await this._storage.put(e,s);let i=await this._storage.get(e);return await this._storage.del(e),i===s}catch(e){return this.logger.error(e),!1}}constructor(t){this._sdkVersion="js-sdk-core:0.56.4",this._cache=new Map,this.logger=d({level:t.logLevel??"ERROR"}),this._storage=t.storage,this._analyticsId=null,this._getOrGenerateClientAnalyticsId(),this.baseUrl=t.baseUrl??"https://auth.privy.io",this.appId=t.appId,this.appClientId=t.appClientId,this._sdkVersion=t.sdkVersion??this._sdkVersion,this.callbacks=t.callbacks,this.nativeAppIdentifier=t.nativeAppIdentifier,this.session=new c({storage:this._storage,isUsingServerCookies:!1,appId:t.appId}),this._fetch=e(globalThis.fetch,{retries:3,retryDelay:e=>3**e*500,retryOn:[408,409,425,500,502,503,504]}),this.session.on("error_storing_tokens",(e=>{this.createAnalyticsEvent("error_updating_tokens_in_storage",{reason:e})}))}}export{u as PrivyInternal};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@privy-io/js-sdk-core",
|
|
3
|
-
"version": "0.56.
|
|
3
|
+
"version": "0.56.4",
|
|
4
4
|
"description": "Vanilla JS client for the Privy Auth API",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"authentication",
|
|
@@ -64,10 +64,10 @@
|
|
|
64
64
|
],
|
|
65
65
|
"dependencies": {
|
|
66
66
|
"@privy-io/api-base": "1.7.1",
|
|
67
|
-
"@privy-io/public-api": "2.
|
|
67
|
+
"@privy-io/public-api": "2.52.0",
|
|
68
68
|
"@privy-io/ethereum": "0.0.2",
|
|
69
69
|
"canonicalize": "^2.0.0",
|
|
70
|
-
"@privy-io/chains": "0.0.
|
|
70
|
+
"@privy-io/chains": "0.0.4",
|
|
71
71
|
"eventemitter3": "^5.0.1",
|
|
72
72
|
"fetch-retry": "^6.0.0",
|
|
73
73
|
"jose": "^4.15.5",
|