@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.
package/dist/index.cjs CHANGED
@@ -38,7 +38,13 @@ var ErrorCode = {
38
38
  // Auth
39
39
  AUTH_JWT_FAILED: "AUTH_JWT_FAILED",
40
40
  AUTH_CREDENTIALS_INVALID: "AUTH_CREDENTIALS_INVALID",
41
- AUTH_TOKEN_INVALID: "AUTH_TOKEN_INVALID"
41
+ AUTH_TOKEN_INVALID: "AUTH_TOKEN_INVALID",
42
+ // Contact Form 7
43
+ CF7_MAIL_SENT: "CF7_MAIL_SENT",
44
+ CF7_MAIL_FAILED: "CF7_MAIL_FAILED",
45
+ CF7_VALIDATION_FAILED: "CF7_VALIDATION_FAILED",
46
+ CF7_SPAM: "CF7_SPAM",
47
+ CF7_ABORTED: "CF7_ABORTED"
42
48
  };
43
49
  var defaultMessages = {
44
50
  WP_REQUEST_FAILED: "WordPress API request failed",
@@ -63,7 +69,12 @@ var defaultMessages = {
63
69
  WOO_SHIPPING_ZONE_NOT_FOUND: "Shipping zone not found",
64
70
  AUTH_JWT_FAILED: "JWT authentication failed",
65
71
  AUTH_CREDENTIALS_INVALID: "Invalid credentials",
66
- AUTH_TOKEN_INVALID: "JWT token is invalid or expired"
72
+ AUTH_TOKEN_INVALID: "JWT token is invalid or expired",
73
+ CF7_MAIL_SENT: "Thank you for your message.",
74
+ CF7_MAIL_FAILED: "There was an error trying to send your message.",
75
+ CF7_VALIDATION_FAILED: "One or more fields have an error. Please check and try again.",
76
+ CF7_SPAM: "There was an error trying to send your message. Please try later.",
77
+ CF7_ABORTED: "Message submission has been aborted."
67
78
  };
68
79
  var defaultMessagesPl = {
69
80
  WP_REQUEST_FAILED: "B\u0142\u0105d \u017C\u0105dania do WordPress API",
@@ -88,149 +99,17 @@ var defaultMessagesPl = {
88
99
  WOO_SHIPPING_ZONE_NOT_FOUND: "Strefa wysy\u0142ki nie istnieje",
89
100
  AUTH_JWT_FAILED: "B\u0142\u0105d uwierzytelnienia JWT",
90
101
  AUTH_CREDENTIALS_INVALID: "Nieprawid\u0142owe dane logowania",
91
- AUTH_TOKEN_INVALID: "Token JWT jest nieprawid\u0142owy lub wygas\u0142"
102
+ AUTH_TOKEN_INVALID: "Token JWT jest nieprawid\u0142owy lub wygas\u0142",
103
+ CF7_MAIL_SENT: "Dzi\u0119kujemy za wiadomo\u015B\u0107.",
104
+ CF7_MAIL_FAILED: "Wyst\u0105pi\u0142 b\u0142\u0105d podczas wysy\u0142ania wiadomo\u015Bci.",
105
+ CF7_VALIDATION_FAILED: "Jedno lub wi\u0119cej p\xF3l zawiera b\u0142\u0105d. Sprawd\u017A i spr\xF3buj ponownie.",
106
+ CF7_SPAM: "Wyst\u0105pi\u0142 b\u0142\u0105d podczas wysy\u0142ania wiadomo\u015Bci. Spr\xF3buj p\xF3\u017Aniej.",
107
+ CF7_ABORTED: "Wysy\u0142anie formularza zosta\u0142o przerwane."
92
108
  };
93
109
  function resolveMessage(code, overrides) {
94
110
  return overrides?.[code] ?? defaultMessages[code];
95
111
  }
96
112
 
97
- // src/integrations/restApi/core/client/types.ts
98
- var WordPressAPIError = class extends Error {
99
- constructor(code, status, endpoint, message, detail) {
100
- super(detail ? `${message}: ${detail}` : message);
101
- __publicField(this, "code", code);
102
- __publicField(this, "status", status);
103
- __publicField(this, "endpoint", endpoint);
104
- this.name = "WordPressAPIError";
105
- }
106
- };
107
-
108
- // src/integrations/restApi/core/client/url.ts
109
- function buildUrl(config, path, query) {
110
- const base = resolveBaseUrl(config);
111
- if (!query) return `${base}${path}`;
112
- const params = new URLSearchParams();
113
- for (const [key, value] of Object.entries(query)) {
114
- if (value !== void 0 && value !== null) {
115
- params.set(key, String(value));
116
- }
117
- }
118
- const qs = params.toString();
119
- return qs ? `${base}${path}?${qs}` : `${base}${path}`;
120
- }
121
-
122
- // src/integrations/restApi/core/client/fetcher.ts
123
- var USER_AGENT = "NextWordpress Client";
124
- function resolveWpErrorCode(status) {
125
- if (status === 401) return ErrorCode.WP_UNAUTHORIZED;
126
- if (status === 403) return ErrorCode.WP_FORBIDDEN;
127
- if (status === 404) return ErrorCode.WP_NOT_FOUND;
128
- return ErrorCode.WP_REQUEST_FAILED;
129
- }
130
- function createFetcher(config) {
131
- const cacheTtl = config.cacheTTL ?? 300;
132
- async function doFetch(url, init = {}) {
133
- const response = await fetch(url, init);
134
- if (!response.ok) {
135
- const code = resolveWpErrorCode(response.status);
136
- throw new WordPressAPIError(
137
- code,
138
- response.status,
139
- url,
140
- resolveMessage(code, config.errorMessages),
141
- response.statusText
142
- );
143
- }
144
- return response;
145
- }
146
- async function wpFetch(path, query, tags = ["wordpress"]) {
147
- const url = buildUrl(config, path, query);
148
- const res = await doFetch(url, {
149
- headers: { "User-Agent": USER_AGENT },
150
- next: { tags, revalidate: cacheTtl }
151
- });
152
- return await res.json();
153
- }
154
- async function wpFetchGraceful(path, fallback, query, tags = ["wordpress"]) {
155
- try {
156
- return await wpFetch(path, query, tags);
157
- } catch {
158
- console.warn(`WordPress fetch failed for ${path}`);
159
- return fallback;
160
- }
161
- }
162
- async function wpFetchPaginated(path, query, tags = ["wordpress"]) {
163
- const url = buildUrl(config, path, query);
164
- const res = await doFetch(url, {
165
- headers: { "User-Agent": USER_AGENT },
166
- next: { tags, revalidate: cacheTtl }
167
- });
168
- return {
169
- data: await res.json(),
170
- headers: {
171
- total: parseInt(res.headers.get("X-WP-Total") ?? "0", 10),
172
- totalPages: parseInt(res.headers.get("X-WP-TotalPages") ?? "0", 10)
173
- }
174
- };
175
- }
176
- async function wpFetchPaginatedGraceful(path, query, tags = ["wordpress"]) {
177
- const empty = { data: [], headers: { total: 0, totalPages: 0 } };
178
- try {
179
- return await wpFetchPaginated(path, query, tags);
180
- } catch {
181
- console.warn(`WordPress paginated fetch failed for ${path}`);
182
- return empty;
183
- }
184
- }
185
- async function wpMutate(path, body, method = "POST", authToken) {
186
- const url = buildUrl(config, path);
187
- const headers = {
188
- "Content-Type": "application/json",
189
- "User-Agent": USER_AGENT
190
- };
191
- if (authToken) {
192
- headers["Authorization"] = authToken.startsWith("Basic ") || authToken.startsWith("Bearer ") ? authToken : `Bearer ${authToken}`;
193
- }
194
- const res = await doFetch(url, {
195
- method,
196
- headers,
197
- body: JSON.stringify(body),
198
- cache: "no-store"
199
- });
200
- return await res.json();
201
- }
202
- async function wpUpload(path, file, filename, mimeType, authToken) {
203
- const url = buildUrl(config, path);
204
- const res = await doFetch(url, {
205
- method: "POST",
206
- headers: {
207
- "Content-Disposition": `attachment; filename="${filename}"`,
208
- "Content-Type": mimeType,
209
- "User-Agent": USER_AGENT,
210
- "Authorization": authToken.startsWith("Basic ") || authToken.startsWith("Bearer ") ? authToken : `Bearer ${authToken}`
211
- },
212
- body: file,
213
- cache: "no-store"
214
- });
215
- return await res.json();
216
- }
217
- return { wpFetch, wpFetchGraceful, wpFetchPaginated, wpFetchPaginatedGraceful, wpMutate, wpUpload };
218
- }
219
-
220
- // src/integrations/restApi/core/contactForm7/queries.ts
221
- function createCF7Queries(config) {
222
- async function submitForm(formId, data) {
223
- const url = buildUrl(config, `/wp-json/contact-form-7/v1/contact-forms/${formId}/feedback`);
224
- const body = new FormData();
225
- for (const [key, value] of Object.entries(data)) {
226
- body.append(key, value);
227
- }
228
- const res = await fetch(url, { method: "POST", body, cache: "no-store" });
229
- return res.json();
230
- }
231
- return { submitForm };
232
- }
233
-
234
113
  // src/integrations/restApi/core/mutations/posts/mutations.ts
235
114
  function createPostsMutations(fetcher) {
236
115
  const { wpMutate } = fetcher;
@@ -424,6 +303,129 @@ function createMediaMutations(fetcher) {
424
303
  return { uploadMedia, updateMedia, deleteMedia };
425
304
  }
426
305
 
306
+ // src/integrations/restApi/core/client/types.ts
307
+ var WordPressAPIError = class extends Error {
308
+ constructor(code, status, endpoint, message, detail) {
309
+ super(detail ? `${message}: ${detail}` : message);
310
+ __publicField(this, "code", code);
311
+ __publicField(this, "status", status);
312
+ __publicField(this, "endpoint", endpoint);
313
+ this.name = "WordPressAPIError";
314
+ }
315
+ };
316
+
317
+ // src/integrations/restApi/core/client/url.ts
318
+ function buildUrl(config, path, query) {
319
+ const base = resolveBaseUrl(config);
320
+ if (!query) return `${base}${path}`;
321
+ const params = new URLSearchParams();
322
+ for (const [key, value] of Object.entries(query)) {
323
+ if (value !== void 0 && value !== null) {
324
+ params.set(key, String(value));
325
+ }
326
+ }
327
+ const qs = params.toString();
328
+ return qs ? `${base}${path}?${qs}` : `${base}${path}`;
329
+ }
330
+
331
+ // src/integrations/restApi/core/client/fetcher.ts
332
+ var USER_AGENT = "NextWordpress Client";
333
+ function resolveWpErrorCode(status) {
334
+ if (status === 401) return ErrorCode.WP_UNAUTHORIZED;
335
+ if (status === 403) return ErrorCode.WP_FORBIDDEN;
336
+ if (status === 404) return ErrorCode.WP_NOT_FOUND;
337
+ return ErrorCode.WP_REQUEST_FAILED;
338
+ }
339
+ function createFetcher(config) {
340
+ const cacheTtl = config.cacheTTL ?? 300;
341
+ async function doFetch(url, init = {}) {
342
+ const response = await fetch(url, init);
343
+ if (!response.ok) {
344
+ const code = resolveWpErrorCode(response.status);
345
+ throw new WordPressAPIError(
346
+ code,
347
+ response.status,
348
+ url,
349
+ resolveMessage(code, config.errorMessages),
350
+ response.statusText
351
+ );
352
+ }
353
+ return response;
354
+ }
355
+ async function wpFetch(path, query, tags = ["wordpress"]) {
356
+ const url = buildUrl(config, path, query);
357
+ const res = await doFetch(url, {
358
+ headers: { "User-Agent": USER_AGENT },
359
+ next: { tags, revalidate: cacheTtl }
360
+ });
361
+ return await res.json();
362
+ }
363
+ async function wpFetchGraceful(path, fallback, query, tags = ["wordpress"]) {
364
+ try {
365
+ return await wpFetch(path, query, tags);
366
+ } catch {
367
+ console.warn(`WordPress fetch failed for ${path}`);
368
+ return fallback;
369
+ }
370
+ }
371
+ async function wpFetchPaginated(path, query, tags = ["wordpress"]) {
372
+ const url = buildUrl(config, path, query);
373
+ const res = await doFetch(url, {
374
+ headers: { "User-Agent": USER_AGENT },
375
+ next: { tags, revalidate: cacheTtl }
376
+ });
377
+ return {
378
+ data: await res.json(),
379
+ headers: {
380
+ total: parseInt(res.headers.get("X-WP-Total") ?? "0", 10),
381
+ totalPages: parseInt(res.headers.get("X-WP-TotalPages") ?? "0", 10)
382
+ }
383
+ };
384
+ }
385
+ async function wpFetchPaginatedGraceful(path, query, tags = ["wordpress"]) {
386
+ const empty = { data: [], headers: { total: 0, totalPages: 0 } };
387
+ try {
388
+ return await wpFetchPaginated(path, query, tags);
389
+ } catch {
390
+ console.warn(`WordPress paginated fetch failed for ${path}`);
391
+ return empty;
392
+ }
393
+ }
394
+ async function wpMutate(path, body, method = "POST", authToken) {
395
+ const url = buildUrl(config, path);
396
+ const headers = {
397
+ "Content-Type": "application/json",
398
+ "User-Agent": USER_AGENT
399
+ };
400
+ if (authToken) {
401
+ headers["Authorization"] = authToken.startsWith("Basic ") || authToken.startsWith("Bearer ") ? authToken : `Bearer ${authToken}`;
402
+ }
403
+ const res = await doFetch(url, {
404
+ method,
405
+ headers,
406
+ body: JSON.stringify(body),
407
+ cache: "no-store"
408
+ });
409
+ return await res.json();
410
+ }
411
+ async function wpUpload(path, file, filename, mimeType, authToken) {
412
+ const url = buildUrl(config, path);
413
+ const res = await doFetch(url, {
414
+ method: "POST",
415
+ headers: {
416
+ "Content-Disposition": `attachment; filename="${filename}"`,
417
+ "Content-Type": mimeType,
418
+ "User-Agent": USER_AGENT,
419
+ "Authorization": authToken.startsWith("Basic ") || authToken.startsWith("Bearer ") ? authToken : `Bearer ${authToken}`
420
+ },
421
+ body: file,
422
+ cache: "no-store"
423
+ });
424
+ return await res.json();
425
+ }
426
+ return { wpFetch, wpFetchGraceful, wpFetchPaginated, wpFetchPaginatedGraceful, wpMutate, wpUpload };
427
+ }
428
+
427
429
  // src/integrations/restApi/core/mutations/index.ts
428
430
  function createWordPressMutationsClient(config) {
429
431
  const fetcher = createFetcher(config);
@@ -2133,6 +2135,20 @@ function createWPULikeClient(config) {
2133
2135
  return createWPULikeQueries(fetcher);
2134
2136
  }
2135
2137
 
2138
+ // src/integrations/restApi/contactForm7/queries.ts
2139
+ function createCF7Queries(config) {
2140
+ async function submitForm(formId, data) {
2141
+ const url = buildUrl(config, `/wp-json/contact-form-7/v1/contact-forms/${formId}/feedback`);
2142
+ const body = new FormData();
2143
+ for (const [key, value] of Object.entries(data)) {
2144
+ body.append(key, value);
2145
+ }
2146
+ const res = await fetch(url, { method: "POST", body, cache: "no-store" });
2147
+ return res.json();
2148
+ }
2149
+ return { submitForm };
2150
+ }
2151
+
2136
2152
  // src/integrations/wpGraphQL/client/types.ts
2137
2153
  var WPGraphQLError = class extends Error {
2138
2154
  constructor(code, status, endpoint, message, gqlErrors) {
@@ -4209,6 +4225,59 @@ async function resolvePreviewSlug(config, postId, previewPathname) {
4209
4225
  return data.slug ?? `preview-${postId}`;
4210
4226
  }
4211
4227
 
4228
+ // src/nextjs/cookieConsent.ts
4229
+ function createCookieConsentHandler(config) {
4230
+ const cookieName = config?.cookieName ?? "cookie_notice_accepted";
4231
+ const maxAge = config?.maxAge ?? 31536e3;
4232
+ return async function handler(request) {
4233
+ if (request.method === "GET") {
4234
+ const consent = parseCookieConsent(request, cookieName);
4235
+ return Response.json(consent ?? null);
4236
+ }
4237
+ if (request.method === "POST") {
4238
+ const body = await request.json();
4239
+ const categories = {
4240
+ necessary: true,
4241
+ analytics: body.analytics ?? false,
4242
+ marketing: body.marketing ?? false
4243
+ };
4244
+ return new Response(JSON.stringify(categories), {
4245
+ status: 200,
4246
+ headers: {
4247
+ "Content-Type": "application/json",
4248
+ "Set-Cookie": buildConsentCookie(cookieName, encodeURIComponent(JSON.stringify(categories)), maxAge)
4249
+ }
4250
+ });
4251
+ }
4252
+ if (request.method === "DELETE") {
4253
+ return new Response(null, {
4254
+ status: 204,
4255
+ headers: {
4256
+ "Set-Cookie": `${cookieName}=; Max-Age=0; Path=/; SameSite=Lax`
4257
+ }
4258
+ });
4259
+ }
4260
+ return Response.json({ error: "Method Not Allowed" }, { status: 405 });
4261
+ };
4262
+ }
4263
+ function parseCookieConsent(request, cookieName = "cookie_notice_accepted") {
4264
+ const header = request.headers.get("cookie") ?? "";
4265
+ const match = header.match(new RegExp(`(?:^|;\\s*)${cookieName}=([^;]*)`));
4266
+ if (!match) return null;
4267
+ if (match[1] === "1") return { necessary: true, analytics: true, marketing: true };
4268
+ try {
4269
+ const parsed = JSON.parse(decodeURIComponent(match[1]));
4270
+ if (typeof parsed !== "object" || parsed === null) return null;
4271
+ return { necessary: true, analytics: parsed.analytics ?? false, marketing: parsed.marketing ?? false };
4272
+ } catch {
4273
+ return null;
4274
+ }
4275
+ }
4276
+ function buildConsentCookie(name, value, maxAge) {
4277
+ const secure = process.env.NODE_ENV === "production" ? "; Secure" : "";
4278
+ return `${name}=${value}; Path=/; SameSite=Lax; Max-Age=${maxAge}${secure}`;
4279
+ }
4280
+
4212
4281
  // src/auth/applicationPassword.ts
4213
4282
  function createApplicationPasswordToken(username, appPassword) {
4214
4283
  const encoded = Buffer.from(`${username}:${appPassword}`).toString("base64");
@@ -4276,6 +4345,7 @@ exports.createCF7Queries = createCF7Queries;
4276
4345
  exports.createCPTQueries = createCPTQueries;
4277
4346
  exports.createCategoriesMutations = createCategoriesMutations;
4278
4347
  exports.createCommentsMutations = createCommentsMutations;
4348
+ exports.createCookieConsentHandler = createCookieConsentHandler;
4279
4349
  exports.createFaustAuthHandler = createFaustAuthHandler;
4280
4350
  exports.createMediaMutations = createMediaMutations;
4281
4351
  exports.createMenusMutations = createMenusMutations;
@@ -4299,6 +4369,7 @@ exports.createYoastQueries = createYoastQueries;
4299
4369
  exports.defaultMessages = defaultMessages;
4300
4370
  exports.defaultMessagesPl = defaultMessagesPl;
4301
4371
  exports.extractOmnibusData = extractOmnibusData;
4372
+ exports.parseCookieConsent = parseCookieConsent;
4302
4373
  exports.resolveBaseUrl = resolveBaseUrl;
4303
4374
  exports.resolveMessage = resolveMessage;
4304
4375
  exports.validateJwtToken = validateJwtToken;