@szymonpiatek/nextwordpress 0.0.18 → 0.0.20

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.
@@ -44,6 +44,8 @@ var ErrorCode = {
44
44
  WOO_REVIEW_NOT_FOUND: "WOO_REVIEW_NOT_FOUND",
45
45
  WOO_WEBHOOK_NOT_FOUND: "WOO_WEBHOOK_NOT_FOUND",
46
46
  WOO_SHIPPING_ZONE_NOT_FOUND: "WOO_SHIPPING_ZONE_NOT_FOUND",
47
+ WOO_WISHLIST_NOT_FOUND: "WOO_WISHLIST_NOT_FOUND",
48
+ WOO_WISHLIST_ITEM_NOT_FOUND: "WOO_WISHLIST_ITEM_NOT_FOUND",
47
49
  // Auth
48
50
  AUTH_JWT_FAILED: "AUTH_JWT_FAILED",
49
51
  AUTH_CREDENTIALS_INVALID: "AUTH_CREDENTIALS_INVALID",
@@ -76,6 +78,8 @@ var defaultMessages = {
76
78
  WOO_REVIEW_NOT_FOUND: "Product review not found",
77
79
  WOO_WEBHOOK_NOT_FOUND: "Webhook not found",
78
80
  WOO_SHIPPING_ZONE_NOT_FOUND: "Shipping zone not found",
81
+ WOO_WISHLIST_NOT_FOUND: "Wishlist not found",
82
+ WOO_WISHLIST_ITEM_NOT_FOUND: "Wishlist item not found",
79
83
  AUTH_JWT_FAILED: "JWT authentication failed",
80
84
  AUTH_CREDENTIALS_INVALID: "Invalid credentials",
81
85
  AUTH_TOKEN_INVALID: "JWT token is invalid or expired",
@@ -1311,6 +1315,265 @@ function useCustomer() {
1311
1315
  return ctx;
1312
1316
  }
1313
1317
 
1318
+ // src/integrations/restApi/woocommerce/wishlist/fetcher.ts
1319
+ var USER_AGENT4 = "NextWordpress YITH Wishlist Client";
1320
+ var DEFAULT_CACHE_TTL2 = 3600;
1321
+ var YithWishlistError = class extends Error {
1322
+ constructor(code, status, url, message, upstreamCode, upstreamMessage) {
1323
+ super(message);
1324
+ __publicField(this, "code", code);
1325
+ __publicField(this, "status", status);
1326
+ __publicField(this, "url", url);
1327
+ __publicField(this, "upstreamCode", upstreamCode);
1328
+ __publicField(this, "upstreamMessage", upstreamMessage);
1329
+ this.name = "YithWishlistError";
1330
+ }
1331
+ };
1332
+ function resolveYithErrorCode(status, upstreamCode) {
1333
+ if (status === 401) return ErrorCode.WOO_UNAUTHORIZED;
1334
+ if (status === 403) return ErrorCode.WOO_FORBIDDEN;
1335
+ if (status === 404) {
1336
+ if (upstreamCode === "yith_wcwl_wishlist_not_found") return ErrorCode.WOO_WISHLIST_NOT_FOUND;
1337
+ if (upstreamCode === "yith_wcwl_wishlist_item_not_found")
1338
+ return ErrorCode.WOO_WISHLIST_ITEM_NOT_FOUND;
1339
+ return ErrorCode.WOO_NOT_FOUND;
1340
+ }
1341
+ return ErrorCode.WOO_REQUEST_FAILED;
1342
+ }
1343
+ function createYithWishlistFetcher(config) {
1344
+ const cacheTTL = config.cacheTTL ?? DEFAULT_CACHE_TTL2;
1345
+ function buildUrl2(path) {
1346
+ const base = typeof window !== "undefined" && config.clientURL ? config.clientURL : config.serverURL;
1347
+ return `${base}${path}`;
1348
+ }
1349
+ function authHeaders() {
1350
+ return {
1351
+ "User-Agent": USER_AGENT4,
1352
+ "Content-Type": "application/json",
1353
+ Authorization: `Bearer ${config.token}`
1354
+ };
1355
+ }
1356
+ async function throwYithError(response, url) {
1357
+ const body = await response.json().catch(() => ({}));
1358
+ const errorCode = resolveYithErrorCode(response.status, body.code);
1359
+ throw new YithWishlistError(
1360
+ errorCode,
1361
+ response.status,
1362
+ url,
1363
+ resolveMessage(errorCode, config.errorMessages),
1364
+ body.code,
1365
+ body.message
1366
+ );
1367
+ }
1368
+ async function yithFetch(path, tags = ["yith-wishlist"], options) {
1369
+ const url = buildUrl2(path);
1370
+ const isMutation = options?.method && options.method !== "GET";
1371
+ const response = await fetch(url, {
1372
+ ...options,
1373
+ headers: {
1374
+ ...authHeaders(),
1375
+ ...options?.headers
1376
+ },
1377
+ next: isMutation ? void 0 : { tags, revalidate: cacheTTL }
1378
+ });
1379
+ if (!response.ok) {
1380
+ await throwYithError(response, url);
1381
+ }
1382
+ return response.json();
1383
+ }
1384
+ async function yithFetchGraceful(path, fallback, tags = ["yith-wishlist"]) {
1385
+ try {
1386
+ return await yithFetch(path, tags);
1387
+ } catch (err) {
1388
+ console.warn(`YITH Wishlist fetch failed for ${path}`, err);
1389
+ return fallback;
1390
+ }
1391
+ }
1392
+ async function yithMutate(path, body, method = "POST") {
1393
+ return yithFetch(path, ["yith-wishlist"], {
1394
+ method,
1395
+ body: JSON.stringify(body)
1396
+ });
1397
+ }
1398
+ return { yithFetch, yithFetchGraceful, yithMutate };
1399
+ }
1400
+
1401
+ // src/integrations/restApi/woocommerce/wishlist/queries.ts
1402
+ var BASE = "/wp-json/yith/wishlist/v1/wishlists";
1403
+ function createWishlistQueries(fetcher) {
1404
+ const { yithFetch, yithFetchGraceful } = fetcher;
1405
+ async function getWishlists() {
1406
+ return yithFetchGraceful(BASE, [], ["yith-wishlist", "wishlists"]);
1407
+ }
1408
+ async function getWishlistByToken(token) {
1409
+ return yithFetch(`${BASE}/${token}`, ["yith-wishlist", "wishlists", `wishlist-${token}`]);
1410
+ }
1411
+ return { getWishlists, getWishlistByToken };
1412
+ }
1413
+
1414
+ // src/hooks/woocommerce/useWishlist.ts
1415
+ function useWishlists(config, swrOptions) {
1416
+ const key = ["yith-wishlists", config.serverURL, config.token];
1417
+ return useSWR2__default.default(
1418
+ key,
1419
+ () => {
1420
+ const fetcher = createYithWishlistFetcher(config);
1421
+ return createWishlistQueries(fetcher).getWishlists();
1422
+ },
1423
+ swrOptions
1424
+ );
1425
+ }
1426
+ function useWishlist(config, wishlistToken, swrOptions) {
1427
+ const key = wishlistToken != null ? ["yith-wishlist", config.serverURL, config.token, wishlistToken] : null;
1428
+ return useSWR2__default.default(
1429
+ key,
1430
+ () => {
1431
+ const fetcher = createYithWishlistFetcher(config);
1432
+ return createWishlistQueries(fetcher).getWishlistByToken(wishlistToken);
1433
+ },
1434
+ swrOptions
1435
+ );
1436
+ }
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
+
1314
1577
  // src/integrations/wpGraphQL/core/posts/queries.ts
1315
1578
  var DEFAULT_FIRST = 10;
1316
1579
  var BATCH_FIRST = 100;
@@ -1652,9 +1915,14 @@ exports.useResetUserPassword = useResetUserPassword;
1652
1915
  exports.useResetUserPasswordRest = useResetUserPasswordRest;
1653
1916
  exports.useSendPasswordResetEmail = useSendPasswordResetEmail;
1654
1917
  exports.useSendPasswordResetEmailRest = useSendPasswordResetEmailRest;
1918
+ exports.useTIWishlistByShareKey = useTIWishlistByShareKey;
1919
+ exports.useTIWishlistProducts = useTIWishlistProducts;
1920
+ exports.useTIWishlistsByUser = useTIWishlistsByUser;
1655
1921
  exports.useWPGraphQL = useWPGraphQL;
1656
1922
  exports.useWPULike = useWPULike;
1657
1923
  exports.useWPULikeCheck = useWPULikeCheck;
1658
1924
  exports.useWPULikeStats = useWPULikeStats;
1925
+ exports.useWishlist = useWishlist;
1926
+ exports.useWishlists = useWishlists;
1659
1927
  //# sourceMappingURL=index.cjs.map
1660
1928
  //# sourceMappingURL=index.cjs.map