@atomic-solutions/woocommerce-api-client 0.5.0 → 0.6.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.
package/dist/index.mjs CHANGED
@@ -1,7 +1,14 @@
1
- import axios, { AxiosError, isAxiosError } from 'axios';
1
+ import axios, { AxiosError, isCancel, isAxiosError } from 'axios';
2
+ import { BaseError as BaseError$1 } from '@atomic-solutions/errors';
2
3
  import { z } from 'zod';
3
4
 
4
- // src/client/createClient.ts
5
+ var __typeError = (msg) => {
6
+ throw TypeError(msg);
7
+ };
8
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
9
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
10
+ var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
11
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), member.set(obj, value), value);
5
12
 
6
13
  // src/errors/classes/BaseError.ts
7
14
  var BaseError = class extends Error {
@@ -131,8 +138,16 @@ var WOOCOMMERCE_ERROR_METADATA = {
131
138
  }
132
139
  };
133
140
 
141
+ // src/errors/classes/http-error.ts
142
+ var redactPath = (url) => url.replace(/^[a-z][a-z0-9+.-]*:\/\/[^/]+/i, "").replace(/[?#].*$/, "").split("/").map((segment) => /^(\d+|[0-9a-f]{16,}|[0-9a-f-]{36})$/i.test(segment) ? ":id" : segment).join("/");
143
+ var classifyHttpStatus = (status) => ({
144
+ domain: status === void 0 ? "network" : status === 401 || status === 403 ? "auth" : "integration",
145
+ severity: status !== void 0 && status < 500 ? "warning" : "error"
146
+ });
147
+
134
148
  // src/errors/classes/WooCommerceApiError.ts
135
- var WooCommerceApiError = class extends BaseError {
149
+ var _options;
150
+ var WooCommerceApiError = class extends BaseError$1 {
136
151
  constructor(options) {
137
152
  const metadata = WOOCOMMERCE_ERROR_METADATA[options.code] ?? {
138
153
  operation: "woocommerce_api_request",
@@ -144,7 +159,30 @@ var WooCommerceApiError = class extends BaseError {
144
159
  userMessage: options.userMessage || metadata.userMessage,
145
160
  retryable: options.retryable ?? metadata.retryable
146
161
  };
147
- super(enrichedOptions);
162
+ const status = enrichedOptions.statusCode;
163
+ const { domain, severity } = classifyHttpStatus(status);
164
+ super({
165
+ domain,
166
+ severity,
167
+ operation: enrichedOptions.operation,
168
+ message: enrichedOptions.message,
169
+ code: enrichedOptions.code,
170
+ retryable: enrichedOptions.retryable,
171
+ userMessage: enrichedOptions.userMessage,
172
+ cause: enrichedOptions.cause,
173
+ fingerprint: [domain, enrichedOptions.code, String(status ?? "network")],
174
+ extras: {
175
+ originalCode: enrichedOptions.originalCode,
176
+ statusCode: status,
177
+ path: redactPath(enrichedOptions.url),
178
+ method: enrichedOptions.method
179
+ }
180
+ });
181
+ __privateAdd(this, _options);
182
+ __privateSet(this, _options, enrichedOptions);
183
+ }
184
+ get options() {
185
+ return __privateGet(this, _options);
148
186
  }
149
187
  // Convenient getters for frequently accessed fields (no duplication)
150
188
  get wooCode() {
@@ -171,24 +209,8 @@ var WooCommerceApiError = class extends BaseError {
171
209
  get data() {
172
210
  return this.options.data;
173
211
  }
174
- /**
175
- * Get reportable data for error tracking/logging
176
- * Includes HTTP context and WooCommerce-specific fields
177
- */
178
- getReportableData() {
179
- return {
180
- code: this.options.code,
181
- operation: this.options.operation,
182
- userMessage: this.options.userMessage,
183
- retryable: this.options.retryable,
184
- statusCode: this.options.statusCode,
185
- url: this.options.url,
186
- method: this.options.method,
187
- wooCode: this.options.code,
188
- originalCode: this.options.originalCode
189
- };
190
- }
191
212
  };
213
+ _options = new WeakMap();
192
214
 
193
215
  // src/errors/classes/WooCommerceDataValidationError.ts
194
216
  var WooCommerceDataValidationError = class _WooCommerceDataValidationError extends BaseError {
@@ -581,12 +603,15 @@ var setupErrorInterceptor = (axiosInstance, config, client) => {
581
603
  axiosInstance.interceptors.response.use(
582
604
  (response) => response,
583
605
  async (axiosError) => {
584
- const status = axiosError.response?.status || 500;
606
+ if (isCancel(axiosError)) {
607
+ return Promise.reject(axiosError);
608
+ }
609
+ const status = axiosError.response?.status;
585
610
  const errorData = axiosError.response?.data;
611
+ const isNetworkError = isAxiosError(axiosError) && !axiosError.response;
586
612
  const apiCode = errorData?.code || "unknown_error";
587
613
  const wpMessage = errorData?.message || axiosError.message || "Unknown error";
588
- const errorCode = mapWooCommerceCode(apiCode);
589
- const errorMetadata = WOOCOMMERCE_ERROR_METADATA[errorCode];
614
+ const errorCode = isNetworkError ? "network_error" : mapWooCommerceCode(apiCode);
590
615
  const wcError = new WooCommerceApiError({
591
616
  message: wpMessage,
592
617
  statusCode: status,
@@ -594,9 +619,6 @@ var setupErrorInterceptor = (axiosInstance, config, client) => {
594
619
  // Transformed app-level code
595
620
  originalCode: apiCode,
596
621
  // Original WooCommerce API code
597
- retryable: errorMetadata.retryable,
598
- userMessage: errorMetadata.userMessage,
599
- operation: errorMetadata.operation,
600
622
  url: axiosError.config?.url || "",
601
623
  method: axiosError.config?.method?.toUpperCase() || "GET",
602
624
  requestBody: axiosError.config?.data,
@@ -1290,11 +1312,13 @@ var checkoutSchema = z.object({
1290
1312
  billing_address: billingAddressSchemaNonMandatory,
1291
1313
  shipping_address: addressSchemaNonMandatory,
1292
1314
  payment_method: z.string(),
1315
+ // Null until an order is placed.
1316
+ // GET /checkout returns null here, so the schema must accept it.
1293
1317
  payment_result: z.object({
1294
1318
  payment_status: z.string(),
1295
1319
  payment_details: z.array(z.unknown()),
1296
1320
  redirect_url: z.string()
1297
- })
1321
+ }).nullable()
1298
1322
  });
1299
1323
  var checkoutInputSchema = z.object({
1300
1324
  payment_method: z.string(),
@@ -1497,6 +1521,14 @@ var productSchema = z.object({
1497
1521
  review_count: z.number(),
1498
1522
  extensions: z.record(z.string(), z.unknown())
1499
1523
  });
1524
+ var productTagSchema = z.object({
1525
+ /** Tag unique identifier */
1526
+ id: z.number(),
1527
+ /** Tag display name, e.g. "Vichy decembar 2025" */
1528
+ name: z.string(),
1529
+ /** URL-friendly tag identifier */
1530
+ slug: z.string()
1531
+ });
1500
1532
  z.object({
1501
1533
  /** Attribution source type (e.g., 'mobile_app', 'organic', 'referral') */
1502
1534
  source_type: z.string(),
@@ -1642,6 +1674,14 @@ var createProductsAPI = (client, endpoints, options) => ({
1642
1674
  categories: async () => {
1643
1675
  const response = await client.get(endpoints.categories);
1644
1676
  return handleApiResponse(response, z.array(productCategorySchema), options);
1677
+ },
1678
+ // Tags, e.g. GET /products/tags?include=996,683.
1679
+ // The Store API wants include as a comma list, so join the ids.
1680
+ tags: async (params) => {
1681
+ const response = await client.get(endpoints.tags, {
1682
+ params: { include: params?.include?.join(",") }
1683
+ });
1684
+ return handleApiResponse(response, z.array(productTagSchema), options);
1645
1685
  }
1646
1686
  });
1647
1687
 
@@ -1652,6 +1692,8 @@ var createEndpoints = (version = "legacy") => {
1652
1692
  // Products
1653
1693
  products: `${prefix}/products`,
1654
1694
  categories: `${prefix}/products/categories`,
1695
+ // Product tags, e.g. to turn a tag id into its name
1696
+ tags: `${prefix}/products/tags`,
1655
1697
  // Cart
1656
1698
  cart: `${prefix}/cart`,
1657
1699
  addItem: `${prefix}/cart/add-item`,