@szymonpiatek/nextwordpress 0.0.11 → 0.0.13

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.
@@ -1,6 +1,6 @@
1
1
  import * as react from 'react';
2
2
  import { ReactNode } from 'react';
3
- import { J as JwtAuthCredentials, N as NextWordpressConfig, P as Post, W as WordPressResponse, a as WooCommerceConfig, b as WCProduct, c as WCProductFilterParams, d as WooCommerceResponse, e as WCCreateOrderInput, f as WCOrder, g as WCCustomer, U as UpdateCustomerInput, h as WPGraphQLConfig, G as GQLPost, i as GQLPostFilter, j as GQLConnection } from '../types-Dxb6tuW_.js';
3
+ import { J as JwtAuthCredentials, N as NextWordpressConfig, C as CookieConsentCategories, P as Post, W as WordPressResponse, a as WooCommerceConfig, b as WCProduct, c as WCProductFilterParams, d as WooCommerceResponse, e as WCCreateOrderInput, f as WCOrder, g as WCCustomer, U as UpdateCustomerInput, h as WPGraphQLConfig, G as GQLPost, i as GQLPostFilter, j as GQLConnection, k as WPULikeStatsParams, l as WPULikeStats, m as WPULikeStatus, n as WPULikeVoteResponse, o as WPULikeCheckParams, p as WPULikeCheckResponse } from '../types-BELSHjQr.js';
4
4
  import * as swr from 'swr';
5
5
  import { SWRConfiguration } from 'swr';
6
6
 
@@ -24,6 +24,13 @@ declare function AuthProvider({ config, children, }: {
24
24
  }): react.JSX.Element;
25
25
  declare function useAuth(): AuthContextValue;
26
26
 
27
+ declare function useCookieConsent(consentPath?: string, cookieName?: string): {
28
+ consent: CookieConsentCategories | null;
29
+ isLoaded: boolean;
30
+ setConsent: (categories: Omit<CookieConsentCategories, "necessary">) => Promise<void>;
31
+ clearConsent: () => Promise<void>;
32
+ };
33
+
27
34
  type PostFilterParams = {
28
35
  author?: string;
29
36
  tag?: string;
@@ -113,4 +120,13 @@ declare function useWPGraphQL<TData>(config: WPGraphQLConfig, query: string, var
113
120
  declare function useGQLPosts(config: WPGraphQLConfig, first?: number, after?: string, filter?: GQLPostFilter, swrOptions?: SWRConfiguration<GQLConnection<GQLPost>>): swr.SWRResponse<GQLConnection<GQLPost>, any, SWRConfiguration<GQLConnection<GQLPost>, any, swr.BareFetcher<GQLConnection<GQLPost>>> | undefined>;
114
121
  declare function useGQLPostBySlug(config: WPGraphQLConfig, slug: string | null | undefined, swrOptions?: SWRConfiguration<GQLPost | null>): swr.SWRResponse<GQLPost | null, any, SWRConfiguration<GQLPost | null, any, swr.BareFetcher<GQLPost | null>> | undefined>;
115
122
 
116
- export { type AuthContextValue, AuthProvider, type AuthUser, type CartAction, type CartContextValue, type CartItem, CartProvider, type CheckoutOptions, type CustomerContextValue, WooCommerceCustomerProvider, cartReducer, useAuth, useCart, useCustomer, useFeaturedProducts, useGQLPostBySlug, useGQLPosts, usePost, usePostBySlug, usePosts, usePostsPaginated, useProduct, useProductBySlug, useProducts, useProductsPaginated, useWPGraphQL };
123
+ declare function useWPULikeStats(config: NextWordpressConfig, params: WPULikeStatsParams | null | undefined, swrOptions?: SWRConfiguration<WPULikeStats>): swr.SWRResponse<WPULikeStats, any, SWRConfiguration<WPULikeStats, any, swr.BareFetcher<WPULikeStats>> | undefined>;
124
+ declare function useWPULikeCheck(config: NextWordpressConfig, params: WPULikeCheckParams | null | undefined, swrOptions?: SWRConfiguration<WPULikeCheckResponse>): swr.SWRResponse<WPULikeCheckResponse, any, SWRConfiguration<WPULikeCheckResponse, any, swr.BareFetcher<WPULikeCheckResponse>> | undefined>;
125
+ declare function useWPULike(config: NextWordpressConfig, params: WPULikeStatsParams | null | undefined, swrOptions?: SWRConfiguration<WPULikeStats>): {
126
+ data: WPULikeStats | undefined;
127
+ error: any;
128
+ isLoading: boolean;
129
+ vote: (status: WPULikeStatus, authToken?: string) => Promise<WPULikeVoteResponse>;
130
+ };
131
+
132
+ export { type AuthContextValue, AuthProvider, type AuthUser, type CartAction, type CartContextValue, type CartItem, CartProvider, type CheckoutOptions, type CustomerContextValue, WooCommerceCustomerProvider, cartReducer, useAuth, useCart, useCookieConsent, useCustomer, useFeaturedProducts, useGQLPostBySlug, useGQLPosts, usePost, usePostBySlug, usePosts, usePostsPaginated, useProduct, useProductBySlug, useProducts, useProductsPaginated, useWPGraphQL, useWPULike, useWPULikeCheck, useWPULikeStats };
@@ -177,6 +177,44 @@ function useAuth() {
177
177
  if (!ctx) throw new Error("useAuth must be used within an AuthProvider");
178
178
  return ctx;
179
179
  }
180
+ function readConsentFromDocument(cookieName) {
181
+ if (typeof document === "undefined") return null;
182
+ const match = document.cookie.match(new RegExp(`(?:^|;\\s*)${cookieName}=([^;]*)`));
183
+ if (!match) return null;
184
+ if (match[1] === "1") return { necessary: true, analytics: true, marketing: true };
185
+ try {
186
+ const parsed = JSON.parse(decodeURIComponent(match[1]));
187
+ if (typeof parsed !== "object" || parsed === null) return null;
188
+ return parsed;
189
+ } catch {
190
+ return null;
191
+ }
192
+ }
193
+ function useCookieConsent(consentPath = "/api/cookie-consent", cookieName = "cookie_notice_accepted") {
194
+ const [consent, setConsentState] = useState(null);
195
+ const [isLoaded, setIsLoaded] = useState(false);
196
+ useEffect(() => {
197
+ setConsentState(readConsentFromDocument(cookieName));
198
+ setIsLoaded(true);
199
+ }, [cookieName]);
200
+ const setConsent = useCallback(
201
+ async (categories) => {
202
+ const res = await fetch(consentPath, {
203
+ method: "POST",
204
+ headers: { "Content-Type": "application/json" },
205
+ body: JSON.stringify(categories)
206
+ });
207
+ const data = await res.json();
208
+ setConsentState(data);
209
+ },
210
+ [consentPath]
211
+ );
212
+ const clearConsent = useCallback(async () => {
213
+ await fetch(consentPath, { method: "DELETE" });
214
+ setConsentState(null);
215
+ }, [consentPath]);
216
+ return { consent, isLoaded, setConsent, clearConsent };
217
+ }
180
218
 
181
219
  // src/integrations/restApi/core/client/types.ts
182
220
  var WordPressAPIError = class extends Error {
@@ -1057,14 +1095,14 @@ var USER_AGENT3 = "NextWordpress WPGraphQL Client";
1057
1095
  function createWPGraphQLFetcher(config) {
1058
1096
  const url = `${resolveBaseUrl(config)}/graphql`;
1059
1097
  const cacheTTL = config.cacheTTL ?? 300;
1060
- async function gqlFetch(document, variables, tags = ["wpgraphql"]) {
1098
+ async function gqlFetch(document2, variables, tags = ["wpgraphql"]) {
1061
1099
  const response = await fetch(url, {
1062
1100
  method: "POST",
1063
1101
  headers: {
1064
1102
  "Content-Type": "application/json",
1065
1103
  "User-Agent": USER_AGENT3
1066
1104
  },
1067
- body: JSON.stringify({ query: document, variables }),
1105
+ body: JSON.stringify({ query: document2, variables }),
1068
1106
  next: { tags, revalidate: cacheTTL }
1069
1107
  });
1070
1108
  if (!response.ok) {
@@ -1095,15 +1133,15 @@ function createWPGraphQLFetcher(config) {
1095
1133
  }
1096
1134
  return parsed.data;
1097
1135
  }
1098
- async function gqlFetchGraceful(document, fallback, variables, tags = ["wpgraphql"]) {
1136
+ async function gqlFetchGraceful(document2, fallback, variables, tags = ["wpgraphql"]) {
1099
1137
  try {
1100
- return await gqlFetch(document, variables, tags);
1138
+ return await gqlFetch(document2, variables, tags);
1101
1139
  } catch {
1102
1140
  console.warn(`WPGraphQL fetch failed for query`);
1103
1141
  return fallback;
1104
1142
  }
1105
1143
  }
1106
- async function gqlMutate(document, variables, authToken) {
1144
+ async function gqlMutate(document2, variables, authToken) {
1107
1145
  const headers = {
1108
1146
  "Content-Type": "application/json",
1109
1147
  "User-Agent": USER_AGENT3
@@ -1114,7 +1152,7 @@ function createWPGraphQLFetcher(config) {
1114
1152
  const response = await fetch(url, {
1115
1153
  method: "POST",
1116
1154
  headers,
1117
- body: JSON.stringify({ query: document, variables }),
1155
+ body: JSON.stringify({ query: document2, variables }),
1118
1156
  cache: "no-store"
1119
1157
  });
1120
1158
  if (!response.ok) {
@@ -1343,6 +1381,76 @@ function useGQLPostBySlug(config, slug, swrOptions) {
1343
1381
  );
1344
1382
  }
1345
1383
 
1346
- export { AuthProvider, CartProvider, WooCommerceCustomerProvider, cartReducer, useAuth, useCart, useCustomer, useFeaturedProducts, useGQLPostBySlug, useGQLPosts, usePost, usePostBySlug, usePosts, usePostsPaginated, useProduct, useProductBySlug, useProducts, useProductsPaginated, useWPGraphQL };
1384
+ // src/integrations/restApi/wpulike/queries.ts
1385
+ function createWPULikeQueries(fetcher) {
1386
+ const { wpFetch, wpMutate } = fetcher;
1387
+ async function getLikeStats(params, tags) {
1388
+ return wpFetch(
1389
+ "/wp-json/wp-ulike/v1/stats",
1390
+ params,
1391
+ tags ?? ["wpulike", `wpulike-${params.id}`]
1392
+ );
1393
+ }
1394
+ async function checkLikeStatus(params, tags) {
1395
+ return wpFetch(
1396
+ "/wp-json/wp-ulike/v1/check",
1397
+ params,
1398
+ tags ?? ["wpulike", `wpulike-${params.id}`]
1399
+ );
1400
+ }
1401
+ async function vote(params, authToken) {
1402
+ return wpMutate(
1403
+ "/wp-json/wp-ulike/v1/vote",
1404
+ params,
1405
+ "POST",
1406
+ authToken
1407
+ );
1408
+ }
1409
+ return { getLikeStats, checkLikeStatus, vote };
1410
+ }
1411
+
1412
+ // src/hooks/wpulike/useWPULike.ts
1413
+ function useWPULikeStats(config, params, swrOptions) {
1414
+ const key = params ? ["wpulike-stats", config.clientURL, params.id, params.type ?? "post"] : null;
1415
+ return useSWR2(
1416
+ key,
1417
+ () => {
1418
+ const fetcher = createFetcher(config);
1419
+ return createWPULikeQueries(fetcher).getLikeStats(params);
1420
+ },
1421
+ swrOptions
1422
+ );
1423
+ }
1424
+ function useWPULikeCheck(config, params, swrOptions) {
1425
+ const key = params ? ["wpulike-check", config.clientURL, params.id, params.type ?? "post"] : null;
1426
+ return useSWR2(
1427
+ key,
1428
+ () => {
1429
+ const fetcher = createFetcher(config);
1430
+ return createWPULikeQueries(fetcher).checkLikeStatus(params);
1431
+ },
1432
+ swrOptions
1433
+ );
1434
+ }
1435
+ function useWPULike(config, params, swrOptions) {
1436
+ const key = params ? ["wpulike-stats", config.clientURL, params.id, params.type ?? "post"] : null;
1437
+ const { data, error, isLoading, mutate } = useSWR2(
1438
+ key,
1439
+ () => {
1440
+ const fetcher = createFetcher(config);
1441
+ return createWPULikeQueries(fetcher).getLikeStats(params);
1442
+ },
1443
+ swrOptions
1444
+ );
1445
+ async function vote(status, authToken) {
1446
+ const fetcher = createFetcher(config);
1447
+ const result = await createWPULikeQueries(fetcher).vote({ ...params, status }, authToken);
1448
+ await mutate();
1449
+ return result;
1450
+ }
1451
+ return { data, error, isLoading, vote };
1452
+ }
1453
+
1454
+ export { AuthProvider, CartProvider, WooCommerceCustomerProvider, cartReducer, useAuth, useCart, useCookieConsent, useCustomer, useFeaturedProducts, useGQLPostBySlug, useGQLPosts, usePost, usePostBySlug, usePosts, usePostsPaginated, useProduct, useProductBySlug, useProducts, useProductsPaginated, useWPGraphQL, useWPULike, useWPULikeCheck, useWPULikeStats };
1347
1455
  //# sourceMappingURL=index.js.map
1348
1456
  //# sourceMappingURL=index.js.map