@szymonpiatek/nextwordpress 0.0.19 → 0.0.21

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.
@@ -1435,6 +1435,145 @@ function useWishlist(config, wishlistToken, swrOptions) {
1435
1435
  );
1436
1436
  }
1437
1437
 
1438
+ // src/integrations/restApi/woocommerce/ti_wishlist/fetcher.ts
1439
+ var USER_AGENT5 = "NextWordpress TI Wishlist Client";
1440
+ var DEFAULT_CACHE_TTL3 = 3600;
1441
+ var TIWishlistError = class extends Error {
1442
+ constructor(code, status, url, message) {
1443
+ super(message);
1444
+ __publicField(this, "code", code);
1445
+ __publicField(this, "status", status);
1446
+ __publicField(this, "url", url);
1447
+ this.name = "TIWishlistError";
1448
+ }
1449
+ };
1450
+ function resolveTIErrorCode(status) {
1451
+ if (status === 401) return ErrorCode.WOO_UNAUTHORIZED;
1452
+ if (status === 403) return ErrorCode.WOO_FORBIDDEN;
1453
+ if (status === 404) return ErrorCode.WOO_WISHLIST_NOT_FOUND;
1454
+ return ErrorCode.WOO_REQUEST_FAILED;
1455
+ }
1456
+ function createTIWishlistFetcher(config) {
1457
+ const cacheTTL = config.cacheTTL ?? DEFAULT_CACHE_TTL3;
1458
+ function buildUrl2(path) {
1459
+ const base = typeof window !== "undefined" && config.clientURL ? config.clientURL : config.serverURL;
1460
+ return `${base}${path}`;
1461
+ }
1462
+ function authHeaders() {
1463
+ return {
1464
+ "User-Agent": USER_AGENT5,
1465
+ "Content-Type": "application/json",
1466
+ Authorization: `Bearer ${config.token}`
1467
+ };
1468
+ }
1469
+ async function throwTIError(response, url) {
1470
+ const errorCode = resolveTIErrorCode(response.status);
1471
+ throw new TIWishlistError(
1472
+ errorCode,
1473
+ response.status,
1474
+ url,
1475
+ resolveMessage(errorCode, config.errorMessages)
1476
+ );
1477
+ }
1478
+ async function tiFetch(path, tags = ["ti-wishlist"], options) {
1479
+ const url = buildUrl2(path);
1480
+ const isMutation = options?.method && options.method !== "GET";
1481
+ const response = await fetch(url, {
1482
+ ...options,
1483
+ headers: {
1484
+ ...authHeaders(),
1485
+ ...options?.headers
1486
+ },
1487
+ next: isMutation ? void 0 : { tags, revalidate: cacheTTL }
1488
+ });
1489
+ if (!response.ok) {
1490
+ await throwTIError(response, url);
1491
+ }
1492
+ return response.json();
1493
+ }
1494
+ async function tiFetchGraceful(path, fallback, tags = ["ti-wishlist"]) {
1495
+ try {
1496
+ return await tiFetch(path, tags);
1497
+ } catch (err) {
1498
+ console.warn(`TI Wishlist fetch failed for ${path}`, err);
1499
+ return fallback;
1500
+ }
1501
+ }
1502
+ async function tiMutate(path, body, method = "POST") {
1503
+ return tiFetch(path, ["ti-wishlist"], {
1504
+ method,
1505
+ body: JSON.stringify(body)
1506
+ });
1507
+ }
1508
+ return { tiFetch, tiFetchGraceful, tiMutate };
1509
+ }
1510
+
1511
+ // src/integrations/restApi/woocommerce/ti_wishlist/queries.ts
1512
+ var BASE2 = "/wp-json/wc/v3/wishlist";
1513
+ function createTIWishlistQueries(fetcher) {
1514
+ const { tiFetch, tiFetchGraceful } = fetcher;
1515
+ async function getWishlistsByUser(userId) {
1516
+ return tiFetchGraceful(
1517
+ `${BASE2}/get_by_user/${userId}`,
1518
+ [],
1519
+ ["ti-wishlist", `ti-wishlists-user-${userId}`]
1520
+ );
1521
+ }
1522
+ async function getWishlistByShareKey(shareKey) {
1523
+ return tiFetch(`${BASE2}/get_by_share_key/${shareKey}`, [
1524
+ "ti-wishlist",
1525
+ `ti-wishlist-${shareKey}`
1526
+ ]);
1527
+ }
1528
+ async function getWishlistProducts(shareKey, params) {
1529
+ const query = new URLSearchParams();
1530
+ if (params?.count != null) query.set("count", String(params.count));
1531
+ if (params?.offset != null) query.set("offset", String(params.offset));
1532
+ if (params?.order != null) query.set("order", params.order);
1533
+ const qs = query.toString() ? `?${query.toString()}` : "";
1534
+ return tiFetch(`${BASE2}/${shareKey}/get_products${qs}`, [
1535
+ "ti-wishlist",
1536
+ `ti-wishlist-products-${shareKey}`
1537
+ ]);
1538
+ }
1539
+ return { getWishlistsByUser, getWishlistByShareKey, getWishlistProducts };
1540
+ }
1541
+
1542
+ // src/hooks/woocommerce/useTIWishlist.ts
1543
+ function useTIWishlistsByUser(config, userId, swrOptions) {
1544
+ const key = ["ti-wishlists", config.serverURL, config.token, userId];
1545
+ return useSWR2__default.default(
1546
+ key,
1547
+ () => {
1548
+ const fetcher = createTIWishlistFetcher(config);
1549
+ return createTIWishlistQueries(fetcher).getWishlistsByUser(userId);
1550
+ },
1551
+ swrOptions
1552
+ );
1553
+ }
1554
+ function useTIWishlistByShareKey(config, shareKey, swrOptions) {
1555
+ const key = shareKey != null ? ["ti-wishlist", config.serverURL, config.token, shareKey] : null;
1556
+ return useSWR2__default.default(
1557
+ key,
1558
+ () => {
1559
+ const fetcher = createTIWishlistFetcher(config);
1560
+ return createTIWishlistQueries(fetcher).getWishlistByShareKey(shareKey);
1561
+ },
1562
+ swrOptions
1563
+ );
1564
+ }
1565
+ function useTIWishlistProducts(config, shareKey, params, swrOptions) {
1566
+ const key = shareKey != null ? ["ti-wishlist-products", config.serverURL, config.token, shareKey, params] : null;
1567
+ return useSWR2__default.default(
1568
+ key,
1569
+ () => {
1570
+ const fetcher = createTIWishlistFetcher(config);
1571
+ return createTIWishlistQueries(fetcher).getWishlistProducts(shareKey, params);
1572
+ },
1573
+ swrOptions
1574
+ );
1575
+ }
1576
+
1438
1577
  // src/integrations/wpGraphQL/core/posts/queries.ts
1439
1578
  var DEFAULT_FIRST = 10;
1440
1579
  var BATCH_FIRST = 100;
@@ -1776,6 +1915,9 @@ exports.useResetUserPassword = useResetUserPassword;
1776
1915
  exports.useResetUserPasswordRest = useResetUserPasswordRest;
1777
1916
  exports.useSendPasswordResetEmail = useSendPasswordResetEmail;
1778
1917
  exports.useSendPasswordResetEmailRest = useSendPasswordResetEmailRest;
1918
+ exports.useTIWishlistByShareKey = useTIWishlistByShareKey;
1919
+ exports.useTIWishlistProducts = useTIWishlistProducts;
1920
+ exports.useTIWishlistsByUser = useTIWishlistsByUser;
1779
1921
  exports.useWPGraphQL = useWPGraphQL;
1780
1922
  exports.useWPULike = useWPULike;
1781
1923
  exports.useWPULikeCheck = useWPULikeCheck;