@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,5 +1,6 @@
1
- import { S as StoreApiVersion, C as CartHeadersAdapter, J as JwtTokenAdapter } from '../types-aYzN4O5x.mjs';
1
+ import { S as StoreApiVersion, C as CartHeadersAdapter, J as JwtTokenAdapter } from '../types-Cbf1-EhK.mjs';
2
2
  import 'axios';
3
+ import '@atomic-solutions/errors';
3
4
  import 'zod';
4
5
 
5
6
  type DescribeFn = (name: string, fn: () => void) => void;
@@ -1,5 +1,6 @@
1
- import { S as StoreApiVersion, C as CartHeadersAdapter, J as JwtTokenAdapter } from '../types-aYzN4O5x.js';
1
+ import { S as StoreApiVersion, C as CartHeadersAdapter, J as JwtTokenAdapter } from '../types-Cbf1-EhK.js';
2
2
  import 'axios';
3
+ import '@atomic-solutions/errors';
3
4
  import 'zod';
4
5
 
5
6
  type DescribeFn = (name: string, fn: () => void) => void;
@@ -1,13 +1,20 @@
1
1
  'use strict';
2
2
 
3
3
  var axios = require('axios');
4
+ var errors = require('@atomic-solutions/errors');
4
5
  var zod = require('zod');
5
6
 
6
7
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
7
8
 
8
9
  var axios__default = /*#__PURE__*/_interopDefault(axios);
9
10
 
10
- // src/client/createClient.ts
11
+ var __typeError = (msg) => {
12
+ throw TypeError(msg);
13
+ };
14
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
15
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
16
+ 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);
17
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), member.set(obj, value), value);
11
18
 
12
19
  // src/errors/classes/BaseError.ts
13
20
  var BaseError = class extends Error {
@@ -136,8 +143,16 @@ var WOOCOMMERCE_ERROR_METADATA = {
136
143
  }
137
144
  };
138
145
 
146
+ // src/errors/classes/http-error.ts
147
+ 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("/");
148
+ var classifyHttpStatus = (status) => ({
149
+ domain: status === void 0 ? "network" : status === 401 || status === 403 ? "auth" : "integration",
150
+ severity: status !== void 0 && status < 500 ? "warning" : "error"
151
+ });
152
+
139
153
  // src/errors/classes/WooCommerceApiError.ts
140
- var WooCommerceApiError = class extends BaseError {
154
+ var _options;
155
+ var WooCommerceApiError = class extends errors.BaseError {
141
156
  constructor(options) {
142
157
  const metadata = WOOCOMMERCE_ERROR_METADATA[options.code] ?? {
143
158
  operation: "woocommerce_api_request",
@@ -149,7 +164,30 @@ var WooCommerceApiError = class extends BaseError {
149
164
  userMessage: options.userMessage || metadata.userMessage,
150
165
  retryable: options.retryable ?? metadata.retryable
151
166
  };
152
- super(enrichedOptions);
167
+ const status = enrichedOptions.statusCode;
168
+ const { domain, severity } = classifyHttpStatus(status);
169
+ super({
170
+ domain,
171
+ severity,
172
+ operation: enrichedOptions.operation,
173
+ message: enrichedOptions.message,
174
+ code: enrichedOptions.code,
175
+ retryable: enrichedOptions.retryable,
176
+ userMessage: enrichedOptions.userMessage,
177
+ cause: enrichedOptions.cause,
178
+ fingerprint: [domain, enrichedOptions.code, String(status ?? "network")],
179
+ extras: {
180
+ originalCode: enrichedOptions.originalCode,
181
+ statusCode: status,
182
+ path: redactPath(enrichedOptions.url),
183
+ method: enrichedOptions.method
184
+ }
185
+ });
186
+ __privateAdd(this, _options);
187
+ __privateSet(this, _options, enrichedOptions);
188
+ }
189
+ get options() {
190
+ return __privateGet(this, _options);
153
191
  }
154
192
  // Convenient getters for frequently accessed fields (no duplication)
155
193
  get wooCode() {
@@ -176,24 +214,8 @@ var WooCommerceApiError = class extends BaseError {
176
214
  get data() {
177
215
  return this.options.data;
178
216
  }
179
- /**
180
- * Get reportable data for error tracking/logging
181
- * Includes HTTP context and WooCommerce-specific fields
182
- */
183
- getReportableData() {
184
- return {
185
- code: this.options.code,
186
- operation: this.options.operation,
187
- userMessage: this.options.userMessage,
188
- retryable: this.options.retryable,
189
- statusCode: this.options.statusCode,
190
- url: this.options.url,
191
- method: this.options.method,
192
- wooCode: this.options.code,
193
- originalCode: this.options.originalCode
194
- };
195
- }
196
217
  };
218
+ _options = new WeakMap();
197
219
 
198
220
  // src/errors/classes/WooCommerceDataValidationError.ts
199
221
  var WooCommerceDataValidationError = class _WooCommerceDataValidationError extends BaseError {
@@ -586,12 +608,15 @@ var setupErrorInterceptor = (axiosInstance, config, client) => {
586
608
  axiosInstance.interceptors.response.use(
587
609
  (response) => response,
588
610
  async (axiosError) => {
589
- const status = axiosError.response?.status || 500;
611
+ if (axios.isCancel(axiosError)) {
612
+ return Promise.reject(axiosError);
613
+ }
614
+ const status = axiosError.response?.status;
590
615
  const errorData = axiosError.response?.data;
616
+ const isNetworkError = axios.isAxiosError(axiosError) && !axiosError.response;
591
617
  const apiCode = errorData?.code || "unknown_error";
592
618
  const wpMessage = errorData?.message || axiosError.message || "Unknown error";
593
- const errorCode = mapWooCommerceCode(apiCode);
594
- const errorMetadata = WOOCOMMERCE_ERROR_METADATA[errorCode];
619
+ const errorCode = isNetworkError ? "network_error" : mapWooCommerceCode(apiCode);
595
620
  const wcError = new WooCommerceApiError({
596
621
  message: wpMessage,
597
622
  statusCode: status,
@@ -599,9 +624,6 @@ var setupErrorInterceptor = (axiosInstance, config, client) => {
599
624
  // Transformed app-level code
600
625
  originalCode: apiCode,
601
626
  // Original WooCommerce API code
602
- retryable: errorMetadata.retryable,
603
- userMessage: errorMetadata.userMessage,
604
- operation: errorMetadata.operation,
605
627
  url: axiosError.config?.url || "",
606
628
  method: axiosError.config?.method?.toUpperCase() || "GET",
607
629
  requestBody: axiosError.config?.data,
@@ -1246,11 +1268,13 @@ var checkoutSchema = zod.z.object({
1246
1268
  billing_address: billingAddressSchemaNonMandatory,
1247
1269
  shipping_address: addressSchemaNonMandatory,
1248
1270
  payment_method: zod.z.string(),
1271
+ // Null until an order is placed.
1272
+ // GET /checkout returns null here, so the schema must accept it.
1249
1273
  payment_result: zod.z.object({
1250
1274
  payment_status: zod.z.string(),
1251
1275
  payment_details: zod.z.array(zod.z.unknown()),
1252
1276
  redirect_url: zod.z.string()
1253
- })
1277
+ }).nullable()
1254
1278
  });
1255
1279
  zod.z.object({
1256
1280
  payment_method: zod.z.string(),
@@ -1352,6 +1376,14 @@ var productSchema = zod.z.object({
1352
1376
  review_count: zod.z.number(),
1353
1377
  extensions: zod.z.record(zod.z.string(), zod.z.unknown())
1354
1378
  });
1379
+ var productTagSchema = zod.z.object({
1380
+ /** Tag unique identifier */
1381
+ id: zod.z.number(),
1382
+ /** Tag display name, e.g. "Vichy decembar 2025" */
1383
+ name: zod.z.string(),
1384
+ /** URL-friendly tag identifier */
1385
+ slug: zod.z.string()
1386
+ });
1355
1387
  zod.z.object({
1356
1388
  /** Attribution source type (e.g., 'mobile_app', 'organic', 'referral') */
1357
1389
  source_type: zod.z.string(),
@@ -1497,6 +1529,14 @@ var createProductsAPI = (client, endpoints, options) => ({
1497
1529
  categories: async () => {
1498
1530
  const response = await client.get(endpoints.categories);
1499
1531
  return handleApiResponse(response, zod.z.array(productCategorySchema), options);
1532
+ },
1533
+ // Tags, e.g. GET /products/tags?include=996,683.
1534
+ // The Store API wants include as a comma list, so join the ids.
1535
+ tags: async (params) => {
1536
+ const response = await client.get(endpoints.tags, {
1537
+ params: { include: params?.include?.join(",") }
1538
+ });
1539
+ return handleApiResponse(response, zod.z.array(productTagSchema), options);
1500
1540
  }
1501
1541
  });
1502
1542
 
@@ -1507,6 +1547,8 @@ var createEndpoints = (version = "legacy") => {
1507
1547
  // Products
1508
1548
  products: `${prefix}/products`,
1509
1549
  categories: `${prefix}/products/categories`,
1550
+ // Product tags, e.g. to turn a tag id into its name
1551
+ tags: `${prefix}/products/tags`,
1510
1552
  // Cart
1511
1553
  cart: `${prefix}/cart`,
1512
1554
  addItem: `${prefix}/cart/add-item`,