@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.js CHANGED
@@ -1067,6 +1067,9 @@ var InventoryService = class {
1067
1067
  };
1068
1068
 
1069
1069
  // src/scheduling.ts
1070
+ function toVariables(input) {
1071
+ return Object.fromEntries(Object.entries(input));
1072
+ }
1070
1073
  function toCimplifyError9(error) {
1071
1074
  if (error instanceof CimplifyError) return error;
1072
1075
  if (error instanceof Error) {
@@ -1099,17 +1102,14 @@ var SchedulingService = class {
1099
1102
  }
1100
1103
  async getAvailableSlots(input) {
1101
1104
  return safe9(
1102
- this.client.query(
1103
- "scheduling.slots",
1104
- input
1105
- )
1105
+ this.client.query("scheduling.slots", toVariables(input))
1106
1106
  );
1107
1107
  }
1108
1108
  async checkSlotAvailability(input) {
1109
1109
  return safe9(
1110
1110
  this.client.query(
1111
1111
  "scheduling.check_availability",
1112
- input
1112
+ toVariables(input)
1113
1113
  )
1114
1114
  );
1115
1115
  }
@@ -1117,7 +1117,7 @@ var SchedulingService = class {
1117
1117
  return safe9(
1118
1118
  this.client.query(
1119
1119
  "scheduling.availability",
1120
- params
1120
+ toVariables(params)
1121
1121
  )
1122
1122
  );
1123
1123
  }
@@ -1266,7 +1266,56 @@ var EVENT_TYPES = {
1266
1266
  };
1267
1267
 
1268
1268
  // src/elements.ts
1269
+ function mapOrderType(orderType) {
1270
+ if (orderType === "dine_in") return "dine-in";
1271
+ return orderType ?? "delivery";
1272
+ }
1273
+ function toCheckoutFormData(data) {
1274
+ return {
1275
+ cart_id: data.cart_id,
1276
+ customer: {
1277
+ name: "",
1278
+ email: "",
1279
+ phone: "",
1280
+ save_details: false
1281
+ },
1282
+ order_type: mapOrderType(data.order_type),
1283
+ address_info: data.address ? {
1284
+ street_address: data.address.street_address,
1285
+ apartment: data.address.apartment,
1286
+ city: data.address.city,
1287
+ region: data.address.region,
1288
+ postal_code: data.address.postal_code,
1289
+ country: data.address.country,
1290
+ delivery_instructions: data.address.delivery_instructions,
1291
+ phone_for_delivery: data.address.phone_for_delivery
1292
+ } : {},
1293
+ payment_method: data.payment_method?.type ?? "mobile_money",
1294
+ mobile_money_details: data.payment_method?.type === "mobile_money" && data.payment_method.phone_number ? {
1295
+ phone_number: data.payment_method.phone_number,
1296
+ provider: data.payment_method.provider ?? "mtn"
1297
+ } : void 0,
1298
+ special_instructions: data.notes,
1299
+ link_address_id: data.address?.id,
1300
+ link_payment_method_id: data.payment_method?.id
1301
+ };
1302
+ }
1269
1303
  var DEFAULT_LINK_URL = "https://link.cimplify.io";
1304
+ function isAllowedOrigin(origin) {
1305
+ try {
1306
+ const url = new URL(origin);
1307
+ const hostname = url.hostname;
1308
+ if (hostname === "localhost" || hostname === "127.0.0.1") {
1309
+ return true;
1310
+ }
1311
+ if (url.protocol !== "https:") {
1312
+ return false;
1313
+ }
1314
+ return hostname === "cimplify.io" || hostname.endsWith(".cimplify.io");
1315
+ } catch {
1316
+ return false;
1317
+ }
1318
+ }
1270
1319
  var CimplifyElements = class {
1271
1320
  constructor(client, businessId, options = {}) {
1272
1321
  this.elements = /* @__PURE__ */ new Map();
@@ -1279,8 +1328,9 @@ var CimplifyElements = class {
1279
1328
  this.businessId = businessId;
1280
1329
  this.linkUrl = options.linkUrl || DEFAULT_LINK_URL;
1281
1330
  this.options = options;
1331
+ this.boundHandleMessage = this.handleMessage.bind(this);
1282
1332
  if (typeof window !== "undefined") {
1283
- window.addEventListener("message", this.handleMessage.bind(this));
1333
+ window.addEventListener("message", this.boundHandleMessage);
1284
1334
  }
1285
1335
  }
1286
1336
  create(type, options = {}) {
@@ -1297,7 +1347,7 @@ var CimplifyElements = class {
1297
1347
  this.elements.forEach((element) => element.destroy());
1298
1348
  this.elements.clear();
1299
1349
  if (typeof window !== "undefined") {
1300
- window.removeEventListener("message", this.handleMessage.bind(this));
1350
+ window.removeEventListener("message", this.boundHandleMessage);
1301
1351
  }
1302
1352
  }
1303
1353
  async submitCheckout(data) {
@@ -1305,15 +1355,24 @@ var CimplifyElements = class {
1305
1355
  if (addressElement) await addressElement.getData();
1306
1356
  const paymentElement = this.elements.get(ELEMENT_TYPES.PAYMENT);
1307
1357
  if (paymentElement) await paymentElement.getData();
1308
- const checkoutData = {
1358
+ const internalData = {
1309
1359
  ...data,
1310
1360
  customer: this.accountId ? { account_id: this.accountId, customer_id: this.customerId } : void 0,
1311
1361
  address: this.addressData,
1312
1362
  payment_method: this.paymentData
1313
1363
  };
1314
- const result = await this.client.checkout.process(checkoutData);
1364
+ const checkoutFormData = toCheckoutFormData(internalData);
1365
+ const result = await this.client.checkout.process(checkoutFormData);
1315
1366
  if (result.ok) {
1316
- return { success: true, order: result.value };
1367
+ return {
1368
+ success: true,
1369
+ order: {
1370
+ id: result.value.order_id,
1371
+ status: result.value.payment_status,
1372
+ total: ""
1373
+ // Total is not returned by checkout result
1374
+ }
1375
+ };
1317
1376
  }
1318
1377
  return {
1319
1378
  success: false,
@@ -1330,7 +1389,7 @@ var CimplifyElements = class {
1330
1389
  return this.accessToken;
1331
1390
  }
1332
1391
  handleMessage(event) {
1333
- if (!event.origin.includes("cimplify.io") && !event.origin.includes("localhost")) {
1392
+ if (!isAllowedOrigin(event.origin)) {
1334
1393
  return;
1335
1394
  }
1336
1395
  const message = event.data;
@@ -1384,8 +1443,9 @@ var CimplifyElement = class {
1384
1443
  this.linkUrl = linkUrl;
1385
1444
  this.options = options;
1386
1445
  this.parent = parent;
1446
+ this.boundHandleMessage = this.handleMessage.bind(this);
1387
1447
  if (typeof window !== "undefined") {
1388
- window.addEventListener("message", this.handleMessage.bind(this));
1448
+ window.addEventListener("message", this.boundHandleMessage);
1389
1449
  }
1390
1450
  }
1391
1451
  mount(container) {
@@ -1411,7 +1471,7 @@ var CimplifyElement = class {
1411
1471
  this.mounted = false;
1412
1472
  this.eventHandlers.clear();
1413
1473
  if (typeof window !== "undefined") {
1414
- window.removeEventListener("message", this.handleMessage.bind(this));
1474
+ window.removeEventListener("message", this.boundHandleMessage);
1415
1475
  }
1416
1476
  }
1417
1477
  on(event, handler) {
@@ -1471,7 +1531,7 @@ var CimplifyElement = class {
1471
1531
  };
1472
1532
  }
1473
1533
  handleMessage(event) {
1474
- if (!event.origin.includes("cimplify.io") && !event.origin.includes("localhost")) {
1534
+ if (!isAllowedOrigin(event.origin)) {
1475
1535
  return;
1476
1536
  }
1477
1537
  const message = event.data;
@@ -1522,8 +1582,7 @@ function createElements(client, businessId, options) {
1522
1582
  }
1523
1583
 
1524
1584
  // src/client.ts
1525
- var SESSION_TOKEN_HEADER = "x-session-token";
1526
- var SESSION_STORAGE_KEY = "cimplify_session_token";
1585
+ var ACCESS_TOKEN_STORAGE_KEY = "cimplify_access_token";
1527
1586
  var DEFAULT_TIMEOUT_MS = 3e4;
1528
1587
  var DEFAULT_MAX_RETRIES = 3;
1529
1588
  var DEFAULT_RETRY_DELAY_MS = 1e3;
@@ -1583,11 +1642,19 @@ function deriveUrls() {
1583
1642
  linkApiUrl: "https://api.cimplify.io"
1584
1643
  };
1585
1644
  }
1645
+ function getEnvPublicKey() {
1646
+ try {
1647
+ const env = globalThis.process;
1648
+ return env?.env.NEXT_PUBLIC_CIMPLIFY_PUBLIC_KEY || void 0;
1649
+ } catch {
1650
+ return void 0;
1651
+ }
1652
+ }
1586
1653
  var CimplifyClient = class {
1587
1654
  constructor(config = {}) {
1588
- this.sessionToken = null;
1655
+ this.accessToken = null;
1589
1656
  this.inflightRequests = /* @__PURE__ */ new Map();
1590
- this.publicKey = config.publicKey || "";
1657
+ this.publicKey = config.publicKey || getEnvPublicKey() || "";
1591
1658
  const urls = deriveUrls();
1592
1659
  this.baseUrl = urls.baseUrl;
1593
1660
  this.linkApiUrl = urls.linkApiUrl;
@@ -1596,15 +1663,28 @@ var CimplifyClient = class {
1596
1663
  this.maxRetries = config.maxRetries ?? DEFAULT_MAX_RETRIES;
1597
1664
  this.retryDelay = config.retryDelay ?? DEFAULT_RETRY_DELAY_MS;
1598
1665
  this.hooks = config.hooks ?? {};
1599
- this.sessionToken = this.loadSessionToken();
1666
+ this.accessToken = this.loadAccessToken();
1667
+ if (!this.publicKey) {
1668
+ console.warn(
1669
+ '[Cimplify] No public key found. Set NEXT_PUBLIC_CIMPLIFY_PUBLIC_KEY in your environment, or pass { publicKey: "pk_..." } to createCimplifyClient().'
1670
+ );
1671
+ }
1600
1672
  }
1673
+ /** @deprecated Use getAccessToken() instead */
1601
1674
  getSessionToken() {
1602
- return this.sessionToken;
1675
+ return this.accessToken;
1603
1676
  }
1677
+ /** @deprecated Use setAccessToken() instead */
1604
1678
  setSessionToken(token) {
1605
- const previous = this.sessionToken;
1606
- this.sessionToken = token;
1607
- this.saveSessionToken(token);
1679
+ this.setAccessToken(token);
1680
+ }
1681
+ getAccessToken() {
1682
+ return this.accessToken;
1683
+ }
1684
+ setAccessToken(token) {
1685
+ const previous = this.accessToken;
1686
+ this.accessToken = token;
1687
+ this.saveAccessToken(token);
1608
1688
  this.hooks.onSessionChange?.({
1609
1689
  previousToken: previous,
1610
1690
  newToken: token,
@@ -1612,27 +1692,27 @@ var CimplifyClient = class {
1612
1692
  });
1613
1693
  }
1614
1694
  clearSession() {
1615
- const previous = this.sessionToken;
1616
- this.sessionToken = null;
1617
- this.saveSessionToken(null);
1695
+ const previous = this.accessToken;
1696
+ this.accessToken = null;
1697
+ this.saveAccessToken(null);
1618
1698
  this.hooks.onSessionChange?.({
1619
1699
  previousToken: previous,
1620
1700
  newToken: null,
1621
1701
  source: "clear"
1622
1702
  });
1623
1703
  }
1624
- loadSessionToken() {
1704
+ loadAccessToken() {
1625
1705
  if (typeof window !== "undefined" && window.localStorage) {
1626
- return localStorage.getItem(SESSION_STORAGE_KEY);
1706
+ return localStorage.getItem(ACCESS_TOKEN_STORAGE_KEY);
1627
1707
  }
1628
1708
  return null;
1629
1709
  }
1630
- saveSessionToken(token) {
1710
+ saveAccessToken(token) {
1631
1711
  if (typeof window !== "undefined" && window.localStorage) {
1632
1712
  if (token) {
1633
- localStorage.setItem(SESSION_STORAGE_KEY, token);
1713
+ localStorage.setItem(ACCESS_TOKEN_STORAGE_KEY, token);
1634
1714
  } else {
1635
- localStorage.removeItem(SESSION_STORAGE_KEY);
1715
+ localStorage.removeItem(ACCESS_TOKEN_STORAGE_KEY);
1636
1716
  }
1637
1717
  }
1638
1718
  }
@@ -1641,24 +1721,11 @@ var CimplifyClient = class {
1641
1721
  "Content-Type": "application/json",
1642
1722
  "X-API-Key": this.publicKey
1643
1723
  };
1644
- if (this.sessionToken) {
1645
- headers[SESSION_TOKEN_HEADER] = this.sessionToken;
1724
+ if (this.accessToken) {
1725
+ headers["Authorization"] = `Bearer ${this.accessToken}`;
1646
1726
  }
1647
1727
  return headers;
1648
1728
  }
1649
- updateSessionFromResponse(response) {
1650
- const newToken = response.headers.get(SESSION_TOKEN_HEADER);
1651
- if (newToken && newToken !== this.sessionToken) {
1652
- const previous = this.sessionToken;
1653
- this.sessionToken = newToken;
1654
- this.saveSessionToken(newToken);
1655
- this.hooks.onSessionChange?.({
1656
- previousToken: previous,
1657
- newToken,
1658
- source: "response"
1659
- });
1660
- }
1661
- }
1662
1729
  async resilientFetch(url, options) {
1663
1730
  const method = options.method || "GET";
1664
1731
  const path = url.replace(this.baseUrl, "").replace(this.linkApiUrl, "");
@@ -1770,7 +1837,6 @@ var CimplifyClient = class {
1770
1837
  headers: this.getHeaders(),
1771
1838
  body: JSON.stringify(body)
1772
1839
  });
1773
- this.updateSessionFromResponse(response);
1774
1840
  return this.handleResponse(response);
1775
1841
  });
1776
1842
  }
@@ -1785,7 +1851,6 @@ var CimplifyClient = class {
1785
1851
  headers: this.getHeaders(),
1786
1852
  body: JSON.stringify(body)
1787
1853
  });
1788
- this.updateSessionFromResponse(response);
1789
1854
  return this.handleResponse(response);
1790
1855
  }
1791
1856
  async get(path) {
@@ -1796,7 +1861,6 @@ var CimplifyClient = class {
1796
1861
  credentials: this.credentials,
1797
1862
  headers: this.getHeaders()
1798
1863
  });
1799
- this.updateSessionFromResponse(response);
1800
1864
  return this.handleRestResponse(response);
1801
1865
  });
1802
1866
  }
@@ -1807,7 +1871,6 @@ var CimplifyClient = class {
1807
1871
  headers: this.getHeaders(),
1808
1872
  body: body ? JSON.stringify(body) : void 0
1809
1873
  });
1810
- this.updateSessionFromResponse(response);
1811
1874
  return this.handleRestResponse(response);
1812
1875
  }
1813
1876
  async delete(path) {
@@ -1816,7 +1879,6 @@ var CimplifyClient = class {
1816
1879
  credentials: this.credentials,
1817
1880
  headers: this.getHeaders()
1818
1881
  });
1819
- this.updateSessionFromResponse(response);
1820
1882
  return this.handleRestResponse(response);
1821
1883
  }
1822
1884
  async linkGet(path) {
@@ -1827,7 +1889,6 @@ var CimplifyClient = class {
1827
1889
  credentials: this.credentials,
1828
1890
  headers: this.getHeaders()
1829
1891
  });
1830
- this.updateSessionFromResponse(response);
1831
1892
  return this.handleRestResponse(response);
1832
1893
  });
1833
1894
  }
@@ -1838,7 +1899,6 @@ var CimplifyClient = class {
1838
1899
  headers: this.getHeaders(),
1839
1900
  body: body ? JSON.stringify(body) : void 0
1840
1901
  });
1841
- this.updateSessionFromResponse(response);
1842
1902
  return this.handleRestResponse(response);
1843
1903
  }
1844
1904
  async linkDelete(path) {
@@ -1847,7 +1907,6 @@ var CimplifyClient = class {
1847
1907
  credentials: this.credentials,
1848
1908
  headers: this.getHeaders()
1849
1909
  });
1850
- this.updateSessionFromResponse(response);
1851
1910
  return this.handleRestResponse(response);
1852
1911
  }
1853
1912
  async handleRestResponse(response) {
@@ -1950,7 +2009,7 @@ var CimplifyClient = class {
1950
2009
  return createElements(this, businessId, options);
1951
2010
  }
1952
2011
  };
1953
- function createCimplifyClient(config) {
2012
+ function createCimplifyClient(config = {}) {
1954
2013
  return new CimplifyClient(config);
1955
2014
  }
1956
2015