@szymonpiatek/nextwordpress 0.0.6 → 0.0.8

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.
@@ -17,16 +17,81 @@ function resolveBaseUrl(config) {
17
17
  return typeof window === "undefined" ? config.serverURL : config.clientURL;
18
18
  }
19
19
 
20
+ // src/shared/errors/codes.ts
21
+ var ErrorCode = {
22
+ // WordPress REST API
23
+ WP_REQUEST_FAILED: "WP_REQUEST_FAILED",
24
+ WP_NOT_FOUND: "WP_NOT_FOUND",
25
+ WP_UNAUTHORIZED: "WP_UNAUTHORIZED",
26
+ WP_FORBIDDEN: "WP_FORBIDDEN",
27
+ // WPGraphQL
28
+ GQL_REQUEST_FAILED: "GQL_REQUEST_FAILED",
29
+ GQL_NO_DATA: "GQL_NO_DATA",
30
+ GQL_ERRORS_IN_RESPONSE: "GQL_ERRORS_IN_RESPONSE",
31
+ // WooCommerce — generic
32
+ WOO_REQUEST_FAILED: "WOO_REQUEST_FAILED",
33
+ WOO_NOT_FOUND: "WOO_NOT_FOUND",
34
+ WOO_UNAUTHORIZED: "WOO_UNAUTHORIZED",
35
+ WOO_FORBIDDEN: "WOO_FORBIDDEN",
36
+ // WooCommerce — resource-specific
37
+ WOO_COUPON_NOT_FOUND: "WOO_COUPON_NOT_FOUND",
38
+ WOO_PRODUCT_NOT_FOUND: "WOO_PRODUCT_NOT_FOUND",
39
+ WOO_ORDER_NOT_FOUND: "WOO_ORDER_NOT_FOUND",
40
+ WOO_CUSTOMER_NOT_FOUND: "WOO_CUSTOMER_NOT_FOUND",
41
+ WOO_CATEGORY_NOT_FOUND: "WOO_CATEGORY_NOT_FOUND",
42
+ WOO_REVIEW_NOT_FOUND: "WOO_REVIEW_NOT_FOUND",
43
+ WOO_WEBHOOK_NOT_FOUND: "WOO_WEBHOOK_NOT_FOUND",
44
+ WOO_SHIPPING_ZONE_NOT_FOUND: "WOO_SHIPPING_ZONE_NOT_FOUND",
45
+ // Auth
46
+ AUTH_JWT_FAILED: "AUTH_JWT_FAILED",
47
+ AUTH_CREDENTIALS_INVALID: "AUTH_CREDENTIALS_INVALID",
48
+ AUTH_TOKEN_INVALID: "AUTH_TOKEN_INVALID"
49
+ };
50
+ var defaultMessages = {
51
+ WP_REQUEST_FAILED: "WordPress API request failed",
52
+ WP_NOT_FOUND: "WordPress resource not found",
53
+ WP_UNAUTHORIZED: "WordPress authentication required",
54
+ WP_FORBIDDEN: "WordPress access denied",
55
+ GQL_REQUEST_FAILED: "WPGraphQL request failed",
56
+ GQL_NO_DATA: "No data returned from WPGraphQL",
57
+ GQL_ERRORS_IN_RESPONSE: "WPGraphQL returned errors",
58
+ WOO_REQUEST_FAILED: "WooCommerce API request failed",
59
+ WOO_NOT_FOUND: "WooCommerce resource not found",
60
+ WOO_UNAUTHORIZED: "WooCommerce authentication required",
61
+ WOO_FORBIDDEN: "WooCommerce access denied",
62
+ WOO_COUPON_NOT_FOUND: "Coupon not found",
63
+ WOO_PRODUCT_NOT_FOUND: "Product not found",
64
+ WOO_ORDER_NOT_FOUND: "Order not found",
65
+ WOO_CUSTOMER_NOT_FOUND: "Customer not found",
66
+ WOO_CATEGORY_NOT_FOUND: "Category not found",
67
+ WOO_TAG_NOT_FOUND: "Tag not found",
68
+ WOO_REVIEW_NOT_FOUND: "Product review not found",
69
+ WOO_WEBHOOK_NOT_FOUND: "Webhook not found",
70
+ WOO_SHIPPING_ZONE_NOT_FOUND: "Shipping zone not found",
71
+ AUTH_JWT_FAILED: "JWT authentication failed",
72
+ AUTH_CREDENTIALS_INVALID: "Invalid credentials",
73
+ AUTH_TOKEN_INVALID: "JWT token is invalid or expired"
74
+ };
75
+ function resolveMessage(code, overrides) {
76
+ return overrides?.[code] ?? defaultMessages[code];
77
+ }
78
+
20
79
  // src/auth/types.ts
21
80
  var AuthenticationError = class extends Error {
22
- constructor(message, status) {
23
- super(message);
81
+ constructor(code, status, message, detail) {
82
+ super(detail ? `${message}: ${detail}` : message);
83
+ __publicField(this, "code", code);
24
84
  __publicField(this, "status", status);
25
85
  this.name = "AuthenticationError";
26
86
  }
27
87
  };
28
88
 
29
89
  // src/auth/jwt.ts
90
+ function resolveAuthErrorCode(status) {
91
+ if (status === 401) return ErrorCode.AUTH_CREDENTIALS_INVALID;
92
+ if (status === 403) return ErrorCode.AUTH_TOKEN_INVALID;
93
+ return ErrorCode.AUTH_JWT_FAILED;
94
+ }
30
95
  async function authenticateJwt(config, credentials) {
31
96
  const url = `${resolveBaseUrl(config)}/wp-json/jwt-auth/v1/token`;
32
97
  const response = await fetch(url, {
@@ -36,9 +101,12 @@ async function authenticateJwt(config, credentials) {
36
101
  cache: "no-store"
37
102
  });
38
103
  if (!response.ok) {
104
+ const code = resolveAuthErrorCode(response.status);
39
105
  throw new AuthenticationError(
40
- `JWT authentication failed: ${response.statusText}`,
41
- response.status
106
+ code,
107
+ response.status,
108
+ resolveMessage(code, config.errorMessages),
109
+ response.statusText
42
110
  );
43
111
  }
44
112
  return response.json();
@@ -118,8 +186,9 @@ function useAuth() {
118
186
 
119
187
  // src/integrations/restApi/core/client/types.ts
120
188
  var WordPressAPIError = class extends Error {
121
- constructor(message, status, endpoint) {
122
- super(message);
189
+ constructor(code, status, endpoint, message, detail) {
190
+ super(detail ? `${message}: ${detail}` : message);
191
+ __publicField(this, "code", code);
123
192
  __publicField(this, "status", status);
124
193
  __publicField(this, "endpoint", endpoint);
125
194
  this.name = "WordPressAPIError";
@@ -142,19 +211,28 @@ function buildUrl(config, path, query) {
142
211
 
143
212
  // src/integrations/restApi/core/client/fetcher.ts
144
213
  var USER_AGENT = "NextWordpress Client";
145
- async function doFetch(url, init = {}) {
146
- const response = await fetch(url, init);
147
- if (!response.ok) {
148
- throw new WordPressAPIError(
149
- `WordPress API request failed: ${response.statusText}`,
150
- response.status,
151
- url
152
- );
153
- }
154
- return response;
214
+ function resolveWpErrorCode(status) {
215
+ if (status === 401) return ErrorCode.WP_UNAUTHORIZED;
216
+ if (status === 403) return ErrorCode.WP_FORBIDDEN;
217
+ if (status === 404) return ErrorCode.WP_NOT_FOUND;
218
+ return ErrorCode.WP_REQUEST_FAILED;
155
219
  }
156
220
  function createFetcher(config) {
157
221
  const cacheTtl = config.cacheTTL ?? 300;
222
+ async function doFetch(url, init = {}) {
223
+ const response = await fetch(url, init);
224
+ if (!response.ok) {
225
+ const code = resolveWpErrorCode(response.status);
226
+ throw new WordPressAPIError(
227
+ code,
228
+ response.status,
229
+ url,
230
+ resolveMessage(code, config.errorMessages),
231
+ response.statusText
232
+ );
233
+ }
234
+ return response;
235
+ }
158
236
  async function wpFetch(path, query, tags = ["wordpress"]) {
159
237
  const url = buildUrl(config, path, query);
160
238
  const res = await doFetch(url, {
@@ -403,6 +481,42 @@ function usePostsPaginated(config, page = 1, perPage = 9, filter, swrOptions) {
403
481
  );
404
482
  }
405
483
 
484
+ // src/integrations/restApi/woocommerce/client/types.ts
485
+ var WooCommerceError = class extends Error {
486
+ constructor(code, status, endpoint, message, upstreamCode, detail) {
487
+ super(detail ? `${message}: ${detail}` : message);
488
+ __publicField(this, "code", code);
489
+ __publicField(this, "status", status);
490
+ __publicField(this, "endpoint", endpoint);
491
+ __publicField(this, "upstreamCode", upstreamCode);
492
+ this.name = "WooCommerceError";
493
+ }
494
+ };
495
+
496
+ // src/integrations/restApi/woocommerce/client/errorMap.ts
497
+ var upstreamToErrorCode = {
498
+ woocommerce_rest_coupon_invalid_id: ErrorCode.WOO_COUPON_NOT_FOUND,
499
+ woocommerce_rest_product_invalid_id: ErrorCode.WOO_PRODUCT_NOT_FOUND,
500
+ woocommerce_rest_order_invalid_id: ErrorCode.WOO_ORDER_NOT_FOUND,
501
+ woocommerce_rest_customer_invalid_id: ErrorCode.WOO_CUSTOMER_NOT_FOUND,
502
+ woocommerce_rest_term_invalid: ErrorCode.WOO_CATEGORY_NOT_FOUND,
503
+ woocommerce_rest_review_invalid_id: ErrorCode.WOO_REVIEW_NOT_FOUND,
504
+ woocommerce_rest_webhook_invalid_id: ErrorCode.WOO_WEBHOOK_NOT_FOUND,
505
+ woocommerce_rest_shipping_zone_invalid_id: ErrorCode.WOO_SHIPPING_ZONE_NOT_FOUND
506
+ };
507
+ function resolveWooErrorCode(status, upstreamCode) {
508
+ if (status === 401) return ErrorCode.WOO_UNAUTHORIZED;
509
+ if (status === 403) return ErrorCode.WOO_FORBIDDEN;
510
+ if (status === 404) {
511
+ if (upstreamCode) {
512
+ const mapped = upstreamToErrorCode[upstreamCode];
513
+ if (mapped) return mapped;
514
+ }
515
+ return ErrorCode.WOO_NOT_FOUND;
516
+ }
517
+ return ErrorCode.WOO_REQUEST_FAILED;
518
+ }
519
+
406
520
  // src/integrations/restApi/woocommerce/client/fetcher.ts
407
521
  var USER_AGENT2 = "NextWordpress WooCommerce Client";
408
522
  var DEFAULT_CACHE_TTL = 3600;
@@ -420,7 +534,20 @@ function createWooCommerceFetcher(config) {
420
534
  return { ...query, consumer_key: config.consumerKey, consumer_secret: config.consumerSecret };
421
535
  }
422
536
  function buildUrl2(path, query) {
423
- return `${config.serverURL}${path}?${toQueryString(withAuth(query))}`;
537
+ const base = typeof window !== "undefined" && config.clientURL ? config.clientURL : config.serverURL;
538
+ return `${base}${path}?${toQueryString(withAuth(query))}`;
539
+ }
540
+ async function throwWooError(response, url) {
541
+ const body = await response.json().catch(() => ({}));
542
+ const errorCode = resolveWooErrorCode(response.status, body.code);
543
+ throw new WooCommerceError(
544
+ errorCode,
545
+ response.status,
546
+ url,
547
+ resolveMessage(errorCode, config.errorMessages),
548
+ body.code,
549
+ body.message
550
+ );
424
551
  }
425
552
  async function wcFetch(path, query, tags = ["woocommerce"], options) {
426
553
  const url = buildUrl2(path, query);
@@ -436,9 +563,7 @@ function createWooCommerceFetcher(config) {
436
563
  next: isMutation || noStore ? void 0 : { tags, revalidate: cacheTTL }
437
564
  });
438
565
  if (!response.ok) {
439
- const body = await response.json().catch(() => ({}));
440
- const detail = body.message ? ` \u2013 ${body.message}` : "";
441
- throw new Error(`WooCommerce API ${response.status}: ${response.statusText}${detail} [${url}]`);
566
+ await throwWooError(response, url);
442
567
  }
443
568
  return response.json();
444
569
  }
@@ -457,7 +582,7 @@ function createWooCommerceFetcher(config) {
457
582
  next: { tags, revalidate: cacheTTL }
458
583
  });
459
584
  if (!response.ok) {
460
- throw new Error(`WooCommerce API ${response.status}: ${response.statusText} [${url}]`);
585
+ await throwWooError(response, url);
461
586
  }
462
587
  return {
463
588
  data: await response.json(),
@@ -923,8 +1048,9 @@ function useCustomer() {
923
1048
 
924
1049
  // src/integrations/wpGraphQL/client/types.ts
925
1050
  var WPGraphQLError = class extends Error {
926
- constructor(message, status, endpoint, gqlErrors) {
1051
+ constructor(code, status, endpoint, message, gqlErrors) {
927
1052
  super(message);
1053
+ __publicField(this, "code", code);
928
1054
  __publicField(this, "status", status);
929
1055
  __publicField(this, "endpoint", endpoint);
930
1056
  __publicField(this, "gqlErrors", gqlErrors);
@@ -949,22 +1075,29 @@ function createWPGraphQLFetcher(config) {
949
1075
  });
950
1076
  if (!response.ok) {
951
1077
  throw new WPGraphQLError(
952
- `WPGraphQL request failed: ${response.statusText}`,
1078
+ ErrorCode.GQL_REQUEST_FAILED,
953
1079
  response.status,
954
- url
1080
+ url,
1081
+ resolveMessage(ErrorCode.GQL_REQUEST_FAILED, config.errorMessages)
955
1082
  );
956
1083
  }
957
1084
  const parsed = await response.json();
958
1085
  if (parsed.errors && parsed.errors.length > 0) {
959
1086
  throw new WPGraphQLError(
960
- parsed.errors[0].message,
1087
+ ErrorCode.GQL_ERRORS_IN_RESPONSE,
961
1088
  200,
962
1089
  url,
1090
+ resolveMessage(ErrorCode.GQL_ERRORS_IN_RESPONSE, config.errorMessages),
963
1091
  parsed.errors
964
1092
  );
965
1093
  }
966
1094
  if (parsed.data === void 0) {
967
- throw new WPGraphQLError("No data returned from WPGraphQL", 200, url);
1095
+ throw new WPGraphQLError(
1096
+ ErrorCode.GQL_NO_DATA,
1097
+ 200,
1098
+ url,
1099
+ resolveMessage(ErrorCode.GQL_NO_DATA, config.errorMessages)
1100
+ );
968
1101
  }
969
1102
  return parsed.data;
970
1103
  }
@@ -992,22 +1125,29 @@ function createWPGraphQLFetcher(config) {
992
1125
  });
993
1126
  if (!response.ok) {
994
1127
  throw new WPGraphQLError(
995
- `WPGraphQL mutation failed: ${response.statusText}`,
1128
+ ErrorCode.GQL_REQUEST_FAILED,
996
1129
  response.status,
997
- url
1130
+ url,
1131
+ resolveMessage(ErrorCode.GQL_REQUEST_FAILED, config.errorMessages)
998
1132
  );
999
1133
  }
1000
1134
  const parsed = await response.json();
1001
1135
  if (parsed.errors && parsed.errors.length > 0) {
1002
1136
  throw new WPGraphQLError(
1003
- parsed.errors[0].message,
1137
+ ErrorCode.GQL_ERRORS_IN_RESPONSE,
1004
1138
  200,
1005
1139
  url,
1140
+ resolveMessage(ErrorCode.GQL_ERRORS_IN_RESPONSE, config.errorMessages),
1006
1141
  parsed.errors
1007
1142
  );
1008
1143
  }
1009
1144
  if (parsed.data === void 0) {
1010
- throw new WPGraphQLError("No data returned from WPGraphQL mutation", 200, url);
1145
+ throw new WPGraphQLError(
1146
+ ErrorCode.GQL_NO_DATA,
1147
+ 200,
1148
+ url,
1149
+ resolveMessage(ErrorCode.GQL_NO_DATA, config.errorMessages)
1150
+ );
1011
1151
  }
1012
1152
  return parsed.data;
1013
1153
  }