@szymonpiatek/nextwordpress 0.0.12 → 0.0.14

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.
@@ -3,10 +3,12 @@
3
3
  var react = require('react');
4
4
  var jsxRuntime = require('react/jsx-runtime');
5
5
  var useSWR2 = require('swr');
6
+ var useSWRMutation = require('swr/mutation');
6
7
 
7
8
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
8
9
 
9
10
  var useSWR2__default = /*#__PURE__*/_interopDefault(useSWR2);
11
+ var useSWRMutation__default = /*#__PURE__*/_interopDefault(useSWRMutation);
10
12
 
11
13
  var __defProp = Object.defineProperty;
12
14
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
@@ -45,7 +47,13 @@ var ErrorCode = {
45
47
  // Auth
46
48
  AUTH_JWT_FAILED: "AUTH_JWT_FAILED",
47
49
  AUTH_CREDENTIALS_INVALID: "AUTH_CREDENTIALS_INVALID",
48
- AUTH_TOKEN_INVALID: "AUTH_TOKEN_INVALID"
50
+ AUTH_TOKEN_INVALID: "AUTH_TOKEN_INVALID",
51
+ // Contact Form 7
52
+ CF7_MAIL_SENT: "CF7_MAIL_SENT",
53
+ CF7_MAIL_FAILED: "CF7_MAIL_FAILED",
54
+ CF7_VALIDATION_FAILED: "CF7_VALIDATION_FAILED",
55
+ CF7_SPAM: "CF7_SPAM",
56
+ CF7_ABORTED: "CF7_ABORTED"
49
57
  };
50
58
  var defaultMessages = {
51
59
  WP_REQUEST_FAILED: "WordPress API request failed",
@@ -70,7 +78,12 @@ var defaultMessages = {
70
78
  WOO_SHIPPING_ZONE_NOT_FOUND: "Shipping zone not found",
71
79
  AUTH_JWT_FAILED: "JWT authentication failed",
72
80
  AUTH_CREDENTIALS_INVALID: "Invalid credentials",
73
- AUTH_TOKEN_INVALID: "JWT token is invalid or expired"
81
+ AUTH_TOKEN_INVALID: "JWT token is invalid or expired",
82
+ CF7_MAIL_SENT: "Thank you for your message.",
83
+ CF7_MAIL_FAILED: "There was an error trying to send your message.",
84
+ CF7_VALIDATION_FAILED: "One or more fields have an error. Please check and try again.",
85
+ CF7_SPAM: "There was an error trying to send your message. Please try later.",
86
+ CF7_ABORTED: "Message submission has been aborted."
74
87
  };
75
88
  function resolveMessage(code, overrides) {
76
89
  return overrides?.[code] ?? defaultMessages[code];
@@ -183,6 +196,44 @@ function useAuth() {
183
196
  if (!ctx) throw new Error("useAuth must be used within an AuthProvider");
184
197
  return ctx;
185
198
  }
199
+ function readConsentFromDocument(cookieName) {
200
+ if (typeof document === "undefined") return null;
201
+ const match = document.cookie.match(new RegExp(`(?:^|;\\s*)${cookieName}=([^;]*)`));
202
+ if (!match) return null;
203
+ if (match[1] === "1") return { necessary: true, analytics: true, marketing: true };
204
+ try {
205
+ const parsed = JSON.parse(decodeURIComponent(match[1]));
206
+ if (typeof parsed !== "object" || parsed === null) return null;
207
+ return parsed;
208
+ } catch {
209
+ return null;
210
+ }
211
+ }
212
+ function useCookieConsent(consentPath = "/api/cookie-consent", cookieName = "cookie_notice_accepted") {
213
+ const [consent, setConsentState] = react.useState(null);
214
+ const [isLoaded, setIsLoaded] = react.useState(false);
215
+ react.useEffect(() => {
216
+ setConsentState(readConsentFromDocument(cookieName));
217
+ setIsLoaded(true);
218
+ }, [cookieName]);
219
+ const setConsent = react.useCallback(
220
+ async (categories) => {
221
+ const res = await fetch(consentPath, {
222
+ method: "POST",
223
+ headers: { "Content-Type": "application/json" },
224
+ body: JSON.stringify(categories)
225
+ });
226
+ const data = await res.json();
227
+ setConsentState(data);
228
+ },
229
+ [consentPath]
230
+ );
231
+ const clearConsent = react.useCallback(async () => {
232
+ await fetch(consentPath, { method: "DELETE" });
233
+ setConsentState(null);
234
+ }, [consentPath]);
235
+ return { consent, isLoaded, setConsent, clearConsent };
236
+ }
186
237
 
187
238
  // src/integrations/restApi/core/client/types.ts
188
239
  var WordPressAPIError = class extends Error {
@@ -1063,14 +1114,14 @@ var USER_AGENT3 = "NextWordpress WPGraphQL Client";
1063
1114
  function createWPGraphQLFetcher(config) {
1064
1115
  const url = `${resolveBaseUrl(config)}/graphql`;
1065
1116
  const cacheTTL = config.cacheTTL ?? 300;
1066
- async function gqlFetch(document, variables, tags = ["wpgraphql"]) {
1117
+ async function gqlFetch(document2, variables, tags = ["wpgraphql"]) {
1067
1118
  const response = await fetch(url, {
1068
1119
  method: "POST",
1069
1120
  headers: {
1070
1121
  "Content-Type": "application/json",
1071
1122
  "User-Agent": USER_AGENT3
1072
1123
  },
1073
- body: JSON.stringify({ query: document, variables }),
1124
+ body: JSON.stringify({ query: document2, variables }),
1074
1125
  next: { tags, revalidate: cacheTTL }
1075
1126
  });
1076
1127
  if (!response.ok) {
@@ -1101,15 +1152,15 @@ function createWPGraphQLFetcher(config) {
1101
1152
  }
1102
1153
  return parsed.data;
1103
1154
  }
1104
- async function gqlFetchGraceful(document, fallback, variables, tags = ["wpgraphql"]) {
1155
+ async function gqlFetchGraceful(document2, fallback, variables, tags = ["wpgraphql"]) {
1105
1156
  try {
1106
- return await gqlFetch(document, variables, tags);
1157
+ return await gqlFetch(document2, variables, tags);
1107
1158
  } catch {
1108
1159
  console.warn(`WPGraphQL fetch failed for query`);
1109
1160
  return fallback;
1110
1161
  }
1111
1162
  }
1112
- async function gqlMutate(document, variables, authToken) {
1163
+ async function gqlMutate(document2, variables, authToken) {
1113
1164
  const headers = {
1114
1165
  "Content-Type": "application/json",
1115
1166
  "User-Agent": USER_AGENT3
@@ -1120,7 +1171,7 @@ function createWPGraphQLFetcher(config) {
1120
1171
  const response = await fetch(url, {
1121
1172
  method: "POST",
1122
1173
  headers,
1123
- body: JSON.stringify({ query: document, variables }),
1174
+ body: JSON.stringify({ query: document2, variables }),
1124
1175
  cache: "no-store"
1125
1176
  });
1126
1177
  if (!response.ok) {
@@ -1419,12 +1470,47 @@ function useWPULike(config, params, swrOptions) {
1419
1470
  return { data, error, isLoading, vote };
1420
1471
  }
1421
1472
 
1473
+ // src/integrations/restApi/contactForm7/queries.ts
1474
+ function createCF7Queries(config) {
1475
+ async function submitForm(formId, data) {
1476
+ const url = buildUrl(config, `/wp-json/contact-form-7/v1/contact-forms/${formId}/feedback`);
1477
+ const body = new FormData();
1478
+ for (const [key, value] of Object.entries(data)) {
1479
+ body.append(key, value);
1480
+ }
1481
+ const res = await fetch(url, { method: "POST", body, cache: "no-store" });
1482
+ return res.json();
1483
+ }
1484
+ return { submitForm };
1485
+ }
1486
+
1487
+ // src/hooks/contactForm7/useCF7Submit.ts
1488
+ var statusToCode = {
1489
+ mail_sent: ErrorCode.CF7_MAIL_SENT,
1490
+ mail_failed: ErrorCode.CF7_MAIL_FAILED,
1491
+ validation_failed: ErrorCode.CF7_VALIDATION_FAILED,
1492
+ spam: ErrorCode.CF7_SPAM,
1493
+ aborted: ErrorCode.CF7_ABORTED
1494
+ };
1495
+ function useCF7Submit(config) {
1496
+ return useSWRMutation__default.default(
1497
+ `cf7-submit:${config.serverURL}`,
1498
+ async (_, { arg }) => {
1499
+ const result = await createCF7Queries(config).submitForm(arg.formId, arg.fields);
1500
+ const override = config.errorMessages?.[statusToCode[result.status]];
1501
+ return override ? { ...result, message: override } : result;
1502
+ }
1503
+ );
1504
+ }
1505
+
1422
1506
  exports.AuthProvider = AuthProvider;
1423
1507
  exports.CartProvider = CartProvider;
1424
1508
  exports.WooCommerceCustomerProvider = WooCommerceCustomerProvider;
1425
1509
  exports.cartReducer = cartReducer;
1426
1510
  exports.useAuth = useAuth;
1511
+ exports.useCF7Submit = useCF7Submit;
1427
1512
  exports.useCart = useCart;
1513
+ exports.useCookieConsent = useCookieConsent;
1428
1514
  exports.useCustomer = useCustomer;
1429
1515
  exports.useFeaturedProducts = useFeaturedProducts;
1430
1516
  exports.useGQLPostBySlug = useGQLPostBySlug;