@atomic-solutions/woocommerce-api-client 0.5.0 → 0.5.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.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,