@szymonpiatek/nextwordpress 0.0.1

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 ADDED
@@ -0,0 +1,798 @@
1
+ 'use strict';
2
+
3
+ var __defProp = Object.defineProperty;
4
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
5
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
6
+
7
+ // src/client/url/url.ts
8
+ function resolveBaseUrl(config) {
9
+ return typeof window === "undefined" ? config.serverURL : config.clientURL;
10
+ }
11
+ function buildUrl(config, path, query) {
12
+ const base = resolveBaseUrl(config);
13
+ if (!query) return `${base}${path}`;
14
+ const params = new URLSearchParams();
15
+ for (const [key, value] of Object.entries(query)) {
16
+ if (value !== void 0 && value !== null) {
17
+ params.set(key, String(value));
18
+ }
19
+ }
20
+ const qs = params.toString();
21
+ return qs ? `${base}${path}?${qs}` : `${base}${path}`;
22
+ }
23
+
24
+ // src/client/types/types.ts
25
+ var WordPressAPIError = class extends Error {
26
+ constructor(message, status, endpoint) {
27
+ super(message);
28
+ __publicField(this, "status", status);
29
+ __publicField(this, "endpoint", endpoint);
30
+ this.name = "WordPressAPIError";
31
+ }
32
+ };
33
+
34
+ // src/client/fetcher/fetcher.ts
35
+ var USER_AGENT = "NextWordpress Client";
36
+ async function doFetch(url, init = {}) {
37
+ const response = await fetch(url, init);
38
+ if (!response.ok) {
39
+ throw new WordPressAPIError(
40
+ `WordPress API request failed: ${response.statusText}`,
41
+ response.status,
42
+ url
43
+ );
44
+ }
45
+ return response;
46
+ }
47
+ function createFetcher(config) {
48
+ const cacheTtl = 300;
49
+ async function wpFetch(path, query, tags = ["wordpress"]) {
50
+ const url = buildUrl(config, path, query);
51
+ const res = await doFetch(url, {
52
+ headers: { "User-Agent": USER_AGENT },
53
+ next: { tags, revalidate: cacheTtl }
54
+ });
55
+ return await res.json();
56
+ }
57
+ async function wpFetchGraceful(path, fallback, query, tags = ["wordpress"]) {
58
+ try {
59
+ return await wpFetch(path, query, tags);
60
+ } catch {
61
+ console.warn(`WordPress fetch failed for ${path}`);
62
+ return fallback;
63
+ }
64
+ }
65
+ async function wpFetchPaginated(path, query, tags = ["wordpress"]) {
66
+ const url = buildUrl(config, path, query);
67
+ const res = await doFetch(url, {
68
+ headers: { "User-Agent": USER_AGENT },
69
+ next: { tags, revalidate: cacheTtl }
70
+ });
71
+ return {
72
+ data: await res.json(),
73
+ headers: {
74
+ total: parseInt(res.headers.get("X-WP-Total") ?? "0", 10),
75
+ totalPages: parseInt(res.headers.get("X-WP-TotalPages") ?? "0", 10)
76
+ }
77
+ };
78
+ }
79
+ async function wpFetchPaginatedGraceful(path, query, tags = ["wordpress"]) {
80
+ const empty = { data: [], headers: { total: 0, totalPages: 0 } };
81
+ try {
82
+ return await wpFetchPaginated(path, query, tags);
83
+ } catch {
84
+ console.warn(`WordPress paginated fetch failed for ${path}`);
85
+ return empty;
86
+ }
87
+ }
88
+ async function wpMutate(path, body, method = "POST") {
89
+ const url = buildUrl(config, path);
90
+ const res = await doFetch(url, {
91
+ method,
92
+ headers: {
93
+ "Content-Type": "application/json",
94
+ "User-Agent": USER_AGENT
95
+ },
96
+ body: JSON.stringify(body),
97
+ cache: "no-store"
98
+ });
99
+ return await res.json();
100
+ }
101
+ return { wpFetch, wpFetchGraceful, wpFetchPaginated, wpFetchPaginatedGraceful, wpMutate };
102
+ }
103
+
104
+ // src/integrations/base/posts/queries.ts
105
+ function createPostsQueries(fetcher) {
106
+ const { wpFetch, wpFetchGraceful, wpFetchPaginated, wpFetchPaginatedGraceful } = fetcher;
107
+ async function getPostsPaginated(page = 1, perPage = 9, filterParams) {
108
+ const query = { _embed: true, per_page: perPage, page };
109
+ const cacheTags = ["wordpress", "posts", `posts-page-${page}`];
110
+ if (filterParams?.search) {
111
+ query.search = filterParams.search;
112
+ cacheTags.push("posts-search");
113
+ }
114
+ if (filterParams?.author) {
115
+ query.author = filterParams.author;
116
+ cacheTags.push(`posts-author-${filterParams.author}`);
117
+ }
118
+ if (filterParams?.tag) {
119
+ query.tags = filterParams.tag;
120
+ cacheTags.push(`posts-tag-${filterParams.tag}`);
121
+ }
122
+ if (filterParams?.category) {
123
+ query.categories = filterParams.category;
124
+ cacheTags.push(`posts-category-${filterParams.category}`);
125
+ }
126
+ return wpFetchPaginatedGraceful("/wp-json/wp/v2/posts", query, cacheTags);
127
+ }
128
+ async function getRecentPosts(filterParams) {
129
+ const query = { _embed: true, per_page: 100 };
130
+ if (filterParams?.search) query.search = filterParams.search;
131
+ if (filterParams?.author) query.author = filterParams.author;
132
+ if (filterParams?.tag) query.tags = filterParams.tag;
133
+ if (filterParams?.category) query.categories = filterParams.category;
134
+ return wpFetchGraceful("/wp-json/wp/v2/posts", [], query, ["wordpress", "posts"]);
135
+ }
136
+ async function getPostById(id) {
137
+ return wpFetch(`/wp-json/wp/v2/posts/${id}`);
138
+ }
139
+ async function getPostBySlug(slug) {
140
+ const posts = await wpFetchGraceful("/wp-json/wp/v2/posts", [], { slug, _embed: true });
141
+ return posts[0];
142
+ }
143
+ async function getPostsByCategory(categoryId) {
144
+ return wpFetch("/wp-json/wp/v2/posts", { categories: categoryId });
145
+ }
146
+ async function getPostsByTag(tagId) {
147
+ return wpFetch("/wp-json/wp/v2/posts", { tags: tagId });
148
+ }
149
+ async function getPostsByAuthor(authorId) {
150
+ return wpFetch("/wp-json/wp/v2/posts", { author: authorId });
151
+ }
152
+ async function getPostsByCategoryPaginated(categoryId, page = 1, perPage = 9) {
153
+ return wpFetchPaginatedGraceful("/wp-json/wp/v2/posts", {
154
+ _embed: true,
155
+ per_page: perPage,
156
+ page,
157
+ categories: categoryId
158
+ });
159
+ }
160
+ async function getPostsByTagPaginated(tagId, page = 1, perPage = 9) {
161
+ return wpFetchPaginatedGraceful("/wp-json/wp/v2/posts", {
162
+ _embed: true,
163
+ per_page: perPage,
164
+ page,
165
+ tags: tagId
166
+ });
167
+ }
168
+ async function getPostsByAuthorPaginated(authorId, page = 1, perPage = 9) {
169
+ return wpFetchPaginatedGraceful("/wp-json/wp/v2/posts", {
170
+ _embed: true,
171
+ per_page: perPage,
172
+ page,
173
+ author: authorId
174
+ });
175
+ }
176
+ async function getAllPostSlugs() {
177
+ try {
178
+ const allSlugs = [];
179
+ let page = 1;
180
+ let hasMore = true;
181
+ while (hasMore) {
182
+ const response = await wpFetchPaginated(
183
+ "/wp-json/wp/v2/posts",
184
+ { per_page: 100, page, _fields: "slug" }
185
+ );
186
+ allSlugs.push(...response.data.map((post) => ({ slug: post.slug })));
187
+ hasMore = page < response.headers.totalPages;
188
+ page++;
189
+ }
190
+ return allSlugs;
191
+ } catch {
192
+ console.warn("WordPress unavailable, skipping static generation for posts");
193
+ return [];
194
+ }
195
+ }
196
+ async function getAllPostsForSitemap() {
197
+ try {
198
+ const allPosts = [];
199
+ let page = 1;
200
+ let hasMore = true;
201
+ while (hasMore) {
202
+ const response = await wpFetchPaginated(
203
+ "/wp-json/wp/v2/posts",
204
+ { per_page: 100, page, _fields: "slug,modified" }
205
+ );
206
+ allPosts.push(...response.data.map((post) => ({ slug: post.slug, modified: post.modified })));
207
+ hasMore = page < response.headers.totalPages;
208
+ page++;
209
+ }
210
+ return allPosts;
211
+ } catch {
212
+ console.warn("WordPress unavailable, skipping sitemap generation");
213
+ return [];
214
+ }
215
+ }
216
+ return {
217
+ getPostsPaginated,
218
+ getRecentPosts,
219
+ getPostById,
220
+ getPostBySlug,
221
+ getPostsByCategory,
222
+ getPostsByTag,
223
+ getPostsByAuthor,
224
+ getPostsByCategoryPaginated,
225
+ getPostsByTagPaginated,
226
+ getPostsByAuthorPaginated,
227
+ getAllPostSlugs,
228
+ getAllPostsForSitemap
229
+ };
230
+ }
231
+
232
+ // src/integrations/base/categories/queries.ts
233
+ function createCategoriesQueries(fetcher) {
234
+ const { wpFetch, wpFetchGraceful } = fetcher;
235
+ async function getAllCategories() {
236
+ return wpFetchGraceful("/wp-json/wp/v2/categories", [], { per_page: 100 }, [
237
+ "wordpress",
238
+ "categories"
239
+ ]);
240
+ }
241
+ async function getCategoryById(id) {
242
+ return wpFetch(`/wp-json/wp/v2/categories/${id}`);
243
+ }
244
+ async function getCategoryBySlug(slug) {
245
+ const categories = await wpFetch("/wp-json/wp/v2/categories", { slug });
246
+ return categories[0];
247
+ }
248
+ async function searchCategories(query) {
249
+ return wpFetchGraceful("/wp-json/wp/v2/categories", [], {
250
+ search: query,
251
+ per_page: 100
252
+ });
253
+ }
254
+ return { getAllCategories, getCategoryById, getCategoryBySlug, searchCategories };
255
+ }
256
+
257
+ // src/integrations/base/tags/queries.ts
258
+ function createTagsQueries(fetcher) {
259
+ const { wpFetch, wpFetchGraceful } = fetcher;
260
+ async function getAllTags() {
261
+ return wpFetchGraceful("/wp-json/wp/v2/tags", [], { per_page: 100 }, [
262
+ "wordpress",
263
+ "tags"
264
+ ]);
265
+ }
266
+ async function getTagById(id) {
267
+ return wpFetch(`/wp-json/wp/v2/tags/${id}`);
268
+ }
269
+ async function getTagBySlug(slug) {
270
+ const tags = await wpFetch("/wp-json/wp/v2/tags", { slug });
271
+ return tags[0];
272
+ }
273
+ async function getTagsByPost(postId) {
274
+ return wpFetch("/wp-json/wp/v2/tags", { post: postId });
275
+ }
276
+ async function searchTags(query) {
277
+ return wpFetchGraceful("/wp-json/wp/v2/tags", [], { search: query, per_page: 100 });
278
+ }
279
+ return { getAllTags, getTagById, getTagBySlug, getTagsByPost, searchTags };
280
+ }
281
+
282
+ // src/integrations/base/authors/queries.ts
283
+ function createAuthorsQueries(fetcher) {
284
+ const { wpFetch, wpFetchGraceful } = fetcher;
285
+ async function getAllAuthors() {
286
+ return wpFetchGraceful("/wp-json/wp/v2/users", [], { per_page: 100 }, [
287
+ "wordpress",
288
+ "authors"
289
+ ]);
290
+ }
291
+ async function getAuthorById(id) {
292
+ return wpFetch(`/wp-json/wp/v2/users/${id}`);
293
+ }
294
+ async function getAuthorBySlug(slug) {
295
+ const users = await wpFetch("/wp-json/wp/v2/users", { slug });
296
+ return users[0];
297
+ }
298
+ async function searchAuthors(query) {
299
+ return wpFetchGraceful("/wp-json/wp/v2/users", [], { search: query, per_page: 100 });
300
+ }
301
+ return { getAllAuthors, getAuthorById, getAuthorBySlug, searchAuthors };
302
+ }
303
+
304
+ // src/integrations/base/media/queries.ts
305
+ function createMediaQueries(fetcher) {
306
+ const { wpFetch } = fetcher;
307
+ async function getFeaturedMediaById(id) {
308
+ return wpFetch(`/wp-json/wp/v2/media/${id}`);
309
+ }
310
+ return { getFeaturedMediaById };
311
+ }
312
+
313
+ // src/integrations/base/pages/queries.ts
314
+ function createPagesQueries(fetcher) {
315
+ const { wpFetch, wpFetchGraceful } = fetcher;
316
+ async function getAllPages() {
317
+ return wpFetchGraceful("/wp-json/wp/v2/pages", [], { per_page: 100 }, [
318
+ "wordpress",
319
+ "pages"
320
+ ]);
321
+ }
322
+ async function getPageById(id) {
323
+ return wpFetch(`/wp-json/wp/v2/pages/${id}`);
324
+ }
325
+ async function getPageBySlug(slug) {
326
+ const pages = await wpFetchGraceful("/wp-json/wp/v2/pages", [], { slug });
327
+ return pages[0];
328
+ }
329
+ return { getAllPages, getPageById, getPageBySlug };
330
+ }
331
+
332
+ // src/integrations/base/index.ts
333
+ function createWordPressClient(config) {
334
+ const fetcher = createFetcher(config);
335
+ return {
336
+ ...createPostsQueries(fetcher),
337
+ ...createCategoriesQueries(fetcher),
338
+ ...createTagsQueries(fetcher),
339
+ ...createAuthorsQueries(fetcher),
340
+ ...createMediaQueries(fetcher),
341
+ ...createPagesQueries(fetcher)
342
+ };
343
+ }
344
+
345
+ // src/integrations/woocommerce/omnibus/queries.ts
346
+ var META_KEY_LOWEST_PRICE = "wpo_lowest_price";
347
+ var META_KEY_PRICE_HISTORY = "_alg_wc_price_history";
348
+ function extractMetaValue(meta_data, key) {
349
+ return meta_data.find((m) => m.key === key)?.value;
350
+ }
351
+ function parseHistory(raw) {
352
+ if (!raw || typeof raw !== "object") return [];
353
+ if (Array.isArray(raw)) {
354
+ return raw.filter((e) => e && typeof e.price === "string" && typeof e.date === "string").map((e) => ({ price: e.price, date: e.date }));
355
+ }
356
+ return Object.entries(raw).map(([date, price]) => ({ date, price })).sort((a, b) => a.date.localeCompare(b.date));
357
+ }
358
+ function extractOmnibusData(meta_data) {
359
+ const lowestPrice = extractMetaValue(meta_data, META_KEY_LOWEST_PRICE) ?? null;
360
+ const rawHistory = extractMetaValue(meta_data, META_KEY_PRICE_HISTORY);
361
+ const priceHistory = parseHistory(rawHistory);
362
+ return { lowestPrice, priceHistory };
363
+ }
364
+ function withOmnibus(product) {
365
+ return { ...product, omnibus: extractOmnibusData(product.meta_data) };
366
+ }
367
+ function withOmnibusVariation(variation) {
368
+ return {
369
+ ...variation,
370
+ omnibus: extractOmnibusData(variation.meta_data ?? [])
371
+ };
372
+ }
373
+
374
+ // src/integrations/woocommerce/client/fetcher.ts
375
+ var USER_AGENT2 = "NextWordpress WooCommerce Client";
376
+ var DEFAULT_CACHE_TTL = 3600;
377
+ function toQueryString(params) {
378
+ const pairs = [];
379
+ for (const [key, value] of Object.entries(params)) {
380
+ if (value === void 0 || value === null) continue;
381
+ pairs.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`);
382
+ }
383
+ return pairs.join("&");
384
+ }
385
+ function createWooCommerceFetcher(config) {
386
+ const cacheTTL = config.cacheTTL ?? DEFAULT_CACHE_TTL;
387
+ function withAuth(query) {
388
+ return { ...query, consumer_key: config.consumerKey, consumer_secret: config.consumerSecret };
389
+ }
390
+ function buildUrl2(path, query) {
391
+ return `${config.serverURL}${path}?${toQueryString(withAuth(query))}`;
392
+ }
393
+ async function wcFetch(path, query, tags = ["woocommerce"], options) {
394
+ const url = buildUrl2(path, query);
395
+ const isMutation = options?.method && options.method !== "GET";
396
+ const noStore = options?.cache === "no-store";
397
+ const response = await fetch(url, {
398
+ ...options,
399
+ headers: {
400
+ "User-Agent": USER_AGENT2,
401
+ "Content-Type": "application/json",
402
+ ...options?.headers
403
+ },
404
+ next: isMutation || noStore ? void 0 : { tags, revalidate: cacheTTL }
405
+ });
406
+ if (!response.ok) {
407
+ const body = await response.json().catch(() => ({}));
408
+ const detail = body.message ? ` \u2013 ${body.message}` : "";
409
+ throw new Error(`WooCommerce API ${response.status}: ${response.statusText}${detail} [${url}]`);
410
+ }
411
+ return response.json();
412
+ }
413
+ async function wcFetchGraceful(path, fallback, query, tags = ["woocommerce"]) {
414
+ try {
415
+ return await wcFetch(path, query, tags);
416
+ } catch (err) {
417
+ console.warn(`WooCommerce fetch failed for ${path}`, err);
418
+ return fallback;
419
+ }
420
+ }
421
+ async function wcFetchPaginated(path, query, tags = ["woocommerce"]) {
422
+ const url = buildUrl2(path, query);
423
+ const response = await fetch(url, {
424
+ headers: { "User-Agent": USER_AGENT2 },
425
+ next: { tags, revalidate: cacheTTL }
426
+ });
427
+ if (!response.ok) {
428
+ throw new Error(`WooCommerce API ${response.status}: ${response.statusText} [${url}]`);
429
+ }
430
+ return {
431
+ data: await response.json(),
432
+ headers: {
433
+ total: parseInt(response.headers.get("X-WP-Total") ?? "0", 10),
434
+ totalPages: parseInt(response.headers.get("X-WP-TotalPages") ?? "0", 10)
435
+ }
436
+ };
437
+ }
438
+ async function wcFetchPaginatedGraceful(path, query, tags = ["woocommerce"]) {
439
+ const empty = { data: [], headers: { total: 0, totalPages: 0 } };
440
+ try {
441
+ return await wcFetchPaginated(path, query, tags);
442
+ } catch (err) {
443
+ console.warn(`WooCommerce paginated fetch failed for ${path}`, err);
444
+ return empty;
445
+ }
446
+ }
447
+ async function wcMutate(path, body, method = "POST") {
448
+ return wcFetch(path, void 0, ["woocommerce"], {
449
+ method,
450
+ body: JSON.stringify(body)
451
+ });
452
+ }
453
+ return { wcFetch, wcFetchGraceful, wcFetchPaginated, wcFetchPaginatedGraceful, wcMutate };
454
+ }
455
+
456
+ // src/integrations/woocommerce/products/queries.ts
457
+ function buildProductQuery(base, filterParams) {
458
+ const query = { ...base };
459
+ if (!filterParams) return query;
460
+ if (filterParams.category !== void 0) query.category = filterParams.category;
461
+ if (filterParams.tag !== void 0) query.tag = filterParams.tag;
462
+ if (filterParams.featured !== void 0) query.featured = filterParams.featured;
463
+ if (filterParams.on_sale !== void 0) query.on_sale = filterParams.on_sale;
464
+ if (filterParams.min_price !== void 0) query.min_price = filterParams.min_price;
465
+ if (filterParams.max_price !== void 0) query.max_price = filterParams.max_price;
466
+ if (filterParams.search !== void 0) query.search = filterParams.search;
467
+ if (filterParams.orderby !== void 0) query.orderby = filterParams.orderby;
468
+ if (filterParams.order !== void 0) query.order = filterParams.order;
469
+ if (filterParams.stock_status !== void 0) query.stock_status = filterParams.stock_status;
470
+ if (filterParams.type !== void 0) query.type = filterParams.type;
471
+ return query;
472
+ }
473
+ function createProductsQueries(fetcher) {
474
+ const { wcFetch, wcFetchGraceful, wcFetchPaginated, wcFetchPaginatedGraceful } = fetcher;
475
+ async function getProducts(filterParams) {
476
+ const query = buildProductQuery({ per_page: 100, status: "publish" }, filterParams);
477
+ return wcFetchGraceful("/wp-json/wc/v3/products", [], query, [
478
+ "woocommerce",
479
+ "products"
480
+ ]);
481
+ }
482
+ async function getProductsPaginated(page = 1, perPage = 12, filterParams) {
483
+ const query = buildProductQuery({ per_page: perPage, page, status: "publish" }, filterParams);
484
+ const cacheTags = ["woocommerce", "products", `products-page-${page}`];
485
+ if (filterParams?.category) cacheTags.push(`products-category-${filterParams.category}`);
486
+ if (filterParams?.tag) cacheTags.push(`products-tag-${filterParams.tag}`);
487
+ if (filterParams?.search) cacheTags.push("products-search");
488
+ return wcFetchPaginatedGraceful("/wp-json/wc/v3/products", query, cacheTags);
489
+ }
490
+ async function getProductById(id) {
491
+ return wcFetch(`/wp-json/wc/v3/products/${id}`, void 0, [
492
+ "woocommerce",
493
+ "products",
494
+ `product-${id}`
495
+ ]);
496
+ }
497
+ async function getProductBySlug(slug) {
498
+ const products = await wcFetchGraceful(
499
+ "/wp-json/wc/v3/products",
500
+ [],
501
+ { slug },
502
+ ["woocommerce", "products", `product-slug-${slug}`]
503
+ );
504
+ return products[0];
505
+ }
506
+ async function getFeaturedProducts(limit = 10) {
507
+ return wcFetchGraceful(
508
+ "/wp-json/wc/v3/products",
509
+ [],
510
+ { featured: true, per_page: limit, status: "publish" },
511
+ ["woocommerce", "products", "featured"]
512
+ );
513
+ }
514
+ async function getOnSaleProducts(limit = 10) {
515
+ return wcFetchGraceful(
516
+ "/wp-json/wc/v3/products",
517
+ [],
518
+ { on_sale: true, per_page: limit, status: "publish" },
519
+ ["woocommerce", "products", "on-sale"]
520
+ );
521
+ }
522
+ async function getRelatedProducts(productId) {
523
+ const product = await getProductById(productId);
524
+ if (!product.related_ids.length) return [];
525
+ return wcFetchGraceful(
526
+ "/wp-json/wc/v3/products",
527
+ [],
528
+ { include: product.related_ids.join(","), per_page: 10, status: "publish" },
529
+ ["woocommerce", "products", `related-${productId}`]
530
+ );
531
+ }
532
+ async function getAllProductSlugs() {
533
+ try {
534
+ const allSlugs = [];
535
+ let page = 1;
536
+ let hasMore = true;
537
+ while (hasMore) {
538
+ const response = await wcFetchPaginated("/wp-json/wc/v3/products", {
539
+ per_page: 100,
540
+ page,
541
+ status: "publish",
542
+ _fields: "slug"
543
+ });
544
+ allSlugs.push(...response.data.map((p) => ({ slug: p.slug })));
545
+ hasMore = page < response.headers.totalPages;
546
+ page++;
547
+ }
548
+ return allSlugs;
549
+ } catch {
550
+ console.warn("WooCommerce unavailable, skipping static generation for products");
551
+ return [];
552
+ }
553
+ }
554
+ async function getProductVariations(productId) {
555
+ return wcFetchGraceful(
556
+ `/wp-json/wc/v3/products/${productId}/variations`,
557
+ [],
558
+ { per_page: 100 },
559
+ ["woocommerce", "products", `product-${productId}`, "variations"]
560
+ );
561
+ }
562
+ async function getProductVariationById(productId, variationId) {
563
+ return wcFetch(
564
+ `/wp-json/wc/v3/products/${productId}/variations/${variationId}`,
565
+ void 0,
566
+ ["woocommerce", "products", `product-${productId}`, `variation-${variationId}`]
567
+ );
568
+ }
569
+ return {
570
+ getProducts,
571
+ getProductsPaginated,
572
+ getProductById,
573
+ getProductBySlug,
574
+ getFeaturedProducts,
575
+ getOnSaleProducts,
576
+ getRelatedProducts,
577
+ getAllProductSlugs,
578
+ getProductVariations,
579
+ getProductVariationById
580
+ };
581
+ }
582
+
583
+ // src/integrations/woocommerce/categories/queries.ts
584
+ function createCategoriesQueries2(fetcher) {
585
+ const { wcFetch, wcFetchGraceful } = fetcher;
586
+ async function getAllProductCategories() {
587
+ return wcFetchGraceful(
588
+ "/wp-json/wc/v3/products/categories",
589
+ [],
590
+ { per_page: 100, hide_empty: false },
591
+ ["woocommerce", "product-categories"]
592
+ );
593
+ }
594
+ async function getProductCategoryById(id) {
595
+ return wcFetch(
596
+ `/wp-json/wc/v3/products/categories/${id}`,
597
+ void 0,
598
+ ["woocommerce", "product-categories", `product-category-${id}`]
599
+ );
600
+ }
601
+ async function getProductCategoryBySlug(slug) {
602
+ const categories = await wcFetchGraceful(
603
+ "/wp-json/wc/v3/products/categories",
604
+ [],
605
+ { slug },
606
+ ["woocommerce", "product-categories", `product-category-slug-${slug}`]
607
+ );
608
+ return categories[0];
609
+ }
610
+ async function getProductsByCategory(categoryId, limit = 100) {
611
+ return wcFetchGraceful(
612
+ "/wp-json/wc/v3/products",
613
+ [],
614
+ { category: categoryId, per_page: limit, status: "publish" },
615
+ ["woocommerce", "products", `products-category-${categoryId}`]
616
+ );
617
+ }
618
+ async function getProductsByCategorySlug(categorySlug) {
619
+ const category = await getProductCategoryBySlug(categorySlug);
620
+ if (!category) return [];
621
+ return getProductsByCategory(category.id);
622
+ }
623
+ return {
624
+ getAllProductCategories,
625
+ getProductCategoryById,
626
+ getProductCategoryBySlug,
627
+ getProductsByCategory,
628
+ getProductsByCategorySlug
629
+ };
630
+ }
631
+
632
+ // src/integrations/woocommerce/tags/queries.ts
633
+ function createTagsQueries2(fetcher) {
634
+ const { wcFetch, wcFetchGraceful } = fetcher;
635
+ async function getAllProductTags() {
636
+ return wcFetchGraceful(
637
+ "/wp-json/wc/v3/products/tags",
638
+ [],
639
+ { per_page: 100 },
640
+ ["woocommerce", "product-tags"]
641
+ );
642
+ }
643
+ async function getProductTagById(id) {
644
+ return wcFetch(`/wp-json/wc/v3/products/tags/${id}`, void 0, [
645
+ "woocommerce",
646
+ "product-tags",
647
+ `product-tag-${id}`
648
+ ]);
649
+ }
650
+ async function getProductsByTag(tagId, limit = 100) {
651
+ return wcFetchGraceful(
652
+ "/wp-json/wc/v3/products",
653
+ [],
654
+ { tag: tagId, per_page: limit },
655
+ ["woocommerce", "products", `products-tag-${tagId}`]
656
+ );
657
+ }
658
+ return {
659
+ getAllProductTags,
660
+ getProductTagById,
661
+ getProductsByTag
662
+ };
663
+ }
664
+
665
+ // src/integrations/woocommerce/orders/queries.ts
666
+ function createOrdersQueries(fetcher) {
667
+ const { wcFetch, wcFetchGraceful, wcMutate } = fetcher;
668
+ async function createOrder(input) {
669
+ return wcMutate("/wp-json/wc/v3/orders", input, "POST");
670
+ }
671
+ async function getOrderById(id) {
672
+ return wcFetch(`/wp-json/wc/v3/orders/${id}`, void 0, [
673
+ "woocommerce",
674
+ "orders",
675
+ `order-${id}`
676
+ ]);
677
+ }
678
+ async function updateOrder(id, data) {
679
+ return wcMutate(`/wp-json/wc/v3/orders/${id}`, data, "PUT");
680
+ }
681
+ async function getOrdersByCustomer(customerId) {
682
+ return wcFetchGraceful(
683
+ "/wp-json/wc/v3/orders",
684
+ [],
685
+ { customer: customerId, per_page: 100 },
686
+ ["woocommerce", "orders", `orders-customer-${customerId}`]
687
+ );
688
+ }
689
+ return {
690
+ createOrder,
691
+ getOrderById,
692
+ updateOrder,
693
+ getOrdersByCustomer
694
+ };
695
+ }
696
+
697
+ // src/integrations/woocommerce/coupons/queries.ts
698
+ function createCouponsQueries(fetcher) {
699
+ const { wcFetchGraceful } = fetcher;
700
+ async function getCouponByCode(code) {
701
+ const coupons = await wcFetchGraceful(
702
+ "/wp-json/wc/v3/coupons",
703
+ [],
704
+ { code },
705
+ ["woocommerce", "coupons"]
706
+ );
707
+ return coupons[0];
708
+ }
709
+ async function validateCoupon(code) {
710
+ const coupon = await getCouponByCode(code);
711
+ if (!coupon) return { valid: false, reason: "Coupon not found" };
712
+ if (coupon.date_expires) {
713
+ const expiry = new Date(coupon.date_expires);
714
+ if (expiry < /* @__PURE__ */ new Date()) return { valid: false, reason: "Coupon has expired", coupon };
715
+ }
716
+ if (coupon.usage_limit !== null && coupon.usage_count >= coupon.usage_limit) {
717
+ return { valid: false, reason: "Coupon usage limit reached", coupon };
718
+ }
719
+ return { valid: true, coupon };
720
+ }
721
+ return {
722
+ getCouponByCode,
723
+ validateCoupon
724
+ };
725
+ }
726
+
727
+ // src/integrations/woocommerce/customers/queries.ts
728
+ function createCustomersQueries(fetcher) {
729
+ const { wcFetch, wcFetchGraceful } = fetcher;
730
+ async function getCustomerById(id) {
731
+ return wcFetch(`/wp-json/wc/v3/customers/${id}`, void 0, [
732
+ "woocommerce",
733
+ "customers",
734
+ `customer-${id}`
735
+ ]);
736
+ }
737
+ async function getCustomerByEmail(email) {
738
+ const customers = await wcFetchGraceful(
739
+ "/wp-json/wc/v3/customers",
740
+ [],
741
+ { email },
742
+ ["woocommerce", "customers"]
743
+ );
744
+ return customers[0];
745
+ }
746
+ async function getCustomerByEmailNoCache(email) {
747
+ try {
748
+ const customers = await wcFetch(
749
+ "/wp-json/wc/v3/customers",
750
+ { email },
751
+ ["woocommerce", "customers"],
752
+ { cache: "no-store" }
753
+ );
754
+ return customers[0];
755
+ } catch {
756
+ return void 0;
757
+ }
758
+ }
759
+ async function createCustomer(data) {
760
+ return wcFetch(
761
+ "/wp-json/wc/v3/customers",
762
+ void 0,
763
+ ["woocommerce", "customers"],
764
+ { method: "POST", body: JSON.stringify(data) }
765
+ );
766
+ }
767
+ return {
768
+ getCustomerById,
769
+ getCustomerByEmail,
770
+ getCustomerByEmailNoCache,
771
+ createCustomer
772
+ };
773
+ }
774
+
775
+ // src/integrations/woocommerce/index.ts
776
+ function createWooCommerceClient(config) {
777
+ const fetcher = createWooCommerceFetcher(config);
778
+ return {
779
+ ...createProductsQueries(fetcher),
780
+ ...createCategoriesQueries2(fetcher),
781
+ ...createTagsQueries2(fetcher),
782
+ ...createOrdersQueries(fetcher),
783
+ ...createCouponsQueries(fetcher),
784
+ ...createCustomersQueries(fetcher)
785
+ };
786
+ }
787
+
788
+ exports.buildUrl = buildUrl;
789
+ exports.createFetcher = createFetcher;
790
+ exports.createWooCommerceClient = createWooCommerceClient;
791
+ exports.createWooCommerceFetcher = createWooCommerceFetcher;
792
+ exports.createWordPressClient = createWordPressClient;
793
+ exports.extractOmnibusData = extractOmnibusData;
794
+ exports.resolveBaseUrl = resolveBaseUrl;
795
+ exports.withOmnibus = withOmnibus;
796
+ exports.withOmnibusVariation = withOmnibusVariation;
797
+ //# sourceMappingURL=index.cjs.map
798
+ //# sourceMappingURL=index.cjs.map