@szymonpiatek/nextwordpress 0.0.3 → 0.0.5
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/hooks/index.cjs +872 -0
- package/dist/hooks/index.cjs.map +1 -0
- package/dist/hooks/index.d.cts +26 -0
- package/dist/hooks/index.d.ts +26 -0
- package/dist/hooks/index.js +855 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/index.cjs +1301 -19
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +790 -440
- package/dist/index.d.ts +790 -440
- package/dist/index.js +1284 -20
- package/dist/index.js.map +1 -1
- package/dist/types-EjYH-eZp.d.cts +446 -0
- package/dist/types-EjYH-eZp.d.ts +446 -0
- package/package.json +24 -4
|
@@ -0,0 +1,872 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var useSWR2 = require('swr');
|
|
4
|
+
|
|
5
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
6
|
+
|
|
7
|
+
var useSWR2__default = /*#__PURE__*/_interopDefault(useSWR2);
|
|
8
|
+
|
|
9
|
+
var __defProp = Object.defineProperty;
|
|
10
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
11
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
12
|
+
|
|
13
|
+
// src/integrations/restApi/core/client/types.ts
|
|
14
|
+
var WordPressAPIError = class extends Error {
|
|
15
|
+
constructor(message, status, endpoint) {
|
|
16
|
+
super(message);
|
|
17
|
+
__publicField(this, "status", status);
|
|
18
|
+
__publicField(this, "endpoint", endpoint);
|
|
19
|
+
this.name = "WordPressAPIError";
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// src/shared/url.ts
|
|
24
|
+
function resolveBaseUrl(config) {
|
|
25
|
+
return typeof window === "undefined" ? config.serverURL : config.clientURL;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// src/integrations/restApi/core/client/url.ts
|
|
29
|
+
function buildUrl(config, path, query) {
|
|
30
|
+
const base = resolveBaseUrl(config);
|
|
31
|
+
if (!query) return `${base}${path}`;
|
|
32
|
+
const params = new URLSearchParams();
|
|
33
|
+
for (const [key, value] of Object.entries(query)) {
|
|
34
|
+
if (value !== void 0 && value !== null) {
|
|
35
|
+
params.set(key, String(value));
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const qs = params.toString();
|
|
39
|
+
return qs ? `${base}${path}?${qs}` : `${base}${path}`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// src/integrations/restApi/core/client/fetcher.ts
|
|
43
|
+
var USER_AGENT = "NextWordpress Client";
|
|
44
|
+
async function doFetch(url, init = {}) {
|
|
45
|
+
const response = await fetch(url, init);
|
|
46
|
+
if (!response.ok) {
|
|
47
|
+
throw new WordPressAPIError(
|
|
48
|
+
`WordPress API request failed: ${response.statusText}`,
|
|
49
|
+
response.status,
|
|
50
|
+
url
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
return response;
|
|
54
|
+
}
|
|
55
|
+
function createFetcher(config) {
|
|
56
|
+
const cacheTtl = 300;
|
|
57
|
+
async function wpFetch(path, query, tags = ["wordpress"]) {
|
|
58
|
+
const url = buildUrl(config, path, query);
|
|
59
|
+
const res = await doFetch(url, {
|
|
60
|
+
headers: { "User-Agent": USER_AGENT },
|
|
61
|
+
next: { tags, revalidate: cacheTtl }
|
|
62
|
+
});
|
|
63
|
+
return await res.json();
|
|
64
|
+
}
|
|
65
|
+
async function wpFetchGraceful(path, fallback, query, tags = ["wordpress"]) {
|
|
66
|
+
try {
|
|
67
|
+
return await wpFetch(path, query, tags);
|
|
68
|
+
} catch {
|
|
69
|
+
console.warn(`WordPress fetch failed for ${path}`);
|
|
70
|
+
return fallback;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
async function wpFetchPaginated(path, query, tags = ["wordpress"]) {
|
|
74
|
+
const url = buildUrl(config, path, query);
|
|
75
|
+
const res = await doFetch(url, {
|
|
76
|
+
headers: { "User-Agent": USER_AGENT },
|
|
77
|
+
next: { tags, revalidate: cacheTtl }
|
|
78
|
+
});
|
|
79
|
+
return {
|
|
80
|
+
data: await res.json(),
|
|
81
|
+
headers: {
|
|
82
|
+
total: parseInt(res.headers.get("X-WP-Total") ?? "0", 10),
|
|
83
|
+
totalPages: parseInt(res.headers.get("X-WP-TotalPages") ?? "0", 10)
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
async function wpFetchPaginatedGraceful(path, query, tags = ["wordpress"]) {
|
|
88
|
+
const empty = { data: [], headers: { total: 0, totalPages: 0 } };
|
|
89
|
+
try {
|
|
90
|
+
return await wpFetchPaginated(path, query, tags);
|
|
91
|
+
} catch {
|
|
92
|
+
console.warn(`WordPress paginated fetch failed for ${path}`);
|
|
93
|
+
return empty;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
async function wpMutate(path, body, method = "POST", authToken) {
|
|
97
|
+
const url = buildUrl(config, path);
|
|
98
|
+
const headers = {
|
|
99
|
+
"Content-Type": "application/json",
|
|
100
|
+
"User-Agent": USER_AGENT
|
|
101
|
+
};
|
|
102
|
+
if (authToken) {
|
|
103
|
+
headers["Authorization"] = authToken;
|
|
104
|
+
}
|
|
105
|
+
const res = await doFetch(url, {
|
|
106
|
+
method,
|
|
107
|
+
headers,
|
|
108
|
+
body: JSON.stringify(body),
|
|
109
|
+
cache: "no-store"
|
|
110
|
+
});
|
|
111
|
+
return await res.json();
|
|
112
|
+
}
|
|
113
|
+
async function wpUpload(path, file, filename, mimeType, authToken) {
|
|
114
|
+
const url = buildUrl(config, path);
|
|
115
|
+
const res = await doFetch(url, {
|
|
116
|
+
method: "POST",
|
|
117
|
+
headers: {
|
|
118
|
+
"Content-Disposition": `attachment; filename="${filename}"`,
|
|
119
|
+
"Content-Type": mimeType,
|
|
120
|
+
"User-Agent": USER_AGENT,
|
|
121
|
+
"Authorization": authToken
|
|
122
|
+
},
|
|
123
|
+
body: file,
|
|
124
|
+
cache: "no-store"
|
|
125
|
+
});
|
|
126
|
+
return await res.json();
|
|
127
|
+
}
|
|
128
|
+
return { wpFetch, wpFetchGraceful, wpFetchPaginated, wpFetchPaginatedGraceful, wpMutate, wpUpload };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// src/integrations/restApi/core/posts/queries.ts
|
|
132
|
+
function createPostsQueries(fetcher) {
|
|
133
|
+
const { wpFetch, wpFetchGraceful, wpFetchPaginated, wpFetchPaginatedGraceful } = fetcher;
|
|
134
|
+
async function getPostsPaginated(page = 1, perPage = 9, filterParams) {
|
|
135
|
+
const query = { _embed: true, per_page: perPage, page };
|
|
136
|
+
const cacheTags = ["wordpress", "posts", `posts-page-${page}`];
|
|
137
|
+
if (filterParams?.search) {
|
|
138
|
+
query.search = filterParams.search;
|
|
139
|
+
cacheTags.push("posts-search");
|
|
140
|
+
}
|
|
141
|
+
if (filterParams?.author) {
|
|
142
|
+
query.author = filterParams.author;
|
|
143
|
+
cacheTags.push(`posts-author-${filterParams.author}`);
|
|
144
|
+
}
|
|
145
|
+
if (filterParams?.tag) {
|
|
146
|
+
query.tags = filterParams.tag;
|
|
147
|
+
cacheTags.push(`posts-tag-${filterParams.tag}`);
|
|
148
|
+
}
|
|
149
|
+
if (filterParams?.category) {
|
|
150
|
+
query.categories = filterParams.category;
|
|
151
|
+
cacheTags.push(`posts-category-${filterParams.category}`);
|
|
152
|
+
}
|
|
153
|
+
return wpFetchPaginatedGraceful("/wp-json/wp/v2/posts", query, cacheTags);
|
|
154
|
+
}
|
|
155
|
+
async function getRecentPosts(filterParams) {
|
|
156
|
+
const query = { _embed: true, per_page: 100 };
|
|
157
|
+
if (filterParams?.search) query.search = filterParams.search;
|
|
158
|
+
if (filterParams?.author) query.author = filterParams.author;
|
|
159
|
+
if (filterParams?.tag) query.tags = filterParams.tag;
|
|
160
|
+
if (filterParams?.category) query.categories = filterParams.category;
|
|
161
|
+
return wpFetchGraceful("/wp-json/wp/v2/posts", [], query, ["wordpress", "posts"]);
|
|
162
|
+
}
|
|
163
|
+
async function getPostById(id) {
|
|
164
|
+
return wpFetch(`/wp-json/wp/v2/posts/${id}`);
|
|
165
|
+
}
|
|
166
|
+
async function getPostBySlug(slug) {
|
|
167
|
+
const posts = await wpFetchGraceful("/wp-json/wp/v2/posts", [], { slug, _embed: true });
|
|
168
|
+
return posts[0];
|
|
169
|
+
}
|
|
170
|
+
async function getPostsByCategory(categoryId) {
|
|
171
|
+
return wpFetch("/wp-json/wp/v2/posts", { categories: categoryId });
|
|
172
|
+
}
|
|
173
|
+
async function getPostsByTag(tagId) {
|
|
174
|
+
return wpFetch("/wp-json/wp/v2/posts", { tags: tagId });
|
|
175
|
+
}
|
|
176
|
+
async function getPostsByAuthor(authorId) {
|
|
177
|
+
return wpFetch("/wp-json/wp/v2/posts", { author: authorId });
|
|
178
|
+
}
|
|
179
|
+
async function getPostsByCategoryPaginated(categoryId, page = 1, perPage = 9) {
|
|
180
|
+
return wpFetchPaginatedGraceful("/wp-json/wp/v2/posts", {
|
|
181
|
+
_embed: true,
|
|
182
|
+
per_page: perPage,
|
|
183
|
+
page,
|
|
184
|
+
categories: categoryId
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
async function getPostsByTagPaginated(tagId, page = 1, perPage = 9) {
|
|
188
|
+
return wpFetchPaginatedGraceful("/wp-json/wp/v2/posts", {
|
|
189
|
+
_embed: true,
|
|
190
|
+
per_page: perPage,
|
|
191
|
+
page,
|
|
192
|
+
tags: tagId
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
async function getPostsByAuthorPaginated(authorId, page = 1, perPage = 9) {
|
|
196
|
+
return wpFetchPaginatedGraceful("/wp-json/wp/v2/posts", {
|
|
197
|
+
_embed: true,
|
|
198
|
+
per_page: perPage,
|
|
199
|
+
page,
|
|
200
|
+
author: authorId
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
async function getAllPostSlugs() {
|
|
204
|
+
try {
|
|
205
|
+
const allSlugs = [];
|
|
206
|
+
let page = 1;
|
|
207
|
+
let hasMore = true;
|
|
208
|
+
while (hasMore) {
|
|
209
|
+
const response = await wpFetchPaginated(
|
|
210
|
+
"/wp-json/wp/v2/posts",
|
|
211
|
+
{ per_page: 100, page, _fields: "slug" }
|
|
212
|
+
);
|
|
213
|
+
allSlugs.push(...response.data.map((post) => ({ slug: post.slug })));
|
|
214
|
+
hasMore = page < response.headers.totalPages;
|
|
215
|
+
page++;
|
|
216
|
+
}
|
|
217
|
+
return allSlugs;
|
|
218
|
+
} catch {
|
|
219
|
+
console.warn("WordPress unavailable, skipping static generation for posts");
|
|
220
|
+
return [];
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
async function getAllPostsForSitemap() {
|
|
224
|
+
try {
|
|
225
|
+
const allPosts = [];
|
|
226
|
+
let page = 1;
|
|
227
|
+
let hasMore = true;
|
|
228
|
+
while (hasMore) {
|
|
229
|
+
const response = await wpFetchPaginated(
|
|
230
|
+
"/wp-json/wp/v2/posts",
|
|
231
|
+
{ per_page: 100, page, _fields: "slug,modified" }
|
|
232
|
+
);
|
|
233
|
+
allPosts.push(...response.data.map((post) => ({ slug: post.slug, modified: post.modified })));
|
|
234
|
+
hasMore = page < response.headers.totalPages;
|
|
235
|
+
page++;
|
|
236
|
+
}
|
|
237
|
+
return allPosts;
|
|
238
|
+
} catch {
|
|
239
|
+
console.warn("WordPress unavailable, skipping sitemap generation");
|
|
240
|
+
return [];
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return {
|
|
244
|
+
getPostsPaginated,
|
|
245
|
+
getRecentPosts,
|
|
246
|
+
getPostById,
|
|
247
|
+
getPostBySlug,
|
|
248
|
+
getPostsByCategory,
|
|
249
|
+
getPostsByTag,
|
|
250
|
+
getPostsByAuthor,
|
|
251
|
+
getPostsByCategoryPaginated,
|
|
252
|
+
getPostsByTagPaginated,
|
|
253
|
+
getPostsByAuthorPaginated,
|
|
254
|
+
getAllPostSlugs,
|
|
255
|
+
getAllPostsForSitemap
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// src/hooks/usePosts.ts
|
|
260
|
+
function usePosts(config, filter, swrOptions) {
|
|
261
|
+
const key = ["wp-posts", config.clientURL, JSON.stringify(filter ?? null)];
|
|
262
|
+
return useSWR2__default.default(
|
|
263
|
+
key,
|
|
264
|
+
() => {
|
|
265
|
+
const fetcher = createFetcher(config);
|
|
266
|
+
return createPostsQueries(fetcher).getRecentPosts(filter);
|
|
267
|
+
},
|
|
268
|
+
swrOptions
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
function usePost(config, id, swrOptions) {
|
|
272
|
+
const key = id != null ? ["wp-post", config.clientURL, id] : null;
|
|
273
|
+
return useSWR2__default.default(
|
|
274
|
+
key,
|
|
275
|
+
() => {
|
|
276
|
+
const fetcher = createFetcher(config);
|
|
277
|
+
return createPostsQueries(fetcher).getPostById(id);
|
|
278
|
+
},
|
|
279
|
+
swrOptions
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
function usePostBySlug(config, slug, swrOptions) {
|
|
283
|
+
const key = slug ? ["wp-post-slug", config.clientURL, slug] : null;
|
|
284
|
+
return useSWR2__default.default(
|
|
285
|
+
key,
|
|
286
|
+
() => {
|
|
287
|
+
const fetcher = createFetcher(config);
|
|
288
|
+
return createPostsQueries(fetcher).getPostBySlug(slug);
|
|
289
|
+
},
|
|
290
|
+
swrOptions
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
function usePostsPaginated(config, page = 1, perPage = 9, filter, swrOptions) {
|
|
294
|
+
const key = ["wp-posts-paginated", config.clientURL, page, perPage, JSON.stringify(filter ?? null)];
|
|
295
|
+
return useSWR2__default.default(
|
|
296
|
+
key,
|
|
297
|
+
() => {
|
|
298
|
+
const fetcher = createFetcher(config);
|
|
299
|
+
return createPostsQueries(fetcher).getPostsPaginated(page, perPage, filter);
|
|
300
|
+
},
|
|
301
|
+
swrOptions
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// src/integrations/restApi/woocommerce/client/fetcher.ts
|
|
306
|
+
var USER_AGENT2 = "NextWordpress WooCommerce Client";
|
|
307
|
+
var DEFAULT_CACHE_TTL = 3600;
|
|
308
|
+
function toQueryString(params) {
|
|
309
|
+
const pairs = [];
|
|
310
|
+
for (const [key, value] of Object.entries(params)) {
|
|
311
|
+
if (value === void 0 || value === null) continue;
|
|
312
|
+
pairs.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`);
|
|
313
|
+
}
|
|
314
|
+
return pairs.join("&");
|
|
315
|
+
}
|
|
316
|
+
function createWooCommerceFetcher(config) {
|
|
317
|
+
const cacheTTL = config.cacheTTL ?? DEFAULT_CACHE_TTL;
|
|
318
|
+
function withAuth(query) {
|
|
319
|
+
return { ...query, consumer_key: config.consumerKey, consumer_secret: config.consumerSecret };
|
|
320
|
+
}
|
|
321
|
+
function buildUrl2(path, query) {
|
|
322
|
+
return `${config.serverURL}${path}?${toQueryString(withAuth(query))}`;
|
|
323
|
+
}
|
|
324
|
+
async function wcFetch(path, query, tags = ["woocommerce"], options) {
|
|
325
|
+
const url = buildUrl2(path, query);
|
|
326
|
+
const isMutation = options?.method && options.method !== "GET";
|
|
327
|
+
const noStore = options?.cache === "no-store";
|
|
328
|
+
const response = await fetch(url, {
|
|
329
|
+
...options,
|
|
330
|
+
headers: {
|
|
331
|
+
"User-Agent": USER_AGENT2,
|
|
332
|
+
"Content-Type": "application/json",
|
|
333
|
+
...options?.headers
|
|
334
|
+
},
|
|
335
|
+
next: isMutation || noStore ? void 0 : { tags, revalidate: cacheTTL }
|
|
336
|
+
});
|
|
337
|
+
if (!response.ok) {
|
|
338
|
+
const body = await response.json().catch(() => ({}));
|
|
339
|
+
const detail = body.message ? ` \u2013 ${body.message}` : "";
|
|
340
|
+
throw new Error(`WooCommerce API ${response.status}: ${response.statusText}${detail} [${url}]`);
|
|
341
|
+
}
|
|
342
|
+
return response.json();
|
|
343
|
+
}
|
|
344
|
+
async function wcFetchGraceful(path, fallback, query, tags = ["woocommerce"]) {
|
|
345
|
+
try {
|
|
346
|
+
return await wcFetch(path, query, tags);
|
|
347
|
+
} catch (err) {
|
|
348
|
+
console.warn(`WooCommerce fetch failed for ${path}`, err);
|
|
349
|
+
return fallback;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
async function wcFetchPaginated(path, query, tags = ["woocommerce"]) {
|
|
353
|
+
const url = buildUrl2(path, query);
|
|
354
|
+
const response = await fetch(url, {
|
|
355
|
+
headers: { "User-Agent": USER_AGENT2 },
|
|
356
|
+
next: { tags, revalidate: cacheTTL }
|
|
357
|
+
});
|
|
358
|
+
if (!response.ok) {
|
|
359
|
+
throw new Error(`WooCommerce API ${response.status}: ${response.statusText} [${url}]`);
|
|
360
|
+
}
|
|
361
|
+
return {
|
|
362
|
+
data: await response.json(),
|
|
363
|
+
headers: {
|
|
364
|
+
total: parseInt(response.headers.get("X-WP-Total") ?? "0", 10),
|
|
365
|
+
totalPages: parseInt(response.headers.get("X-WP-TotalPages") ?? "0", 10)
|
|
366
|
+
}
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
async function wcFetchPaginatedGraceful(path, query, tags = ["woocommerce"]) {
|
|
370
|
+
const empty = { data: [], headers: { total: 0, totalPages: 0 } };
|
|
371
|
+
try {
|
|
372
|
+
return await wcFetchPaginated(path, query, tags);
|
|
373
|
+
} catch (err) {
|
|
374
|
+
console.warn(`WooCommerce paginated fetch failed for ${path}`, err);
|
|
375
|
+
return empty;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
async function wcMutate(path, body, method = "POST") {
|
|
379
|
+
return wcFetch(path, void 0, ["woocommerce"], {
|
|
380
|
+
method,
|
|
381
|
+
body: JSON.stringify(body)
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
return { wcFetch, wcFetchGraceful, wcFetchPaginated, wcFetchPaginatedGraceful, wcMutate };
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// src/integrations/restApi/woocommerce/products/queries.ts
|
|
388
|
+
function buildProductQuery(base, filterParams) {
|
|
389
|
+
const query = { ...base };
|
|
390
|
+
if (!filterParams) return query;
|
|
391
|
+
if (filterParams.category !== void 0) query.category = filterParams.category;
|
|
392
|
+
if (filterParams.tag !== void 0) query.tag = filterParams.tag;
|
|
393
|
+
if (filterParams.featured !== void 0) query.featured = filterParams.featured;
|
|
394
|
+
if (filterParams.on_sale !== void 0) query.on_sale = filterParams.on_sale;
|
|
395
|
+
if (filterParams.min_price !== void 0) query.min_price = filterParams.min_price;
|
|
396
|
+
if (filterParams.max_price !== void 0) query.max_price = filterParams.max_price;
|
|
397
|
+
if (filterParams.search !== void 0) query.search = filterParams.search;
|
|
398
|
+
if (filterParams.orderby !== void 0) query.orderby = filterParams.orderby;
|
|
399
|
+
if (filterParams.order !== void 0) query.order = filterParams.order;
|
|
400
|
+
if (filterParams.stock_status !== void 0) query.stock_status = filterParams.stock_status;
|
|
401
|
+
if (filterParams.type !== void 0) query.type = filterParams.type;
|
|
402
|
+
return query;
|
|
403
|
+
}
|
|
404
|
+
function createProductsQueries(fetcher) {
|
|
405
|
+
const { wcFetch, wcFetchGraceful, wcFetchPaginated, wcFetchPaginatedGraceful } = fetcher;
|
|
406
|
+
async function getProducts(filterParams) {
|
|
407
|
+
const query = buildProductQuery({ per_page: 100, status: "publish" }, filterParams);
|
|
408
|
+
return wcFetchGraceful("/wp-json/wc/v3/products", [], query, [
|
|
409
|
+
"woocommerce",
|
|
410
|
+
"products"
|
|
411
|
+
]);
|
|
412
|
+
}
|
|
413
|
+
async function getProductsPaginated(page = 1, perPage = 12, filterParams) {
|
|
414
|
+
const query = buildProductQuery({ per_page: perPage, page, status: "publish" }, filterParams);
|
|
415
|
+
const cacheTags = ["woocommerce", "products", `products-page-${page}`];
|
|
416
|
+
if (filterParams?.category) cacheTags.push(`products-category-${filterParams.category}`);
|
|
417
|
+
if (filterParams?.tag) cacheTags.push(`products-tag-${filterParams.tag}`);
|
|
418
|
+
if (filterParams?.search) cacheTags.push("products-search");
|
|
419
|
+
return wcFetchPaginatedGraceful("/wp-json/wc/v3/products", query, cacheTags);
|
|
420
|
+
}
|
|
421
|
+
async function getProductById(id) {
|
|
422
|
+
return wcFetch(`/wp-json/wc/v3/products/${id}`, void 0, [
|
|
423
|
+
"woocommerce",
|
|
424
|
+
"products",
|
|
425
|
+
`product-${id}`
|
|
426
|
+
]);
|
|
427
|
+
}
|
|
428
|
+
async function getProductBySlug(slug) {
|
|
429
|
+
const products = await wcFetchGraceful(
|
|
430
|
+
"/wp-json/wc/v3/products",
|
|
431
|
+
[],
|
|
432
|
+
{ slug },
|
|
433
|
+
["woocommerce", "products", `product-slug-${slug}`]
|
|
434
|
+
);
|
|
435
|
+
return products[0];
|
|
436
|
+
}
|
|
437
|
+
async function getFeaturedProducts(limit = 10) {
|
|
438
|
+
return wcFetchGraceful(
|
|
439
|
+
"/wp-json/wc/v3/products",
|
|
440
|
+
[],
|
|
441
|
+
{ featured: true, per_page: limit, status: "publish" },
|
|
442
|
+
["woocommerce", "products", "featured"]
|
|
443
|
+
);
|
|
444
|
+
}
|
|
445
|
+
async function getOnSaleProducts(limit = 10) {
|
|
446
|
+
return wcFetchGraceful(
|
|
447
|
+
"/wp-json/wc/v3/products",
|
|
448
|
+
[],
|
|
449
|
+
{ on_sale: true, per_page: limit, status: "publish" },
|
|
450
|
+
["woocommerce", "products", "on-sale"]
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
async function getRelatedProducts(productId) {
|
|
454
|
+
const product = await getProductById(productId);
|
|
455
|
+
if (!product.related_ids.length) return [];
|
|
456
|
+
return wcFetchGraceful(
|
|
457
|
+
"/wp-json/wc/v3/products",
|
|
458
|
+
[],
|
|
459
|
+
{ include: product.related_ids.join(","), per_page: 10, status: "publish" },
|
|
460
|
+
["woocommerce", "products", `related-${productId}`]
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
async function getAllProductSlugs() {
|
|
464
|
+
try {
|
|
465
|
+
const allSlugs = [];
|
|
466
|
+
let page = 1;
|
|
467
|
+
let hasMore = true;
|
|
468
|
+
while (hasMore) {
|
|
469
|
+
const response = await wcFetchPaginated("/wp-json/wc/v3/products", {
|
|
470
|
+
per_page: 100,
|
|
471
|
+
page,
|
|
472
|
+
status: "publish",
|
|
473
|
+
_fields: "slug"
|
|
474
|
+
});
|
|
475
|
+
allSlugs.push(...response.data.map((p) => ({ slug: p.slug })));
|
|
476
|
+
hasMore = page < response.headers.totalPages;
|
|
477
|
+
page++;
|
|
478
|
+
}
|
|
479
|
+
return allSlugs;
|
|
480
|
+
} catch {
|
|
481
|
+
console.warn("WooCommerce unavailable, skipping static generation for products");
|
|
482
|
+
return [];
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
async function getProductVariations(productId) {
|
|
486
|
+
return wcFetchGraceful(
|
|
487
|
+
`/wp-json/wc/v3/products/${productId}/variations`,
|
|
488
|
+
[],
|
|
489
|
+
{ per_page: 100 },
|
|
490
|
+
["woocommerce", "products", `product-${productId}`, "variations"]
|
|
491
|
+
);
|
|
492
|
+
}
|
|
493
|
+
async function getProductVariationById(productId, variationId) {
|
|
494
|
+
return wcFetch(
|
|
495
|
+
`/wp-json/wc/v3/products/${productId}/variations/${variationId}`,
|
|
496
|
+
void 0,
|
|
497
|
+
["woocommerce", "products", `product-${productId}`, `variation-${variationId}`]
|
|
498
|
+
);
|
|
499
|
+
}
|
|
500
|
+
return {
|
|
501
|
+
getProducts,
|
|
502
|
+
getProductsPaginated,
|
|
503
|
+
getProductById,
|
|
504
|
+
getProductBySlug,
|
|
505
|
+
getFeaturedProducts,
|
|
506
|
+
getOnSaleProducts,
|
|
507
|
+
getRelatedProducts,
|
|
508
|
+
getAllProductSlugs,
|
|
509
|
+
getProductVariations,
|
|
510
|
+
getProductVariationById
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
// src/hooks/useProducts.ts
|
|
515
|
+
function useProducts(config, filter, swrOptions) {
|
|
516
|
+
const key = ["wc-products", config.serverURL, JSON.stringify(filter ?? null)];
|
|
517
|
+
return useSWR2__default.default(
|
|
518
|
+
key,
|
|
519
|
+
() => {
|
|
520
|
+
const fetcher = createWooCommerceFetcher(config);
|
|
521
|
+
return createProductsQueries(fetcher).getProducts(filter);
|
|
522
|
+
},
|
|
523
|
+
swrOptions
|
|
524
|
+
);
|
|
525
|
+
}
|
|
526
|
+
function useProduct(config, id, swrOptions) {
|
|
527
|
+
const key = id != null ? ["wc-product", config.serverURL, id] : null;
|
|
528
|
+
return useSWR2__default.default(
|
|
529
|
+
key,
|
|
530
|
+
() => {
|
|
531
|
+
const fetcher = createWooCommerceFetcher(config);
|
|
532
|
+
return createProductsQueries(fetcher).getProductById(id);
|
|
533
|
+
},
|
|
534
|
+
swrOptions
|
|
535
|
+
);
|
|
536
|
+
}
|
|
537
|
+
function useProductBySlug(config, slug, swrOptions) {
|
|
538
|
+
const key = slug ? ["wc-product-slug", config.serverURL, slug] : null;
|
|
539
|
+
return useSWR2__default.default(
|
|
540
|
+
key,
|
|
541
|
+
() => {
|
|
542
|
+
const fetcher = createWooCommerceFetcher(config);
|
|
543
|
+
return createProductsQueries(fetcher).getProductBySlug(slug);
|
|
544
|
+
},
|
|
545
|
+
swrOptions
|
|
546
|
+
);
|
|
547
|
+
}
|
|
548
|
+
function useProductsPaginated(config, page = 1, perPage = 12, filter, swrOptions) {
|
|
549
|
+
const key = ["wc-products-paginated", config.serverURL, page, perPage, JSON.stringify(filter ?? null)];
|
|
550
|
+
return useSWR2__default.default(
|
|
551
|
+
key,
|
|
552
|
+
() => {
|
|
553
|
+
const fetcher = createWooCommerceFetcher(config);
|
|
554
|
+
return createProductsQueries(fetcher).getProductsPaginated(page, perPage, filter);
|
|
555
|
+
},
|
|
556
|
+
swrOptions
|
|
557
|
+
);
|
|
558
|
+
}
|
|
559
|
+
function useFeaturedProducts(config, limit = 10, swrOptions) {
|
|
560
|
+
const key = ["wc-products-featured", config.serverURL, limit];
|
|
561
|
+
return useSWR2__default.default(
|
|
562
|
+
key,
|
|
563
|
+
() => {
|
|
564
|
+
const fetcher = createWooCommerceFetcher(config);
|
|
565
|
+
return createProductsQueries(fetcher).getFeaturedProducts(limit);
|
|
566
|
+
},
|
|
567
|
+
swrOptions
|
|
568
|
+
);
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
// src/integrations/wpGraphQL/client/types.ts
|
|
572
|
+
var WPGraphQLError = class extends Error {
|
|
573
|
+
constructor(message, status, endpoint, gqlErrors) {
|
|
574
|
+
super(message);
|
|
575
|
+
__publicField(this, "status", status);
|
|
576
|
+
__publicField(this, "endpoint", endpoint);
|
|
577
|
+
__publicField(this, "gqlErrors", gqlErrors);
|
|
578
|
+
this.name = "WPGraphQLError";
|
|
579
|
+
}
|
|
580
|
+
};
|
|
581
|
+
|
|
582
|
+
// src/integrations/wpGraphQL/client/fetcher.ts
|
|
583
|
+
var USER_AGENT3 = "NextWordpress WPGraphQL Client";
|
|
584
|
+
var CACHE_TTL = 300;
|
|
585
|
+
function createWPGraphQLFetcher(config) {
|
|
586
|
+
const url = `${resolveBaseUrl(config)}/graphql`;
|
|
587
|
+
async function gqlFetch(document, variables, tags = ["wpgraphql"]) {
|
|
588
|
+
const response = await fetch(url, {
|
|
589
|
+
method: "POST",
|
|
590
|
+
headers: {
|
|
591
|
+
"Content-Type": "application/json",
|
|
592
|
+
"User-Agent": USER_AGENT3
|
|
593
|
+
},
|
|
594
|
+
body: JSON.stringify({ query: document, variables }),
|
|
595
|
+
next: { tags, revalidate: CACHE_TTL }
|
|
596
|
+
});
|
|
597
|
+
if (!response.ok) {
|
|
598
|
+
throw new WPGraphQLError(
|
|
599
|
+
`WPGraphQL request failed: ${response.statusText}`,
|
|
600
|
+
response.status,
|
|
601
|
+
url
|
|
602
|
+
);
|
|
603
|
+
}
|
|
604
|
+
const parsed = await response.json();
|
|
605
|
+
if (parsed.errors && parsed.errors.length > 0) {
|
|
606
|
+
throw new WPGraphQLError(
|
|
607
|
+
parsed.errors[0].message,
|
|
608
|
+
200,
|
|
609
|
+
url,
|
|
610
|
+
parsed.errors
|
|
611
|
+
);
|
|
612
|
+
}
|
|
613
|
+
if (parsed.data === void 0) {
|
|
614
|
+
throw new WPGraphQLError("No data returned from WPGraphQL", 200, url);
|
|
615
|
+
}
|
|
616
|
+
return parsed.data;
|
|
617
|
+
}
|
|
618
|
+
async function gqlFetchGraceful(document, fallback, variables, tags = ["wpgraphql"]) {
|
|
619
|
+
try {
|
|
620
|
+
return await gqlFetch(document, variables, tags);
|
|
621
|
+
} catch {
|
|
622
|
+
console.warn(`WPGraphQL fetch failed for query`);
|
|
623
|
+
return fallback;
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
async function gqlMutate(document, variables, authToken) {
|
|
627
|
+
const headers = {
|
|
628
|
+
"Content-Type": "application/json",
|
|
629
|
+
"User-Agent": USER_AGENT3
|
|
630
|
+
};
|
|
631
|
+
if (authToken) {
|
|
632
|
+
headers["Authorization"] = authToken;
|
|
633
|
+
}
|
|
634
|
+
const response = await fetch(url, {
|
|
635
|
+
method: "POST",
|
|
636
|
+
headers,
|
|
637
|
+
body: JSON.stringify({ query: document, variables }),
|
|
638
|
+
cache: "no-store"
|
|
639
|
+
});
|
|
640
|
+
if (!response.ok) {
|
|
641
|
+
throw new WPGraphQLError(
|
|
642
|
+
`WPGraphQL mutation failed: ${response.statusText}`,
|
|
643
|
+
response.status,
|
|
644
|
+
url
|
|
645
|
+
);
|
|
646
|
+
}
|
|
647
|
+
const parsed = await response.json();
|
|
648
|
+
if (parsed.errors && parsed.errors.length > 0) {
|
|
649
|
+
throw new WPGraphQLError(
|
|
650
|
+
parsed.errors[0].message,
|
|
651
|
+
200,
|
|
652
|
+
url,
|
|
653
|
+
parsed.errors
|
|
654
|
+
);
|
|
655
|
+
}
|
|
656
|
+
if (parsed.data === void 0) {
|
|
657
|
+
throw new WPGraphQLError("No data returned from WPGraphQL mutation", 200, url);
|
|
658
|
+
}
|
|
659
|
+
return parsed.data;
|
|
660
|
+
}
|
|
661
|
+
return { gqlFetch, gqlFetchGraceful, gqlMutate };
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
// src/integrations/wpGraphQL/core/posts/queries.ts
|
|
665
|
+
var DEFAULT_FIRST = 10;
|
|
666
|
+
var BATCH_FIRST = 100;
|
|
667
|
+
var POST_FIELDS = `
|
|
668
|
+
id
|
|
669
|
+
databaseId
|
|
670
|
+
slug
|
|
671
|
+
title
|
|
672
|
+
excerpt
|
|
673
|
+
date
|
|
674
|
+
modified
|
|
675
|
+
status
|
|
676
|
+
uri
|
|
677
|
+
author {
|
|
678
|
+
node {
|
|
679
|
+
databaseId
|
|
680
|
+
name
|
|
681
|
+
slug
|
|
682
|
+
avatar { url }
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
featuredImage {
|
|
686
|
+
node {
|
|
687
|
+
sourceUrl
|
|
688
|
+
altText
|
|
689
|
+
mediaDetails { width height }
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
categories {
|
|
693
|
+
nodes { databaseId name slug }
|
|
694
|
+
}
|
|
695
|
+
tags {
|
|
696
|
+
nodes { databaseId name slug }
|
|
697
|
+
}
|
|
698
|
+
`;
|
|
699
|
+
var POST_FIELDS_WITH_CONTENT = `
|
|
700
|
+
${POST_FIELDS}
|
|
701
|
+
content
|
|
702
|
+
`;
|
|
703
|
+
var GQL_GET_POSTS = `
|
|
704
|
+
query GetPosts($first: Int, $after: String, $where: RootQueryToPostConnectionWhereArgs) {
|
|
705
|
+
posts(first: $first, after: $after, where: $where) {
|
|
706
|
+
nodes { ${POST_FIELDS} }
|
|
707
|
+
pageInfo { hasNextPage hasPreviousPage startCursor endCursor }
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
`;
|
|
711
|
+
var GQL_GET_POST_BY_SLUG = `
|
|
712
|
+
query GetPostBySlug($slug: ID!) {
|
|
713
|
+
post(id: $slug, idType: SLUG) { ${POST_FIELDS_WITH_CONTENT} }
|
|
714
|
+
}
|
|
715
|
+
`;
|
|
716
|
+
var GQL_GET_POST_BY_DATABASE_ID = `
|
|
717
|
+
query GetPostByDatabaseId($id: ID!) {
|
|
718
|
+
post(id: $id, idType: DATABASE_ID) { ${POST_FIELDS_WITH_CONTENT} }
|
|
719
|
+
}
|
|
720
|
+
`;
|
|
721
|
+
var GQL_GET_ALL_POST_SLUGS = `
|
|
722
|
+
query GetAllPostSlugs($first: Int, $after: String) {
|
|
723
|
+
posts(first: $first, after: $after) {
|
|
724
|
+
nodes { slug }
|
|
725
|
+
pageInfo { hasNextPage endCursor }
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
`;
|
|
729
|
+
var GQL_GET_ALL_POSTS_FOR_SITEMAP = `
|
|
730
|
+
query GetAllPostsForSitemap($first: Int, $after: String) {
|
|
731
|
+
posts(first: $first, after: $after) {
|
|
732
|
+
nodes { slug modified }
|
|
733
|
+
pageInfo { hasNextPage endCursor }
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
`;
|
|
737
|
+
function createPostsQueries2(fetcher) {
|
|
738
|
+
const { gqlFetch, gqlFetchGraceful } = fetcher;
|
|
739
|
+
async function getPosts(first = DEFAULT_FIRST, after, filter) {
|
|
740
|
+
const where = {};
|
|
741
|
+
if (filter?.authorName) where["authorName"] = filter.authorName;
|
|
742
|
+
if (filter?.categoryName) where["categoryName"] = filter.categoryName;
|
|
743
|
+
if (filter?.tag) where["tag"] = filter.tag;
|
|
744
|
+
if (filter?.search) where["search"] = filter.search;
|
|
745
|
+
if (filter?.status) where["status"] = filter.status;
|
|
746
|
+
const data = await gqlFetchGraceful(
|
|
747
|
+
GQL_GET_POSTS,
|
|
748
|
+
{ posts: { nodes: [], pageInfo: { hasNextPage: false, hasPreviousPage: false } } },
|
|
749
|
+
{ first, after, where: Object.keys(where).length > 0 ? where : void 0 },
|
|
750
|
+
["wpgraphql", "gql-posts"]
|
|
751
|
+
);
|
|
752
|
+
return data.posts;
|
|
753
|
+
}
|
|
754
|
+
async function getPostBySlug(slug) {
|
|
755
|
+
const data = await gqlFetchGraceful(
|
|
756
|
+
GQL_GET_POST_BY_SLUG,
|
|
757
|
+
{ post: null },
|
|
758
|
+
{ slug },
|
|
759
|
+
["wpgraphql", "gql-posts", `gql-post-${slug}`]
|
|
760
|
+
);
|
|
761
|
+
return data.post;
|
|
762
|
+
}
|
|
763
|
+
async function getPostByDatabaseId(id) {
|
|
764
|
+
const data = await gqlFetchGraceful(
|
|
765
|
+
GQL_GET_POST_BY_DATABASE_ID,
|
|
766
|
+
{ post: null },
|
|
767
|
+
{ id: String(id) },
|
|
768
|
+
["wpgraphql", "gql-posts", `gql-post-id-${id}`]
|
|
769
|
+
);
|
|
770
|
+
return data.post;
|
|
771
|
+
}
|
|
772
|
+
async function getRecentPosts(first = DEFAULT_FIRST) {
|
|
773
|
+
const connection = await getPosts(first);
|
|
774
|
+
return connection.nodes;
|
|
775
|
+
}
|
|
776
|
+
async function getAllPostSlugs() {
|
|
777
|
+
try {
|
|
778
|
+
const all = [];
|
|
779
|
+
let after = void 0;
|
|
780
|
+
let hasNextPage = true;
|
|
781
|
+
while (hasNextPage) {
|
|
782
|
+
const data = await gqlFetch(GQL_GET_ALL_POST_SLUGS, { first: BATCH_FIRST, after }, ["wpgraphql"]);
|
|
783
|
+
all.push(...data.posts.nodes);
|
|
784
|
+
hasNextPage = data.posts.pageInfo.hasNextPage;
|
|
785
|
+
after = data.posts.pageInfo.endCursor;
|
|
786
|
+
}
|
|
787
|
+
return all;
|
|
788
|
+
} catch {
|
|
789
|
+
console.warn("WPGraphQL unavailable, skipping static generation for posts");
|
|
790
|
+
return [];
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
async function getAllPostsForSitemap() {
|
|
794
|
+
try {
|
|
795
|
+
const all = [];
|
|
796
|
+
let after = void 0;
|
|
797
|
+
let hasNextPage = true;
|
|
798
|
+
while (hasNextPage) {
|
|
799
|
+
const data = await gqlFetch(
|
|
800
|
+
GQL_GET_ALL_POSTS_FOR_SITEMAP,
|
|
801
|
+
{ first: BATCH_FIRST, after },
|
|
802
|
+
["wpgraphql"]
|
|
803
|
+
);
|
|
804
|
+
all.push(...data.posts.nodes);
|
|
805
|
+
hasNextPage = data.posts.pageInfo.hasNextPage;
|
|
806
|
+
after = data.posts.pageInfo.endCursor;
|
|
807
|
+
}
|
|
808
|
+
return all;
|
|
809
|
+
} catch {
|
|
810
|
+
console.warn("WPGraphQL unavailable, skipping sitemap generation");
|
|
811
|
+
return [];
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
return {
|
|
815
|
+
getPosts,
|
|
816
|
+
getPostBySlug,
|
|
817
|
+
getPostByDatabaseId,
|
|
818
|
+
getRecentPosts,
|
|
819
|
+
getAllPostSlugs,
|
|
820
|
+
getAllPostsForSitemap
|
|
821
|
+
};
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
// src/hooks/useWPGraphQL.ts
|
|
825
|
+
function useWPGraphQL(config, query, variables, tags, swrOptions) {
|
|
826
|
+
const key = ["wpgraphql", config.clientURL, query, JSON.stringify(variables ?? null)];
|
|
827
|
+
return useSWR2__default.default(
|
|
828
|
+
key,
|
|
829
|
+
() => {
|
|
830
|
+
const fetcher = createWPGraphQLFetcher(config);
|
|
831
|
+
return fetcher.gqlFetch(query, variables, tags);
|
|
832
|
+
},
|
|
833
|
+
swrOptions
|
|
834
|
+
);
|
|
835
|
+
}
|
|
836
|
+
function useGQLPosts(config, first, after, filter, swrOptions) {
|
|
837
|
+
const key = ["wpgraphql-posts", config.clientURL, first, after, JSON.stringify(filter ?? null)];
|
|
838
|
+
return useSWR2__default.default(
|
|
839
|
+
key,
|
|
840
|
+
() => {
|
|
841
|
+
const fetcher = createWPGraphQLFetcher(config);
|
|
842
|
+
return createPostsQueries2(fetcher).getPosts(first, after, filter);
|
|
843
|
+
},
|
|
844
|
+
swrOptions
|
|
845
|
+
);
|
|
846
|
+
}
|
|
847
|
+
function useGQLPostBySlug(config, slug, swrOptions) {
|
|
848
|
+
const key = slug ? ["wpgraphql-post-slug", config.clientURL, slug] : null;
|
|
849
|
+
return useSWR2__default.default(
|
|
850
|
+
key,
|
|
851
|
+
() => {
|
|
852
|
+
const fetcher = createWPGraphQLFetcher(config);
|
|
853
|
+
return createPostsQueries2(fetcher).getPostBySlug(slug);
|
|
854
|
+
},
|
|
855
|
+
swrOptions
|
|
856
|
+
);
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
exports.useFeaturedProducts = useFeaturedProducts;
|
|
860
|
+
exports.useGQLPostBySlug = useGQLPostBySlug;
|
|
861
|
+
exports.useGQLPosts = useGQLPosts;
|
|
862
|
+
exports.usePost = usePost;
|
|
863
|
+
exports.usePostBySlug = usePostBySlug;
|
|
864
|
+
exports.usePosts = usePosts;
|
|
865
|
+
exports.usePostsPaginated = usePostsPaginated;
|
|
866
|
+
exports.useProduct = useProduct;
|
|
867
|
+
exports.useProductBySlug = useProductBySlug;
|
|
868
|
+
exports.useProducts = useProducts;
|
|
869
|
+
exports.useProductsPaginated = useProductsPaginated;
|
|
870
|
+
exports.useWPGraphQL = useWPGraphQL;
|
|
871
|
+
//# sourceMappingURL=index.cjs.map
|
|
872
|
+
//# sourceMappingURL=index.cjs.map
|