@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.
@@ -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 {
@@ -130,8 +137,16 @@ var WOOCOMMERCE_ERROR_METADATA = {
130
137
  }
131
138
  };
132
139
 
140
+ // src/errors/classes/http-error.ts
141
+ 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("/");
142
+ var classifyHttpStatus = (status) => ({
143
+ domain: status === void 0 ? "network" : status === 401 || status === 403 ? "auth" : "integration",
144
+ severity: status !== void 0 && status < 500 ? "warning" : "error"
145
+ });
146
+
133
147
  // src/errors/classes/WooCommerceApiError.ts
134
- var WooCommerceApiError = class extends BaseError {
148
+ var _options;
149
+ var WooCommerceApiError = class extends BaseError$1 {
135
150
  constructor(options) {
136
151
  const metadata = WOOCOMMERCE_ERROR_METADATA[options.code] ?? {
137
152
  operation: "woocommerce_api_request",
@@ -143,7 +158,30 @@ var WooCommerceApiError = class extends BaseError {
143
158
  userMessage: options.userMessage || metadata.userMessage,
144
159
  retryable: options.retryable ?? metadata.retryable
145
160
  };
146
- super(enrichedOptions);
161
+ const status = enrichedOptions.statusCode;
162
+ const { domain, severity } = classifyHttpStatus(status);
163
+ super({
164
+ domain,
165
+ severity,
166
+ operation: enrichedOptions.operation,
167
+ message: enrichedOptions.message,
168
+ code: enrichedOptions.code,
169
+ retryable: enrichedOptions.retryable,
170
+ userMessage: enrichedOptions.userMessage,
171
+ cause: enrichedOptions.cause,
172
+ fingerprint: [domain, enrichedOptions.code, String(status ?? "network")],
173
+ extras: {
174
+ originalCode: enrichedOptions.originalCode,
175
+ statusCode: status,
176
+ path: redactPath(enrichedOptions.url),
177
+ method: enrichedOptions.method
178
+ }
179
+ });
180
+ __privateAdd(this, _options);
181
+ __privateSet(this, _options, enrichedOptions);
182
+ }
183
+ get options() {
184
+ return __privateGet(this, _options);
147
185
  }
148
186
  // Convenient getters for frequently accessed fields (no duplication)
149
187
  get wooCode() {
@@ -170,24 +208,8 @@ var WooCommerceApiError = class extends BaseError {
170
208
  get data() {
171
209
  return this.options.data;
172
210
  }
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
211
  };
212
+ _options = new WeakMap();
191
213
 
192
214
  // src/errors/classes/WooCommerceDataValidationError.ts
193
215
  var WooCommerceDataValidationError = class _WooCommerceDataValidationError extends BaseError {
@@ -580,12 +602,15 @@ var setupErrorInterceptor = (axiosInstance, config, client) => {
580
602
  axiosInstance.interceptors.response.use(
581
603
  (response) => response,
582
604
  async (axiosError) => {
583
- const status = axiosError.response?.status || 500;
605
+ if (isCancel(axiosError)) {
606
+ return Promise.reject(axiosError);
607
+ }
608
+ const status = axiosError.response?.status;
584
609
  const errorData = axiosError.response?.data;
610
+ const isNetworkError = isAxiosError(axiosError) && !axiosError.response;
585
611
  const apiCode = errorData?.code || "unknown_error";
586
612
  const wpMessage = errorData?.message || axiosError.message || "Unknown error";
587
- const errorCode = mapWooCommerceCode(apiCode);
588
- const errorMetadata = WOOCOMMERCE_ERROR_METADATA[errorCode];
613
+ const errorCode = isNetworkError ? "network_error" : mapWooCommerceCode(apiCode);
589
614
  const wcError = new WooCommerceApiError({
590
615
  message: wpMessage,
591
616
  statusCode: status,
@@ -593,9 +618,6 @@ var setupErrorInterceptor = (axiosInstance, config, client) => {
593
618
  // Transformed app-level code
594
619
  originalCode: apiCode,
595
620
  // Original WooCommerce API code
596
- retryable: errorMetadata.retryable,
597
- userMessage: errorMetadata.userMessage,
598
- operation: errorMetadata.operation,
599
621
  url: axiosError.config?.url || "",
600
622
  method: axiosError.config?.method?.toUpperCase() || "GET",
601
623
  requestBody: axiosError.config?.data,
@@ -1240,11 +1262,13 @@ var checkoutSchema = z.object({
1240
1262
  billing_address: billingAddressSchemaNonMandatory,
1241
1263
  shipping_address: addressSchemaNonMandatory,
1242
1264
  payment_method: z.string(),
1265
+ // Null until an order is placed.
1266
+ // GET /checkout returns null here, so the schema must accept it.
1243
1267
  payment_result: z.object({
1244
1268
  payment_status: z.string(),
1245
1269
  payment_details: z.array(z.unknown()),
1246
1270
  redirect_url: z.string()
1247
- })
1271
+ }).nullable()
1248
1272
  });
1249
1273
  z.object({
1250
1274
  payment_method: z.string(),
@@ -1346,6 +1370,14 @@ var productSchema = z.object({
1346
1370
  review_count: z.number(),
1347
1371
  extensions: z.record(z.string(), z.unknown())
1348
1372
  });
1373
+ var productTagSchema = z.object({
1374
+ /** Tag unique identifier */
1375
+ id: z.number(),
1376
+ /** Tag display name, e.g. "Vichy decembar 2025" */
1377
+ name: z.string(),
1378
+ /** URL-friendly tag identifier */
1379
+ slug: z.string()
1380
+ });
1349
1381
  z.object({
1350
1382
  /** Attribution source type (e.g., 'mobile_app', 'organic', 'referral') */
1351
1383
  source_type: z.string(),
@@ -1491,6 +1523,14 @@ var createProductsAPI = (client, endpoints, options) => ({
1491
1523
  categories: async () => {
1492
1524
  const response = await client.get(endpoints.categories);
1493
1525
  return handleApiResponse(response, z.array(productCategorySchema), options);
1526
+ },
1527
+ // Tags, e.g. GET /products/tags?include=996,683.
1528
+ // The Store API wants include as a comma list, so join the ids.
1529
+ tags: async (params) => {
1530
+ const response = await client.get(endpoints.tags, {
1531
+ params: { include: params?.include?.join(",") }
1532
+ });
1533
+ return handleApiResponse(response, z.array(productTagSchema), options);
1494
1534
  }
1495
1535
  });
1496
1536
 
@@ -1501,6 +1541,8 @@ var createEndpoints = (version = "legacy") => {
1501
1541
  // Products
1502
1542
  products: `${prefix}/products`,
1503
1543
  categories: `${prefix}/products/categories`,
1544
+ // Product tags, e.g. to turn a tag id into its name
1545
+ tags: `${prefix}/products/tags`,
1504
1546
  // Cart
1505
1547
  cart: `${prefix}/cart`,
1506
1548
  addItem: `${prefix}/cart/add-item`,