@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.
package/dist/index.cjs CHANGED
@@ -9,10 +9,96 @@ function resolveBaseUrl(config) {
9
9
  return typeof window === "undefined" ? config.serverURL : config.clientURL;
10
10
  }
11
11
 
12
+ // src/shared/errors/codes.ts
13
+ var ErrorCode = {
14
+ // WordPress REST API
15
+ WP_REQUEST_FAILED: "WP_REQUEST_FAILED",
16
+ WP_NOT_FOUND: "WP_NOT_FOUND",
17
+ WP_UNAUTHORIZED: "WP_UNAUTHORIZED",
18
+ WP_FORBIDDEN: "WP_FORBIDDEN",
19
+ // WPGraphQL
20
+ GQL_REQUEST_FAILED: "GQL_REQUEST_FAILED",
21
+ GQL_NO_DATA: "GQL_NO_DATA",
22
+ GQL_ERRORS_IN_RESPONSE: "GQL_ERRORS_IN_RESPONSE",
23
+ // WooCommerce — generic
24
+ WOO_REQUEST_FAILED: "WOO_REQUEST_FAILED",
25
+ WOO_NOT_FOUND: "WOO_NOT_FOUND",
26
+ WOO_UNAUTHORIZED: "WOO_UNAUTHORIZED",
27
+ WOO_FORBIDDEN: "WOO_FORBIDDEN",
28
+ // WooCommerce — resource-specific
29
+ WOO_COUPON_NOT_FOUND: "WOO_COUPON_NOT_FOUND",
30
+ WOO_PRODUCT_NOT_FOUND: "WOO_PRODUCT_NOT_FOUND",
31
+ WOO_ORDER_NOT_FOUND: "WOO_ORDER_NOT_FOUND",
32
+ WOO_CUSTOMER_NOT_FOUND: "WOO_CUSTOMER_NOT_FOUND",
33
+ WOO_CATEGORY_NOT_FOUND: "WOO_CATEGORY_NOT_FOUND",
34
+ WOO_TAG_NOT_FOUND: "WOO_TAG_NOT_FOUND",
35
+ WOO_REVIEW_NOT_FOUND: "WOO_REVIEW_NOT_FOUND",
36
+ WOO_WEBHOOK_NOT_FOUND: "WOO_WEBHOOK_NOT_FOUND",
37
+ WOO_SHIPPING_ZONE_NOT_FOUND: "WOO_SHIPPING_ZONE_NOT_FOUND",
38
+ // Auth
39
+ AUTH_JWT_FAILED: "AUTH_JWT_FAILED",
40
+ AUTH_CREDENTIALS_INVALID: "AUTH_CREDENTIALS_INVALID",
41
+ AUTH_TOKEN_INVALID: "AUTH_TOKEN_INVALID"
42
+ };
43
+ var defaultMessages = {
44
+ WP_REQUEST_FAILED: "WordPress API request failed",
45
+ WP_NOT_FOUND: "WordPress resource not found",
46
+ WP_UNAUTHORIZED: "WordPress authentication required",
47
+ WP_FORBIDDEN: "WordPress access denied",
48
+ GQL_REQUEST_FAILED: "WPGraphQL request failed",
49
+ GQL_NO_DATA: "No data returned from WPGraphQL",
50
+ GQL_ERRORS_IN_RESPONSE: "WPGraphQL returned errors",
51
+ WOO_REQUEST_FAILED: "WooCommerce API request failed",
52
+ WOO_NOT_FOUND: "WooCommerce resource not found",
53
+ WOO_UNAUTHORIZED: "WooCommerce authentication required",
54
+ WOO_FORBIDDEN: "WooCommerce access denied",
55
+ WOO_COUPON_NOT_FOUND: "Coupon not found",
56
+ WOO_PRODUCT_NOT_FOUND: "Product not found",
57
+ WOO_ORDER_NOT_FOUND: "Order not found",
58
+ WOO_CUSTOMER_NOT_FOUND: "Customer not found",
59
+ WOO_CATEGORY_NOT_FOUND: "Category not found",
60
+ WOO_TAG_NOT_FOUND: "Tag not found",
61
+ WOO_REVIEW_NOT_FOUND: "Product review not found",
62
+ WOO_WEBHOOK_NOT_FOUND: "Webhook not found",
63
+ WOO_SHIPPING_ZONE_NOT_FOUND: "Shipping zone not found",
64
+ AUTH_JWT_FAILED: "JWT authentication failed",
65
+ AUTH_CREDENTIALS_INVALID: "Invalid credentials",
66
+ AUTH_TOKEN_INVALID: "JWT token is invalid or expired"
67
+ };
68
+ var defaultMessagesPl = {
69
+ WP_REQUEST_FAILED: "B\u0142\u0105d \u017C\u0105dania do WordPress API",
70
+ WP_NOT_FOUND: "Zas\xF3b WordPress nie istnieje",
71
+ WP_UNAUTHORIZED: "Wymagane uwierzytelnienie WordPress",
72
+ WP_FORBIDDEN: "Brak dost\u0119pu do zasobu WordPress",
73
+ GQL_REQUEST_FAILED: "B\u0142\u0105d \u017C\u0105dania WPGraphQL",
74
+ GQL_NO_DATA: "WPGraphQL nie zwr\xF3ci\u0142o danych",
75
+ GQL_ERRORS_IN_RESPONSE: "WPGraphQL zwr\xF3ci\u0142o b\u0142\u0119dy",
76
+ WOO_REQUEST_FAILED: "B\u0142\u0105d \u017C\u0105dania do WooCommerce API",
77
+ WOO_NOT_FOUND: "Zas\xF3b WooCommerce nie istnieje",
78
+ WOO_UNAUTHORIZED: "Wymagane uwierzytelnienie WooCommerce",
79
+ WOO_FORBIDDEN: "Brak dost\u0119pu do zasobu WooCommerce",
80
+ WOO_COUPON_NOT_FOUND: "Kupon nie istnieje",
81
+ WOO_PRODUCT_NOT_FOUND: "Produkt nie istnieje",
82
+ WOO_ORDER_NOT_FOUND: "Zam\xF3wienie nie istnieje",
83
+ WOO_CUSTOMER_NOT_FOUND: "Klient nie istnieje",
84
+ WOO_CATEGORY_NOT_FOUND: "Kategoria produkt\xF3w nie istnieje",
85
+ WOO_TAG_NOT_FOUND: "Tag produkt\xF3w nie istnieje",
86
+ WOO_REVIEW_NOT_FOUND: "Opinia o produkcie nie istnieje",
87
+ WOO_WEBHOOK_NOT_FOUND: "Webhook nie istnieje",
88
+ WOO_SHIPPING_ZONE_NOT_FOUND: "Strefa wysy\u0142ki nie istnieje",
89
+ AUTH_JWT_FAILED: "B\u0142\u0105d uwierzytelnienia JWT",
90
+ AUTH_CREDENTIALS_INVALID: "Nieprawid\u0142owe dane logowania",
91
+ AUTH_TOKEN_INVALID: "Token JWT jest nieprawid\u0142owy lub wygas\u0142"
92
+ };
93
+ function resolveMessage(code, overrides) {
94
+ return overrides?.[code] ?? defaultMessages[code];
95
+ }
96
+
12
97
  // src/integrations/restApi/core/client/types.ts
13
98
  var WordPressAPIError = class extends Error {
14
- constructor(message, status, endpoint) {
15
- super(message);
99
+ constructor(code, status, endpoint, message, detail) {
100
+ super(detail ? `${message}: ${detail}` : message);
101
+ __publicField(this, "code", code);
16
102
  __publicField(this, "status", status);
17
103
  __publicField(this, "endpoint", endpoint);
18
104
  this.name = "WordPressAPIError";
@@ -35,19 +121,28 @@ function buildUrl(config, path, query) {
35
121
 
36
122
  // src/integrations/restApi/core/client/fetcher.ts
37
123
  var USER_AGENT = "NextWordpress Client";
38
- async function doFetch(url, init = {}) {
39
- const response = await fetch(url, init);
40
- if (!response.ok) {
41
- throw new WordPressAPIError(
42
- `WordPress API request failed: ${response.statusText}`,
43
- response.status,
44
- url
45
- );
46
- }
47
- return response;
124
+ function resolveWpErrorCode(status) {
125
+ if (status === 401) return ErrorCode.WP_UNAUTHORIZED;
126
+ if (status === 403) return ErrorCode.WP_FORBIDDEN;
127
+ if (status === 404) return ErrorCode.WP_NOT_FOUND;
128
+ return ErrorCode.WP_REQUEST_FAILED;
48
129
  }
49
130
  function createFetcher(config) {
50
131
  const cacheTtl = config.cacheTTL ?? 300;
132
+ async function doFetch(url, init = {}) {
133
+ const response = await fetch(url, init);
134
+ if (!response.ok) {
135
+ const code = resolveWpErrorCode(response.status);
136
+ throw new WordPressAPIError(
137
+ code,
138
+ response.status,
139
+ url,
140
+ resolveMessage(code, config.errorMessages),
141
+ response.statusText
142
+ );
143
+ }
144
+ return response;
145
+ }
51
146
  async function wpFetch(path, query, tags = ["wordpress"]) {
52
147
  const url = buildUrl(config, path, query);
53
148
  const res = await doFetch(url, {
@@ -710,6 +805,42 @@ function withOmnibusVariation(variation) {
710
805
  };
711
806
  }
712
807
 
808
+ // src/integrations/restApi/woocommerce/client/types.ts
809
+ var WooCommerceError = class extends Error {
810
+ constructor(code, status, endpoint, message, upstreamCode, detail) {
811
+ super(detail ? `${message}: ${detail}` : message);
812
+ __publicField(this, "code", code);
813
+ __publicField(this, "status", status);
814
+ __publicField(this, "endpoint", endpoint);
815
+ __publicField(this, "upstreamCode", upstreamCode);
816
+ this.name = "WooCommerceError";
817
+ }
818
+ };
819
+
820
+ // src/integrations/restApi/woocommerce/client/errorMap.ts
821
+ var upstreamToErrorCode = {
822
+ woocommerce_rest_coupon_invalid_id: ErrorCode.WOO_COUPON_NOT_FOUND,
823
+ woocommerce_rest_product_invalid_id: ErrorCode.WOO_PRODUCT_NOT_FOUND,
824
+ woocommerce_rest_order_invalid_id: ErrorCode.WOO_ORDER_NOT_FOUND,
825
+ woocommerce_rest_customer_invalid_id: ErrorCode.WOO_CUSTOMER_NOT_FOUND,
826
+ woocommerce_rest_term_invalid: ErrorCode.WOO_CATEGORY_NOT_FOUND,
827
+ woocommerce_rest_review_invalid_id: ErrorCode.WOO_REVIEW_NOT_FOUND,
828
+ woocommerce_rest_webhook_invalid_id: ErrorCode.WOO_WEBHOOK_NOT_FOUND,
829
+ woocommerce_rest_shipping_zone_invalid_id: ErrorCode.WOO_SHIPPING_ZONE_NOT_FOUND
830
+ };
831
+ function resolveWooErrorCode(status, upstreamCode) {
832
+ if (status === 401) return ErrorCode.WOO_UNAUTHORIZED;
833
+ if (status === 403) return ErrorCode.WOO_FORBIDDEN;
834
+ if (status === 404) {
835
+ if (upstreamCode) {
836
+ const mapped = upstreamToErrorCode[upstreamCode];
837
+ if (mapped) return mapped;
838
+ }
839
+ return ErrorCode.WOO_NOT_FOUND;
840
+ }
841
+ return ErrorCode.WOO_REQUEST_FAILED;
842
+ }
843
+
713
844
  // src/integrations/restApi/woocommerce/client/fetcher.ts
714
845
  var USER_AGENT2 = "NextWordpress WooCommerce Client";
715
846
  var DEFAULT_CACHE_TTL = 3600;
@@ -730,6 +861,18 @@ function createWooCommerceFetcher(config) {
730
861
  const base = typeof window !== "undefined" && config.clientURL ? config.clientURL : config.serverURL;
731
862
  return `${base}${path}?${toQueryString(withAuth(query))}`;
732
863
  }
864
+ async function throwWooError(response, url) {
865
+ const body = await response.json().catch(() => ({}));
866
+ const errorCode = resolveWooErrorCode(response.status, body.code);
867
+ throw new WooCommerceError(
868
+ errorCode,
869
+ response.status,
870
+ url,
871
+ resolveMessage(errorCode, config.errorMessages),
872
+ body.code,
873
+ body.message
874
+ );
875
+ }
733
876
  async function wcFetch(path, query, tags = ["woocommerce"], options) {
734
877
  const url = buildUrl2(path, query);
735
878
  const isMutation = options?.method && options.method !== "GET";
@@ -744,9 +887,7 @@ function createWooCommerceFetcher(config) {
744
887
  next: isMutation || noStore ? void 0 : { tags, revalidate: cacheTTL }
745
888
  });
746
889
  if (!response.ok) {
747
- const body = await response.json().catch(() => ({}));
748
- const detail = body.message ? ` \u2013 ${body.message}` : "";
749
- throw new Error(`WooCommerce API ${response.status}: ${response.statusText}${detail} [${url}]`);
890
+ await throwWooError(response, url);
750
891
  }
751
892
  return response.json();
752
893
  }
@@ -765,7 +906,7 @@ function createWooCommerceFetcher(config) {
765
906
  next: { tags, revalidate: cacheTTL }
766
907
  });
767
908
  if (!response.ok) {
768
- throw new Error(`WooCommerce API ${response.status}: ${response.statusText} [${url}]`);
909
+ await throwWooError(response, url);
769
910
  }
770
911
  return {
771
912
  data: await response.json(),
@@ -1962,8 +2103,9 @@ function createWooCommerceClient(config) {
1962
2103
 
1963
2104
  // src/integrations/wpGraphQL/client/types.ts
1964
2105
  var WPGraphQLError = class extends Error {
1965
- constructor(message, status, endpoint, gqlErrors) {
2106
+ constructor(code, status, endpoint, message, gqlErrors) {
1966
2107
  super(message);
2108
+ __publicField(this, "code", code);
1967
2109
  __publicField(this, "status", status);
1968
2110
  __publicField(this, "endpoint", endpoint);
1969
2111
  __publicField(this, "gqlErrors", gqlErrors);
@@ -1988,22 +2130,29 @@ function createWPGraphQLFetcher(config) {
1988
2130
  });
1989
2131
  if (!response.ok) {
1990
2132
  throw new WPGraphQLError(
1991
- `WPGraphQL request failed: ${response.statusText}`,
2133
+ ErrorCode.GQL_REQUEST_FAILED,
1992
2134
  response.status,
1993
- url
2135
+ url,
2136
+ resolveMessage(ErrorCode.GQL_REQUEST_FAILED, config.errorMessages)
1994
2137
  );
1995
2138
  }
1996
2139
  const parsed = await response.json();
1997
2140
  if (parsed.errors && parsed.errors.length > 0) {
1998
2141
  throw new WPGraphQLError(
1999
- parsed.errors[0].message,
2142
+ ErrorCode.GQL_ERRORS_IN_RESPONSE,
2000
2143
  200,
2001
2144
  url,
2145
+ resolveMessage(ErrorCode.GQL_ERRORS_IN_RESPONSE, config.errorMessages),
2002
2146
  parsed.errors
2003
2147
  );
2004
2148
  }
2005
2149
  if (parsed.data === void 0) {
2006
- throw new WPGraphQLError("No data returned from WPGraphQL", 200, url);
2150
+ throw new WPGraphQLError(
2151
+ ErrorCode.GQL_NO_DATA,
2152
+ 200,
2153
+ url,
2154
+ resolveMessage(ErrorCode.GQL_NO_DATA, config.errorMessages)
2155
+ );
2007
2156
  }
2008
2157
  return parsed.data;
2009
2158
  }
@@ -2031,22 +2180,29 @@ function createWPGraphQLFetcher(config) {
2031
2180
  });
2032
2181
  if (!response.ok) {
2033
2182
  throw new WPGraphQLError(
2034
- `WPGraphQL mutation failed: ${response.statusText}`,
2183
+ ErrorCode.GQL_REQUEST_FAILED,
2035
2184
  response.status,
2036
- url
2185
+ url,
2186
+ resolveMessage(ErrorCode.GQL_REQUEST_FAILED, config.errorMessages)
2037
2187
  );
2038
2188
  }
2039
2189
  const parsed = await response.json();
2040
2190
  if (parsed.errors && parsed.errors.length > 0) {
2041
2191
  throw new WPGraphQLError(
2042
- parsed.errors[0].message,
2192
+ ErrorCode.GQL_ERRORS_IN_RESPONSE,
2043
2193
  200,
2044
2194
  url,
2195
+ resolveMessage(ErrorCode.GQL_ERRORS_IN_RESPONSE, config.errorMessages),
2045
2196
  parsed.errors
2046
2197
  );
2047
2198
  }
2048
2199
  if (parsed.data === void 0) {
2049
- throw new WPGraphQLError("No data returned from WPGraphQL mutation", 200, url);
2200
+ throw new WPGraphQLError(
2201
+ ErrorCode.GQL_NO_DATA,
2202
+ 200,
2203
+ url,
2204
+ resolveMessage(ErrorCode.GQL_NO_DATA, config.errorMessages)
2205
+ );
2050
2206
  }
2051
2207
  return parsed.data;
2052
2208
  }
@@ -4029,14 +4185,20 @@ function createApplicationPasswordToken(username, appPassword) {
4029
4185
 
4030
4186
  // src/auth/types.ts
4031
4187
  var AuthenticationError = class extends Error {
4032
- constructor(message, status) {
4033
- super(message);
4188
+ constructor(code, status, message, detail) {
4189
+ super(detail ? `${message}: ${detail}` : message);
4190
+ __publicField(this, "code", code);
4034
4191
  __publicField(this, "status", status);
4035
4192
  this.name = "AuthenticationError";
4036
4193
  }
4037
4194
  };
4038
4195
 
4039
4196
  // src/auth/jwt.ts
4197
+ function resolveAuthErrorCode(status) {
4198
+ if (status === 401) return ErrorCode.AUTH_CREDENTIALS_INVALID;
4199
+ if (status === 403) return ErrorCode.AUTH_TOKEN_INVALID;
4200
+ return ErrorCode.AUTH_JWT_FAILED;
4201
+ }
4040
4202
  async function authenticateJwt(config, credentials) {
4041
4203
  const url = `${resolveBaseUrl(config)}/wp-json/jwt-auth/v1/token`;
4042
4204
  const response = await fetch(url, {
@@ -4046,9 +4208,12 @@ async function authenticateJwt(config, credentials) {
4046
4208
  cache: "no-store"
4047
4209
  });
4048
4210
  if (!response.ok) {
4211
+ const code = resolveAuthErrorCode(response.status);
4049
4212
  throw new AuthenticationError(
4050
- `JWT authentication failed: ${response.statusText}`,
4051
- response.status
4213
+ code,
4214
+ response.status,
4215
+ resolveMessage(code, config.errorMessages),
4216
+ response.statusText
4052
4217
  );
4053
4218
  }
4054
4219
  return response.json();
@@ -4064,7 +4229,10 @@ async function validateJwtToken(config, token) {
4064
4229
  }
4065
4230
 
4066
4231
  exports.AuthenticationError = AuthenticationError;
4232
+ exports.ErrorCode = ErrorCode;
4067
4233
  exports.WPGraphQLError = WPGraphQLError;
4234
+ exports.WooCommerceError = WooCommerceError;
4235
+ exports.WordPressAPIError = WordPressAPIError;
4068
4236
  exports.authenticateJwt = authenticateJwt;
4069
4237
  exports.buildACFFragment = buildACFFragment;
4070
4238
  exports.buildPageWithACFQuery = buildPageWithACFQuery;
@@ -4094,8 +4262,11 @@ exports.createWooCommerceFetcher = createWooCommerceFetcher;
4094
4262
  exports.createWordPressClient = createWordPressClient;
4095
4263
  exports.createWordPressMutationsClient = createWordPressMutationsClient;
4096
4264
  exports.createYoastQueries = createYoastQueries;
4265
+ exports.defaultMessages = defaultMessages;
4266
+ exports.defaultMessagesPl = defaultMessagesPl;
4097
4267
  exports.extractOmnibusData = extractOmnibusData;
4098
4268
  exports.resolveBaseUrl = resolveBaseUrl;
4269
+ exports.resolveMessage = resolveMessage;
4099
4270
  exports.validateJwtToken = validateJwtToken;
4100
4271
  exports.withOmnibus = withOmnibus;
4101
4272
  exports.withOmnibusVariation = withOmnibusVariation;