@atomic-solutions/woocommerce-api-client 0.1.0

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.
Files changed (51) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +124 -0
  3. package/dist/client/index.d.mts +9 -0
  4. package/dist/client/index.d.ts +9 -0
  5. package/dist/client/index.js +1461 -0
  6. package/dist/client/index.js.map +1 -0
  7. package/dist/client/index.mjs +1455 -0
  8. package/dist/client/index.mjs.map +1 -0
  9. package/dist/http/index.d.mts +11 -0
  10. package/dist/http/index.d.ts +11 -0
  11. package/dist/http/index.js +638 -0
  12. package/dist/http/index.js.map +1 -0
  13. package/dist/http/index.mjs +631 -0
  14. package/dist/http/index.mjs.map +1 -0
  15. package/dist/index.d.mts +29 -0
  16. package/dist/index.d.ts +29 -0
  17. package/dist/index.js +2152 -0
  18. package/dist/index.js.map +1 -0
  19. package/dist/index.mjs +2105 -0
  20. package/dist/index.mjs.map +1 -0
  21. package/dist/pagination.schema-CdjWGZJr.d.mts +190 -0
  22. package/dist/pagination.schema-CdjWGZJr.d.ts +190 -0
  23. package/dist/products-Cxl54crz.d.mts +3412 -0
  24. package/dist/products-Cxl54crz.d.ts +3412 -0
  25. package/dist/schemas/admin-api/index.d.mts +5340 -0
  26. package/dist/schemas/admin-api/index.d.ts +5340 -0
  27. package/dist/schemas/admin-api/index.js +584 -0
  28. package/dist/schemas/admin-api/index.js.map +1 -0
  29. package/dist/schemas/admin-api/index.mjs +545 -0
  30. package/dist/schemas/admin-api/index.mjs.map +1 -0
  31. package/dist/schemas/index.d.mts +4 -0
  32. package/dist/schemas/index.d.ts +4 -0
  33. package/dist/schemas/index.js +887 -0
  34. package/dist/schemas/index.js.map +1 -0
  35. package/dist/schemas/index.mjs +844 -0
  36. package/dist/schemas/index.mjs.map +1 -0
  37. package/dist/schemas/store-api/index.d.mts +1076 -0
  38. package/dist/schemas/store-api/index.d.ts +1076 -0
  39. package/dist/schemas/store-api/index.js +887 -0
  40. package/dist/schemas/store-api/index.js.map +1 -0
  41. package/dist/schemas/store-api/index.mjs +844 -0
  42. package/dist/schemas/store-api/index.mjs.map +1 -0
  43. package/dist/types-B-zy1xrP.d.mts +183 -0
  44. package/dist/types-qKWtrw7A.d.ts +183 -0
  45. package/dist/utils/index.d.mts +17 -0
  46. package/dist/utils/index.d.ts +17 -0
  47. package/dist/utils/index.js +316 -0
  48. package/dist/utils/index.js.map +1 -0
  49. package/dist/utils/index.mjs +308 -0
  50. package/dist/utils/index.mjs.map +1 -0
  51. package/package.json +117 -0
@@ -0,0 +1,1455 @@
1
+ import axios, { isAxiosError } from 'axios';
2
+ import { z } from 'zod';
3
+
4
+ // src/client/createClient.ts
5
+
6
+ // src/errors/classes/BaseError.ts
7
+ var BaseError = class extends Error {
8
+ constructor(options) {
9
+ super(options.message);
10
+ this.options = options;
11
+ Object.setPrototypeOf(this, new.target.prototype);
12
+ this.name = this.constructor.name;
13
+ if (Error.captureStackTrace) {
14
+ Error.captureStackTrace(this, this.constructor);
15
+ }
16
+ if (this.options.cause instanceof Error && this.options.cause.stack) {
17
+ this.stack = `${this.stack}
18
+ Caused by: ${this.options.cause.stack}`;
19
+ }
20
+ }
21
+ /**
22
+ * Get reportable data for error tracking/logging
23
+ * Subclasses should override this to add their own reportable fields
24
+ */
25
+ getReportableData() {
26
+ const { code, operation, userMessage, retryable } = this.options;
27
+ return {
28
+ code,
29
+ operation,
30
+ userMessage,
31
+ retryable
32
+ };
33
+ }
34
+ /**
35
+ * Custom JSON serialization
36
+ * Makes the error properly serializable for logging/reporting
37
+ */
38
+ toJSON() {
39
+ return {
40
+ ...this.getReportableData(),
41
+ name: this.name,
42
+ message: this.message,
43
+ stack: this.stack
44
+ };
45
+ }
46
+ };
47
+
48
+ // src/errors/mapping/mapWooCommerceCode.ts
49
+ var mapWooCommerceCode = (apiCode) => {
50
+ const directMatches = {
51
+ woocommerce_rest_cart_empty: "cart_empty",
52
+ woocommerce_rest_cart_coupon_error: "invalid_coupon",
53
+ woocommerce_rest_missing_nonce: "missing_nonce",
54
+ woocommerce_rest_invalid_nonce: "invalid_nonce",
55
+ rest_invalid_param: "invalid_params",
56
+ woocommerce_rest_product_invalid_id: "product_not_found",
57
+ woocommerce_rest_product_out_of_stock: "out_of_stock",
58
+ woocommerce_rest_product_not_enough_stock: "insufficient_stock",
59
+ woocommerce_rest_checkout_invalid_payment_method: "invalid_payment_method"
60
+ };
61
+ if (directMatches[apiCode]) {
62
+ return directMatches[apiCode];
63
+ }
64
+ if (apiCode.includes("nonce")) return "invalid_nonce";
65
+ if (apiCode.includes("coupon")) return "invalid_coupon";
66
+ if (apiCode.includes("stock")) return "insufficient_stock";
67
+ if (apiCode.includes("checkout")) return "checkout_error";
68
+ if (apiCode.includes("payment")) return "invalid_payment_method";
69
+ return "unknown_error";
70
+ };
71
+
72
+ // src/errors/mapping/constants.ts
73
+ var WOOCOMMERCE_ERROR_METADATA = {
74
+ cart_empty: {
75
+ operation: "get_cart",
76
+ retryable: false,
77
+ userMessage: "Your cart is empty"
78
+ },
79
+ invalid_coupon: {
80
+ operation: "apply_coupon",
81
+ retryable: false,
82
+ userMessage: "Invalid coupon code"
83
+ },
84
+ missing_nonce: {
85
+ operation: "cart_request",
86
+ retryable: true
87
+ // Auto-retry with new nonce
88
+ },
89
+ invalid_nonce: {
90
+ operation: "cart_request",
91
+ retryable: true
92
+ // Auto-retry with new nonce
93
+ },
94
+ invalid_params: {
95
+ operation: "api_request",
96
+ retryable: false,
97
+ userMessage: "Invalid request parameters"
98
+ },
99
+ product_not_found: {
100
+ operation: "get_product",
101
+ retryable: false,
102
+ userMessage: "Product not found"
103
+ },
104
+ out_of_stock: {
105
+ operation: "add_to_cart",
106
+ retryable: false,
107
+ userMessage: "This product is out of stock"
108
+ },
109
+ insufficient_stock: {
110
+ operation: "add_to_cart",
111
+ retryable: false,
112
+ userMessage: "Not enough stock available"
113
+ },
114
+ invalid_payment_method: {
115
+ operation: "process_checkout",
116
+ retryable: false,
117
+ userMessage: "Invalid payment method"
118
+ },
119
+ checkout_error: {
120
+ operation: "process_checkout",
121
+ retryable: false
122
+ },
123
+ network_error: {
124
+ operation: "network_request",
125
+ retryable: true
126
+ },
127
+ unknown_error: {
128
+ operation: "unknown",
129
+ retryable: false
130
+ }
131
+ };
132
+
133
+ // src/errors/classes/WooCommerceApiError.ts
134
+ var WooCommerceApiError = class extends BaseError {
135
+ constructor(options) {
136
+ const metadata = WOOCOMMERCE_ERROR_METADATA[options.code] ?? {
137
+ operation: "woocommerce_api_request",
138
+ retryable: false
139
+ };
140
+ const enrichedOptions = {
141
+ ...options,
142
+ operation: options.operation || metadata.operation,
143
+ userMessage: options.userMessage || metadata.userMessage,
144
+ retryable: options.retryable ?? metadata.retryable
145
+ };
146
+ super(enrichedOptions);
147
+ }
148
+ // Convenient getters for frequently accessed fields (no duplication)
149
+ get wooCode() {
150
+ return this.options.code;
151
+ }
152
+ get originalCode() {
153
+ return this.options.originalCode;
154
+ }
155
+ get statusCode() {
156
+ return this.options.statusCode;
157
+ }
158
+ get url() {
159
+ return this.options.url;
160
+ }
161
+ get method() {
162
+ return this.options.method;
163
+ }
164
+ get requestBody() {
165
+ return this.options.requestBody;
166
+ }
167
+ get responseBody() {
168
+ return this.options.responseBody;
169
+ }
170
+ get data() {
171
+ return this.options.data;
172
+ }
173
+ /**
174
+ * Get reportable data for error tracking/logging
175
+ * Includes HTTP context and WooCommerce-specific fields
176
+ */
177
+ getReportableData() {
178
+ return {
179
+ code: this.options.code,
180
+ operation: this.options.operation,
181
+ userMessage: this.options.userMessage,
182
+ retryable: this.options.retryable,
183
+ statusCode: this.options.statusCode,
184
+ url: this.options.url,
185
+ method: this.options.method,
186
+ wooCode: this.options.code,
187
+ originalCode: this.options.originalCode
188
+ };
189
+ }
190
+ };
191
+
192
+ // src/errors/classes/WooCommerceDataValidationError.ts
193
+ var WooCommerceDataValidationError = class _WooCommerceDataValidationError extends BaseError {
194
+ constructor(options) {
195
+ const fieldErrors = options.zodError ? _WooCommerceDataValidationError.extractFieldErrorsFromZod(options.zodError) : void 0;
196
+ super({
197
+ code: "validation_error",
198
+ message: options.message,
199
+ operation: options.operation,
200
+ userMessage: options.userMessage || "Received unexpected data from WooCommerce",
201
+ retryable: false,
202
+ cause: options.cause,
203
+ url: options.url,
204
+ zodError: options.zodError,
205
+ value: options.value,
206
+ fieldErrors
207
+ });
208
+ }
209
+ // Convenient getters for validation-specific fields (no duplication)
210
+ get url() {
211
+ return this.options.url;
212
+ }
213
+ get zodError() {
214
+ return this.options.zodError;
215
+ }
216
+ get invalidValue() {
217
+ return this.options.value;
218
+ }
219
+ get fieldErrors() {
220
+ return this.options.fieldErrors;
221
+ }
222
+ /**
223
+ * Get reportable data for error tracking/logging
224
+ * Includes validation-specific fields
225
+ */
226
+ getReportableData() {
227
+ return {
228
+ code: this.options.code,
229
+ operation: this.options.operation,
230
+ userMessage: this.options.userMessage,
231
+ retryable: false,
232
+ fieldErrors: this.options.fieldErrors,
233
+ url: this.options.url
234
+ };
235
+ }
236
+ /**
237
+ * Extract field errors from Zod error
238
+ */
239
+ static extractFieldErrorsFromZod(zodError) {
240
+ if (!zodError?.errors) {
241
+ return void 0;
242
+ }
243
+ const fieldErrors = {};
244
+ for (const issue of zodError.errors) {
245
+ const path = issue.path.join(".");
246
+ if (!fieldErrors[path]) {
247
+ fieldErrors[path] = [];
248
+ }
249
+ fieldErrors[path].push(issue.message);
250
+ }
251
+ return fieldErrors;
252
+ }
253
+ /**
254
+ * Get all error messages as a flat array
255
+ */
256
+ getMessages() {
257
+ if (!this.fieldErrors) {
258
+ return [this.message];
259
+ }
260
+ const messages = [];
261
+ for (const fieldMessages of Object.values(this.fieldErrors)) {
262
+ messages.push(...fieldMessages);
263
+ }
264
+ return messages;
265
+ }
266
+ /**
267
+ * Check if a specific field has errors
268
+ */
269
+ hasFieldError(field) {
270
+ return this.fieldErrors ? field in this.fieldErrors : false;
271
+ }
272
+ /**
273
+ * Get error messages for a specific field
274
+ */
275
+ getFieldError(field) {
276
+ return this.fieldErrors?.[field];
277
+ }
278
+ };
279
+
280
+ // src/http/handleApiResponse.ts
281
+ var handleApiResponse = (response, schema, options) => {
282
+ const validationMode = options?.validationMode ?? "strict";
283
+ if (validationMode === "warn") {
284
+ const result = schema.safeParse(response.data);
285
+ if (!result.success) {
286
+ const validationError = new WooCommerceDataValidationError({
287
+ message: `WooCommerce API response validation failed for ${response.config.url || "unknown endpoint"}`,
288
+ url: response.config.url,
289
+ zodError: result.error,
290
+ value: response.data,
291
+ userMessage: "Received unexpected data from WooCommerce"
292
+ });
293
+ options?.onValidationError?.(validationError);
294
+ options?.errorReporter?.(validationError);
295
+ console.warn("[woocommerce-utils] Validation warning:", validationError.message);
296
+ return response.data;
297
+ }
298
+ return result.data;
299
+ }
300
+ try {
301
+ return schema.parse(response.data);
302
+ } catch (error2) {
303
+ if (error2 && typeof error2 === "object" && "issues" in error2) {
304
+ const validationError = new WooCommerceDataValidationError({
305
+ message: `WooCommerce API response validation failed for ${response.config.url || "unknown endpoint"}`,
306
+ url: response.config.url,
307
+ zodError: error2,
308
+ value: response.data,
309
+ userMessage: "Received unexpected data from WooCommerce"
310
+ });
311
+ options?.onValidationError?.(validationError);
312
+ options?.errorReporter?.(validationError);
313
+ throw validationError;
314
+ }
315
+ throw error2;
316
+ }
317
+ };
318
+
319
+ // src/utils/calculatePagination.ts
320
+ var calculatePagination = (input) => {
321
+ const { page, perPage, total, totalPages } = input;
322
+ const hasNextPage = page < totalPages;
323
+ const hasPrevPage = page > 1;
324
+ return {
325
+ page,
326
+ perPage,
327
+ total,
328
+ totalPages,
329
+ hasNextPage,
330
+ hasPrevPage,
331
+ nextPage: hasNextPage ? page + 1 : null,
332
+ prevPage: hasPrevPage ? page - 1 : null
333
+ };
334
+ };
335
+
336
+ // src/utils/getPaginationMeta.ts
337
+ var getPaginationMeta = (headers) => {
338
+ if (!headers) {
339
+ return {
340
+ total: 0,
341
+ totalPages: 0
342
+ };
343
+ }
344
+ const wpTotal = headers["x-wp-total"];
345
+ const wpTotalPages = headers["x-wp-totalpages"];
346
+ if (!wpTotal || !wpTotalPages) {
347
+ return {
348
+ total: 0,
349
+ totalPages: 0
350
+ };
351
+ }
352
+ const total = parseInt(wpTotal, 10);
353
+ const totalPages = parseInt(wpTotalPages, 10);
354
+ return {
355
+ total,
356
+ totalPages
357
+ };
358
+ };
359
+
360
+ // src/http/handlePaginatedApiResponse.ts
361
+ var handlePaginatedApiResponse = (response, schema, params = {}, options) => {
362
+ const validationMode = options?.validationMode ?? "strict";
363
+ let data;
364
+ if (validationMode === "warn") {
365
+ const result = schema.safeParse(response.data);
366
+ if (!result.success) {
367
+ const validationError = new WooCommerceDataValidationError({
368
+ message: `WooCommerce API response validation failed for ${response.config.url || "unknown endpoint"}`,
369
+ url: response.config.url,
370
+ zodError: result.error,
371
+ value: response.data,
372
+ userMessage: "Received unexpected data from WooCommerce"
373
+ });
374
+ options?.onValidationError?.(validationError);
375
+ options?.errorReporter?.(validationError);
376
+ console.warn("[woocommerce-utils] Validation warning:", validationError.message);
377
+ data = Array.isArray(response.data) ? response.data : [];
378
+ } else {
379
+ data = result.data;
380
+ }
381
+ } else {
382
+ try {
383
+ data = schema.parse(response.data);
384
+ } catch (error2) {
385
+ if (error2 && typeof error2 === "object" && "issues" in error2) {
386
+ const validationError = new WooCommerceDataValidationError({
387
+ message: `WooCommerce API response validation failed for ${response.config.url || "unknown endpoint"}`,
388
+ url: response.config.url,
389
+ zodError: error2,
390
+ value: response.data,
391
+ userMessage: "Received unexpected data from WooCommerce"
392
+ });
393
+ options?.onValidationError?.(validationError);
394
+ options?.errorReporter?.(validationError);
395
+ throw validationError;
396
+ }
397
+ throw error2;
398
+ }
399
+ }
400
+ const { total, totalPages } = getPaginationMeta(response.headers);
401
+ const page = params.page || 1;
402
+ const perPage = params.per_page || 10;
403
+ const pagination = calculatePagination({
404
+ page,
405
+ perPage,
406
+ total,
407
+ totalPages
408
+ });
409
+ return {
410
+ data,
411
+ pagination
412
+ };
413
+ };
414
+
415
+ // src/logging/logger.ts
416
+ var DEBUG_PREFIX = "[woocommerce-utils]";
417
+ var isDebugEnabled = () => {
418
+ if (typeof process !== "undefined" && process.env) {
419
+ return process.env.DEBUG === "true" || process.env.DEBUG === "1";
420
+ }
421
+ return false;
422
+ };
423
+ var debug = (...args) => {
424
+ if (isDebugEnabled()) {
425
+ console.log(DEBUG_PREFIX, ...args);
426
+ }
427
+ };
428
+ var error = (...args) => {
429
+ console.error(DEBUG_PREFIX, ...args);
430
+ };
431
+
432
+ // src/http/interceptors/request.ts
433
+ var requiresAuth = (url) => {
434
+ if (!url) return false;
435
+ const authEndpoints = ["/jwt-auth/", "/customers/me", "/wp/v2/users/me"];
436
+ return authEndpoints.some((endpoint) => url.includes(endpoint));
437
+ };
438
+ var requiresCartAuth = (url) => {
439
+ if (!url) return false;
440
+ return /\/wc\/store\/(v\d+\/)?cart/.test(url) || /\/wc\/store\/(v\d+\/)?checkout/.test(url);
441
+ };
442
+ var setupRequestInterceptor = (axiosInstance, config, client) => {
443
+ axiosInstance.interceptors.request.use(
444
+ async (requestConfig) => {
445
+ const url = requestConfig.url;
446
+ if (requiresAuth(url) && config.jwtToken) {
447
+ const token = await config.jwtToken.get();
448
+ if (token) {
449
+ requestConfig.headers.Authorization = `Bearer ${token}`;
450
+ if (config.debug) {
451
+ debug("Injected JWT token for", url);
452
+ }
453
+ }
454
+ }
455
+ if (requiresCartAuth(url)) {
456
+ const headers = await config.cartHeaders.get();
457
+ if (headers.nonce) {
458
+ requestConfig.headers.Nonce = headers.nonce;
459
+ }
460
+ if (headers.cartToken) {
461
+ requestConfig.headers["Cart-Token"] = headers.cartToken;
462
+ }
463
+ requestConfig.headers["Cache-Control"] = "no-cache, no-store, must-revalidate";
464
+ requestConfig.headers.Pragma = "no-cache";
465
+ requestConfig.headers.Expires = "0";
466
+ if (config.debug) {
467
+ debug("Injected cart headers for", url, {
468
+ hasNonce: !!headers.nonce,
469
+ hasCartToken: !!headers.cartToken
470
+ });
471
+ }
472
+ }
473
+ if (config.debug) {
474
+ debug(
475
+ `${requestConfig.method?.toUpperCase()} ${requestConfig.url}`,
476
+ requestConfig.params || requestConfig.data
477
+ );
478
+ }
479
+ if (config.onRequest) {
480
+ return config.onRequest(requestConfig, client);
481
+ }
482
+ return requestConfig;
483
+ },
484
+ (error2) => {
485
+ return Promise.reject(error2);
486
+ }
487
+ );
488
+ };
489
+
490
+ // src/http/interceptors/response.ts
491
+ var isCartResponse = (url) => {
492
+ if (!url) return false;
493
+ return /\/wc\/store\/(v\d+\/)?cart/.test(url) || /\/wc\/store\/(v\d+\/)?checkout/.test(url);
494
+ };
495
+ var setupResponseInterceptor = (axiosInstance, config, client) => {
496
+ axiosInstance.interceptors.response.use(
497
+ async (response) => {
498
+ if (isCartResponse(response.config.url)) {
499
+ const nonce = response.headers["nonce"] || response.headers["Nonce"];
500
+ const cartToken = response.headers["cart-token"] || response.headers["Cart-Token"];
501
+ if (nonce || cartToken) {
502
+ await config.cartHeaders.save({
503
+ nonce: nonce || void 0,
504
+ cartToken: cartToken || void 0
505
+ });
506
+ if (config.debug) {
507
+ debug(
508
+ "Saved updated cart headers:",
509
+ response.config.url,
510
+ "nonce:",
511
+ !!nonce,
512
+ "cartToken:",
513
+ !!cartToken
514
+ );
515
+ }
516
+ }
517
+ }
518
+ if (config.debug) {
519
+ debug("Response:", response.config.url, response.status);
520
+ }
521
+ if (config.onResponse) {
522
+ return config.onResponse(response, client);
523
+ }
524
+ return response;
525
+ },
526
+ (error2) => {
527
+ return Promise.reject(error2);
528
+ }
529
+ );
530
+ };
531
+
532
+ // src/utils/initializeCartSession.ts
533
+ var initializeCartSession = async (client, cartHeaders) => {
534
+ try {
535
+ debug("session", "Initializing cart session");
536
+ const response = await client.get("/wc/store/v1/cart");
537
+ const nonce = response.headers["nonce"] || response.headers["Nonce"];
538
+ const cartToken = response.headers["cart-token"] || response.headers["Cart-Token"];
539
+ if (nonce || cartToken) {
540
+ await cartHeaders.save({
541
+ nonce: nonce || void 0,
542
+ cartToken: cartToken || void 0
543
+ });
544
+ debug("session", "Cart session initialized", {
545
+ hasNonce: !!nonce,
546
+ hasCartToken: !!cartToken
547
+ });
548
+ }
549
+ } catch (error2) {
550
+ debug("session", "Failed to initialize cart session", error2);
551
+ throw error2;
552
+ }
553
+ };
554
+
555
+ // src/http/interceptors/error.ts
556
+ var setupErrorInterceptor = (axiosInstance, config, client) => {
557
+ axiosInstance.interceptors.response.use(
558
+ (response) => response,
559
+ async (axiosError) => {
560
+ const status = axiosError.response?.status || 500;
561
+ const errorData = axiosError.response?.data;
562
+ const apiCode = errorData?.code || "unknown_error";
563
+ const wpMessage = errorData?.message || axiosError.message || "Unknown error";
564
+ const errorCode = mapWooCommerceCode(apiCode);
565
+ const errorMetadata = WOOCOMMERCE_ERROR_METADATA[errorCode];
566
+ const wcError = new WooCommerceApiError({
567
+ message: wpMessage,
568
+ statusCode: status,
569
+ code: errorCode,
570
+ // Transformed app-level code
571
+ originalCode: apiCode,
572
+ // Original WooCommerce API code
573
+ retryable: errorMetadata.retryable,
574
+ userMessage: errorMetadata.userMessage,
575
+ operation: errorMetadata.operation,
576
+ url: axiosError.config?.url || "",
577
+ method: axiosError.config?.method?.toUpperCase() || "GET",
578
+ requestBody: axiosError.config?.data,
579
+ responseBody: errorData,
580
+ data: errorData?.data,
581
+ cause: axiosError
582
+ });
583
+ if (config.debug) {
584
+ debug("WooCommerce error:", apiCode, status, wpMessage);
585
+ error(wcError);
586
+ }
587
+ if ((status === 401 || status === 403) && config.onAuthError && isAxiosError(axiosError) && !axiosError.config?._retry) {
588
+ if (config.debug) {
589
+ debug("Auth error detected, calling onAuthError handler");
590
+ }
591
+ try {
592
+ const shouldRetry = await config.onAuthError(wcError, client);
593
+ if (shouldRetry) {
594
+ if (config.debug) {
595
+ debug("Retrying request after auth error");
596
+ }
597
+ axiosError.config._retry = true;
598
+ return axiosInstance.request(axiosError.config);
599
+ }
600
+ } catch (authErrorHandlerError) {
601
+ if (config.debug) {
602
+ debug("onAuthError handler threw error", authErrorHandlerError);
603
+ }
604
+ }
605
+ }
606
+ if (config.onError) {
607
+ config.onError(wcError, client);
608
+ }
609
+ const isNonceError = errorCode === "missing_nonce" || errorCode === "invalid_nonce";
610
+ if (isNonceError && isAxiosError(axiosError) && !axiosError.config?._retry) {
611
+ if (config.debug) {
612
+ debug("Nonce error detected, retrying with new session");
613
+ }
614
+ try {
615
+ await initializeCartSession(axiosInstance, config.cartHeaders);
616
+ axiosError.config._retry = true;
617
+ return axiosInstance.request(axiosError.config);
618
+ } catch (_retryError) {
619
+ config.errorReporter?.report(wcError);
620
+ return Promise.reject(wcError);
621
+ }
622
+ }
623
+ config.errorReporter?.report(wcError);
624
+ return Promise.reject(wcError);
625
+ }
626
+ );
627
+ };
628
+ var addressSchema = z.object({
629
+ /** First name */
630
+ first_name: z.string().min(1, "First name is required"),
631
+ /** Last name */
632
+ last_name: z.string().min(1, "Last name is required"),
633
+ /** Company name (optional) */
634
+ company: z.string(),
635
+ /** Address line 1 */
636
+ address_1: z.string().min(1, "Address is required"),
637
+ /** Address line 2 (optional) */
638
+ address_2: z.string(),
639
+ /** City */
640
+ city: z.string().min(1, "City is required"),
641
+ /** State/Province/Region */
642
+ state: z.string(),
643
+ /** Postal code */
644
+ postcode: z.string().min(1, "Postal code is required"),
645
+ /** Country code (ISO 3166-1 alpha-2) */
646
+ country: z.string(),
647
+ /** Phone number */
648
+ phone: z.string().min(1, "Phone number is required")
649
+ });
650
+ var addressSchemaNonMandatory = z.object({
651
+ /** First name */
652
+ first_name: z.string(),
653
+ /** Last name */
654
+ last_name: z.string(),
655
+ /** Company name (optional) */
656
+ company: z.string(),
657
+ /** Address line 1 */
658
+ address_1: z.string(),
659
+ /** Address line 2 (optional) */
660
+ address_2: z.string(),
661
+ /** City */
662
+ city: z.string(),
663
+ /** State/Province/Region */
664
+ state: z.string(),
665
+ /** Postal code */
666
+ postcode: z.string(),
667
+ /** Country code (ISO 3166-1 alpha-2) */
668
+ country: z.string(),
669
+ /** Phone number */
670
+ phone: z.string()
671
+ });
672
+ var billingAddressSchemaMandatory = addressSchema.extend({
673
+ /** Email address (required for billing) */
674
+ email: z.string().email("Valid email is required")
675
+ });
676
+ var billingAddressSchemaNonMandatory = addressSchemaNonMandatory.extend({
677
+ /** Email address (can be null for guest carts) */
678
+ email: z.string().nullable()
679
+ });
680
+ var moneySchema = z.object({
681
+ /** Currency code (e.g., 'USD', 'EUR', 'BAM') */
682
+ currency_code: z.string(),
683
+ /** Currency symbol (e.g., '$', '€', 'KM') */
684
+ currency_symbol: z.string(),
685
+ /** Number of decimal places (e.g., 2 for dollars/euros) */
686
+ currency_minor_unit: z.number(),
687
+ /** Decimal separator character (e.g., '.', ',') */
688
+ currency_decimal_separator: z.string(),
689
+ /** Thousands separator character (e.g., ',', '.') */
690
+ currency_thousand_separator: z.string(),
691
+ /** Currency symbol prefix (empty if symbol is suffix) */
692
+ currency_prefix: z.string(),
693
+ /** Currency symbol suffix (empty if symbol is prefix) */
694
+ currency_suffix: z.string()
695
+ });
696
+ var productImageSchema = z.object({
697
+ /** Alternative text for the image */
698
+ alt: z.string(),
699
+ /** Image unique identifier */
700
+ id: z.number(),
701
+ /** Image filename */
702
+ name: z.string(),
703
+ /** Available image sizes in JSON format */
704
+ sizes: z.string(),
705
+ /** Full-size image URL */
706
+ src: z.string().url(),
707
+ /** Srcset attribute for responsive images */
708
+ srcset: z.string(),
709
+ /** Thumbnail image URL */
710
+ thumbnail: z.string().url()
711
+ });
712
+ var cartItemImageSchema = z.object({
713
+ /** Image ID */
714
+ id: z.number(),
715
+ /** Full image URL */
716
+ src: z.string(),
717
+ /** Thumbnail URL */
718
+ thumbnail: z.string(),
719
+ /** Responsive image srcset */
720
+ srcset: z.string(),
721
+ /** Responsive image sizes */
722
+ sizes: z.string(),
723
+ /** Image name */
724
+ name: z.string(),
725
+ /** Image alt text */
726
+ alt: z.string()
727
+ });
728
+ var pricesSchema = z.object({
729
+ /** Current active price (sale price if on sale, otherwise regular) */
730
+ price: z.string(),
731
+ /** Regular price (before any discounts) */
732
+ regular_price: z.string(),
733
+ /** Sale price (empty string if not on sale) */
734
+ sale_price: z.string(),
735
+ /** Price range for variable products (null for simple products) */
736
+ price_range: z.object({
737
+ min_amount: z.string(),
738
+ max_amount: z.string()
739
+ }).nullable(),
740
+ /** Raw price data without formatting */
741
+ raw_prices: z.object({
742
+ /** Decimal precision */
743
+ precision: z.number(),
744
+ /** Raw price value */
745
+ price: z.string(),
746
+ /** Raw regular price value */
747
+ regular_price: z.string(),
748
+ /** Raw sale price value */
749
+ sale_price: z.string()
750
+ }).optional()
751
+ }).merge(moneySchema);
752
+
753
+ // src/schemas/store-api/cart-item.schema.ts
754
+ var itemTotalsSchema = z.object({
755
+ /** Subtotal before taxes */
756
+ line_subtotal: z.string(),
757
+ /** Tax amount on subtotal */
758
+ line_subtotal_tax: z.string(),
759
+ /** Total after discounts, before taxes */
760
+ line_total: z.string(),
761
+ /** Tax amount on total */
762
+ line_total_tax: z.string()
763
+ }).merge(moneySchema);
764
+ var cartItemSchema = z.object({
765
+ /** Unique cart item key (use this for update/remove operations) */
766
+ key: z.string(),
767
+ /** Product ID */
768
+ id: z.number(),
769
+ /** Quantity of this item in cart */
770
+ quantity: z.number(),
771
+ /** Quantity limits for this product */
772
+ quantity_limits: z.object({
773
+ /** Minimum quantity allowed */
774
+ minimum: z.number(),
775
+ /** Maximum quantity allowed */
776
+ maximum: z.number(),
777
+ /** Quantity must be multiple of this value */
778
+ multiple_of: z.number(),
779
+ /** Whether quantity can be changed */
780
+ editable: z.boolean()
781
+ }),
782
+ /** Product name */
783
+ name: z.string(),
784
+ /** Short product description */
785
+ short_description: z.string(),
786
+ /** Full product description */
787
+ description: z.string(),
788
+ /** Product SKU */
789
+ sku: z.string(),
790
+ /** Remaining stock count if low (null if not low or unlimited) */
791
+ low_stock_remaining: z.number().nullable(),
792
+ /** Whether backorders are allowed */
793
+ backorders_allowed: z.boolean(),
794
+ /** Whether to show backorder badge */
795
+ show_backorder_badge: z.boolean(),
796
+ /** Whether product can only be purchased individually */
797
+ sold_individually: z.boolean(),
798
+ /** Product permalink URL */
799
+ permalink: z.string(),
800
+ /** Product images */
801
+ images: z.array(cartItemImageSchema),
802
+ /** Variation attributes (for variable products) */
803
+ variation: z.array(
804
+ z.object({
805
+ /** Attribute name (e.g., 'Color', 'Size') */
806
+ attribute: z.string(),
807
+ /** Selected value (e.g., 'Red', 'Large') */
808
+ value: z.string()
809
+ })
810
+ ),
811
+ /** Additional item data/metadata */
812
+ item_data: z.array(
813
+ z.object({
814
+ key: z.string(),
815
+ value: z.string()
816
+ })
817
+ ),
818
+ /** Product pricing information */
819
+ prices: pricesSchema,
820
+ /** Line item totals */
821
+ totals: itemTotalsSchema,
822
+ /** Catalog visibility setting */
823
+ catalog_visibility: z.string(),
824
+ /** Product type */
825
+ type: z.enum(["simple", "variable", "grouped", "external"]),
826
+ /** Extension data from plugins */
827
+ extensions: z.record(z.string(), z.unknown())
828
+ });
829
+ var cartTotalsSchema = z.object({
830
+ /** Total of all cart items (before discounts and taxes) */
831
+ total_items: z.string(),
832
+ /** Tax on cart items */
833
+ total_items_tax: z.string(),
834
+ /** Total fees */
835
+ total_fees: z.string(),
836
+ /** Tax on fees */
837
+ total_fees_tax: z.string(),
838
+ /** Total discount amount from coupons */
839
+ total_discount: z.string(),
840
+ /** Tax on discounts */
841
+ total_discount_tax: z.string(),
842
+ /** Shipping cost (null if not calculated yet) */
843
+ total_shipping: z.string().nullable(),
844
+ /** Shipping tax (null if not calculated yet) */
845
+ total_shipping_tax: z.string().nullable(),
846
+ /** Final total price (includes all taxes and fees) */
847
+ total_price: z.string(),
848
+ /** Total tax amount */
849
+ total_tax: z.string(),
850
+ /** Tax breakdown by rate */
851
+ tax_lines: z.array(
852
+ z.object({
853
+ /** Tax rate name */
854
+ name: z.string(),
855
+ /** Tax amount */
856
+ price: z.string(),
857
+ /** Tax rate percentage */
858
+ rate: z.string()
859
+ })
860
+ )
861
+ }).merge(moneySchema);
862
+ var cartCouponSchema = z.object({
863
+ /** Coupon code */
864
+ code: z.string(),
865
+ /** Discount totals for this coupon */
866
+ totals: z.object({
867
+ /** Total discount amount (before tax) */
868
+ total_discount: z.string(),
869
+ /** Tax amount on discount */
870
+ total_discount_tax: z.string()
871
+ }).merge(moneySchema)
872
+ });
873
+ var shippingRateSchema = z.object({
874
+ /** Unique rate identifier (format: instance_id:method_id) */
875
+ rate_id: z.string(),
876
+ /** Shipping method name */
877
+ name: z.string(),
878
+ /** Shipping method description */
879
+ description: z.string(),
880
+ /** Estimated delivery time */
881
+ delivery_time: z.string(),
882
+ /** Shipping cost */
883
+ price: z.string(),
884
+ /** Shipping instance ID */
885
+ instance_id: z.number(),
886
+ /** Shipping method ID */
887
+ method_id: z.string(),
888
+ /** Additional metadata for this shipping method */
889
+ meta_data: z.array(
890
+ z.object({
891
+ key: z.string(),
892
+ value: z.string()
893
+ })
894
+ ),
895
+ /** Whether this rate is currently selected */
896
+ selected: z.boolean(),
897
+ /** Currency code for the price */
898
+ currency_code: z.string(),
899
+ /** Currency symbol for the price */
900
+ currency_symbol: z.string()
901
+ });
902
+ var shippingPackageSchema = z.object({
903
+ /** Package identifier (0-indexed) */
904
+ package_id: z.number(),
905
+ /** Package name/label */
906
+ name: z.string(),
907
+ /** Shipping destination address */
908
+ destination: z.object({
909
+ address_1: z.string(),
910
+ address_2: z.string().optional(),
911
+ city: z.string(),
912
+ state: z.string(),
913
+ postcode: z.string(),
914
+ country: z.string()
915
+ }),
916
+ /** Items included in this package */
917
+ items: z.array(
918
+ z.object({
919
+ /** Cart item key */
920
+ key: z.string(),
921
+ /** Product name */
922
+ name: z.string(),
923
+ /** Quantity in package */
924
+ quantity: z.number()
925
+ })
926
+ ),
927
+ /** Available shipping rates for this package */
928
+ shipping_rates: z.array(shippingRateSchema)
929
+ });
930
+
931
+ // src/schemas/store-api/cart.ts
932
+ var cartSchema = z.object({
933
+ items: z.array(cartItemSchema),
934
+ items_count: z.number(),
935
+ items_weight: z.number(),
936
+ needs_payment: z.boolean(),
937
+ needs_shipping: z.boolean(),
938
+ payment_methods: z.array(z.enum(["cod", "monri"])),
939
+ payment_requirements: z.array(z.string()),
940
+ has_calculated_shipping: z.boolean(),
941
+ shipping_address: addressSchemaNonMandatory,
942
+ billing_address: billingAddressSchemaNonMandatory,
943
+ shipping_rates: z.array(shippingPackageSchema),
944
+ coupons: z.array(cartCouponSchema),
945
+ totals: cartTotalsSchema,
946
+ errors: z.array(
947
+ z.object({
948
+ code: z.string(),
949
+ message: z.string()
950
+ })
951
+ ),
952
+ extensions: z.record(z.string(), z.unknown()).optional(),
953
+ cross_sells: z.array(z.unknown()),
954
+ fees: z.array(z.unknown())
955
+ });
956
+ z.object({
957
+ id: z.number(),
958
+ quantity: z.number().int().positive(),
959
+ variation: z.array(
960
+ z.object({
961
+ attribute: z.string(),
962
+ value: z.string()
963
+ })
964
+ ).optional()
965
+ });
966
+ z.object({
967
+ key: z.string(),
968
+ quantity: z.number().int().positive()
969
+ });
970
+ z.object({
971
+ key: z.string()
972
+ });
973
+ z.object({
974
+ code: z.string().min(1)
975
+ });
976
+ z.object({
977
+ billing_address: billingAddressSchemaMandatory.partial(),
978
+ shipping_address: addressSchema.partial()
979
+ }).partial();
980
+ z.object({
981
+ package_id: z.union([z.number(), z.string()]).default(0),
982
+ rate_id: z.string()
983
+ });
984
+ var orderItemImageSchema = z.object({
985
+ /** Image ID */
986
+ id: z.number(),
987
+ /** Full image URL */
988
+ src: z.string().url(),
989
+ /** Thumbnail URL */
990
+ thumbnail: z.string().url(),
991
+ /** Responsive image srcset */
992
+ srcset: z.string(),
993
+ /** Responsive image sizes attribute */
994
+ sizes: z.string(),
995
+ /** Image name */
996
+ name: z.string(),
997
+ /** Image alt text */
998
+ alt: z.string()
999
+ });
1000
+ var orderItemPricesSchema = z.object({
1001
+ /** Formatted price (current price) */
1002
+ price: z.string(),
1003
+ /** Formatted regular price */
1004
+ regular_price: z.string(),
1005
+ /** Formatted sale price */
1006
+ sale_price: z.string(),
1007
+ /** Price range (null for simple products) */
1008
+ price_range: z.null(),
1009
+ /** Currency code (e.g., 'USD', 'EUR') */
1010
+ currency_code: z.string(),
1011
+ /** Currency symbol (e.g., '$', '€') */
1012
+ currency_symbol: z.string(),
1013
+ /** Number of decimal places for currency */
1014
+ currency_minor_unit: z.number(),
1015
+ /** Decimal separator (e.g., '.', ',') */
1016
+ currency_decimal_separator: z.string(),
1017
+ /** Thousand separator (e.g., ',', '.') */
1018
+ currency_thousand_separator: z.string(),
1019
+ /** Currency prefix (e.g., '$') */
1020
+ currency_prefix: z.string(),
1021
+ /** Currency suffix (e.g., 'USD') */
1022
+ currency_suffix: z.string(),
1023
+ /** Raw price values for calculations */
1024
+ raw_prices: z.object({
1025
+ /** Precision for price calculations */
1026
+ precision: z.number(),
1027
+ /** Raw price (in minor units) */
1028
+ price: z.string(),
1029
+ /** Raw regular price (in minor units) */
1030
+ regular_price: z.string(),
1031
+ /** Raw sale price (in minor units) */
1032
+ sale_price: z.string()
1033
+ })
1034
+ });
1035
+ var orderItemTotalsSchema = z.object({
1036
+ /** Line subtotal (before discounts) */
1037
+ line_subtotal: z.string(),
1038
+ /** Line subtotal tax */
1039
+ line_subtotal_tax: z.string(),
1040
+ /** Line total (after discounts) */
1041
+ line_total: z.string(),
1042
+ /** Line total tax */
1043
+ line_total_tax: z.string(),
1044
+ /** Currency code */
1045
+ currency_code: z.string(),
1046
+ /** Currency symbol */
1047
+ currency_symbol: z.string(),
1048
+ /** Currency minor unit */
1049
+ currency_minor_unit: z.number(),
1050
+ /** Decimal separator */
1051
+ currency_decimal_separator: z.string(),
1052
+ /** Thousand separator */
1053
+ currency_thousand_separator: z.string(),
1054
+ /** Currency prefix */
1055
+ currency_prefix: z.string(),
1056
+ /** Currency suffix */
1057
+ currency_suffix: z.string()
1058
+ });
1059
+ var quantityLimitsSchema = z.object({
1060
+ /** Minimum quantity allowed */
1061
+ minimum: z.number(),
1062
+ /** Maximum quantity allowed */
1063
+ maximum: z.number(),
1064
+ /** Quantity must be multiple of this value */
1065
+ multiple_of: z.number(),
1066
+ /** Whether quantity is editable */
1067
+ editable: z.boolean()
1068
+ });
1069
+ var orderItemSchema = z.object({
1070
+ /** Unique cart/order item key */
1071
+ key: z.string(),
1072
+ /** Product ID */
1073
+ id: z.number(),
1074
+ /** Quantity ordered */
1075
+ quantity: z.number(),
1076
+ /** Quantity limits for this product */
1077
+ quantity_limits: quantityLimitsSchema,
1078
+ /** Product name */
1079
+ name: z.string(),
1080
+ /** Short description */
1081
+ short_description: z.string(),
1082
+ /** Full description */
1083
+ description: z.string(),
1084
+ /** Product SKU */
1085
+ sku: z.string(),
1086
+ /** Low stock warning (null if not low) */
1087
+ low_stock_remaining: z.null(),
1088
+ /** Whether backorders are allowed */
1089
+ backorders_allowed: z.boolean(),
1090
+ /** Whether to show backorder badge */
1091
+ show_backorder_badge: z.boolean(),
1092
+ /** Whether product can only be purchased individually */
1093
+ sold_individually: z.boolean(),
1094
+ /** Product permalink URL */
1095
+ permalink: z.string().url(),
1096
+ /** Product images */
1097
+ images: z.array(orderItemImageSchema),
1098
+ /** Variation attributes (empty for simple products) */
1099
+ variation: z.array(z.unknown()),
1100
+ /** Custom item data/metadata */
1101
+ item_data: z.array(z.unknown()),
1102
+ /** Price information */
1103
+ prices: orderItemPricesSchema,
1104
+ /** Totals for this line item */
1105
+ totals: orderItemTotalsSchema,
1106
+ /** Catalog visibility setting */
1107
+ catalog_visibility: z.string()
1108
+ });
1109
+ var orderTotalsSchema = z.object({
1110
+ /** Subtotal (before discounts and fees) */
1111
+ subtotal: z.string(),
1112
+ /** Total discount amount */
1113
+ total_discount: z.string(),
1114
+ /** Total shipping cost */
1115
+ total_shipping: z.string(),
1116
+ /** Total fees */
1117
+ total_fees: z.string(),
1118
+ /** Total tax */
1119
+ total_tax: z.string(),
1120
+ /** Total refund amount */
1121
+ total_refund: z.string(),
1122
+ /** Final total price */
1123
+ total_price: z.string(),
1124
+ /** Total items cost (subtotal after item-level discounts) */
1125
+ total_items: z.string(),
1126
+ /** Tax on items */
1127
+ total_items_tax: z.string(),
1128
+ /** Tax on fees */
1129
+ total_fees_tax: z.string(),
1130
+ /** Tax on discounts */
1131
+ total_discount_tax: z.string(),
1132
+ /** Tax on shipping */
1133
+ total_shipping_tax: z.string(),
1134
+ /** Tax line items breakdown */
1135
+ tax_lines: z.array(z.unknown()),
1136
+ /** Currency code (e.g., 'USD', 'EUR', 'BAM') */
1137
+ currency_code: z.string(),
1138
+ /** Currency symbol (e.g., '$', '€', 'KM') */
1139
+ currency_symbol: z.string(),
1140
+ /** Number of decimal places for currency */
1141
+ currency_minor_unit: z.number(),
1142
+ /** Decimal separator (e.g., '.', ',') */
1143
+ currency_decimal_separator: z.string(),
1144
+ /** Thousand separator (e.g., ',', '.') */
1145
+ currency_thousand_separator: z.string(),
1146
+ /** Currency prefix (e.g., '$') */
1147
+ currency_prefix: z.string(),
1148
+ /** Currency suffix (e.g., 'USD') */
1149
+ currency_suffix: z.string()
1150
+ });
1151
+
1152
+ // src/schemas/store-api/order.schema.ts
1153
+ var orderStatusEnum = z.enum([
1154
+ "pending",
1155
+ // Order received, awaiting payment
1156
+ "processing",
1157
+ // Payment received, order is being processed
1158
+ "on-hold",
1159
+ // Order on hold, awaiting confirmation
1160
+ "completed",
1161
+ // Order fulfilled and complete
1162
+ "cancelled",
1163
+ // Order cancelled by customer or admin
1164
+ "refunded",
1165
+ // Order refunded
1166
+ "failed"
1167
+ // Payment failed or order declined
1168
+ ]);
1169
+ var storeApiOrderSchema = z.object({
1170
+ /** Order ID */
1171
+ id: z.number(),
1172
+ /** Order status */
1173
+ status: orderStatusEnum,
1174
+ /** Order line items (products purchased) */
1175
+ items: z.array(orderItemSchema),
1176
+ /** Applied coupons */
1177
+ coupons: z.array(z.unknown()),
1178
+ /** Order fees */
1179
+ fees: z.array(z.unknown()),
1180
+ /** Order totals breakdown */
1181
+ totals: orderTotalsSchema,
1182
+ /** Shipping address */
1183
+ shipping_address: addressSchema,
1184
+ /** Billing address (includes email) */
1185
+ billing_address: billingAddressSchemaMandatory,
1186
+ /** Whether order still needs payment */
1187
+ needs_payment: z.boolean(),
1188
+ /** Whether order needs shipping */
1189
+ needs_shipping: z.boolean(),
1190
+ /** Payment method requirements */
1191
+ payment_requirements: z.array(z.string()),
1192
+ /** Order errors (if any) */
1193
+ errors: z.array(z.unknown()),
1194
+ /** Payment method (optional - returned from checkout) */
1195
+ payment_method: z.string().optional(),
1196
+ /** Payment method title (optional - returned from checkout) */
1197
+ payment_method_title: z.string().optional()
1198
+ });
1199
+ var checkoutSchema = z.object({
1200
+ order_id: z.number(),
1201
+ status: z.string(),
1202
+ order_key: z.string(),
1203
+ customer_note: z.string(),
1204
+ customer_id: z.number(),
1205
+ billing_address: billingAddressSchemaNonMandatory,
1206
+ shipping_address: addressSchemaNonMandatory,
1207
+ payment_method: z.string(),
1208
+ payment_result: z.object({
1209
+ payment_status: z.string(),
1210
+ payment_details: z.array(z.unknown()),
1211
+ redirect_url: z.string()
1212
+ })
1213
+ });
1214
+ z.object({
1215
+ payment_method: z.string(),
1216
+ payment_data: z.array(
1217
+ z.object({
1218
+ key: z.string(),
1219
+ value: z.union([z.string(), z.boolean()])
1220
+ })
1221
+ ).optional(),
1222
+ billing_address: billingAddressSchemaMandatory.optional(),
1223
+ shipping_address: addressSchema.optional(),
1224
+ customer_note: z.string().optional(),
1225
+ create_account: z.boolean().optional(),
1226
+ extensions: z.record(z.string(), z.unknown()).optional()
1227
+ });
1228
+ var addToCartSchema = z.object({
1229
+ /** Description for add to cart action */
1230
+ description: z.string(),
1231
+ /** Maximum quantity that can be added */
1232
+ maximum: z.number(),
1233
+ /** Minimum quantity that can be added */
1234
+ minimum: z.number(),
1235
+ /** Quantity must be a multiple of this number */
1236
+ multiple_of: z.number(),
1237
+ /** Text for add to cart button */
1238
+ text: z.string(),
1239
+ /** URL for add to cart action */
1240
+ url: z.string()
1241
+ });
1242
+ var embeddedCategorySchema = z.object({
1243
+ /** Category unique identifier */
1244
+ id: z.number(),
1245
+ /** Category display name */
1246
+ name: z.string(),
1247
+ /** URL-friendly category identifier */
1248
+ slug: z.string(),
1249
+ /** Link to category page */
1250
+ link: z.string(),
1251
+ /** Parent category ID (0 for root categories) - optional in embedded */
1252
+ parent: z.number().optional(),
1253
+ /** Category description - optional in embedded */
1254
+ description: z.string().optional(),
1255
+ /** Display type - optional in embedded */
1256
+ display: z.string().optional(),
1257
+ /** Category image - optional in embedded */
1258
+ image: productImageSchema.nullable().optional(),
1259
+ /** Menu order for sorting - optional in embedded */
1260
+ menu_order: z.number().optional(),
1261
+ /** Number of products in this category - optional in embedded */
1262
+ count: z.number().optional()
1263
+ });
1264
+ var productCategorySchema = z.object({
1265
+ /** Category unique identifier */
1266
+ id: z.number(),
1267
+ /** Category display name */
1268
+ name: z.string(),
1269
+ /** URL-friendly category identifier */
1270
+ slug: z.string(),
1271
+ /** Parent category ID (0 for root categories) */
1272
+ parent: z.number(),
1273
+ /** Category description */
1274
+ description: z.string(),
1275
+ /** Category image */
1276
+ image: productImageSchema.nullable(),
1277
+ /** Menu order for sorting */
1278
+ menu_order: z.number().optional(),
1279
+ /** Number of products in this category */
1280
+ count: z.number()
1281
+ });
1282
+
1283
+ // src/schemas/store-api/products.ts
1284
+ var productSchema = z.object({
1285
+ id: z.number(),
1286
+ name: z.string(),
1287
+ slug: z.string(),
1288
+ type: z.string(),
1289
+ variation: z.string(),
1290
+ permalink: z.string().url(),
1291
+ sku: z.string(),
1292
+ short_description: z.string(),
1293
+ description: z.string(),
1294
+ is_purchasable: z.boolean(),
1295
+ is_in_stock: z.boolean(),
1296
+ is_on_backorder: z.boolean(),
1297
+ low_stock_remaining: z.number().nullable(),
1298
+ sold_individually: z.boolean(),
1299
+ add_to_cart: addToCartSchema,
1300
+ has_options: z.boolean(),
1301
+ on_sale: z.boolean(),
1302
+ parent: z.number(),
1303
+ attributes: z.array(z.unknown()),
1304
+ categories: z.array(embeddedCategorySchema),
1305
+ tags: z.array(z.unknown()),
1306
+ images: z.array(productImageSchema),
1307
+ variations: z.array(z.unknown()),
1308
+ price_html: z.string(),
1309
+ prices: pricesSchema,
1310
+ average_rating: z.string(),
1311
+ review_count: z.number(),
1312
+ extensions: z.record(z.string(), z.unknown())
1313
+ });
1314
+
1315
+ // src/api/cart.ts
1316
+ var createCartAPI = (client, endpoints, options) => ({
1317
+ get: async () => {
1318
+ const response = await client.get(endpoints.cart);
1319
+ return handleApiResponse(response, cartSchema, options);
1320
+ },
1321
+ addItem: async (input) => {
1322
+ const response = await client.post(endpoints.addItem, input);
1323
+ return handleApiResponse(response, cartSchema, options);
1324
+ },
1325
+ updateItem: async (input) => {
1326
+ const response = await client.post(endpoints.updateItem, input);
1327
+ return handleApiResponse(response, cartSchema, options);
1328
+ },
1329
+ removeItem: async (input) => {
1330
+ const response = await client.post(endpoints.removeItem, input);
1331
+ return handleApiResponse(response, cartSchema, options);
1332
+ },
1333
+ applyCoupon: async (input) => {
1334
+ const response = await client.post(endpoints.applyCoupon, input);
1335
+ return handleApiResponse(response, cartSchema, options);
1336
+ },
1337
+ removeCoupon: async (input) => {
1338
+ const response = await client.post(endpoints.removeCoupon, input);
1339
+ return handleApiResponse(response, cartSchema, options);
1340
+ },
1341
+ updateCustomer: async (input) => {
1342
+ const response = await client.post(endpoints.updateCustomer, input);
1343
+ return handleApiResponse(response, cartSchema, options);
1344
+ },
1345
+ selectShippingRate: async (input) => {
1346
+ const response = await client.post(endpoints.selectShippingRate, input);
1347
+ return handleApiResponse(response, cartSchema, options);
1348
+ }
1349
+ });
1350
+
1351
+ // src/api/checkout.ts
1352
+ var createCheckoutAPI = (client, endpoints, options) => ({
1353
+ get: async () => {
1354
+ const response = await client.get(endpoints.checkout);
1355
+ return handleApiResponse(response, checkoutSchema, options);
1356
+ },
1357
+ process: async (input) => {
1358
+ const response = await client.post(endpoints.checkout, input);
1359
+ return handleApiResponse(response, checkoutSchema, options);
1360
+ }
1361
+ });
1362
+
1363
+ // src/api/orders.ts
1364
+ var createOrdersAPI = (client, endpoints, options) => ({
1365
+ /**
1366
+ * Get a single order by ID
1367
+ * Uses Store API - supports guest lookup with billing_email + key
1368
+ */
1369
+ get: async (input) => {
1370
+ const { id, billing_email, key } = input;
1371
+ const params = billing_email && key ? { billing_email, key } : void 0;
1372
+ const response = await client.get(`${endpoints.order}/${id}`, { params });
1373
+ return handleApiResponse(response, storeApiOrderSchema, options);
1374
+ }
1375
+ });
1376
+ var createProductsAPI = (client, endpoints, options) => ({
1377
+ list: async (params) => {
1378
+ const response = await client.get(endpoints.products, { params });
1379
+ return handlePaginatedApiResponse(response, z.array(productSchema), params, options);
1380
+ },
1381
+ get: async (id) => {
1382
+ const response = await client.get(`${endpoints.products}/${id}`);
1383
+ return handleApiResponse(response, productSchema, options);
1384
+ },
1385
+ categories: async () => {
1386
+ const response = await client.get(endpoints.categories);
1387
+ return handleApiResponse(response, z.array(productCategorySchema), options);
1388
+ }
1389
+ });
1390
+
1391
+ // src/constants/endpoints.ts
1392
+ var createEndpoints = (version = "legacy") => {
1393
+ const prefix = version === "v1" ? "/wc/store/v1" : "/wc/store";
1394
+ return {
1395
+ // Products
1396
+ products: `${prefix}/products`,
1397
+ categories: `${prefix}/products/categories`,
1398
+ // Cart
1399
+ cart: `${prefix}/cart`,
1400
+ addItem: `${prefix}/cart/add-item`,
1401
+ updateItem: `${prefix}/cart/update-item`,
1402
+ removeItem: `${prefix}/cart/remove-item`,
1403
+ applyCoupon: `${prefix}/cart/apply-coupon`,
1404
+ removeCoupon: `${prefix}/cart/remove-coupon`,
1405
+ updateCustomer: `${prefix}/cart/update-customer`,
1406
+ selectShippingRate: `${prefix}/cart/select-shipping-rate`,
1407
+ // Checkout
1408
+ checkout: `${prefix}/checkout`,
1409
+ // Orders
1410
+ order: `${prefix}/order`
1411
+ };
1412
+ };
1413
+
1414
+ // src/client/createClient.ts
1415
+ var DEFAULT_ERROR_REPORTER = {
1416
+ report: (error2) => {
1417
+ console.error("[woocommerce-utils]", error2);
1418
+ }
1419
+ };
1420
+ var createClient = (config) => {
1421
+ const fullConfig = {
1422
+ ...config,
1423
+ errorReporter: config.errorReporter ?? DEFAULT_ERROR_REPORTER
1424
+ };
1425
+ const axiosInstance = axios.create({
1426
+ baseURL: fullConfig.baseURL,
1427
+ timeout: fullConfig.timeout || 3e4,
1428
+ headers: {
1429
+ "Content-Type": "application/json",
1430
+ ...fullConfig.headers
1431
+ }
1432
+ });
1433
+ const endpoints = createEndpoints(fullConfig.storeApiVersion ?? "legacy");
1434
+ const responseOptions = {
1435
+ validationMode: fullConfig.validationMode,
1436
+ errorReporter: fullConfig.errorReporter?.report,
1437
+ onValidationError: fullConfig.onValidationError
1438
+ };
1439
+ const client = {
1440
+ config: fullConfig,
1441
+ axios: axiosInstance,
1442
+ products: createProductsAPI(axiosInstance, endpoints, responseOptions),
1443
+ cart: createCartAPI(axiosInstance, endpoints, responseOptions),
1444
+ checkout: createCheckoutAPI(axiosInstance, endpoints, responseOptions),
1445
+ orders: createOrdersAPI(axiosInstance, endpoints, responseOptions)
1446
+ };
1447
+ setupRequestInterceptor(axiosInstance, fullConfig, client);
1448
+ setupResponseInterceptor(axiosInstance, fullConfig, client);
1449
+ setupErrorInterceptor(axiosInstance, fullConfig, client);
1450
+ return client;
1451
+ };
1452
+
1453
+ export { createClient };
1454
+ //# sourceMappingURL=index.mjs.map
1455
+ //# sourceMappingURL=index.mjs.map