@szymonpiatek/nextwordpress 0.0.7 → 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.
@@ -11,16 +11,81 @@ function resolveBaseUrl(config) {
11
11
  return typeof window === "undefined" ? config.serverURL : config.clientURL;
12
12
  }
13
13
 
14
+ // src/shared/errors/codes.ts
15
+ var ErrorCode = {
16
+ // WordPress REST API
17
+ WP_REQUEST_FAILED: "WP_REQUEST_FAILED",
18
+ WP_NOT_FOUND: "WP_NOT_FOUND",
19
+ WP_UNAUTHORIZED: "WP_UNAUTHORIZED",
20
+ WP_FORBIDDEN: "WP_FORBIDDEN",
21
+ // WPGraphQL
22
+ GQL_REQUEST_FAILED: "GQL_REQUEST_FAILED",
23
+ GQL_NO_DATA: "GQL_NO_DATA",
24
+ GQL_ERRORS_IN_RESPONSE: "GQL_ERRORS_IN_RESPONSE",
25
+ // WooCommerce — generic
26
+ WOO_REQUEST_FAILED: "WOO_REQUEST_FAILED",
27
+ WOO_NOT_FOUND: "WOO_NOT_FOUND",
28
+ WOO_UNAUTHORIZED: "WOO_UNAUTHORIZED",
29
+ WOO_FORBIDDEN: "WOO_FORBIDDEN",
30
+ // WooCommerce — resource-specific
31
+ WOO_COUPON_NOT_FOUND: "WOO_COUPON_NOT_FOUND",
32
+ WOO_PRODUCT_NOT_FOUND: "WOO_PRODUCT_NOT_FOUND",
33
+ WOO_ORDER_NOT_FOUND: "WOO_ORDER_NOT_FOUND",
34
+ WOO_CUSTOMER_NOT_FOUND: "WOO_CUSTOMER_NOT_FOUND",
35
+ WOO_CATEGORY_NOT_FOUND: "WOO_CATEGORY_NOT_FOUND",
36
+ WOO_REVIEW_NOT_FOUND: "WOO_REVIEW_NOT_FOUND",
37
+ WOO_WEBHOOK_NOT_FOUND: "WOO_WEBHOOK_NOT_FOUND",
38
+ WOO_SHIPPING_ZONE_NOT_FOUND: "WOO_SHIPPING_ZONE_NOT_FOUND",
39
+ // Auth
40
+ AUTH_JWT_FAILED: "AUTH_JWT_FAILED",
41
+ AUTH_CREDENTIALS_INVALID: "AUTH_CREDENTIALS_INVALID",
42
+ AUTH_TOKEN_INVALID: "AUTH_TOKEN_INVALID"
43
+ };
44
+ var defaultMessages = {
45
+ WP_REQUEST_FAILED: "WordPress API request failed",
46
+ WP_NOT_FOUND: "WordPress resource not found",
47
+ WP_UNAUTHORIZED: "WordPress authentication required",
48
+ WP_FORBIDDEN: "WordPress access denied",
49
+ GQL_REQUEST_FAILED: "WPGraphQL request failed",
50
+ GQL_NO_DATA: "No data returned from WPGraphQL",
51
+ GQL_ERRORS_IN_RESPONSE: "WPGraphQL returned errors",
52
+ WOO_REQUEST_FAILED: "WooCommerce API request failed",
53
+ WOO_NOT_FOUND: "WooCommerce resource not found",
54
+ WOO_UNAUTHORIZED: "WooCommerce authentication required",
55
+ WOO_FORBIDDEN: "WooCommerce access denied",
56
+ WOO_COUPON_NOT_FOUND: "Coupon not found",
57
+ WOO_PRODUCT_NOT_FOUND: "Product not found",
58
+ WOO_ORDER_NOT_FOUND: "Order not found",
59
+ WOO_CUSTOMER_NOT_FOUND: "Customer not found",
60
+ WOO_CATEGORY_NOT_FOUND: "Category not found",
61
+ WOO_TAG_NOT_FOUND: "Tag not found",
62
+ WOO_REVIEW_NOT_FOUND: "Product review not found",
63
+ WOO_WEBHOOK_NOT_FOUND: "Webhook not found",
64
+ WOO_SHIPPING_ZONE_NOT_FOUND: "Shipping zone not found",
65
+ AUTH_JWT_FAILED: "JWT authentication failed",
66
+ AUTH_CREDENTIALS_INVALID: "Invalid credentials",
67
+ AUTH_TOKEN_INVALID: "JWT token is invalid or expired"
68
+ };
69
+ function resolveMessage(code, overrides) {
70
+ return overrides?.[code] ?? defaultMessages[code];
71
+ }
72
+
14
73
  // src/auth/types.ts
15
74
  var AuthenticationError = class extends Error {
16
- constructor(message, status) {
17
- super(message);
75
+ constructor(code, status, message, detail) {
76
+ super(detail ? `${message}: ${detail}` : message);
77
+ __publicField(this, "code", code);
18
78
  __publicField(this, "status", status);
19
79
  this.name = "AuthenticationError";
20
80
  }
21
81
  };
22
82
 
23
83
  // src/auth/jwt.ts
84
+ function resolveAuthErrorCode(status) {
85
+ if (status === 401) return ErrorCode.AUTH_CREDENTIALS_INVALID;
86
+ if (status === 403) return ErrorCode.AUTH_TOKEN_INVALID;
87
+ return ErrorCode.AUTH_JWT_FAILED;
88
+ }
24
89
  async function authenticateJwt(config, credentials) {
25
90
  const url = `${resolveBaseUrl(config)}/wp-json/jwt-auth/v1/token`;
26
91
  const response = await fetch(url, {
@@ -30,9 +95,12 @@ async function authenticateJwt(config, credentials) {
30
95
  cache: "no-store"
31
96
  });
32
97
  if (!response.ok) {
98
+ const code = resolveAuthErrorCode(response.status);
33
99
  throw new AuthenticationError(
34
- `JWT authentication failed: ${response.statusText}`,
35
- response.status
100
+ code,
101
+ response.status,
102
+ resolveMessage(code, config.errorMessages),
103
+ response.statusText
36
104
  );
37
105
  }
38
106
  return response.json();
@@ -112,8 +180,9 @@ function useAuth() {
112
180
 
113
181
  // src/integrations/restApi/core/client/types.ts
114
182
  var WordPressAPIError = class extends Error {
115
- constructor(message, status, endpoint) {
116
- super(message);
183
+ constructor(code, status, endpoint, message, detail) {
184
+ super(detail ? `${message}: ${detail}` : message);
185
+ __publicField(this, "code", code);
117
186
  __publicField(this, "status", status);
118
187
  __publicField(this, "endpoint", endpoint);
119
188
  this.name = "WordPressAPIError";
@@ -136,19 +205,28 @@ function buildUrl(config, path, query) {
136
205
 
137
206
  // src/integrations/restApi/core/client/fetcher.ts
138
207
  var USER_AGENT = "NextWordpress Client";
139
- async function doFetch(url, init = {}) {
140
- const response = await fetch(url, init);
141
- if (!response.ok) {
142
- throw new WordPressAPIError(
143
- `WordPress API request failed: ${response.statusText}`,
144
- response.status,
145
- url
146
- );
147
- }
148
- return response;
208
+ function resolveWpErrorCode(status) {
209
+ if (status === 401) return ErrorCode.WP_UNAUTHORIZED;
210
+ if (status === 403) return ErrorCode.WP_FORBIDDEN;
211
+ if (status === 404) return ErrorCode.WP_NOT_FOUND;
212
+ return ErrorCode.WP_REQUEST_FAILED;
149
213
  }
150
214
  function createFetcher(config) {
151
215
  const cacheTtl = config.cacheTTL ?? 300;
216
+ async function doFetch(url, init = {}) {
217
+ const response = await fetch(url, init);
218
+ if (!response.ok) {
219
+ const code = resolveWpErrorCode(response.status);
220
+ throw new WordPressAPIError(
221
+ code,
222
+ response.status,
223
+ url,
224
+ resolveMessage(code, config.errorMessages),
225
+ response.statusText
226
+ );
227
+ }
228
+ return response;
229
+ }
152
230
  async function wpFetch(path, query, tags = ["wordpress"]) {
153
231
  const url = buildUrl(config, path, query);
154
232
  const res = await doFetch(url, {
@@ -397,6 +475,42 @@ function usePostsPaginated(config, page = 1, perPage = 9, filter, swrOptions) {
397
475
  );
398
476
  }
399
477
 
478
+ // src/integrations/restApi/woocommerce/client/types.ts
479
+ var WooCommerceError = class extends Error {
480
+ constructor(code, status, endpoint, message, upstreamCode, detail) {
481
+ super(detail ? `${message}: ${detail}` : message);
482
+ __publicField(this, "code", code);
483
+ __publicField(this, "status", status);
484
+ __publicField(this, "endpoint", endpoint);
485
+ __publicField(this, "upstreamCode", upstreamCode);
486
+ this.name = "WooCommerceError";
487
+ }
488
+ };
489
+
490
+ // src/integrations/restApi/woocommerce/client/errorMap.ts
491
+ var upstreamToErrorCode = {
492
+ woocommerce_rest_coupon_invalid_id: ErrorCode.WOO_COUPON_NOT_FOUND,
493
+ woocommerce_rest_product_invalid_id: ErrorCode.WOO_PRODUCT_NOT_FOUND,
494
+ woocommerce_rest_order_invalid_id: ErrorCode.WOO_ORDER_NOT_FOUND,
495
+ woocommerce_rest_customer_invalid_id: ErrorCode.WOO_CUSTOMER_NOT_FOUND,
496
+ woocommerce_rest_term_invalid: ErrorCode.WOO_CATEGORY_NOT_FOUND,
497
+ woocommerce_rest_review_invalid_id: ErrorCode.WOO_REVIEW_NOT_FOUND,
498
+ woocommerce_rest_webhook_invalid_id: ErrorCode.WOO_WEBHOOK_NOT_FOUND,
499
+ woocommerce_rest_shipping_zone_invalid_id: ErrorCode.WOO_SHIPPING_ZONE_NOT_FOUND
500
+ };
501
+ function resolveWooErrorCode(status, upstreamCode) {
502
+ if (status === 401) return ErrorCode.WOO_UNAUTHORIZED;
503
+ if (status === 403) return ErrorCode.WOO_FORBIDDEN;
504
+ if (status === 404) {
505
+ if (upstreamCode) {
506
+ const mapped = upstreamToErrorCode[upstreamCode];
507
+ if (mapped) return mapped;
508
+ }
509
+ return ErrorCode.WOO_NOT_FOUND;
510
+ }
511
+ return ErrorCode.WOO_REQUEST_FAILED;
512
+ }
513
+
400
514
  // src/integrations/restApi/woocommerce/client/fetcher.ts
401
515
  var USER_AGENT2 = "NextWordpress WooCommerce Client";
402
516
  var DEFAULT_CACHE_TTL = 3600;
@@ -417,6 +531,18 @@ function createWooCommerceFetcher(config) {
417
531
  const base = typeof window !== "undefined" && config.clientURL ? config.clientURL : config.serverURL;
418
532
  return `${base}${path}?${toQueryString(withAuth(query))}`;
419
533
  }
534
+ async function throwWooError(response, url) {
535
+ const body = await response.json().catch(() => ({}));
536
+ const errorCode = resolveWooErrorCode(response.status, body.code);
537
+ throw new WooCommerceError(
538
+ errorCode,
539
+ response.status,
540
+ url,
541
+ resolveMessage(errorCode, config.errorMessages),
542
+ body.code,
543
+ body.message
544
+ );
545
+ }
420
546
  async function wcFetch(path, query, tags = ["woocommerce"], options) {
421
547
  const url = buildUrl2(path, query);
422
548
  const isMutation = options?.method && options.method !== "GET";
@@ -431,9 +557,7 @@ function createWooCommerceFetcher(config) {
431
557
  next: isMutation || noStore ? void 0 : { tags, revalidate: cacheTTL }
432
558
  });
433
559
  if (!response.ok) {
434
- const body = await response.json().catch(() => ({}));
435
- const detail = body.message ? ` \u2013 ${body.message}` : "";
436
- throw new Error(`WooCommerce API ${response.status}: ${response.statusText}${detail} [${url}]`);
560
+ await throwWooError(response, url);
437
561
  }
438
562
  return response.json();
439
563
  }
@@ -452,7 +576,7 @@ function createWooCommerceFetcher(config) {
452
576
  next: { tags, revalidate: cacheTTL }
453
577
  });
454
578
  if (!response.ok) {
455
- throw new Error(`WooCommerce API ${response.status}: ${response.statusText} [${url}]`);
579
+ await throwWooError(response, url);
456
580
  }
457
581
  return {
458
582
  data: await response.json(),
@@ -918,8 +1042,9 @@ function useCustomer() {
918
1042
 
919
1043
  // src/integrations/wpGraphQL/client/types.ts
920
1044
  var WPGraphQLError = class extends Error {
921
- constructor(message, status, endpoint, gqlErrors) {
1045
+ constructor(code, status, endpoint, message, gqlErrors) {
922
1046
  super(message);
1047
+ __publicField(this, "code", code);
923
1048
  __publicField(this, "status", status);
924
1049
  __publicField(this, "endpoint", endpoint);
925
1050
  __publicField(this, "gqlErrors", gqlErrors);
@@ -944,22 +1069,29 @@ function createWPGraphQLFetcher(config) {
944
1069
  });
945
1070
  if (!response.ok) {
946
1071
  throw new WPGraphQLError(
947
- `WPGraphQL request failed: ${response.statusText}`,
1072
+ ErrorCode.GQL_REQUEST_FAILED,
948
1073
  response.status,
949
- url
1074
+ url,
1075
+ resolveMessage(ErrorCode.GQL_REQUEST_FAILED, config.errorMessages)
950
1076
  );
951
1077
  }
952
1078
  const parsed = await response.json();
953
1079
  if (parsed.errors && parsed.errors.length > 0) {
954
1080
  throw new WPGraphQLError(
955
- parsed.errors[0].message,
1081
+ ErrorCode.GQL_ERRORS_IN_RESPONSE,
956
1082
  200,
957
1083
  url,
1084
+ resolveMessage(ErrorCode.GQL_ERRORS_IN_RESPONSE, config.errorMessages),
958
1085
  parsed.errors
959
1086
  );
960
1087
  }
961
1088
  if (parsed.data === void 0) {
962
- throw new WPGraphQLError("No data returned from WPGraphQL", 200, url);
1089
+ throw new WPGraphQLError(
1090
+ ErrorCode.GQL_NO_DATA,
1091
+ 200,
1092
+ url,
1093
+ resolveMessage(ErrorCode.GQL_NO_DATA, config.errorMessages)
1094
+ );
963
1095
  }
964
1096
  return parsed.data;
965
1097
  }
@@ -987,22 +1119,29 @@ function createWPGraphQLFetcher(config) {
987
1119
  });
988
1120
  if (!response.ok) {
989
1121
  throw new WPGraphQLError(
990
- `WPGraphQL mutation failed: ${response.statusText}`,
1122
+ ErrorCode.GQL_REQUEST_FAILED,
991
1123
  response.status,
992
- url
1124
+ url,
1125
+ resolveMessage(ErrorCode.GQL_REQUEST_FAILED, config.errorMessages)
993
1126
  );
994
1127
  }
995
1128
  const parsed = await response.json();
996
1129
  if (parsed.errors && parsed.errors.length > 0) {
997
1130
  throw new WPGraphQLError(
998
- parsed.errors[0].message,
1131
+ ErrorCode.GQL_ERRORS_IN_RESPONSE,
999
1132
  200,
1000
1133
  url,
1134
+ resolveMessage(ErrorCode.GQL_ERRORS_IN_RESPONSE, config.errorMessages),
1001
1135
  parsed.errors
1002
1136
  );
1003
1137
  }
1004
1138
  if (parsed.data === void 0) {
1005
- throw new WPGraphQLError("No data returned from WPGraphQL mutation", 200, url);
1139
+ throw new WPGraphQLError(
1140
+ ErrorCode.GQL_NO_DATA,
1141
+ 200,
1142
+ url,
1143
+ resolveMessage(ErrorCode.GQL_NO_DATA, config.errorMessages)
1144
+ );
1006
1145
  }
1007
1146
  return parsed.data;
1008
1147
  }