@cimplify/sdk 0.4.0 → 0.5.1

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;
@@ -1581,11 +1640,19 @@ function deriveUrls() {
1581
1640
  linkApiUrl: "https://api.cimplify.io"
1582
1641
  };
1583
1642
  }
1643
+ function getEnvPublicKey() {
1644
+ try {
1645
+ const env = globalThis.process;
1646
+ return env?.env.NEXT_PUBLIC_CIMPLIFY_PUBLIC_KEY || void 0;
1647
+ } catch {
1648
+ return void 0;
1649
+ }
1650
+ }
1584
1651
  var CimplifyClient = class {
1585
1652
  constructor(config = {}) {
1586
- this.sessionToken = null;
1653
+ this.accessToken = null;
1587
1654
  this.inflightRequests = /* @__PURE__ */ new Map();
1588
- this.publicKey = config.publicKey || "";
1655
+ this.publicKey = config.publicKey || getEnvPublicKey() || "";
1589
1656
  const urls = deriveUrls();
1590
1657
  this.baseUrl = urls.baseUrl;
1591
1658
  this.linkApiUrl = urls.linkApiUrl;
@@ -1594,15 +1661,28 @@ var CimplifyClient = class {
1594
1661
  this.maxRetries = config.maxRetries ?? DEFAULT_MAX_RETRIES;
1595
1662
  this.retryDelay = config.retryDelay ?? DEFAULT_RETRY_DELAY_MS;
1596
1663
  this.hooks = config.hooks ?? {};
1597
- this.sessionToken = this.loadSessionToken();
1664
+ this.accessToken = this.loadAccessToken();
1665
+ if (!this.publicKey) {
1666
+ console.warn(
1667
+ '[Cimplify] No public key found. Set NEXT_PUBLIC_CIMPLIFY_PUBLIC_KEY in your environment, or pass { publicKey: "pk_..." } to createCimplifyClient().'
1668
+ );
1669
+ }
1598
1670
  }
1671
+ /** @deprecated Use getAccessToken() instead */
1599
1672
  getSessionToken() {
1600
- return this.sessionToken;
1673
+ return this.accessToken;
1601
1674
  }
1675
+ /** @deprecated Use setAccessToken() instead */
1602
1676
  setSessionToken(token) {
1603
- const previous = this.sessionToken;
1604
- this.sessionToken = token;
1605
- this.saveSessionToken(token);
1677
+ this.setAccessToken(token);
1678
+ }
1679
+ getAccessToken() {
1680
+ return this.accessToken;
1681
+ }
1682
+ setAccessToken(token) {
1683
+ const previous = this.accessToken;
1684
+ this.accessToken = token;
1685
+ this.saveAccessToken(token);
1606
1686
  this.hooks.onSessionChange?.({
1607
1687
  previousToken: previous,
1608
1688
  newToken: token,
@@ -1610,27 +1690,27 @@ var CimplifyClient = class {
1610
1690
  });
1611
1691
  }
1612
1692
  clearSession() {
1613
- const previous = this.sessionToken;
1614
- this.sessionToken = null;
1615
- this.saveSessionToken(null);
1693
+ const previous = this.accessToken;
1694
+ this.accessToken = null;
1695
+ this.saveAccessToken(null);
1616
1696
  this.hooks.onSessionChange?.({
1617
1697
  previousToken: previous,
1618
1698
  newToken: null,
1619
1699
  source: "clear"
1620
1700
  });
1621
1701
  }
1622
- loadSessionToken() {
1702
+ loadAccessToken() {
1623
1703
  if (typeof window !== "undefined" && window.localStorage) {
1624
- return localStorage.getItem(SESSION_STORAGE_KEY);
1704
+ return localStorage.getItem(ACCESS_TOKEN_STORAGE_KEY);
1625
1705
  }
1626
1706
  return null;
1627
1707
  }
1628
- saveSessionToken(token) {
1708
+ saveAccessToken(token) {
1629
1709
  if (typeof window !== "undefined" && window.localStorage) {
1630
1710
  if (token) {
1631
- localStorage.setItem(SESSION_STORAGE_KEY, token);
1711
+ localStorage.setItem(ACCESS_TOKEN_STORAGE_KEY, token);
1632
1712
  } else {
1633
- localStorage.removeItem(SESSION_STORAGE_KEY);
1713
+ localStorage.removeItem(ACCESS_TOKEN_STORAGE_KEY);
1634
1714
  }
1635
1715
  }
1636
1716
  }
@@ -1639,24 +1719,11 @@ var CimplifyClient = class {
1639
1719
  "Content-Type": "application/json",
1640
1720
  "X-API-Key": this.publicKey
1641
1721
  };
1642
- if (this.sessionToken) {
1643
- headers[SESSION_TOKEN_HEADER] = this.sessionToken;
1722
+ if (this.accessToken) {
1723
+ headers["Authorization"] = `Bearer ${this.accessToken}`;
1644
1724
  }
1645
1725
  return headers;
1646
1726
  }
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
1727
  async resilientFetch(url, options) {
1661
1728
  const method = options.method || "GET";
1662
1729
  const path = url.replace(this.baseUrl, "").replace(this.linkApiUrl, "");
@@ -1768,7 +1835,6 @@ var CimplifyClient = class {
1768
1835
  headers: this.getHeaders(),
1769
1836
  body: JSON.stringify(body)
1770
1837
  });
1771
- this.updateSessionFromResponse(response);
1772
1838
  return this.handleResponse(response);
1773
1839
  });
1774
1840
  }
@@ -1783,7 +1849,6 @@ var CimplifyClient = class {
1783
1849
  headers: this.getHeaders(),
1784
1850
  body: JSON.stringify(body)
1785
1851
  });
1786
- this.updateSessionFromResponse(response);
1787
1852
  return this.handleResponse(response);
1788
1853
  }
1789
1854
  async get(path) {
@@ -1794,7 +1859,6 @@ var CimplifyClient = class {
1794
1859
  credentials: this.credentials,
1795
1860
  headers: this.getHeaders()
1796
1861
  });
1797
- this.updateSessionFromResponse(response);
1798
1862
  return this.handleRestResponse(response);
1799
1863
  });
1800
1864
  }
@@ -1805,7 +1869,6 @@ var CimplifyClient = class {
1805
1869
  headers: this.getHeaders(),
1806
1870
  body: body ? JSON.stringify(body) : void 0
1807
1871
  });
1808
- this.updateSessionFromResponse(response);
1809
1872
  return this.handleRestResponse(response);
1810
1873
  }
1811
1874
  async delete(path) {
@@ -1814,7 +1877,6 @@ var CimplifyClient = class {
1814
1877
  credentials: this.credentials,
1815
1878
  headers: this.getHeaders()
1816
1879
  });
1817
- this.updateSessionFromResponse(response);
1818
1880
  return this.handleRestResponse(response);
1819
1881
  }
1820
1882
  async linkGet(path) {
@@ -1825,7 +1887,6 @@ var CimplifyClient = class {
1825
1887
  credentials: this.credentials,
1826
1888
  headers: this.getHeaders()
1827
1889
  });
1828
- this.updateSessionFromResponse(response);
1829
1890
  return this.handleRestResponse(response);
1830
1891
  });
1831
1892
  }
@@ -1836,7 +1897,6 @@ var CimplifyClient = class {
1836
1897
  headers: this.getHeaders(),
1837
1898
  body: body ? JSON.stringify(body) : void 0
1838
1899
  });
1839
- this.updateSessionFromResponse(response);
1840
1900
  return this.handleRestResponse(response);
1841
1901
  }
1842
1902
  async linkDelete(path) {
@@ -1845,7 +1905,6 @@ var CimplifyClient = class {
1845
1905
  credentials: this.credentials,
1846
1906
  headers: this.getHeaders()
1847
1907
  });
1848
- this.updateSessionFromResponse(response);
1849
1908
  return this.handleRestResponse(response);
1850
1909
  }
1851
1910
  async handleRestResponse(response) {
@@ -1948,7 +2007,7 @@ var CimplifyClient = class {
1948
2007
  return createElements(this, businessId, options);
1949
2008
  }
1950
2009
  };
1951
- function createCimplifyClient(config) {
2010
+ function createCimplifyClient(config = {}) {
1952
2011
  return new CimplifyClient(config);
1953
2012
  }
1954
2013
 
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-CsufxnHH.mjs';
2
+ export { eq as AdConfig } from './ads-CsufxnHH.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-CsufxnHH.js';
2
+ export { eq as AdConfig } from './ads-CsufxnHH.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 };