@cimplify/sdk 0.106.2 → 0.107.0

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.
Files changed (58) hide show
  1. package/dist/advanced.d.mts +3 -3
  2. package/dist/advanced.d.ts +3 -3
  3. package/dist/advanced.js +20 -20
  4. package/dist/advanced.mjs +1 -1
  5. package/dist/{catalogue-ChBjPPME.d.ts → catalogue-BohD-jU_.d.mts} +44 -38
  6. package/dist/{catalogue-CVKH9faR.d.mts → catalogue-C4xKpirp.d.ts} +44 -38
  7. package/dist/{chunk-T3MXI7JD.mjs → chunk-2I6YKE6Q.mjs} +85 -126
  8. package/dist/{chunk-7O2BBPDM.js → chunk-73HY36Z7.js} +4 -4
  9. package/dist/{chunk-B7JH5D3J.js → chunk-HKCB73AJ.js} +85 -126
  10. package/dist/{chunk-4VTFTLH7.mjs → chunk-IWZKCL3P.mjs} +2 -2
  11. package/dist/{chunk-L4XKIMUO.mjs → chunk-MKZXZDHE.mjs} +17 -0
  12. package/dist/{chunk-NJK3THWT.js → chunk-NX5YBQZP.js} +17 -0
  13. package/dist/{chunk-LJAZ7H4I.mjs → chunk-P4HYAV3L.mjs} +102 -82
  14. package/dist/{chunk-BCLGREHS.js → chunk-W4ZFZPPY.js} +181 -160
  15. package/dist/{client-BfKFK7cn.d.mts → client-BBFfAtn4.d.mts} +2 -2
  16. package/dist/{client-DoczTJbs.d.ts → client-ByURQZM9.d.ts} +2 -2
  17. package/dist/{checkout-XoV4HNg2.d.mts → elements-Dtqe9Ekd.d.mts} +348 -361
  18. package/dist/{checkout-XoV4HNg2.d.ts → elements-Dtqe9Ekd.d.ts} +348 -361
  19. package/dist/{index-DEUorPUO.d.mts → index-7oz-hVVA.d.mts} +2 -2
  20. package/dist/{index-DRMBo26v.d.mts → index-B6mukQYB.d.mts} +2 -2
  21. package/dist/{index-Dn6xCpDe.d.ts → index-CvLuAMjQ.d.ts} +2 -2
  22. package/dist/{index-DweR60Cv.d.ts → index-DFqDU6yU.d.ts} +2 -2
  23. package/dist/index.d.mts +7 -7
  24. package/dist/index.d.ts +7 -7
  25. package/dist/index.js +71 -67
  26. package/dist/index.mjs +3 -3
  27. package/dist/mock/cli.mjs +17 -0
  28. package/dist/mock/library.js +17 -0
  29. package/dist/mock/library.mjs +17 -0
  30. package/dist/mock/msw.js +17 -0
  31. package/dist/mock/msw.mjs +17 -0
  32. package/dist/{price-De-sxcuj.d.ts → price-B4jL81ef.d.ts} +1 -1
  33. package/dist/{price-CUGIYRtD.d.mts → price-Pyq-vlrm.d.mts} +1 -1
  34. package/dist/react.d.mts +3 -3
  35. package/dist/react.d.ts +3 -3
  36. package/dist/react.js +34 -34
  37. package/dist/react.mjs +5 -5
  38. package/dist/{server-7soMMUgP.d.ts → server-Cowuvfbr.d.ts} +1 -1
  39. package/dist/{server-DDEJ7e9s.d.mts → server-KilvZJU0.d.mts} +1 -1
  40. package/dist/server.d.mts +3 -3
  41. package/dist/server.d.ts +3 -3
  42. package/dist/server.js +7 -7
  43. package/dist/server.mjs +2 -2
  44. package/dist/testing/msw.d.mts +2 -2
  45. package/dist/testing/msw.d.ts +2 -2
  46. package/dist/testing/msw.js +2 -2
  47. package/dist/testing/msw.mjs +1 -1
  48. package/dist/testing/suite.d.mts +5 -5
  49. package/dist/testing/suite.d.ts +5 -5
  50. package/dist/testing/suite.js +24 -24
  51. package/dist/testing/suite.mjs +4 -4
  52. package/dist/testing.d.mts +4 -4
  53. package/dist/testing.d.ts +4 -4
  54. package/dist/testing.js +116 -116
  55. package/dist/testing.mjs +5 -5
  56. package/dist/utils.d.mts +3 -3
  57. package/dist/utils.d.ts +3 -3
  58. package/package.json +1 -1
@@ -2405,7 +2405,7 @@ var LinkService = class {
2405
2405
  this.client.activity.refreshIdentity();
2406
2406
  return result;
2407
2407
  }
2408
- const claim = await this.client.support.claimConversation();
2408
+ const claim = await this.client.session.claim();
2409
2409
  if (fence.generation !== this.activationGeneration || fence.publicKeyEpoch !== this.client.getPublicKeyEpoch() || this.client.getAccessToken() !== accessToken) {
2410
2410
  return this.supersededActivation();
2411
2411
  }
@@ -2629,7 +2629,7 @@ var AuthService = class {
2629
2629
  if (result.ok && result.value.session_token) {
2630
2630
  this.client.setAccessToken(result.value.session_token);
2631
2631
  this.client.activity.refreshIdentity();
2632
- this.client.autoClaimChatIdentity();
2632
+ this.client.autoClaimSession();
2633
2633
  }
2634
2634
  return result;
2635
2635
  }
@@ -2922,13 +2922,10 @@ var ELEMENT_TYPES = {
2922
2922
  };
2923
2923
  var MESSAGE_TYPES = {
2924
2924
  INIT: "init",
2925
- SET_TOKEN: "set_token",
2926
- SET_SESSION: "set_session",
2925
+ IDENTITY: "identity",
2927
2926
  SESSION_CLEARED: "session_cleared",
2928
2927
  SET_CART: "set_cart",
2929
2928
  GET_DATA: "get_data",
2930
- NAVIGATE: "navigate",
2931
- REFRESH_TOKEN: "refresh_token",
2932
2929
  LOGOUT: "logout",
2933
2930
  PROCESS_CHECKOUT: "process_checkout",
2934
2931
  ABORT_CHECKOUT: "abort_checkout",
@@ -2941,7 +2938,6 @@ var MESSAGE_TYPES = {
2941
2938
  LOGOUT_COMPLETE: "logout_complete",
2942
2939
  AUTH_REQUESTED: "auth_requested",
2943
2940
  SESSION_ESTABLISHED: "session_established",
2944
- SESSION_ESTABLISHMENT_FAILED: "session_establishment_failed",
2945
2941
  CONTINUE_WITHOUT_SAVED_DETAILS: "continue_without_saved_details",
2946
2942
  CONTACT_PROVIDED: "contact_provided",
2947
2943
  CHECKOUT_STATUS: "checkout_status",
@@ -3345,7 +3341,6 @@ function toCheckoutError(code, message, recoverable) {
3345
3341
  };
3346
3342
  }
3347
3343
  var DEFAULT_LINK_URL = "https://link.cimplify.io";
3348
- var AUTH_ESTABLISHMENT_WAIT_MS = 8e3;
3349
3344
  var AUTH_CONTACT_TYPES = new Set(Object.values(ELEMENTS_AUTH_CONTACT_TYPES));
3350
3345
  var AUTH_MODES = new Set(Object.values(ELEMENTS_AUTH_MODES));
3351
3346
  var AUTH_PROMPTS = new Set(Object.values(ELEMENTS_AUTH_PROMPTS));
@@ -3376,6 +3371,11 @@ function decodeJwtSub(token) {
3376
3371
  return "";
3377
3372
  }
3378
3373
  }
3374
+ function isClaimedCustomer(value) {
3375
+ if (!value || typeof value !== "object") return false;
3376
+ const record = value;
3377
+ return typeof record.id === "string" && typeof record.name === "string" && (record.email === null || typeof record.email === "string") && (record.phone === null || typeof record.phone === "string");
3378
+ }
3379
3379
  function isAllowedOrigin(origin) {
3380
3380
  try {
3381
3381
  const url = new URL(origin);
@@ -3432,8 +3432,10 @@ function parseIframeMessage(data) {
3432
3432
  }
3433
3433
  break;
3434
3434
  case MESSAGE_TYPES.SESSION_ESTABLISHED:
3435
+ if (typeof record.sessionToken !== "string" || !record.sessionToken.trim()) return null;
3435
3436
  if (typeof record.accessToken !== "string" || !record.accessToken.trim()) return null;
3436
3437
  if (record.refreshToken !== void 0 && typeof record.refreshToken !== "string") return null;
3438
+ if (!isClaimedCustomer(record.customer)) return null;
3437
3439
  break;
3438
3440
  case MESSAGE_TYPES.ADDRESS_CHANGED:
3439
3441
  case MESSAGE_TYPES.ADDRESS_SELECTED:
@@ -3484,7 +3486,6 @@ var CimplifyElements = class {
3484
3486
  this.paymentData = null;
3485
3487
  this.checkoutInProgress = false;
3486
3488
  this.authInProgress = false;
3487
- this.authEstablishmentInProgress = false;
3488
3489
  this.authGeneration = 0;
3489
3490
  this.activeCheckoutAbort = null;
3490
3491
  this.businessIdResolvePromise = null;
@@ -3499,7 +3500,7 @@ var CimplifyElements = class {
3499
3500
  window.addEventListener("message", this.boundHandleMessage);
3500
3501
  }
3501
3502
  this.stopSessionSync = client.events.on(SESSION_CLAIMED, () => {
3502
- if (!this.authInProgress && !this.authEstablishmentInProgress) this.syncHostSession();
3503
+ if (!this.authInProgress) this._syncHostSession();
3503
3504
  });
3504
3505
  void this.restoreSession();
3505
3506
  }
@@ -3508,20 +3509,24 @@ var CimplifyElements = class {
3508
3509
  console.debug(...args);
3509
3510
  }
3510
3511
  }
3511
- syncHostSession(element) {
3512
- if (this.authEstablishmentInProgress) return;
3512
+ _syncHostSession(element) {
3513
3513
  const sessionToken = this.client.getSessionToken();
3514
- const token = this.client.getAccessToken() ?? this.accessToken;
3514
+ if (!sessionToken) return;
3515
+ const identity = {
3516
+ type: MESSAGE_TYPES.IDENTITY,
3517
+ sessionToken,
3518
+ accessToken: this.client.getAccessToken() ?? this.accessToken,
3519
+ customer: this.identityCustomer()
3520
+ };
3515
3521
  const targets = element ? [element] : Array.from(this.elements.values());
3516
3522
  for (const target of targets) {
3517
- if (sessionToken) {
3518
- target.sendMessage({ type: MESSAGE_TYPES.SET_SESSION, sessionToken });
3519
- }
3520
- if (token) {
3521
- target.sendMessage({ type: MESSAGE_TYPES.SET_TOKEN, token });
3522
- }
3523
+ target.sendMessage(identity);
3523
3524
  }
3524
3525
  }
3526
+ identityCustomer() {
3527
+ if (!this.accessToken || !this.customerId || !this.customerData) return null;
3528
+ return { id: this.customerId, ...this.customerData };
3529
+ }
3525
3530
  create(type, options = {}) {
3526
3531
  const existing = this.elements.get(type);
3527
3532
  if (existing) return existing;
@@ -3540,11 +3545,7 @@ var CimplifyElements = class {
3540
3545
  }
3541
3546
  destroy() {
3542
3547
  this.authGeneration += 1;
3543
- if (this.authEstablishmentInProgress) {
3544
- this.client.setAccessToken(null);
3545
- }
3546
3548
  this.authInProgress = false;
3547
- this.authEstablishmentInProgress = false;
3548
3549
  this.stopSessionSync();
3549
3550
  this.stopSessionSync = () => {
3550
3551
  };
@@ -3604,17 +3605,6 @@ var CimplifyElements = class {
3604
3605
  );
3605
3606
  }
3606
3607
  this.checkoutInProgress = true;
3607
- if (this.authEstablishmentInProgress) {
3608
- await this.waitForAuthEstablishment(AUTH_ESTABLISHMENT_WAIT_MS);
3609
- if (this.authEstablishmentInProgress) {
3610
- this.checkoutInProgress = false;
3611
- return toCheckoutError(
3612
- ErrorCode.AUTH_INCOMPLETE,
3613
- "Authentication must finish establishing the checkout session.",
3614
- true
3615
- );
3616
- }
3617
- }
3618
3608
  if (!options.cart_id && !options.intent_id) {
3619
3609
  this.debug("[cimplify:checkout] BLOCKED: no cart_id or intent_id");
3620
3610
  this.checkoutInProgress = false;
@@ -3819,7 +3809,7 @@ var CimplifyElements = class {
3819
3809
  hasAddress: !!processMessage.address,
3820
3810
  isUnifiedCheckout
3821
3811
  });
3822
- this.syncHostSession(checkoutElement);
3812
+ this._syncHostSession(checkoutElement);
3823
3813
  checkoutElement.sendMessage(processMessage);
3824
3814
  this.debug("[cimplify:checkout] postMessage sent, waiting for checkout_complete...");
3825
3815
  });
@@ -3930,7 +3920,6 @@ var CimplifyElements = class {
3930
3920
  this.customerData = null;
3931
3921
  this.addressData = null;
3932
3922
  this.paymentData = null;
3933
- this.authEstablishmentInProgress = false;
3934
3923
  this.client.clearSession();
3935
3924
  this.broadcastClearedSession();
3936
3925
  }
@@ -3966,73 +3955,73 @@ var CimplifyElements = class {
3966
3955
  this.broadcastClearedSession();
3967
3956
  return;
3968
3957
  }
3969
- if (application.kind !== "authenticated" || !this.broadcastAccessToken(result.value.session_token, generation)) {
3970
- return;
3971
- }
3958
+ if (application.kind !== "authenticated") return;
3959
+ this._syncHostSession();
3972
3960
  this.elements.forEach((element) => element._emitAuthenticated(application.data));
3973
3961
  }
3974
3962
  async applyAccessToken(token, generation) {
3975
3963
  if (this.authGeneration !== generation) return { kind: "incomplete" };
3976
- this.authEstablishmentInProgress = true;
3977
- try {
3978
- this.accessToken = null;
3979
- this.accountId = null;
3980
- this.customerId = null;
3981
- this.customerData = null;
3982
- this.addressData = null;
3983
- this.paymentData = null;
3984
- this.client.setAccessToken(token);
3985
- const claim = await this.client.support.claimConversation();
3986
- if (!this.isAuthGenerationCurrent(generation, token)) return { kind: "incomplete" };
3987
- if (!claim.ok && claim.error.code !== "already_claimed") {
3988
- if (this.client.hasClaimedSessionAuthority()) {
3989
- this.client.clearSession();
3990
- } else {
3991
- this.client.setAccessToken(null);
3992
- }
3993
- return {
3994
- kind: "claim_failed",
3995
- code: claim.error.code,
3996
- message: claim.error.userMessage
3997
- };
3998
- }
3999
- this.accessToken = token;
4000
- this.accountId = decodeJwtSub(token);
4001
- if (!await this.hydrateCustomerData(generation, token, true)) {
4002
- return { kind: "incomplete" };
3964
+ this.accessToken = null;
3965
+ this.accountId = null;
3966
+ this.customerId = null;
3967
+ this.customerData = null;
3968
+ this.addressData = null;
3969
+ this.paymentData = null;
3970
+ this.client.setAccessToken(token);
3971
+ const claim = await this.client.session.claim();
3972
+ if (!this.isAuthGenerationCurrent(generation, token)) return { kind: "incomplete" };
3973
+ if (!claim.ok && claim.error.code !== "already_claimed") {
3974
+ if (this.client.hasClaimedSessionAuthority()) {
3975
+ this.client.clearSession();
3976
+ } else {
3977
+ this.client.setAccessToken(null);
4003
3978
  }
4004
- if (!this.isAuthGenerationCurrent(generation, token)) return { kind: "incomplete" };
4005
3979
  return {
4006
- kind: "authenticated",
4007
- data: {
4008
- accountId: this.accountId ?? "",
4009
- customerId: this.customerId ?? "",
4010
- token,
4011
- customer: this.customerData ?? { name: "", email: null, phone: null }
4012
- }
3980
+ kind: "claim_failed",
3981
+ code: claim.error.code,
3982
+ message: claim.error.userMessage
4013
3983
  };
4014
- } finally {
4015
- this.authEstablishmentInProgress = false;
4016
- }
4017
- }
4018
- async waitForAuthEstablishment(timeoutMs) {
4019
- const deadline = Date.now() + timeoutMs;
4020
- while (this.authEstablishmentInProgress && Date.now() < deadline) {
4021
- await new Promise((resolve) => setTimeout(resolve, 100));
4022
3984
  }
4023
- }
4024
- broadcastAccessToken(token, generation) {
4025
- if (!this.isAuthGenerationCurrent(generation, token) || this.accessToken !== token || this.authEstablishmentInProgress) {
4026
- return false;
3985
+ this.accessToken = token;
3986
+ this.accountId = decodeJwtSub(token);
3987
+ if (!await this.hydrateCustomerData(generation, token, true)) {
3988
+ return { kind: "incomplete" };
4027
3989
  }
4028
- const sessionToken = this.client.getSessionToken();
4029
- this.elements.forEach((element) => {
4030
- if (sessionToken) {
4031
- element.sendMessage({ type: MESSAGE_TYPES.SET_SESSION, sessionToken });
3990
+ if (!this.isAuthGenerationCurrent(generation, token)) return { kind: "incomplete" };
3991
+ return {
3992
+ kind: "authenticated",
3993
+ data: {
3994
+ accountId: this.accountId ?? "",
3995
+ customerId: this.customerId ?? "",
3996
+ token,
3997
+ customer: this.customerData ?? { name: "", email: null, phone: null }
4032
3998
  }
4033
- element.sendMessage({ type: MESSAGE_TYPES.SET_TOKEN, token });
3999
+ };
4000
+ }
4001
+ adoptEstablishedSession(sourceElement, session) {
4002
+ this.authGeneration += 1;
4003
+ this.authInProgress = false;
4004
+ const { id, ...customer } = session.customer;
4005
+ this.accessToken = session.accessToken;
4006
+ this.accountId = decodeJwtSub(session.accessToken);
4007
+ this.customerId = id;
4008
+ this.customerData = customer;
4009
+ this.addressData = null;
4010
+ this.paymentData = null;
4011
+ this.client.setAccessToken(session.accessToken);
4012
+ this.persistRefreshToken(session.refreshToken);
4013
+ this.client.session.adopt(session.accessToken, {
4014
+ sessionToken: session.sessionToken,
4015
+ customerName: session.customer.name,
4016
+ customer: session.customer,
4017
+ cart: null
4018
+ });
4019
+ sourceElement._emitAuthenticated({
4020
+ accountId: this.accountId,
4021
+ customerId: id,
4022
+ token: session.accessToken,
4023
+ customer
4034
4024
  });
4035
- return true;
4036
4025
  }
4037
4026
  broadcastClearedSession() {
4038
4027
  const sessionToken = this.client.getSessionToken();
@@ -4079,7 +4068,8 @@ var CimplifyElements = class {
4079
4068
  return;
4080
4069
  }
4081
4070
  const application = await this.applyAccessToken(accessToken, generation);
4082
- if (application.kind === "authenticated" && this.broadcastAccessToken(accessToken, generation)) {
4071
+ if (application.kind === "authenticated") {
4072
+ this._syncHostSession();
4083
4073
  sourceElement._emitAuthenticated(application.data);
4084
4074
  } else if (application.kind === "claim_failed") {
4085
4075
  sourceElement._emitError({
@@ -4118,29 +4108,7 @@ var CimplifyElements = class {
4118
4108
  this.startEmbeddedAuth(sourceElement, message);
4119
4109
  break;
4120
4110
  case MESSAGE_TYPES.SESSION_ESTABLISHED:
4121
- {
4122
- this.authInProgress = false;
4123
- const generation = ++this.authGeneration;
4124
- void this.applyAccessToken(message.accessToken, generation).then((application) => {
4125
- if (this.authGeneration !== generation) return;
4126
- if (application.kind === "claim_failed") {
4127
- this.clearPersistedSession();
4128
- sourceElement.sendMessage({
4129
- type: MESSAGE_TYPES.SESSION_ESTABLISHMENT_FAILED,
4130
- accessToken: message.accessToken,
4131
- code: application.code,
4132
- message: application.message
4133
- });
4134
- this.broadcastClearedSession();
4135
- return;
4136
- }
4137
- if (application.kind !== "authenticated" || !this.broadcastAccessToken(message.accessToken, generation)) {
4138
- return;
4139
- }
4140
- this.persistRefreshToken(message.refreshToken);
4141
- sourceElement._emitAuthenticated(application.data);
4142
- });
4143
- }
4111
+ this.adoptEstablishedSession(sourceElement, message);
4144
4112
  break;
4145
4113
  case MESSAGE_TYPES.ADDRESS_CHANGED:
4146
4114
  case MESSAGE_TYPES.ADDRESS_SELECTED:
@@ -4162,7 +4130,6 @@ var CimplifyElements = class {
4162
4130
  case MESSAGE_TYPES.CONTINUE_WITHOUT_SAVED_DETAILS: {
4163
4131
  this.authGeneration += 1;
4164
4132
  this.authInProgress = false;
4165
- this.authEstablishmentInProgress = false;
4166
4133
  const carriedIdentityAuthority = Boolean(this.client.getAccessToken()) || this.client.hasClaimedSessionAuthority();
4167
4134
  this.accessToken = null;
4168
4135
  this.accountId = null;
@@ -4187,7 +4154,6 @@ var CimplifyElements = class {
4187
4154
  case MESSAGE_TYPES.LOGOUT_COMPLETE:
4188
4155
  this.authGeneration += 1;
4189
4156
  this.authInProgress = false;
4190
- this.authEstablishmentInProgress = false;
4191
4157
  if (this.checkoutInProgress && this.activeCheckoutAbort) {
4192
4158
  this.activeCheckoutAbort();
4193
4159
  }
@@ -4436,14 +4402,7 @@ var CimplifyElement = class {
4436
4402
  hostedSessionId: this.options.hostedSessionId,
4437
4403
  initialTerms: this.options.initialTerms
4438
4404
  });
4439
- const token = this.parent.getAccessToken();
4440
- const sessionToken = this.parent.getSessionToken();
4441
- if (sessionToken) {
4442
- this.sendMessage({ type: MESSAGE_TYPES.SET_SESSION, sessionToken });
4443
- }
4444
- if (token) {
4445
- this.sendMessage({ type: MESSAGE_TYPES.SET_TOKEN, token });
4446
- }
4405
+ this.parent._syncHostSession(this);
4447
4406
  this.pendingInit = null;
4448
4407
  }
4449
4408
  this.flushPending();
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- var chunkNJK3THWT_js = require('./chunk-NJK3THWT.js');
4
- var chunkBCLGREHS_js = require('./chunk-BCLGREHS.js');
3
+ var chunkNX5YBQZP_js = require('./chunk-NX5YBQZP.js');
4
+ var chunkW4ZFZPPY_js = require('./chunk-W4ZFZPPY.js');
5
5
  var chunkEV2EVOYS_js = require('./chunk-EV2EVOYS.js');
6
6
  var chunkAVXA4F6G_js = require('./chunk-AVXA4F6G.js');
7
7
  var zod = require('zod');
@@ -1185,7 +1185,7 @@ async function toForwardedRequest(input, init) {
1185
1185
  };
1186
1186
  }
1187
1187
  function createTestClient(options = {}) {
1188
- const mock = chunkNJK3THWT_js.createMockApp(options);
1188
+ const mock = chunkNX5YBQZP_js.createMockApp(options);
1189
1189
  let sessionToken;
1190
1190
  const fetchImpl = async (input, init) => {
1191
1191
  const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
@@ -1204,7 +1204,7 @@ function createTestClient(options = {}) {
1204
1204
  if (echoed) sessionToken = echoed;
1205
1205
  return res;
1206
1206
  };
1207
- const client = chunkBCLGREHS_js.createCimplifyClient({
1207
+ const client = chunkW4ZFZPPY_js.createCimplifyClient({
1208
1208
  baseUrl: TEST_BASE_URL,
1209
1209
  // Link mock lives at /v1/link on the same Hono app, so point the SDK's
1210
1210
  // separate Link transport at the same TEST_BASE_URL.