@privy-io/js-sdk-core 0.30.1 → 0.30.3

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.
@@ -121,6 +121,7 @@ export default class EmailApi {
121
121
  wallet_client_type: "privy";
122
122
  connector_type: "embedded";
123
123
  imported: boolean;
124
+ delegated: boolean;
124
125
  wallet_index: number;
125
126
  recovery_method: "user-passcode" | "google-drive" | "icloud" | "privy";
126
127
  } | {
@@ -136,6 +137,7 @@ export default class EmailApi {
136
137
  wallet_client_type: "privy";
137
138
  connector_type: "embedded";
138
139
  imported: boolean;
140
+ delegated: boolean;
139
141
  wallet_index: number;
140
142
  recovery_method: "privy";
141
143
  } | {
@@ -231,10 +233,6 @@ export default class EmailApi {
231
233
  address: string;
232
234
  }[];
233
235
  })[];
234
- delegated_wallets: {
235
- address: string;
236
- chain_type: "solana";
237
- }[];
238
236
  created_at: number;
239
237
  has_accepted_terms: boolean;
240
238
  is_guest: boolean;
@@ -341,6 +339,7 @@ export default class EmailApi {
341
339
  wallet_client_type: "privy";
342
340
  connector_type: "embedded";
343
341
  imported: boolean;
342
+ delegated: boolean;
344
343
  wallet_index: number;
345
344
  recovery_method: "user-passcode" | "google-drive" | "icloud" | "privy";
346
345
  } | {
@@ -356,6 +355,7 @@ export default class EmailApi {
356
355
  wallet_client_type: "privy";
357
356
  connector_type: "embedded";
358
357
  imported: boolean;
358
+ delegated: boolean;
359
359
  wallet_index: number;
360
360
  recovery_method: "privy";
361
361
  } | {
@@ -451,10 +451,6 @@ export default class EmailApi {
451
451
  address: string;
452
452
  }[];
453
453
  })[];
454
- delegated_wallets: {
455
- address: string;
456
- chain_type: "solana";
457
- }[];
458
454
  created_at: number;
459
455
  has_accepted_terms: boolean;
460
456
  is_guest: boolean;
@@ -121,6 +121,7 @@ export default class EmailApi {
121
121
  wallet_client_type: "privy";
122
122
  connector_type: "embedded";
123
123
  imported: boolean;
124
+ delegated: boolean;
124
125
  wallet_index: number;
125
126
  recovery_method: "user-passcode" | "google-drive" | "icloud" | "privy";
126
127
  } | {
@@ -136,6 +137,7 @@ export default class EmailApi {
136
137
  wallet_client_type: "privy";
137
138
  connector_type: "embedded";
138
139
  imported: boolean;
140
+ delegated: boolean;
139
141
  wallet_index: number;
140
142
  recovery_method: "privy";
141
143
  } | {
@@ -231,10 +233,6 @@ export default class EmailApi {
231
233
  address: string;
232
234
  }[];
233
235
  })[];
234
- delegated_wallets: {
235
- address: string;
236
- chain_type: "solana";
237
- }[];
238
236
  created_at: number;
239
237
  has_accepted_terms: boolean;
240
238
  is_guest: boolean;
@@ -341,6 +339,7 @@ export default class EmailApi {
341
339
  wallet_client_type: "privy";
342
340
  connector_type: "embedded";
343
341
  imported: boolean;
342
+ delegated: boolean;
344
343
  wallet_index: number;
345
344
  recovery_method: "user-passcode" | "google-drive" | "icloud" | "privy";
346
345
  } | {
@@ -356,6 +355,7 @@ export default class EmailApi {
356
355
  wallet_client_type: "privy";
357
356
  connector_type: "embedded";
358
357
  imported: boolean;
358
+ delegated: boolean;
359
359
  wallet_index: number;
360
360
  recovery_method: "privy";
361
361
  } | {
@@ -451,10 +451,6 @@ export default class EmailApi {
451
451
  address: string;
452
452
  }[];
453
453
  })[];
454
- delegated_wallets: {
455
- address: string;
456
- chain_type: "solana";
457
- }[];
458
454
  created_at: number;
459
455
  has_accepted_terms: boolean;
460
456
  is_guest: boolean;
@@ -132,6 +132,7 @@ export default class FarcasterApi {
132
132
  wallet_client_type: "privy";
133
133
  connector_type: "embedded";
134
134
  imported: boolean;
135
+ delegated: boolean;
135
136
  wallet_index: number;
136
137
  recovery_method: "user-passcode" | "google-drive" | "icloud" | "privy";
137
138
  } | {
@@ -147,6 +148,7 @@ export default class FarcasterApi {
147
148
  wallet_client_type: "privy";
148
149
  connector_type: "embedded";
149
150
  imported: boolean;
151
+ delegated: boolean;
150
152
  wallet_index: number;
151
153
  recovery_method: "privy";
152
154
  } | {
@@ -242,10 +244,6 @@ export default class FarcasterApi {
242
244
  address: string;
243
245
  }[];
244
246
  })[];
245
- delegated_wallets: {
246
- address: string;
247
- chain_type: "solana";
248
- }[];
249
247
  created_at: number;
250
248
  has_accepted_terms: boolean;
251
249
  is_guest: boolean;
@@ -357,6 +355,7 @@ export default class FarcasterApi {
357
355
  wallet_client_type: "privy";
358
356
  connector_type: "embedded";
359
357
  imported: boolean;
358
+ delegated: boolean;
360
359
  wallet_index: number;
361
360
  recovery_method: "user-passcode" | "google-drive" | "icloud" | "privy";
362
361
  } | {
@@ -372,6 +371,7 @@ export default class FarcasterApi {
372
371
  wallet_client_type: "privy";
373
372
  connector_type: "embedded";
374
373
  imported: boolean;
374
+ delegated: boolean;
375
375
  wallet_index: number;
376
376
  recovery_method: "privy";
377
377
  } | {
@@ -467,10 +467,6 @@ export default class FarcasterApi {
467
467
  address: string;
468
468
  }[];
469
469
  })[];
470
- delegated_wallets: {
471
- address: string;
472
- chain_type: "solana";
473
- }[];
474
470
  created_at: number;
475
471
  has_accepted_terms: boolean;
476
472
  is_guest: boolean;
@@ -132,6 +132,7 @@ export default class FarcasterApi {
132
132
  wallet_client_type: "privy";
133
133
  connector_type: "embedded";
134
134
  imported: boolean;
135
+ delegated: boolean;
135
136
  wallet_index: number;
136
137
  recovery_method: "user-passcode" | "google-drive" | "icloud" | "privy";
137
138
  } | {
@@ -147,6 +148,7 @@ export default class FarcasterApi {
147
148
  wallet_client_type: "privy";
148
149
  connector_type: "embedded";
149
150
  imported: boolean;
151
+ delegated: boolean;
150
152
  wallet_index: number;
151
153
  recovery_method: "privy";
152
154
  } | {
@@ -242,10 +244,6 @@ export default class FarcasterApi {
242
244
  address: string;
243
245
  }[];
244
246
  })[];
245
- delegated_wallets: {
246
- address: string;
247
- chain_type: "solana";
248
- }[];
249
247
  created_at: number;
250
248
  has_accepted_terms: boolean;
251
249
  is_guest: boolean;
@@ -357,6 +355,7 @@ export default class FarcasterApi {
357
355
  wallet_client_type: "privy";
358
356
  connector_type: "embedded";
359
357
  imported: boolean;
358
+ delegated: boolean;
360
359
  wallet_index: number;
361
360
  recovery_method: "user-passcode" | "google-drive" | "icloud" | "privy";
362
361
  } | {
@@ -372,6 +371,7 @@ export default class FarcasterApi {
372
371
  wallet_client_type: "privy";
373
372
  connector_type: "embedded";
374
373
  imported: boolean;
374
+ delegated: boolean;
375
375
  wallet_index: number;
376
376
  recovery_method: "privy";
377
377
  } | {
@@ -467,10 +467,6 @@ export default class FarcasterApi {
467
467
  address: string;
468
468
  }[];
469
469
  })[];
470
- delegated_wallets: {
471
- address: string;
472
- chain_type: "solana";
473
- }[];
474
470
  created_at: number;
475
471
  has_accepted_terms: boolean;
476
472
  is_guest: boolean;
@@ -125,6 +125,7 @@ export default class OAuthApi {
125
125
  wallet_client_type: "privy";
126
126
  connector_type: "embedded";
127
127
  imported: boolean;
128
+ delegated: boolean;
128
129
  wallet_index: number;
129
130
  recovery_method: "user-passcode" | "google-drive" | "icloud" | "privy";
130
131
  } | {
@@ -140,6 +141,7 @@ export default class OAuthApi {
140
141
  wallet_client_type: "privy";
141
142
  connector_type: "embedded";
142
143
  imported: boolean;
144
+ delegated: boolean;
143
145
  wallet_index: number;
144
146
  recovery_method: "privy";
145
147
  } | {
@@ -235,10 +237,6 @@ export default class OAuthApi {
235
237
  address: string;
236
238
  }[];
237
239
  })[];
238
- delegated_wallets: {
239
- address: string;
240
- chain_type: "solana";
241
- }[];
242
240
  created_at: number;
243
241
  has_accepted_terms: boolean;
244
242
  is_guest: boolean;
@@ -351,6 +349,7 @@ export default class OAuthApi {
351
349
  wallet_client_type: "privy";
352
350
  connector_type: "embedded";
353
351
  imported: boolean;
352
+ delegated: boolean;
354
353
  wallet_index: number;
355
354
  recovery_method: "user-passcode" | "google-drive" | "icloud" | "privy";
356
355
  } | {
@@ -366,6 +365,7 @@ export default class OAuthApi {
366
365
  wallet_client_type: "privy";
367
366
  connector_type: "embedded";
368
367
  imported: boolean;
368
+ delegated: boolean;
369
369
  wallet_index: number;
370
370
  recovery_method: "privy";
371
371
  } | {
@@ -461,10 +461,6 @@ export default class OAuthApi {
461
461
  address: string;
462
462
  }[];
463
463
  })[];
464
- delegated_wallets: {
465
- address: string;
466
- chain_type: "solana";
467
- }[];
468
464
  created_at: number;
469
465
  has_accepted_terms: boolean;
470
466
  is_guest: boolean;
@@ -125,6 +125,7 @@ export default class OAuthApi {
125
125
  wallet_client_type: "privy";
126
126
  connector_type: "embedded";
127
127
  imported: boolean;
128
+ delegated: boolean;
128
129
  wallet_index: number;
129
130
  recovery_method: "user-passcode" | "google-drive" | "icloud" | "privy";
130
131
  } | {
@@ -140,6 +141,7 @@ export default class OAuthApi {
140
141
  wallet_client_type: "privy";
141
142
  connector_type: "embedded";
142
143
  imported: boolean;
144
+ delegated: boolean;
143
145
  wallet_index: number;
144
146
  recovery_method: "privy";
145
147
  } | {
@@ -235,10 +237,6 @@ export default class OAuthApi {
235
237
  address: string;
236
238
  }[];
237
239
  })[];
238
- delegated_wallets: {
239
- address: string;
240
- chain_type: "solana";
241
- }[];
242
240
  created_at: number;
243
241
  has_accepted_terms: boolean;
244
242
  is_guest: boolean;
@@ -351,6 +349,7 @@ export default class OAuthApi {
351
349
  wallet_client_type: "privy";
352
350
  connector_type: "embedded";
353
351
  imported: boolean;
352
+ delegated: boolean;
354
353
  wallet_index: number;
355
354
  recovery_method: "user-passcode" | "google-drive" | "icloud" | "privy";
356
355
  } | {
@@ -366,6 +365,7 @@ export default class OAuthApi {
366
365
  wallet_client_type: "privy";
367
366
  connector_type: "embedded";
368
367
  imported: boolean;
368
+ delegated: boolean;
369
369
  wallet_index: number;
370
370
  recovery_method: "privy";
371
371
  } | {
@@ -461,10 +461,6 @@ export default class OAuthApi {
461
461
  address: string;
462
462
  }[];
463
463
  })[];
464
- delegated_wallets: {
465
- address: string;
466
- chain_type: "solana";
467
- }[];
468
464
  created_at: number;
469
465
  has_accepted_terms: boolean;
470
466
  is_guest: boolean;
@@ -121,6 +121,7 @@ export default class PhoneApi {
121
121
  wallet_client_type: "privy";
122
122
  connector_type: "embedded";
123
123
  imported: boolean;
124
+ delegated: boolean;
124
125
  wallet_index: number;
125
126
  recovery_method: "user-passcode" | "google-drive" | "icloud" | "privy";
126
127
  } | {
@@ -136,6 +137,7 @@ export default class PhoneApi {
136
137
  wallet_client_type: "privy";
137
138
  connector_type: "embedded";
138
139
  imported: boolean;
140
+ delegated: boolean;
139
141
  wallet_index: number;
140
142
  recovery_method: "privy";
141
143
  } | {
@@ -231,10 +233,6 @@ export default class PhoneApi {
231
233
  address: string;
232
234
  }[];
233
235
  })[];
234
- delegated_wallets: {
235
- address: string;
236
- chain_type: "solana";
237
- }[];
238
236
  created_at: number;
239
237
  has_accepted_terms: boolean;
240
238
  is_guest: boolean;
@@ -341,6 +339,7 @@ export default class PhoneApi {
341
339
  wallet_client_type: "privy";
342
340
  connector_type: "embedded";
343
341
  imported: boolean;
342
+ delegated: boolean;
344
343
  wallet_index: number;
345
344
  recovery_method: "user-passcode" | "google-drive" | "icloud" | "privy";
346
345
  } | {
@@ -356,6 +355,7 @@ export default class PhoneApi {
356
355
  wallet_client_type: "privy";
357
356
  connector_type: "embedded";
358
357
  imported: boolean;
358
+ delegated: boolean;
359
359
  wallet_index: number;
360
360
  recovery_method: "privy";
361
361
  } | {
@@ -451,10 +451,6 @@ export default class PhoneApi {
451
451
  address: string;
452
452
  }[];
453
453
  })[];
454
- delegated_wallets: {
455
- address: string;
456
- chain_type: "solana";
457
- }[];
458
454
  created_at: number;
459
455
  has_accepted_terms: boolean;
460
456
  is_guest: boolean;
@@ -121,6 +121,7 @@ export default class PhoneApi {
121
121
  wallet_client_type: "privy";
122
122
  connector_type: "embedded";
123
123
  imported: boolean;
124
+ delegated: boolean;
124
125
  wallet_index: number;
125
126
  recovery_method: "user-passcode" | "google-drive" | "icloud" | "privy";
126
127
  } | {
@@ -136,6 +137,7 @@ export default class PhoneApi {
136
137
  wallet_client_type: "privy";
137
138
  connector_type: "embedded";
138
139
  imported: boolean;
140
+ delegated: boolean;
139
141
  wallet_index: number;
140
142
  recovery_method: "privy";
141
143
  } | {
@@ -231,10 +233,6 @@ export default class PhoneApi {
231
233
  address: string;
232
234
  }[];
233
235
  })[];
234
- delegated_wallets: {
235
- address: string;
236
- chain_type: "solana";
237
- }[];
238
236
  created_at: number;
239
237
  has_accepted_terms: boolean;
240
238
  is_guest: boolean;
@@ -341,6 +339,7 @@ export default class PhoneApi {
341
339
  wallet_client_type: "privy";
342
340
  connector_type: "embedded";
343
341
  imported: boolean;
342
+ delegated: boolean;
344
343
  wallet_index: number;
345
344
  recovery_method: "user-passcode" | "google-drive" | "icloud" | "privy";
346
345
  } | {
@@ -356,6 +355,7 @@ export default class PhoneApi {
356
355
  wallet_client_type: "privy";
357
356
  connector_type: "embedded";
358
357
  imported: boolean;
358
+ delegated: boolean;
359
359
  wallet_index: number;
360
360
  recovery_method: "privy";
361
361
  } | {
@@ -451,10 +451,6 @@ export default class PhoneApi {
451
451
  address: string;
452
452
  }[];
453
453
  })[];
454
- delegated_wallets: {
455
- address: string;
456
- chain_type: "solana";
457
- }[];
458
454
  created_at: number;
459
455
  has_accepted_terms: boolean;
460
456
  is_guest: boolean;
@@ -104,6 +104,7 @@ export default class UserApi {
104
104
  wallet_client_type: "privy";
105
105
  connector_type: "embedded";
106
106
  imported: boolean;
107
+ delegated: boolean;
107
108
  wallet_index: number;
108
109
  recovery_method: "user-passcode" | "google-drive" | "icloud" | "privy";
109
110
  } | {
@@ -119,6 +120,7 @@ export default class UserApi {
119
120
  wallet_client_type: "privy";
120
121
  connector_type: "embedded";
121
122
  imported: boolean;
123
+ delegated: boolean;
122
124
  wallet_index: number;
123
125
  recovery_method: "privy";
124
126
  } | {
@@ -214,10 +216,6 @@ export default class UserApi {
214
216
  address: string;
215
217
  }[];
216
218
  })[];
217
- delegated_wallets: {
218
- address: string;
219
- chain_type: "solana";
220
- }[];
221
219
  created_at: number;
222
220
  has_accepted_terms: boolean;
223
221
  is_guest: boolean;
@@ -104,6 +104,7 @@ export default class UserApi {
104
104
  wallet_client_type: "privy";
105
105
  connector_type: "embedded";
106
106
  imported: boolean;
107
+ delegated: boolean;
107
108
  wallet_index: number;
108
109
  recovery_method: "user-passcode" | "google-drive" | "icloud" | "privy";
109
110
  } | {
@@ -119,6 +120,7 @@ export default class UserApi {
119
120
  wallet_client_type: "privy";
120
121
  connector_type: "embedded";
121
122
  imported: boolean;
123
+ delegated: boolean;
122
124
  wallet_index: number;
123
125
  recovery_method: "privy";
124
126
  } | {
@@ -214,10 +216,6 @@ export default class UserApi {
214
216
  address: string;
215
217
  }[];
216
218
  })[];
217
- delegated_wallets: {
218
- address: string;
219
- chain_type: "solana";
220
- }[];
221
219
  created_at: number;
222
220
  has_accepted_terms: boolean;
223
221
  is_guest: boolean;
@@ -1 +1 @@
1
- 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 a,AnalyticsEvent as n,RefreshSession as r}from"@privy-io/public-api";import{PrivyClientError as o,PrivyApiError as c}from"./Error.mjs";import{Session as h}from"./Session.mjs";import l from"./toAbortSignalTimeout.mjs";import"eventemitter3";import"jose";import"js-cookie";import"./Token.mjs";const d="privy:caid";class p{baseUrl;_config;appId;appClientId;_storage;_analyticsId;_sdkVersion="js-sdk-core:0.30.1";_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,appId:e.appId}),this._fetch=t(fetch,{retries:3,retryDelay:500}),this.session.on("error_storing_tokens",(t=>{this.createAnalyticsEvent("error_updating_tokens_in_storage",{reason:t})}))}setCallbacks(t){this.callbacks={...this.callbacks,...t}}get isReady(){return Boolean(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.createAnalyticsEvent("sdk_initialize",{})}}async fetch(t,{body:e,params:s,headers:a,options:n={onRequest:this._beforeRequest.bind(this)}}){const r=`${this.baseUrl}${i(t.path,s)}`,o=new Request(r,{method:t.method,body:JSON.stringify(e),headers:a}),h=await n.onRequest(o),l=await this._fetch(o,h),d=await l.json();if(l.status>299)throw new c(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:l(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(a,{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(d);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(d,this._analyticsId)}catch(t){console.error(`Unable to store clientId: ${this._analyticsId}`,t)}return this._analyticsId}async destroyClientAnalyticsId(){try{return await this._storage.del(d)}catch(t){console.error("Unable to delete clientId",t)}}async createAnalyticsEvent(t,e){try{await this.fetch(n,{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 c({code:s.MISSING_OR_INVALID_TOKEN,error:"No tokens found in storage"});try{const e=await this.fetch(r,{body:{refresh_token:t},options:{onRequest:this.beforeRequestWithoutRefresh.bind(this)}}),s=e.session_update_action;return this.callbacks?.setUser?.(e.user),"set"===s&&await this.session.updateWithTokensResponse(e),"clear"===s&&(await this.session.destroyLocalState(),this.callbacks?.setUser?.(null)),"ignore"===s&&e.token&&(await this.session.storeToken(e.token),e.identity_token&&await this.session.storeIdentityToken(e.identity_token)),e}catch(t){throw t instanceof c&&t.code===s.MISSING_OR_INVALID_TOKEN&&(await this.session.destroyLocalState(),this.callbacks?.setUser?.(null)),t}}async getAccessToken(){const[t,e]=await Promise.all([this.session.getToken(),this.session.getRefreshToken()]);return!this.session.tokenIsActive(t)&&this.session.hasRefreshCredentials(t,e)?(await this.refreshSession(),await this.session.getToken()):t}async getIdentityToken(){return await this.session.getIdentityToken()}async isStorageAccessible(){try{const t="privy:__storage__test",e="blobby";await this._storage.put(t,e);const s=await this._storage.get(t);return await this._storage.del(t),s===e}catch(t){return console.error(t),!1}}}export{p as PrivyInternal};
1
+ 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 a,AnalyticsEvent as n,RefreshSession as r}from"@privy-io/public-api";import{PrivyClientError as o,PrivyApiError as c}from"./Error.mjs";import{Session as h}from"./Session.mjs";import l from"./toAbortSignalTimeout.mjs";import"eventemitter3";import"jose";import"js-cookie";import"./Token.mjs";const d="privy:caid";class p{baseUrl;_config;appId;appClientId;_storage;_analyticsId;_sdkVersion="js-sdk-core:0.30.3";_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,appId:e.appId}),this._fetch=t(fetch,{retries:3,retryDelay:500}),this.session.on("error_storing_tokens",(t=>{this.createAnalyticsEvent("error_updating_tokens_in_storage",{reason:t})}))}setCallbacks(t){this.callbacks={...this.callbacks,...t}}get isReady(){return Boolean(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.createAnalyticsEvent("sdk_initialize",{})}}async fetch(t,{body:e,params:s,headers:a,options:n={onRequest:this._beforeRequest.bind(this)}}){const r=`${this.baseUrl}${i(t.path,s)}`,o=new Request(r,{method:t.method,body:JSON.stringify(e),headers:a}),h=await n.onRequest(o),l=await this._fetch(o,h),d=await l.json();if(l.status>299)throw new c(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:l(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(a,{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(d);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(d,this._analyticsId)}catch(t){console.error(`Unable to store clientId: ${this._analyticsId}`,t)}return this._analyticsId}async destroyClientAnalyticsId(){try{return await this._storage.del(d)}catch(t){console.error("Unable to delete clientId",t)}}async createAnalyticsEvent(t,e){try{await this.fetch(n,{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 c({code:s.MISSING_OR_INVALID_TOKEN,error:"No tokens found in storage"});try{const e=await this.fetch(r,{body:{refresh_token:t},options:{onRequest:this.beforeRequestWithoutRefresh.bind(this)}}),s=e.session_update_action;return this.callbacks?.setUser?.(e.user),"set"===s&&await this.session.updateWithTokensResponse(e),"clear"===s&&(await this.session.destroyLocalState(),this.callbacks?.setUser?.(null)),"ignore"===s&&e.token&&(await this.session.storeToken(e.token),e.identity_token&&await this.session.storeIdentityToken(e.identity_token)),e}catch(t){throw t instanceof c&&t.code===s.MISSING_OR_INVALID_TOKEN&&(await this.session.destroyLocalState(),this.callbacks?.setUser?.(null)),t}}async getAccessToken(){const[t,e]=await Promise.all([this.session.getToken(),this.session.getRefreshToken()]);return!this.session.tokenIsActive(t)&&this.session.hasRefreshCredentials(t,e)?(await this.refreshSession(),await this.session.getToken()):t}async getIdentityToken(){return await this.session.getIdentityToken()}async isStorageAccessible(){try{const t="privy:__storage__test",e="blobby";await this._storage.put(t,e);const s=await this._storage.get(t);return await this._storage.del(t),s===e}catch(t){return console.error(t),!1}}}export{p as PrivyInternal};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@privy-io/js-sdk-core",
3
- "version": "0.30.1",
3
+ "version": "0.30.3",
4
4
  "description": "Vanilla JS client for the Privy Auth API",
5
5
  "keywords": [
6
6
  "authentication",
@@ -59,7 +59,7 @@
59
59
  "@ethersproject/transactions": "^5.7.0",
60
60
  "@ethersproject/units": "^5.7.0",
61
61
  "@privy-io/api-base": "^1.4.0",
62
- "@privy-io/public-api": "2.11.4",
62
+ "@privy-io/public-api": "2.11.6",
63
63
  "eventemitter3": "^5.0.1",
64
64
  "fetch-retry": "^5.0.6",
65
65
  "jose": "^4.15.5",