@szymonpiatek/nextwordpress 0.0.18 → 0.0.19

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,126 @@ 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
+
1314
1438
  // src/integrations/wpGraphQL/core/posts/queries.ts
1315
1439
  var DEFAULT_FIRST = 10;
1316
1440
  var BATCH_FIRST = 100;
@@ -1656,5 +1780,7 @@ exports.useWPGraphQL = useWPGraphQL;
1656
1780
  exports.useWPULike = useWPULike;
1657
1781
  exports.useWPULikeCheck = useWPULikeCheck;
1658
1782
  exports.useWPULikeStats = useWPULikeStats;
1783
+ exports.useWishlist = useWishlist;
1784
+ exports.useWishlists = useWishlists;
1659
1785
  //# sourceMappingURL=index.cjs.map
1660
1786
  //# sourceMappingURL=index.cjs.map