@cimplify/sdk 0.4.0 → 0.5.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.
package/dist/index.mjs CHANGED
@@ -1065,6 +1065,9 @@ var InventoryService = class {
1065
1065
  };
1066
1066
 
1067
1067
  // src/scheduling.ts
1068
+ function toVariables(input) {
1069
+ return Object.fromEntries(Object.entries(input));
1070
+ }
1068
1071
  function toCimplifyError9(error) {
1069
1072
  if (error instanceof CimplifyError) return error;
1070
1073
  if (error instanceof Error) {
@@ -1097,17 +1100,14 @@ var SchedulingService = class {
1097
1100
  }
1098
1101
  async getAvailableSlots(input) {
1099
1102
  return safe9(
1100
- this.client.query(
1101
- "scheduling.slots",
1102
- input
1103
- )
1103
+ this.client.query("scheduling.slots", toVariables(input))
1104
1104
  );
1105
1105
  }
1106
1106
  async checkSlotAvailability(input) {
1107
1107
  return safe9(
1108
1108
  this.client.query(
1109
1109
  "scheduling.check_availability",
1110
- input
1110
+ toVariables(input)
1111
1111
  )
1112
1112
  );
1113
1113
  }
@@ -1115,7 +1115,7 @@ var SchedulingService = class {
1115
1115
  return safe9(
1116
1116
  this.client.query(
1117
1117
  "scheduling.availability",
1118
- params
1118
+ toVariables(params)
1119
1119
  )
1120
1120
  );
1121
1121
  }
@@ -1264,7 +1264,56 @@ var EVENT_TYPES = {
1264
1264
  };
1265
1265
 
1266
1266
  // src/elements.ts
1267
+ function mapOrderType(orderType) {
1268
+ if (orderType === "dine_in") return "dine-in";
1269
+ return orderType ?? "delivery";
1270
+ }
1271
+ function toCheckoutFormData(data) {
1272
+ return {
1273
+ cart_id: data.cart_id,
1274
+ customer: {
1275
+ name: "",
1276
+ email: "",
1277
+ phone: "",
1278
+ save_details: false
1279
+ },
1280
+ order_type: mapOrderType(data.order_type),
1281
+ address_info: data.address ? {
1282
+ street_address: data.address.street_address,
1283
+ apartment: data.address.apartment,
1284
+ city: data.address.city,
1285
+ region: data.address.region,
1286
+ postal_code: data.address.postal_code,
1287
+ country: data.address.country,
1288
+ delivery_instructions: data.address.delivery_instructions,
1289
+ phone_for_delivery: data.address.phone_for_delivery
1290
+ } : {},
1291
+ payment_method: data.payment_method?.type ?? "mobile_money",
1292
+ mobile_money_details: data.payment_method?.type === "mobile_money" && data.payment_method.phone_number ? {
1293
+ phone_number: data.payment_method.phone_number,
1294
+ provider: data.payment_method.provider ?? "mtn"
1295
+ } : void 0,
1296
+ special_instructions: data.notes,
1297
+ link_address_id: data.address?.id,
1298
+ link_payment_method_id: data.payment_method?.id
1299
+ };
1300
+ }
1267
1301
  var DEFAULT_LINK_URL = "https://link.cimplify.io";
1302
+ function isAllowedOrigin(origin) {
1303
+ try {
1304
+ const url = new URL(origin);
1305
+ const hostname = url.hostname;
1306
+ if (hostname === "localhost" || hostname === "127.0.0.1") {
1307
+ return true;
1308
+ }
1309
+ if (url.protocol !== "https:") {
1310
+ return false;
1311
+ }
1312
+ return hostname === "cimplify.io" || hostname.endsWith(".cimplify.io");
1313
+ } catch {
1314
+ return false;
1315
+ }
1316
+ }
1268
1317
  var CimplifyElements = class {
1269
1318
  constructor(client, businessId, options = {}) {
1270
1319
  this.elements = /* @__PURE__ */ new Map();
@@ -1277,8 +1326,9 @@ var CimplifyElements = class {
1277
1326
  this.businessId = businessId;
1278
1327
  this.linkUrl = options.linkUrl || DEFAULT_LINK_URL;
1279
1328
  this.options = options;
1329
+ this.boundHandleMessage = this.handleMessage.bind(this);
1280
1330
  if (typeof window !== "undefined") {
1281
- window.addEventListener("message", this.handleMessage.bind(this));
1331
+ window.addEventListener("message", this.boundHandleMessage);
1282
1332
  }
1283
1333
  }
1284
1334
  create(type, options = {}) {
@@ -1295,7 +1345,7 @@ var CimplifyElements = class {
1295
1345
  this.elements.forEach((element) => element.destroy());
1296
1346
  this.elements.clear();
1297
1347
  if (typeof window !== "undefined") {
1298
- window.removeEventListener("message", this.handleMessage.bind(this));
1348
+ window.removeEventListener("message", this.boundHandleMessage);
1299
1349
  }
1300
1350
  }
1301
1351
  async submitCheckout(data) {
@@ -1303,15 +1353,24 @@ var CimplifyElements = class {
1303
1353
  if (addressElement) await addressElement.getData();
1304
1354
  const paymentElement = this.elements.get(ELEMENT_TYPES.PAYMENT);
1305
1355
  if (paymentElement) await paymentElement.getData();
1306
- const checkoutData = {
1356
+ const internalData = {
1307
1357
  ...data,
1308
1358
  customer: this.accountId ? { account_id: this.accountId, customer_id: this.customerId } : void 0,
1309
1359
  address: this.addressData,
1310
1360
  payment_method: this.paymentData
1311
1361
  };
1312
- const result = await this.client.checkout.process(checkoutData);
1362
+ const checkoutFormData = toCheckoutFormData(internalData);
1363
+ const result = await this.client.checkout.process(checkoutFormData);
1313
1364
  if (result.ok) {
1314
- return { success: true, order: result.value };
1365
+ return {
1366
+ success: true,
1367
+ order: {
1368
+ id: result.value.order_id,
1369
+ status: result.value.payment_status,
1370
+ total: ""
1371
+ // Total is not returned by checkout result
1372
+ }
1373
+ };
1315
1374
  }
1316
1375
  return {
1317
1376
  success: false,
@@ -1328,7 +1387,7 @@ var CimplifyElements = class {
1328
1387
  return this.accessToken;
1329
1388
  }
1330
1389
  handleMessage(event) {
1331
- if (!event.origin.includes("cimplify.io") && !event.origin.includes("localhost")) {
1390
+ if (!isAllowedOrigin(event.origin)) {
1332
1391
  return;
1333
1392
  }
1334
1393
  const message = event.data;
@@ -1382,8 +1441,9 @@ var CimplifyElement = class {
1382
1441
  this.linkUrl = linkUrl;
1383
1442
  this.options = options;
1384
1443
  this.parent = parent;
1444
+ this.boundHandleMessage = this.handleMessage.bind(this);
1385
1445
  if (typeof window !== "undefined") {
1386
- window.addEventListener("message", this.handleMessage.bind(this));
1446
+ window.addEventListener("message", this.boundHandleMessage);
1387
1447
  }
1388
1448
  }
1389
1449
  mount(container) {
@@ -1409,7 +1469,7 @@ var CimplifyElement = class {
1409
1469
  this.mounted = false;
1410
1470
  this.eventHandlers.clear();
1411
1471
  if (typeof window !== "undefined") {
1412
- window.removeEventListener("message", this.handleMessage.bind(this));
1472
+ window.removeEventListener("message", this.boundHandleMessage);
1413
1473
  }
1414
1474
  }
1415
1475
  on(event, handler) {
@@ -1469,7 +1529,7 @@ var CimplifyElement = class {
1469
1529
  };
1470
1530
  }
1471
1531
  handleMessage(event) {
1472
- if (!event.origin.includes("cimplify.io") && !event.origin.includes("localhost")) {
1532
+ if (!isAllowedOrigin(event.origin)) {
1473
1533
  return;
1474
1534
  }
1475
1535
  const message = event.data;
@@ -1520,8 +1580,7 @@ function createElements(client, businessId, options) {
1520
1580
  }
1521
1581
 
1522
1582
  // src/client.ts
1523
- var SESSION_TOKEN_HEADER = "x-session-token";
1524
- var SESSION_STORAGE_KEY = "cimplify_session_token";
1583
+ var ACCESS_TOKEN_STORAGE_KEY = "cimplify_access_token";
1525
1584
  var DEFAULT_TIMEOUT_MS = 3e4;
1526
1585
  var DEFAULT_MAX_RETRIES = 3;
1527
1586
  var DEFAULT_RETRY_DELAY_MS = 1e3;
@@ -1583,7 +1642,7 @@ function deriveUrls() {
1583
1642
  }
1584
1643
  var CimplifyClient = class {
1585
1644
  constructor(config = {}) {
1586
- this.sessionToken = null;
1645
+ this.accessToken = null;
1587
1646
  this.inflightRequests = /* @__PURE__ */ new Map();
1588
1647
  this.publicKey = config.publicKey || "";
1589
1648
  const urls = deriveUrls();
@@ -1594,15 +1653,23 @@ var CimplifyClient = class {
1594
1653
  this.maxRetries = config.maxRetries ?? DEFAULT_MAX_RETRIES;
1595
1654
  this.retryDelay = config.retryDelay ?? DEFAULT_RETRY_DELAY_MS;
1596
1655
  this.hooks = config.hooks ?? {};
1597
- this.sessionToken = this.loadSessionToken();
1656
+ this.accessToken = this.loadAccessToken();
1598
1657
  }
1658
+ /** @deprecated Use getAccessToken() instead */
1599
1659
  getSessionToken() {
1600
- return this.sessionToken;
1660
+ return this.accessToken;
1601
1661
  }
1662
+ /** @deprecated Use setAccessToken() instead */
1602
1663
  setSessionToken(token) {
1603
- const previous = this.sessionToken;
1604
- this.sessionToken = token;
1605
- this.saveSessionToken(token);
1664
+ this.setAccessToken(token);
1665
+ }
1666
+ getAccessToken() {
1667
+ return this.accessToken;
1668
+ }
1669
+ setAccessToken(token) {
1670
+ const previous = this.accessToken;
1671
+ this.accessToken = token;
1672
+ this.saveAccessToken(token);
1606
1673
  this.hooks.onSessionChange?.({
1607
1674
  previousToken: previous,
1608
1675
  newToken: token,
@@ -1610,27 +1677,27 @@ var CimplifyClient = class {
1610
1677
  });
1611
1678
  }
1612
1679
  clearSession() {
1613
- const previous = this.sessionToken;
1614
- this.sessionToken = null;
1615
- this.saveSessionToken(null);
1680
+ const previous = this.accessToken;
1681
+ this.accessToken = null;
1682
+ this.saveAccessToken(null);
1616
1683
  this.hooks.onSessionChange?.({
1617
1684
  previousToken: previous,
1618
1685
  newToken: null,
1619
1686
  source: "clear"
1620
1687
  });
1621
1688
  }
1622
- loadSessionToken() {
1689
+ loadAccessToken() {
1623
1690
  if (typeof window !== "undefined" && window.localStorage) {
1624
- return localStorage.getItem(SESSION_STORAGE_KEY);
1691
+ return localStorage.getItem(ACCESS_TOKEN_STORAGE_KEY);
1625
1692
  }
1626
1693
  return null;
1627
1694
  }
1628
- saveSessionToken(token) {
1695
+ saveAccessToken(token) {
1629
1696
  if (typeof window !== "undefined" && window.localStorage) {
1630
1697
  if (token) {
1631
- localStorage.setItem(SESSION_STORAGE_KEY, token);
1698
+ localStorage.setItem(ACCESS_TOKEN_STORAGE_KEY, token);
1632
1699
  } else {
1633
- localStorage.removeItem(SESSION_STORAGE_KEY);
1700
+ localStorage.removeItem(ACCESS_TOKEN_STORAGE_KEY);
1634
1701
  }
1635
1702
  }
1636
1703
  }
@@ -1639,24 +1706,11 @@ var CimplifyClient = class {
1639
1706
  "Content-Type": "application/json",
1640
1707
  "X-API-Key": this.publicKey
1641
1708
  };
1642
- if (this.sessionToken) {
1643
- headers[SESSION_TOKEN_HEADER] = this.sessionToken;
1709
+ if (this.accessToken) {
1710
+ headers["Authorization"] = `Bearer ${this.accessToken}`;
1644
1711
  }
1645
1712
  return headers;
1646
1713
  }
1647
- updateSessionFromResponse(response) {
1648
- const newToken = response.headers.get(SESSION_TOKEN_HEADER);
1649
- if (newToken && newToken !== this.sessionToken) {
1650
- const previous = this.sessionToken;
1651
- this.sessionToken = newToken;
1652
- this.saveSessionToken(newToken);
1653
- this.hooks.onSessionChange?.({
1654
- previousToken: previous,
1655
- newToken,
1656
- source: "response"
1657
- });
1658
- }
1659
- }
1660
1714
  async resilientFetch(url, options) {
1661
1715
  const method = options.method || "GET";
1662
1716
  const path = url.replace(this.baseUrl, "").replace(this.linkApiUrl, "");
@@ -1768,7 +1822,6 @@ var CimplifyClient = class {
1768
1822
  headers: this.getHeaders(),
1769
1823
  body: JSON.stringify(body)
1770
1824
  });
1771
- this.updateSessionFromResponse(response);
1772
1825
  return this.handleResponse(response);
1773
1826
  });
1774
1827
  }
@@ -1783,7 +1836,6 @@ var CimplifyClient = class {
1783
1836
  headers: this.getHeaders(),
1784
1837
  body: JSON.stringify(body)
1785
1838
  });
1786
- this.updateSessionFromResponse(response);
1787
1839
  return this.handleResponse(response);
1788
1840
  }
1789
1841
  async get(path) {
@@ -1794,7 +1846,6 @@ var CimplifyClient = class {
1794
1846
  credentials: this.credentials,
1795
1847
  headers: this.getHeaders()
1796
1848
  });
1797
- this.updateSessionFromResponse(response);
1798
1849
  return this.handleRestResponse(response);
1799
1850
  });
1800
1851
  }
@@ -1805,7 +1856,6 @@ var CimplifyClient = class {
1805
1856
  headers: this.getHeaders(),
1806
1857
  body: body ? JSON.stringify(body) : void 0
1807
1858
  });
1808
- this.updateSessionFromResponse(response);
1809
1859
  return this.handleRestResponse(response);
1810
1860
  }
1811
1861
  async delete(path) {
@@ -1814,7 +1864,6 @@ var CimplifyClient = class {
1814
1864
  credentials: this.credentials,
1815
1865
  headers: this.getHeaders()
1816
1866
  });
1817
- this.updateSessionFromResponse(response);
1818
1867
  return this.handleRestResponse(response);
1819
1868
  }
1820
1869
  async linkGet(path) {
@@ -1825,7 +1874,6 @@ var CimplifyClient = class {
1825
1874
  credentials: this.credentials,
1826
1875
  headers: this.getHeaders()
1827
1876
  });
1828
- this.updateSessionFromResponse(response);
1829
1877
  return this.handleRestResponse(response);
1830
1878
  });
1831
1879
  }
@@ -1836,7 +1884,6 @@ var CimplifyClient = class {
1836
1884
  headers: this.getHeaders(),
1837
1885
  body: body ? JSON.stringify(body) : void 0
1838
1886
  });
1839
- this.updateSessionFromResponse(response);
1840
1887
  return this.handleRestResponse(response);
1841
1888
  }
1842
1889
  async linkDelete(path) {
@@ -1845,7 +1892,6 @@ var CimplifyClient = class {
1845
1892
  credentials: this.credentials,
1846
1893
  headers: this.getHeaders()
1847
1894
  });
1848
- this.updateSessionFromResponse(response);
1849
1895
  return this.handleRestResponse(response);
1850
1896
  }
1851
1897
  async handleRestResponse(response) {
package/dist/react.d.mts CHANGED
@@ -1,6 +1,52 @@
1
- import { r as CimplifyElements, C as CimplifyClient, v as ElementsOptions, eh as AuthenticatedData, ef as AddressInfo, eg as PaymentMethodInfo, ej as ElementsCheckoutResult } from './client-Ug3b8v1t.mjs';
1
+ import { en as AdSlot, eo as AdPosition, es as AdContextValue, r as CimplifyElements, C as CimplifyClient, v as ElementsOptions, eh as AuthenticatedData, ef as AddressInfo, eg as PaymentMethodInfo, ej as ElementsCheckoutResult } from './ads-BOUIrN73.mjs';
2
+ export { eq as AdConfig } from './ads-BOUIrN73.mjs';
2
3
  import React, { ReactNode } from 'react';
3
4
 
5
+ interface UserIdentity {
6
+ uid: string;
7
+ isAuthenticated: boolean;
8
+ fingerprint: string;
9
+ signals: DeviceSignals;
10
+ }
11
+ interface DeviceSignals {
12
+ userAgent: string;
13
+ language: string;
14
+ languages: string[];
15
+ platform: string;
16
+ screenWidth: number;
17
+ screenHeight: number;
18
+ colorDepth: number;
19
+ pixelRatio: number;
20
+ timezone: string;
21
+ timezoneOffset: number;
22
+ touchSupport: boolean;
23
+ cookiesEnabled: boolean;
24
+ doNotTrack: boolean;
25
+ }
26
+
27
+ interface AdContextValueExtended extends AdContextValue {
28
+ identity: UserIdentity | null;
29
+ apiBase: string;
30
+ }
31
+ declare function useAds(): AdContextValueExtended;
32
+ interface AdProviderProps {
33
+ siteId: string;
34
+ apiBase?: string;
35
+ authenticatedAccountId?: string;
36
+ children: ReactNode;
37
+ }
38
+ declare function AdProvider({ siteId, apiBase, authenticatedAccountId, children, }: AdProviderProps): React.ReactElement;
39
+ interface AdProps {
40
+ slot: AdSlot;
41
+ position?: AdPosition;
42
+ className?: string;
43
+ style?: React.CSSProperties;
44
+ fallback?: ReactNode;
45
+ onImpression?: (adId: string) => void;
46
+ onClick?: (adId: string) => void;
47
+ }
48
+ declare function Ad({ slot, position, className, style, fallback, onImpression, onClick, }: AdProps): React.ReactElement | null;
49
+
4
50
  declare function useElements(): CimplifyElements | null;
5
51
  declare function useElementsReady(): boolean;
6
52
  interface ElementsProviderProps {
@@ -56,4 +102,4 @@ declare function useCheckout(): {
56
102
  isLoading: boolean;
57
103
  };
58
104
 
59
- export { AddressElement, AuthElement, ElementsProvider, PaymentElement, useCheckout, useElements, useElementsReady };
105
+ export { Ad, AdPosition, type AdProps, AdProvider, AdSlot, AddressElement, AuthElement, ElementsProvider, PaymentElement, useAds, useCheckout, useElements, useElementsReady };
package/dist/react.d.ts CHANGED
@@ -1,6 +1,52 @@
1
- import { r as CimplifyElements, C as CimplifyClient, v as ElementsOptions, eh as AuthenticatedData, ef as AddressInfo, eg as PaymentMethodInfo, ej as ElementsCheckoutResult } from './client-Ug3b8v1t.js';
1
+ import { en as AdSlot, eo as AdPosition, es as AdContextValue, r as CimplifyElements, C as CimplifyClient, v as ElementsOptions, eh as AuthenticatedData, ef as AddressInfo, eg as PaymentMethodInfo, ej as ElementsCheckoutResult } from './ads-BOUIrN73.js';
2
+ export { eq as AdConfig } from './ads-BOUIrN73.js';
2
3
  import React, { ReactNode } from 'react';
3
4
 
5
+ interface UserIdentity {
6
+ uid: string;
7
+ isAuthenticated: boolean;
8
+ fingerprint: string;
9
+ signals: DeviceSignals;
10
+ }
11
+ interface DeviceSignals {
12
+ userAgent: string;
13
+ language: string;
14
+ languages: string[];
15
+ platform: string;
16
+ screenWidth: number;
17
+ screenHeight: number;
18
+ colorDepth: number;
19
+ pixelRatio: number;
20
+ timezone: string;
21
+ timezoneOffset: number;
22
+ touchSupport: boolean;
23
+ cookiesEnabled: boolean;
24
+ doNotTrack: boolean;
25
+ }
26
+
27
+ interface AdContextValueExtended extends AdContextValue {
28
+ identity: UserIdentity | null;
29
+ apiBase: string;
30
+ }
31
+ declare function useAds(): AdContextValueExtended;
32
+ interface AdProviderProps {
33
+ siteId: string;
34
+ apiBase?: string;
35
+ authenticatedAccountId?: string;
36
+ children: ReactNode;
37
+ }
38
+ declare function AdProvider({ siteId, apiBase, authenticatedAccountId, children, }: AdProviderProps): React.ReactElement;
39
+ interface AdProps {
40
+ slot: AdSlot;
41
+ position?: AdPosition;
42
+ className?: string;
43
+ style?: React.CSSProperties;
44
+ fallback?: ReactNode;
45
+ onImpression?: (adId: string) => void;
46
+ onClick?: (adId: string) => void;
47
+ }
48
+ declare function Ad({ slot, position, className, style, fallback, onImpression, onClick, }: AdProps): React.ReactElement | null;
49
+
4
50
  declare function useElements(): CimplifyElements | null;
5
51
  declare function useElementsReady(): boolean;
6
52
  interface ElementsProviderProps {
@@ -56,4 +102,4 @@ declare function useCheckout(): {
56
102
  isLoading: boolean;
57
103
  };
58
104
 
59
- export { AddressElement, AuthElement, ElementsProvider, PaymentElement, useCheckout, useElements, useElementsReady };
105
+ export { Ad, AdPosition, type AdProps, AdProvider, AdSlot, AddressElement, AuthElement, ElementsProvider, PaymentElement, useAds, useCheckout, useElements, useElementsReady };