@atomic-solutions/wordpress-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 (45) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +295 -0
  3. package/dist/calculatePagination-WUlN3OdM.d.cts +19 -0
  4. package/dist/calculatePagination-WUlN3OdM.d.ts +19 -0
  5. package/dist/client/index.cjs +1143 -0
  6. package/dist/client/index.cjs.map +1 -0
  7. package/dist/client/index.d.cts +10 -0
  8. package/dist/client/index.d.ts +10 -0
  9. package/dist/client/index.js +1137 -0
  10. package/dist/client/index.js.map +1 -0
  11. package/dist/http/index.cjs +674 -0
  12. package/dist/http/index.cjs.map +1 -0
  13. package/dist/http/index.d.cts +12 -0
  14. package/dist/http/index.d.ts +12 -0
  15. package/dist/http/index.js +667 -0
  16. package/dist/http/index.js.map +1 -0
  17. package/dist/index.cjs +1251 -0
  18. package/dist/index.cjs.map +1 -0
  19. package/dist/index.d.cts +51 -0
  20. package/dist/index.d.ts +51 -0
  21. package/dist/index.js +1205 -0
  22. package/dist/index.js.map +1 -0
  23. package/dist/schemas/index.cjs +414 -0
  24. package/dist/schemas/index.cjs.map +1 -0
  25. package/dist/schemas/index.d.cts +1339 -0
  26. package/dist/schemas/index.d.ts +1339 -0
  27. package/dist/schemas/index.js +376 -0
  28. package/dist/schemas/index.js.map +1 -0
  29. package/dist/testing/index.cjs +1435 -0
  30. package/dist/testing/index.cjs.map +1 -0
  31. package/dist/testing/index.d.cts +50 -0
  32. package/dist/testing/index.d.ts +50 -0
  33. package/dist/testing/index.js +1426 -0
  34. package/dist/testing/index.js.map +1 -0
  35. package/dist/types-8pbwmNdu.d.ts +154 -0
  36. package/dist/types-BTo088EY.d.cts +154 -0
  37. package/dist/user.schema-eeUHQ4sI.d.cts +10614 -0
  38. package/dist/user.schema-eeUHQ4sI.d.ts +10614 -0
  39. package/dist/utils/index.cjs +47 -0
  40. package/dist/utils/index.cjs.map +1 -0
  41. package/dist/utils/index.d.cts +10 -0
  42. package/dist/utils/index.d.ts +10 -0
  43. package/dist/utils/index.js +44 -0
  44. package/dist/utils/index.js.map +1 -0
  45. package/package.json +103 -0
@@ -0,0 +1,674 @@
1
+ 'use strict';
2
+
3
+ var axios = require('axios');
4
+
5
+ // src/errors/classes/BaseError.ts
6
+ var BaseError = class extends Error {
7
+ constructor(options) {
8
+ super(options.message);
9
+ this.options = options;
10
+ Object.setPrototypeOf(this, new.target.prototype);
11
+ this.name = this.constructor.name;
12
+ if (Error.captureStackTrace) {
13
+ Error.captureStackTrace(this, this.constructor);
14
+ }
15
+ if (this.options.cause instanceof Error && this.options.cause.stack) {
16
+ this.stack = `${this.stack}
17
+ Caused by: ${this.options.cause.stack}`;
18
+ }
19
+ }
20
+ /**
21
+ * Get reportable data for error tracking/logging
22
+ * Subclasses should override this to add their own reportable fields
23
+ */
24
+ getReportableData() {
25
+ const { code, operation, userMessage, retryable } = this.options;
26
+ return {
27
+ code,
28
+ operation,
29
+ userMessage,
30
+ retryable
31
+ };
32
+ }
33
+ /**
34
+ * Custom JSON serialization
35
+ * Makes the error properly serializable for logging/reporting
36
+ */
37
+ toJSON() {
38
+ return {
39
+ ...this.getReportableData(),
40
+ name: this.name,
41
+ message: this.message,
42
+ stack: this.stack
43
+ };
44
+ }
45
+ };
46
+
47
+ // src/errors/mapping/auth.ts
48
+ var AUTH_CODE_TRANSFORMATIONS = {
49
+ // Authentication
50
+ jwt_auth_failed: "auth_failed",
51
+ jwt_auth_invalid_token: "token_expired",
52
+ jwt_auth_bad_request: "invalid_auth_request",
53
+ // Authorization
54
+ rest_forbidden: "forbidden",
55
+ rest_cannot_read: "cannot_read",
56
+ rest_cannot_edit: "cannot_edit",
57
+ rest_cannot_create: "cannot_create",
58
+ rest_cannot_delete: "cannot_delete"
59
+ };
60
+ var AUTH_ERROR_METADATA = {
61
+ // Authentication
62
+ auth_failed: {
63
+ operation: "authenticate",
64
+ retryable: false,
65
+ userMessage: "Invalid username or password"
66
+ },
67
+ token_expired: {
68
+ operation: "validate_token",
69
+ retryable: false,
70
+ userMessage: "Your session has expired. Please sign in again."
71
+ },
72
+ invalid_auth_request: {
73
+ operation: "authenticate",
74
+ retryable: false,
75
+ userMessage: "Invalid login request"
76
+ },
77
+ // Authorization
78
+ forbidden: {
79
+ operation: "authorize",
80
+ retryable: false,
81
+ userMessage: "You do not have permission to access this resource"
82
+ },
83
+ cannot_read: {
84
+ operation: "read_resource",
85
+ retryable: false,
86
+ userMessage: "You do not have permission to view this content"
87
+ },
88
+ cannot_edit: {
89
+ operation: "edit_resource",
90
+ retryable: false,
91
+ userMessage: "You do not have permission to edit this content"
92
+ },
93
+ cannot_create: {
94
+ operation: "create_resource",
95
+ retryable: false,
96
+ userMessage: "You do not have permission to create content"
97
+ },
98
+ cannot_delete: {
99
+ operation: "delete_resource",
100
+ retryable: false,
101
+ userMessage: "You do not have permission to delete this content"
102
+ }
103
+ };
104
+
105
+ // src/errors/mapping/resources.ts
106
+ var RESOURCE_CODE_TRANSFORMATIONS = {
107
+ rest_post_invalid_id: "post_not_found",
108
+ rest_term_invalid: "term_not_found",
109
+ rest_user_invalid_id: "user_not_found",
110
+ rest_no_route: "route_not_found"
111
+ };
112
+ var RESOURCE_ERROR_METADATA = {
113
+ post_not_found: {
114
+ operation: "get_post",
115
+ retryable: false,
116
+ userMessage: "Post not found"
117
+ },
118
+ term_not_found: {
119
+ operation: "get_term",
120
+ retryable: false,
121
+ userMessage: "Category or tag not found"
122
+ },
123
+ user_not_found: {
124
+ operation: "get_user",
125
+ retryable: false,
126
+ userMessage: "User not found"
127
+ },
128
+ route_not_found: {
129
+ operation: "api_request",
130
+ retryable: false,
131
+ userMessage: "The requested resource was not found"
132
+ }
133
+ };
134
+
135
+ // src/errors/mapping/server.ts
136
+ var SERVER_CODE_TRANSFORMATIONS = {
137
+ rest_cookie_invalid_nonce: "invalid_nonce",
138
+ internal_error: "server_error"
139
+ };
140
+ var SERVER_ERROR_METADATA = {
141
+ invalid_nonce: {
142
+ operation: "api_request",
143
+ retryable: true,
144
+ userMessage: "Session expired. Please try again."
145
+ },
146
+ server_error: {
147
+ operation: "api_request",
148
+ retryable: true,
149
+ userMessage: "An unexpected error occurred. Please try again."
150
+ },
151
+ unknown_error: {
152
+ operation: "api_request",
153
+ retryable: false,
154
+ userMessage: "An error occurred"
155
+ }
156
+ };
157
+
158
+ // src/errors/mapping/constants.ts
159
+ var VALIDATION_CODE_TRANSFORMATIONS = {
160
+ rest_invalid_param: "invalid_params",
161
+ rest_missing_callback_param: "missing_params"
162
+ };
163
+ var VALIDATION_ERROR_METADATA = {
164
+ invalid_params: {
165
+ operation: "api_request",
166
+ retryable: false,
167
+ userMessage: "Invalid request parameters"
168
+ },
169
+ missing_params: {
170
+ operation: "api_request",
171
+ retryable: false,
172
+ userMessage: "Missing required parameters"
173
+ }
174
+ };
175
+ var WORDPRESS_ERROR_METADATA = {
176
+ ...AUTH_ERROR_METADATA,
177
+ ...RESOURCE_ERROR_METADATA,
178
+ ...VALIDATION_ERROR_METADATA,
179
+ ...SERVER_ERROR_METADATA
180
+ };
181
+
182
+ // src/errors/mapping/index.ts
183
+ var CODE_TRANSFORMATIONS = {
184
+ ...AUTH_CODE_TRANSFORMATIONS,
185
+ ...RESOURCE_CODE_TRANSFORMATIONS,
186
+ ...VALIDATION_CODE_TRANSFORMATIONS,
187
+ ...SERVER_CODE_TRANSFORMATIONS
188
+ };
189
+ var ERROR_METADATA = {
190
+ ...AUTH_ERROR_METADATA,
191
+ ...RESOURCE_ERROR_METADATA,
192
+ ...VALIDATION_ERROR_METADATA,
193
+ ...SERVER_ERROR_METADATA
194
+ };
195
+ var mapWordPressCode = (apiCode, status) => {
196
+ const appCode = CODE_TRANSFORMATIONS[apiCode];
197
+ if (appCode) {
198
+ const metadata = ERROR_METADATA[appCode];
199
+ return {
200
+ code: appCode,
201
+ ...metadata
202
+ };
203
+ }
204
+ if (apiCode.includes("invalid_id") || apiCode.includes("not_found")) {
205
+ return {
206
+ code: "route_not_found",
207
+ ...ERROR_METADATA.route_not_found
208
+ };
209
+ }
210
+ if (apiCode.includes("cannot_") || apiCode.includes("forbidden")) {
211
+ return {
212
+ code: "forbidden",
213
+ ...ERROR_METADATA.forbidden
214
+ };
215
+ }
216
+ if (apiCode.includes("invalid_param") || apiCode.includes("missing_")) {
217
+ return {
218
+ code: "invalid_params",
219
+ ...ERROR_METADATA.invalid_params
220
+ };
221
+ }
222
+ if (apiCode.includes("nonce")) {
223
+ return {
224
+ code: "invalid_nonce",
225
+ ...ERROR_METADATA.invalid_nonce
226
+ };
227
+ }
228
+ if (status === 401) {
229
+ return {
230
+ code: "auth_failed",
231
+ retryable: false,
232
+ userMessage: "Authentication required"
233
+ };
234
+ }
235
+ if (status === 403) {
236
+ return {
237
+ code: "forbidden",
238
+ ...ERROR_METADATA.forbidden
239
+ };
240
+ }
241
+ if (status === 404) {
242
+ return {
243
+ code: "route_not_found",
244
+ ...ERROR_METADATA.route_not_found
245
+ };
246
+ }
247
+ if (status >= 500) {
248
+ return {
249
+ code: "server_error",
250
+ ...ERROR_METADATA.server_error
251
+ };
252
+ }
253
+ return {
254
+ code: "unknown_error",
255
+ ...ERROR_METADATA.unknown_error
256
+ };
257
+ };
258
+
259
+ // src/errors/classes/WordPressApiError.ts
260
+ var WordPressApiError = class extends BaseError {
261
+ constructor(options) {
262
+ const metadata = WORDPRESS_ERROR_METADATA[options.code] ?? {
263
+ operation: "wordpress_api_request",
264
+ retryable: false
265
+ };
266
+ const enrichedOptions = {
267
+ ...options,
268
+ operation: options.operation || metadata.operation,
269
+ userMessage: options.userMessage || metadata.userMessage,
270
+ retryable: options.retryable ?? metadata.retryable
271
+ };
272
+ super(enrichedOptions);
273
+ }
274
+ // Convenient getters for frequently accessed fields (no duplication)
275
+ get wpCode() {
276
+ return this.options.code;
277
+ }
278
+ get originalCode() {
279
+ return this.options.originalCode;
280
+ }
281
+ get statusCode() {
282
+ return this.options.statusCode;
283
+ }
284
+ get url() {
285
+ return this.options.url;
286
+ }
287
+ get method() {
288
+ return this.options.method;
289
+ }
290
+ get requestBody() {
291
+ return this.options.requestBody;
292
+ }
293
+ get responseBody() {
294
+ return this.options.responseBody;
295
+ }
296
+ get data() {
297
+ return this.options.data;
298
+ }
299
+ /**
300
+ * Check if error is an authentication error
301
+ */
302
+ get isAuthError() {
303
+ return this.statusCode === 401 || this.wpCode === "auth_failed" || this.wpCode === "token_expired" || this.wpCode === "invalid_auth_request";
304
+ }
305
+ /**
306
+ * Check if error is a not found error
307
+ */
308
+ get isNotFoundError() {
309
+ return this.statusCode === 404 || this.wpCode === "route_not_found" || this.wpCode === "post_not_found" || this.wpCode === "term_not_found" || this.wpCode === "user_not_found";
310
+ }
311
+ /**
312
+ * Get reportable data for error tracking/logging
313
+ * Includes HTTP context and WordPress-specific fields
314
+ */
315
+ getReportableData() {
316
+ return {
317
+ code: this.options.code,
318
+ operation: this.options.operation,
319
+ userMessage: this.options.userMessage,
320
+ retryable: this.options.retryable,
321
+ statusCode: this.options.statusCode,
322
+ url: this.options.url,
323
+ method: this.options.method,
324
+ wpCode: this.options.code,
325
+ originalCode: this.options.originalCode
326
+ };
327
+ }
328
+ };
329
+
330
+ // src/errors/classes/WordPressDataValidationError.ts
331
+ var WordPressDataValidationError = class _WordPressDataValidationError extends BaseError {
332
+ constructor(options) {
333
+ const fieldErrors = options.zodError ? _WordPressDataValidationError.extractFieldErrorsFromZod(options.zodError) : void 0;
334
+ super({
335
+ code: "validation_error",
336
+ message: options.message,
337
+ operation: options.operation,
338
+ userMessage: options.userMessage || "Received unexpected data from WordPress",
339
+ retryable: false,
340
+ cause: options.cause,
341
+ url: options.url,
342
+ zodError: options.zodError,
343
+ value: options.value,
344
+ fieldErrors
345
+ });
346
+ }
347
+ // Convenient getters for validation-specific fields (no duplication)
348
+ get url() {
349
+ return this.options.url;
350
+ }
351
+ get zodError() {
352
+ return this.options.zodError;
353
+ }
354
+ get invalidValue() {
355
+ return this.options.value;
356
+ }
357
+ get fieldErrors() {
358
+ return this.options.fieldErrors;
359
+ }
360
+ /**
361
+ * Get reportable data for error tracking/logging
362
+ * Includes validation-specific fields
363
+ */
364
+ getReportableData() {
365
+ return {
366
+ code: this.options.code,
367
+ operation: this.options.operation,
368
+ userMessage: this.options.userMessage,
369
+ retryable: false,
370
+ fieldErrors: this.options.fieldErrors,
371
+ url: this.options.url
372
+ };
373
+ }
374
+ /**
375
+ * Extract field errors from Zod error
376
+ */
377
+ static extractFieldErrorsFromZod(zodError) {
378
+ if (!zodError?.errors) {
379
+ return void 0;
380
+ }
381
+ const fieldErrors = {};
382
+ for (const issue of zodError.errors) {
383
+ const path = issue.path.join(".");
384
+ if (!fieldErrors[path]) {
385
+ fieldErrors[path] = [];
386
+ }
387
+ fieldErrors[path].push(issue.message);
388
+ }
389
+ return fieldErrors;
390
+ }
391
+ /**
392
+ * Get all error messages as a flat array
393
+ */
394
+ getMessages() {
395
+ if (!this.fieldErrors) {
396
+ return [this.message];
397
+ }
398
+ const messages = [];
399
+ for (const fieldMessages of Object.values(this.fieldErrors)) {
400
+ messages.push(...fieldMessages);
401
+ }
402
+ return messages;
403
+ }
404
+ /**
405
+ * Check if a specific field has errors
406
+ */
407
+ hasFieldError(field) {
408
+ return this.fieldErrors ? field in this.fieldErrors : false;
409
+ }
410
+ /**
411
+ * Get error messages for a specific field
412
+ */
413
+ getFieldError(field) {
414
+ return this.fieldErrors?.[field];
415
+ }
416
+ };
417
+
418
+ // src/http/handleApiResponse.ts
419
+ var handleApiResponse = (response, schema, options) => {
420
+ const validationMode = options?.validationMode ?? "strict";
421
+ if (validationMode === "warn") {
422
+ const result = schema.safeParse(response.data);
423
+ if (!result.success) {
424
+ const validationError = new WordPressDataValidationError({
425
+ message: `WordPress API response validation failed for ${response.config.url || "unknown endpoint"}`,
426
+ url: response.config.url,
427
+ zodError: result.error,
428
+ value: response.data,
429
+ userMessage: "Received unexpected data from WordPress"
430
+ });
431
+ options?.onValidationError?.(validationError);
432
+ options?.errorReporter?.(validationError);
433
+ console.warn("[wordpress-utils] Validation warning:", validationError.message);
434
+ return response.data;
435
+ }
436
+ return result.data;
437
+ }
438
+ try {
439
+ return schema.parse(response.data);
440
+ } catch (error2) {
441
+ if (error2 && typeof error2 === "object" && "issues" in error2) {
442
+ const validationError = new WordPressDataValidationError({
443
+ message: `WordPress API response validation failed for ${response.config.url || "unknown endpoint"}`,
444
+ url: response.config.url,
445
+ zodError: error2,
446
+ value: response.data,
447
+ userMessage: "Received unexpected data from WordPress"
448
+ });
449
+ options?.onValidationError?.(validationError);
450
+ options?.errorReporter?.(validationError);
451
+ throw validationError;
452
+ }
453
+ throw error2;
454
+ }
455
+ };
456
+ var isZodError = (error2) => {
457
+ return error2 instanceof Error && error2.name === "ZodError";
458
+ };
459
+
460
+ // src/utils/calculatePagination.ts
461
+ var calculatePagination = (input) => {
462
+ const { page, perPage, total, totalPages } = input;
463
+ const hasNextPage = page < totalPages;
464
+ const hasPrevPage = page > 1;
465
+ return {
466
+ page,
467
+ perPage,
468
+ total,
469
+ totalPages,
470
+ hasNextPage,
471
+ hasPrevPage,
472
+ nextPage: hasNextPage ? page + 1 : null,
473
+ prevPage: hasPrevPage ? page - 1 : null
474
+ };
475
+ };
476
+
477
+ // src/utils/getPaginationMeta.ts
478
+ var getPaginationMeta = (headers) => {
479
+ if (!headers) {
480
+ return {
481
+ total: 0,
482
+ totalPages: 0
483
+ };
484
+ }
485
+ const wpTotal = headers["x-wp-total"];
486
+ const wpTotalPages = headers["x-wp-totalpages"];
487
+ if (!wpTotal || !wpTotalPages) {
488
+ return {
489
+ total: 0,
490
+ totalPages: 0
491
+ };
492
+ }
493
+ const total = parseInt(wpTotal, 10);
494
+ const totalPages = parseInt(wpTotalPages, 10);
495
+ return {
496
+ total,
497
+ totalPages
498
+ };
499
+ };
500
+
501
+ // src/http/handlePaginatedApiResponse.ts
502
+ var handlePaginatedApiResponse = (response, schema, params = {}, options) => {
503
+ const validationMode = options?.validationMode ?? "strict";
504
+ let data;
505
+ if (validationMode === "warn") {
506
+ const result = schema.safeParse(response.data);
507
+ if (!result.success) {
508
+ const validationError = new WordPressDataValidationError({
509
+ message: `WordPress API response validation failed for ${response.config.url || "unknown endpoint"}`,
510
+ url: response.config.url,
511
+ zodError: result.error,
512
+ value: response.data,
513
+ userMessage: "Received unexpected data from WordPress"
514
+ });
515
+ options?.onValidationError?.(validationError);
516
+ options?.errorReporter?.(validationError);
517
+ console.warn("[wordpress-utils] Validation warning:", validationError.message);
518
+ data = Array.isArray(response.data) ? response.data : [];
519
+ } else {
520
+ data = result.data;
521
+ }
522
+ } else {
523
+ try {
524
+ data = schema.parse(response.data);
525
+ } catch (error2) {
526
+ if (error2 && typeof error2 === "object" && "issues" in error2) {
527
+ const validationError = new WordPressDataValidationError({
528
+ message: `WordPress API response validation failed for ${response.config.url || "unknown endpoint"}`,
529
+ url: response.config.url,
530
+ zodError: error2,
531
+ value: response.data,
532
+ userMessage: "Received unexpected data from WordPress"
533
+ });
534
+ options?.onValidationError?.(validationError);
535
+ options?.errorReporter?.(validationError);
536
+ throw validationError;
537
+ }
538
+ throw error2;
539
+ }
540
+ }
541
+ const { total, totalPages } = getPaginationMeta(response.headers);
542
+ const page = params.page || 1;
543
+ const perPage = params.per_page || 10;
544
+ const pagination = calculatePagination({
545
+ page,
546
+ perPage,
547
+ total,
548
+ totalPages
549
+ });
550
+ return {
551
+ data,
552
+ pagination
553
+ };
554
+ };
555
+
556
+ // src/logging/logger.ts
557
+ var DEBUG_PREFIX = "[wordpress-utils]";
558
+ var isDebugEnabled = () => {
559
+ if (typeof process !== "undefined" && process.env) {
560
+ return process.env.DEBUG === "true" || process.env.DEBUG === "1";
561
+ }
562
+ return false;
563
+ };
564
+ var debug = (...args) => {
565
+ if (isDebugEnabled()) {
566
+ console.log(DEBUG_PREFIX, ...args);
567
+ }
568
+ };
569
+ var error = (...args) => {
570
+ console.error(DEBUG_PREFIX, ...args);
571
+ };
572
+
573
+ // src/http/interceptors/request.ts
574
+ var setupRequestInterceptor = (axiosInstance, config, client) => {
575
+ axiosInstance.interceptors.request.use(
576
+ async (requestConfig) => {
577
+ if (config.jwtToken) {
578
+ const token = await config.jwtToken.get();
579
+ if (token) {
580
+ requestConfig.headers.Authorization = `Bearer ${token}`;
581
+ debug("JWT token injected");
582
+ }
583
+ }
584
+ if (config.onRequest) {
585
+ return config.onRequest(requestConfig, client);
586
+ }
587
+ return requestConfig;
588
+ },
589
+ (error2) => {
590
+ return Promise.reject(error2);
591
+ }
592
+ );
593
+ };
594
+
595
+ // src/http/interceptors/response.ts
596
+ var setupResponseInterceptor = (axiosInstance, config, client) => {
597
+ axiosInstance.interceptors.response.use(
598
+ async (response) => {
599
+ debug("Response:", response.config.url, response.status);
600
+ if (config.onResponse) {
601
+ return config.onResponse(response, client);
602
+ }
603
+ return response;
604
+ },
605
+ (error2) => {
606
+ return Promise.reject(error2);
607
+ }
608
+ );
609
+ };
610
+ var setupErrorInterceptor = (axiosInstance, config, client) => {
611
+ axiosInstance.interceptors.response.use(
612
+ (response) => response,
613
+ async (axiosError) => {
614
+ const status = axiosError.response?.status || 500;
615
+ const errorData = axiosError.response?.data;
616
+ const apiCode = errorData?.code || "unknown_error";
617
+ const wpMessage = errorData?.message || axiosError.message || "Unknown error";
618
+ const mappedError = mapWordPressCode(apiCode, status);
619
+ const wpError = new WordPressApiError({
620
+ message: wpMessage,
621
+ statusCode: status,
622
+ code: mappedError.code,
623
+ // Transformed app-level code
624
+ originalCode: apiCode,
625
+ // Original WordPress API code
626
+ retryable: mappedError.retryable,
627
+ userMessage: mappedError.userMessage,
628
+ operation: axiosError.config?.url || "wordpress_api_request",
629
+ url: axiosError.config?.url || "",
630
+ method: axiosError.config?.method?.toUpperCase() || "GET",
631
+ requestBody: axiosError.config?.data,
632
+ responseBody: errorData,
633
+ cause: axiosError
634
+ });
635
+ if (config.debug) {
636
+ debug("WordPress error:", apiCode, status, wpMessage);
637
+ error(wpError);
638
+ }
639
+ if ((status === 401 || status === 403) && config.onAuthError && axios.isAxiosError(axiosError) && !axiosError.config?._retry) {
640
+ if (config.debug) {
641
+ debug("Auth error detected, calling onAuthError handler");
642
+ }
643
+ try {
644
+ const shouldRetry = await config.onAuthError(wpError, client);
645
+ if (shouldRetry) {
646
+ if (config.debug) {
647
+ debug("Retrying request after auth error");
648
+ }
649
+ axiosError.config._retry = true;
650
+ return axiosInstance.request(axiosError.config);
651
+ }
652
+ } catch (authErrorHandlerError) {
653
+ if (config.debug) {
654
+ debug("onAuthError handler threw error", authErrorHandlerError);
655
+ }
656
+ }
657
+ }
658
+ if (config.onError) {
659
+ config.onError(wpError, client);
660
+ }
661
+ config.errorReporter?.report(wpError);
662
+ return Promise.reject(wpError);
663
+ }
664
+ );
665
+ };
666
+
667
+ exports.handleApiResponse = handleApiResponse;
668
+ exports.handlePaginatedApiResponse = handlePaginatedApiResponse;
669
+ exports.isZodError = isZodError;
670
+ exports.setupErrorInterceptor = setupErrorInterceptor;
671
+ exports.setupRequestInterceptor = setupRequestInterceptor;
672
+ exports.setupResponseInterceptor = setupResponseInterceptor;
673
+ //# sourceMappingURL=index.cjs.map
674
+ //# sourceMappingURL=index.cjs.map