@szymonpiatek/nextwordpress 0.0.12 → 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.
@@ -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) {
@@ -1413,6 +1451,6 @@ function useWPULike(config, params, swrOptions) {
1413
1451
  return { data, error, isLoading, vote };
1414
1452
  }
1415
1453
 
1416
- export { AuthProvider, CartProvider, WooCommerceCustomerProvider, cartReducer, useAuth, useCart, useCustomer, useFeaturedProducts, useGQLPostBySlug, useGQLPosts, usePost, usePostBySlug, usePosts, usePostsPaginated, useProduct, useProductBySlug, useProducts, useProductsPaginated, useWPGraphQL, useWPULike, useWPULikeCheck, useWPULikeStats };
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 };
1417
1455
  //# sourceMappingURL=index.js.map
1418
1456
  //# sourceMappingURL=index.js.map