@cimplify/sdk 0.3.1 → 0.3.2

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.d.mts CHANGED
@@ -2824,6 +2824,12 @@ declare class LiteService {
2824
2824
  interface CimplifyConfig {
2825
2825
  publicKey?: string;
2826
2826
  credentials?: RequestCredentials;
2827
+ /** Request timeout in milliseconds (default: 30000) */
2828
+ timeout?: number;
2829
+ /** Maximum retry attempts for retryable errors (default: 3) */
2830
+ maxRetries?: number;
2831
+ /** Base delay between retries in milliseconds (default: 1000) */
2832
+ retryDelay?: number;
2827
2833
  }
2828
2834
  declare class CimplifyClient {
2829
2835
  private baseUrl;
@@ -2831,6 +2837,9 @@ declare class CimplifyClient {
2831
2837
  private publicKey;
2832
2838
  private credentials;
2833
2839
  private sessionToken;
2840
+ private timeout;
2841
+ private maxRetries;
2842
+ private retryDelay;
2834
2843
  private _catalogue?;
2835
2844
  private _cart?;
2836
2845
  private _checkout?;
@@ -2849,6 +2858,11 @@ declare class CimplifyClient {
2849
2858
  private saveSessionToken;
2850
2859
  private getHeaders;
2851
2860
  private updateSessionFromResponse;
2861
+ /**
2862
+ * Resilient fetch with timeout and automatic retries for network errors.
2863
+ * Uses exponential backoff: 1s, 2s, 4s between retries.
2864
+ */
2865
+ private resilientFetch;
2852
2866
  query<T = unknown>(query: string, variables?: Record<string, unknown>): Promise<T>;
2853
2867
  call<T = unknown>(method: string, args?: unknown): Promise<T>;
2854
2868
  get<T = unknown>(path: string): Promise<T>;
package/dist/index.d.ts CHANGED
@@ -2824,6 +2824,12 @@ declare class LiteService {
2824
2824
  interface CimplifyConfig {
2825
2825
  publicKey?: string;
2826
2826
  credentials?: RequestCredentials;
2827
+ /** Request timeout in milliseconds (default: 30000) */
2828
+ timeout?: number;
2829
+ /** Maximum retry attempts for retryable errors (default: 3) */
2830
+ maxRetries?: number;
2831
+ /** Base delay between retries in milliseconds (default: 1000) */
2832
+ retryDelay?: number;
2827
2833
  }
2828
2834
  declare class CimplifyClient {
2829
2835
  private baseUrl;
@@ -2831,6 +2837,9 @@ declare class CimplifyClient {
2831
2837
  private publicKey;
2832
2838
  private credentials;
2833
2839
  private sessionToken;
2840
+ private timeout;
2841
+ private maxRetries;
2842
+ private retryDelay;
2834
2843
  private _catalogue?;
2835
2844
  private _cart?;
2836
2845
  private _checkout?;
@@ -2849,6 +2858,11 @@ declare class CimplifyClient {
2849
2858
  private saveSessionToken;
2850
2859
  private getHeaders;
2851
2860
  private updateSessionFromResponse;
2861
+ /**
2862
+ * Resilient fetch with timeout and automatic retries for network errors.
2863
+ * Uses exponential backoff: 1s, 2s, 4s between retries.
2864
+ */
2865
+ private resilientFetch;
2852
2866
  query<T = unknown>(query: string, variables?: Record<string, unknown>): Promise<T>;
2853
2867
  call<T = unknown>(method: string, args?: unknown): Promise<T>;
2854
2868
  get<T = unknown>(path: string): Promise<T>;
package/dist/index.js CHANGED
@@ -1391,6 +1391,51 @@ var LiteService = class {
1391
1391
  // src/client.ts
1392
1392
  var SESSION_TOKEN_HEADER = "x-session-token";
1393
1393
  var SESSION_STORAGE_KEY = "cimplify_session_token";
1394
+ var DEFAULT_TIMEOUT_MS = 3e4;
1395
+ var DEFAULT_MAX_RETRIES = 3;
1396
+ var DEFAULT_RETRY_DELAY_MS = 1e3;
1397
+ function sleep(ms) {
1398
+ return new Promise((resolve) => setTimeout(resolve, ms));
1399
+ }
1400
+ function isRetryable(error) {
1401
+ if (error instanceof TypeError && error.message.includes("fetch")) {
1402
+ return true;
1403
+ }
1404
+ if (error instanceof DOMException && error.name === "AbortError") {
1405
+ return true;
1406
+ }
1407
+ if (error instanceof CimplifyError) {
1408
+ return error.retryable;
1409
+ }
1410
+ if (error instanceof CimplifyError && error.code === "SERVER_ERROR") {
1411
+ return true;
1412
+ }
1413
+ return false;
1414
+ }
1415
+ function toNetworkError(error) {
1416
+ if (error instanceof DOMException && error.name === "AbortError") {
1417
+ return new CimplifyError(
1418
+ ErrorCode.TIMEOUT,
1419
+ "Request timed out. Please check your connection and try again.",
1420
+ true
1421
+ );
1422
+ }
1423
+ if (error instanceof TypeError && error.message.includes("fetch")) {
1424
+ return new CimplifyError(
1425
+ ErrorCode.NETWORK_ERROR,
1426
+ "Network error. Please check your internet connection.",
1427
+ true
1428
+ );
1429
+ }
1430
+ if (error instanceof CimplifyError) {
1431
+ return error;
1432
+ }
1433
+ return new CimplifyError(
1434
+ ErrorCode.UNKNOWN_ERROR,
1435
+ error instanceof Error ? error.message : "An unknown error occurred",
1436
+ false
1437
+ );
1438
+ }
1394
1439
  function deriveUrls() {
1395
1440
  const hostname = typeof window !== "undefined" ? window.location.hostname : "";
1396
1441
  if (hostname === "localhost" || hostname === "127.0.0.1") {
@@ -1413,6 +1458,9 @@ var CimplifyClient = class {
1413
1458
  this.baseUrl = urls.baseUrl;
1414
1459
  this.linkApiUrl = urls.linkApiUrl;
1415
1460
  this.credentials = config.credentials || "include";
1461
+ this.timeout = config.timeout ?? DEFAULT_TIMEOUT_MS;
1462
+ this.maxRetries = config.maxRetries ?? DEFAULT_MAX_RETRIES;
1463
+ this.retryDelay = config.retryDelay ?? DEFAULT_RETRY_DELAY_MS;
1416
1464
  this.sessionToken = this.loadSessionToken();
1417
1465
  }
1418
1466
  getSessionToken() {
@@ -1458,12 +1506,47 @@ var CimplifyClient = class {
1458
1506
  this.saveSessionToken(newToken);
1459
1507
  }
1460
1508
  }
1509
+ /**
1510
+ * Resilient fetch with timeout and automatic retries for network errors.
1511
+ * Uses exponential backoff: 1s, 2s, 4s between retries.
1512
+ */
1513
+ async resilientFetch(url, options) {
1514
+ let lastError;
1515
+ for (let attempt = 0; attempt <= this.maxRetries; attempt++) {
1516
+ try {
1517
+ const controller = new AbortController();
1518
+ const timeoutId = setTimeout(() => controller.abort(), this.timeout);
1519
+ const response = await fetch(url, {
1520
+ ...options,
1521
+ signal: controller.signal
1522
+ });
1523
+ clearTimeout(timeoutId);
1524
+ if (response.ok || response.status >= 400 && response.status < 500) {
1525
+ return response;
1526
+ }
1527
+ if (response.status >= 500 && attempt < this.maxRetries) {
1528
+ const delay = this.retryDelay * Math.pow(2, attempt);
1529
+ await sleep(delay);
1530
+ continue;
1531
+ }
1532
+ return response;
1533
+ } catch (error) {
1534
+ lastError = error;
1535
+ if (!isRetryable(error) || attempt >= this.maxRetries) {
1536
+ throw toNetworkError(error);
1537
+ }
1538
+ const delay = this.retryDelay * Math.pow(2, attempt);
1539
+ await sleep(delay);
1540
+ }
1541
+ }
1542
+ throw toNetworkError(lastError);
1543
+ }
1461
1544
  async query(query2, variables) {
1462
1545
  const body = { query: query2 };
1463
1546
  if (variables) {
1464
1547
  body.variables = variables;
1465
1548
  }
1466
- const response = await fetch(`${this.baseUrl}/api/q`, {
1549
+ const response = await this.resilientFetch(`${this.baseUrl}/api/q`, {
1467
1550
  method: "POST",
1468
1551
  credentials: this.credentials,
1469
1552
  headers: this.getHeaders(),
@@ -1477,7 +1560,7 @@ var CimplifyClient = class {
1477
1560
  method,
1478
1561
  args: args !== void 0 ? [args] : []
1479
1562
  };
1480
- const response = await fetch(`${this.baseUrl}/api/m`, {
1563
+ const response = await this.resilientFetch(`${this.baseUrl}/api/m`, {
1481
1564
  method: "POST",
1482
1565
  credentials: this.credentials,
1483
1566
  headers: this.getHeaders(),
@@ -1487,7 +1570,7 @@ var CimplifyClient = class {
1487
1570
  return this.handleResponse(response);
1488
1571
  }
1489
1572
  async get(path) {
1490
- const response = await fetch(`${this.baseUrl}${path}`, {
1573
+ const response = await this.resilientFetch(`${this.baseUrl}${path}`, {
1491
1574
  method: "GET",
1492
1575
  credentials: this.credentials,
1493
1576
  headers: this.getHeaders()
@@ -1496,7 +1579,7 @@ var CimplifyClient = class {
1496
1579
  return this.handleRestResponse(response);
1497
1580
  }
1498
1581
  async post(path, body) {
1499
- const response = await fetch(`${this.baseUrl}${path}`, {
1582
+ const response = await this.resilientFetch(`${this.baseUrl}${path}`, {
1500
1583
  method: "POST",
1501
1584
  credentials: this.credentials,
1502
1585
  headers: this.getHeaders(),
@@ -1506,7 +1589,7 @@ var CimplifyClient = class {
1506
1589
  return this.handleRestResponse(response);
1507
1590
  }
1508
1591
  async delete(path) {
1509
- const response = await fetch(`${this.baseUrl}${path}`, {
1592
+ const response = await this.resilientFetch(`${this.baseUrl}${path}`, {
1510
1593
  method: "DELETE",
1511
1594
  credentials: this.credentials,
1512
1595
  headers: this.getHeaders()
@@ -1515,7 +1598,7 @@ var CimplifyClient = class {
1515
1598
  return this.handleRestResponse(response);
1516
1599
  }
1517
1600
  async linkGet(path) {
1518
- const response = await fetch(`${this.linkApiUrl}${path}`, {
1601
+ const response = await this.resilientFetch(`${this.linkApiUrl}${path}`, {
1519
1602
  method: "GET",
1520
1603
  credentials: this.credentials,
1521
1604
  headers: this.getHeaders()
@@ -1524,7 +1607,7 @@ var CimplifyClient = class {
1524
1607
  return this.handleRestResponse(response);
1525
1608
  }
1526
1609
  async linkPost(path, body) {
1527
- const response = await fetch(`${this.linkApiUrl}${path}`, {
1610
+ const response = await this.resilientFetch(`${this.linkApiUrl}${path}`, {
1528
1611
  method: "POST",
1529
1612
  credentials: this.credentials,
1530
1613
  headers: this.getHeaders(),
@@ -1534,7 +1617,7 @@ var CimplifyClient = class {
1534
1617
  return this.handleRestResponse(response);
1535
1618
  }
1536
1619
  async linkDelete(path) {
1537
- const response = await fetch(`${this.linkApiUrl}${path}`, {
1620
+ const response = await this.resilientFetch(`${this.linkApiUrl}${path}`, {
1538
1621
  method: "DELETE",
1539
1622
  credentials: this.credentials,
1540
1623
  headers: this.getHeaders()
package/dist/index.mjs CHANGED
@@ -1389,6 +1389,51 @@ var LiteService = class {
1389
1389
  // src/client.ts
1390
1390
  var SESSION_TOKEN_HEADER = "x-session-token";
1391
1391
  var SESSION_STORAGE_KEY = "cimplify_session_token";
1392
+ var DEFAULT_TIMEOUT_MS = 3e4;
1393
+ var DEFAULT_MAX_RETRIES = 3;
1394
+ var DEFAULT_RETRY_DELAY_MS = 1e3;
1395
+ function sleep(ms) {
1396
+ return new Promise((resolve) => setTimeout(resolve, ms));
1397
+ }
1398
+ function isRetryable(error) {
1399
+ if (error instanceof TypeError && error.message.includes("fetch")) {
1400
+ return true;
1401
+ }
1402
+ if (error instanceof DOMException && error.name === "AbortError") {
1403
+ return true;
1404
+ }
1405
+ if (error instanceof CimplifyError) {
1406
+ return error.retryable;
1407
+ }
1408
+ if (error instanceof CimplifyError && error.code === "SERVER_ERROR") {
1409
+ return true;
1410
+ }
1411
+ return false;
1412
+ }
1413
+ function toNetworkError(error) {
1414
+ if (error instanceof DOMException && error.name === "AbortError") {
1415
+ return new CimplifyError(
1416
+ ErrorCode.TIMEOUT,
1417
+ "Request timed out. Please check your connection and try again.",
1418
+ true
1419
+ );
1420
+ }
1421
+ if (error instanceof TypeError && error.message.includes("fetch")) {
1422
+ return new CimplifyError(
1423
+ ErrorCode.NETWORK_ERROR,
1424
+ "Network error. Please check your internet connection.",
1425
+ true
1426
+ );
1427
+ }
1428
+ if (error instanceof CimplifyError) {
1429
+ return error;
1430
+ }
1431
+ return new CimplifyError(
1432
+ ErrorCode.UNKNOWN_ERROR,
1433
+ error instanceof Error ? error.message : "An unknown error occurred",
1434
+ false
1435
+ );
1436
+ }
1392
1437
  function deriveUrls() {
1393
1438
  const hostname = typeof window !== "undefined" ? window.location.hostname : "";
1394
1439
  if (hostname === "localhost" || hostname === "127.0.0.1") {
@@ -1411,6 +1456,9 @@ var CimplifyClient = class {
1411
1456
  this.baseUrl = urls.baseUrl;
1412
1457
  this.linkApiUrl = urls.linkApiUrl;
1413
1458
  this.credentials = config.credentials || "include";
1459
+ this.timeout = config.timeout ?? DEFAULT_TIMEOUT_MS;
1460
+ this.maxRetries = config.maxRetries ?? DEFAULT_MAX_RETRIES;
1461
+ this.retryDelay = config.retryDelay ?? DEFAULT_RETRY_DELAY_MS;
1414
1462
  this.sessionToken = this.loadSessionToken();
1415
1463
  }
1416
1464
  getSessionToken() {
@@ -1456,12 +1504,47 @@ var CimplifyClient = class {
1456
1504
  this.saveSessionToken(newToken);
1457
1505
  }
1458
1506
  }
1507
+ /**
1508
+ * Resilient fetch with timeout and automatic retries for network errors.
1509
+ * Uses exponential backoff: 1s, 2s, 4s between retries.
1510
+ */
1511
+ async resilientFetch(url, options) {
1512
+ let lastError;
1513
+ for (let attempt = 0; attempt <= this.maxRetries; attempt++) {
1514
+ try {
1515
+ const controller = new AbortController();
1516
+ const timeoutId = setTimeout(() => controller.abort(), this.timeout);
1517
+ const response = await fetch(url, {
1518
+ ...options,
1519
+ signal: controller.signal
1520
+ });
1521
+ clearTimeout(timeoutId);
1522
+ if (response.ok || response.status >= 400 && response.status < 500) {
1523
+ return response;
1524
+ }
1525
+ if (response.status >= 500 && attempt < this.maxRetries) {
1526
+ const delay = this.retryDelay * Math.pow(2, attempt);
1527
+ await sleep(delay);
1528
+ continue;
1529
+ }
1530
+ return response;
1531
+ } catch (error) {
1532
+ lastError = error;
1533
+ if (!isRetryable(error) || attempt >= this.maxRetries) {
1534
+ throw toNetworkError(error);
1535
+ }
1536
+ const delay = this.retryDelay * Math.pow(2, attempt);
1537
+ await sleep(delay);
1538
+ }
1539
+ }
1540
+ throw toNetworkError(lastError);
1541
+ }
1459
1542
  async query(query2, variables) {
1460
1543
  const body = { query: query2 };
1461
1544
  if (variables) {
1462
1545
  body.variables = variables;
1463
1546
  }
1464
- const response = await fetch(`${this.baseUrl}/api/q`, {
1547
+ const response = await this.resilientFetch(`${this.baseUrl}/api/q`, {
1465
1548
  method: "POST",
1466
1549
  credentials: this.credentials,
1467
1550
  headers: this.getHeaders(),
@@ -1475,7 +1558,7 @@ var CimplifyClient = class {
1475
1558
  method,
1476
1559
  args: args !== void 0 ? [args] : []
1477
1560
  };
1478
- const response = await fetch(`${this.baseUrl}/api/m`, {
1561
+ const response = await this.resilientFetch(`${this.baseUrl}/api/m`, {
1479
1562
  method: "POST",
1480
1563
  credentials: this.credentials,
1481
1564
  headers: this.getHeaders(),
@@ -1485,7 +1568,7 @@ var CimplifyClient = class {
1485
1568
  return this.handleResponse(response);
1486
1569
  }
1487
1570
  async get(path) {
1488
- const response = await fetch(`${this.baseUrl}${path}`, {
1571
+ const response = await this.resilientFetch(`${this.baseUrl}${path}`, {
1489
1572
  method: "GET",
1490
1573
  credentials: this.credentials,
1491
1574
  headers: this.getHeaders()
@@ -1494,7 +1577,7 @@ var CimplifyClient = class {
1494
1577
  return this.handleRestResponse(response);
1495
1578
  }
1496
1579
  async post(path, body) {
1497
- const response = await fetch(`${this.baseUrl}${path}`, {
1580
+ const response = await this.resilientFetch(`${this.baseUrl}${path}`, {
1498
1581
  method: "POST",
1499
1582
  credentials: this.credentials,
1500
1583
  headers: this.getHeaders(),
@@ -1504,7 +1587,7 @@ var CimplifyClient = class {
1504
1587
  return this.handleRestResponse(response);
1505
1588
  }
1506
1589
  async delete(path) {
1507
- const response = await fetch(`${this.baseUrl}${path}`, {
1590
+ const response = await this.resilientFetch(`${this.baseUrl}${path}`, {
1508
1591
  method: "DELETE",
1509
1592
  credentials: this.credentials,
1510
1593
  headers: this.getHeaders()
@@ -1513,7 +1596,7 @@ var CimplifyClient = class {
1513
1596
  return this.handleRestResponse(response);
1514
1597
  }
1515
1598
  async linkGet(path) {
1516
- const response = await fetch(`${this.linkApiUrl}${path}`, {
1599
+ const response = await this.resilientFetch(`${this.linkApiUrl}${path}`, {
1517
1600
  method: "GET",
1518
1601
  credentials: this.credentials,
1519
1602
  headers: this.getHeaders()
@@ -1522,7 +1605,7 @@ var CimplifyClient = class {
1522
1605
  return this.handleRestResponse(response);
1523
1606
  }
1524
1607
  async linkPost(path, body) {
1525
- const response = await fetch(`${this.linkApiUrl}${path}`, {
1608
+ const response = await this.resilientFetch(`${this.linkApiUrl}${path}`, {
1526
1609
  method: "POST",
1527
1610
  credentials: this.credentials,
1528
1611
  headers: this.getHeaders(),
@@ -1532,7 +1615,7 @@ var CimplifyClient = class {
1532
1615
  return this.handleRestResponse(response);
1533
1616
  }
1534
1617
  async linkDelete(path) {
1535
- const response = await fetch(`${this.linkApiUrl}${path}`, {
1618
+ const response = await this.resilientFetch(`${this.linkApiUrl}${path}`, {
1536
1619
  method: "DELETE",
1537
1620
  credentials: this.credentials,
1538
1621
  headers: this.getHeaders()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cimplify/sdk",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "description": "Cimplify Commerce SDK for storefronts",
5
5
  "keywords": [
6
6
  "cimplify",