@szymonpiatek/nextwordpress 0.0.7 → 0.0.9

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, {
@@ -94,7 +189,7 @@ function createFetcher(config) {
94
189
  "User-Agent": USER_AGENT
95
190
  };
96
191
  if (authToken) {
97
- headers["Authorization"] = authToken;
192
+ headers["Authorization"] = `Bearer ${authToken}`;
98
193
  }
99
194
  const res = await doFetch(url, {
100
195
  method,
@@ -112,7 +207,7 @@ function createFetcher(config) {
112
207
  "Content-Disposition": `attachment; filename="${filename}"`,
113
208
  "Content-Type": mimeType,
114
209
  "User-Agent": USER_AGENT,
115
- "Authorization": authToken
210
+ "Authorization": `Bearer ${authToken}`
116
211
  },
117
212
  body: file,
118
213
  cache: "no-store"
@@ -140,14 +235,14 @@ function createCF7Queries(config) {
140
235
  function createPostsMutations(fetcher) {
141
236
  const { wpMutate } = fetcher;
142
237
  async function createPost(input, authToken) {
143
- return wpMutate("/wp/v2/posts", input, "POST", authToken);
238
+ return wpMutate("/wp-json/wp/v2/posts", input, "POST", authToken);
144
239
  }
145
240
  async function updatePost(id, input, authToken) {
146
- return wpMutate(`/wp/v2/posts/${id}`, input, "PATCH", authToken);
241
+ return wpMutate(`/wp-json/wp/v2/posts/${id}`, input, "PATCH", authToken);
147
242
  }
148
243
  async function deletePost(id, authToken, force = false) {
149
244
  return wpMutate(
150
- `/wp/v2/posts/${id}`,
245
+ `/wp-json/wp/v2/posts/${id}`,
151
246
  { force },
152
247
  "DELETE",
153
248
  authToken
@@ -160,14 +255,14 @@ function createPostsMutations(fetcher) {
160
255
  function createCommentsMutations(fetcher) {
161
256
  const { wpMutate } = fetcher;
162
257
  async function createComment(input, authToken) {
163
- return wpMutate("/wp/v2/comments", input, "POST", authToken);
258
+ return wpMutate("/wp-json/wp/v2/comments", input, "POST", authToken);
164
259
  }
165
260
  async function updateComment(id, input, authToken) {
166
- return wpMutate(`/wp/v2/comments/${id}`, input, "PATCH", authToken);
261
+ return wpMutate(`/wp-json/wp/v2/comments/${id}`, input, "PATCH", authToken);
167
262
  }
168
263
  async function deleteComment(id, authToken, force = false) {
169
264
  return wpMutate(
170
- `/wp/v2/comments/${id}`,
265
+ `/wp-json/wp/v2/comments/${id}`,
171
266
  { force },
172
267
  "DELETE",
173
268
  authToken
@@ -180,14 +275,14 @@ function createCommentsMutations(fetcher) {
180
275
  function createPagesMutations(fetcher) {
181
276
  const { wpMutate } = fetcher;
182
277
  async function createPage(input, authToken) {
183
- return wpMutate("/wp/v2/pages", input, "POST", authToken);
278
+ return wpMutate("/wp-json/wp/v2/pages", input, "POST", authToken);
184
279
  }
185
280
  async function updatePage(id, input, authToken) {
186
- return wpMutate(`/wp/v2/pages/${id}`, input, "PATCH", authToken);
281
+ return wpMutate(`/wp-json/wp/v2/pages/${id}`, input, "PATCH", authToken);
187
282
  }
188
283
  async function deletePage(id, authToken, force = false) {
189
284
  return wpMutate(
190
- `/wp/v2/pages/${id}`,
285
+ `/wp-json/wp/v2/pages/${id}`,
191
286
  { force },
192
287
  "DELETE",
193
288
  authToken
@@ -200,14 +295,14 @@ function createPagesMutations(fetcher) {
200
295
  function createCategoriesMutations(fetcher) {
201
296
  const { wpMutate } = fetcher;
202
297
  async function createCategory(input, authToken) {
203
- return wpMutate("/wp/v2/categories", input, "POST", authToken);
298
+ return wpMutate("/wp-json/wp/v2/categories", input, "POST", authToken);
204
299
  }
205
300
  async function updateCategory(id, input, authToken) {
206
- return wpMutate(`/wp/v2/categories/${id}`, input, "PATCH", authToken);
301
+ return wpMutate(`/wp-json/wp/v2/categories/${id}`, input, "PATCH", authToken);
207
302
  }
208
303
  async function deleteCategory(id, authToken, force = false) {
209
304
  return wpMutate(
210
- `/wp/v2/categories/${id}`,
305
+ `/wp-json/wp/v2/categories/${id}`,
211
306
  { force },
212
307
  "DELETE",
213
308
  authToken
@@ -220,14 +315,14 @@ function createCategoriesMutations(fetcher) {
220
315
  function createTagsMutations(fetcher) {
221
316
  const { wpMutate } = fetcher;
222
317
  async function createTag(input, authToken) {
223
- return wpMutate("/wp/v2/tags", input, "POST", authToken);
318
+ return wpMutate("/wp-json/wp/v2/tags", input, "POST", authToken);
224
319
  }
225
320
  async function updateTag(id, input, authToken) {
226
- return wpMutate(`/wp/v2/tags/${id}`, input, "PATCH", authToken);
321
+ return wpMutate(`/wp-json/wp/v2/tags/${id}`, input, "PATCH", authToken);
227
322
  }
228
323
  async function deleteTag(id, authToken, force = false) {
229
324
  return wpMutate(
230
- `/wp/v2/tags/${id}`,
325
+ `/wp-json/wp/v2/tags/${id}`,
231
326
  { force },
232
327
  "DELETE",
233
328
  authToken
@@ -240,14 +335,14 @@ function createTagsMutations(fetcher) {
240
335
  function createAuthorsMutations(fetcher) {
241
336
  const { wpMutate } = fetcher;
242
337
  async function createAuthor(input, authToken) {
243
- return wpMutate("/wp/v2/users", input, "POST", authToken);
338
+ return wpMutate("/wp-json/wp/v2/users", input, "POST", authToken);
244
339
  }
245
340
  async function updateAuthor(id, input, authToken) {
246
- return wpMutate(`/wp/v2/users/${id}`, input, "PATCH", authToken);
341
+ return wpMutate(`/wp-json/wp/v2/users/${id}`, input, "PATCH", authToken);
247
342
  }
248
343
  async function deleteAuthor(id, authToken, reassign) {
249
344
  return wpMutate(
250
- `/wp/v2/users/${id}`,
345
+ `/wp-json/wp/v2/users/${id}`,
251
346
  { force: true, reassign },
252
347
  "DELETE",
253
348
  authToken
@@ -260,28 +355,28 @@ function createAuthorsMutations(fetcher) {
260
355
  function createMenusMutations(fetcher) {
261
356
  const { wpMutate } = fetcher;
262
357
  async function createMenu(input, authToken) {
263
- return wpMutate("/wp/v2/menus", input, "POST", authToken);
358
+ return wpMutate("/wp-json/wp/v2/menus", input, "POST", authToken);
264
359
  }
265
360
  async function updateMenu(id, input, authToken) {
266
- return wpMutate(`/wp/v2/menus/${id}`, input, "PATCH", authToken);
361
+ return wpMutate(`/wp-json/wp/v2/menus/${id}`, input, "PATCH", authToken);
267
362
  }
268
363
  async function deleteMenu(id, authToken) {
269
364
  return wpMutate(
270
- `/wp/v2/menus/${id}`,
365
+ `/wp-json/wp/v2/menus/${id}`,
271
366
  {},
272
367
  "DELETE",
273
368
  authToken
274
369
  );
275
370
  }
276
371
  async function createMenuItem(input, authToken) {
277
- return wpMutate("/wp/v2/menu-items", input, "POST", authToken);
372
+ return wpMutate("/wp-json/wp/v2/menu-items", input, "POST", authToken);
278
373
  }
279
374
  async function updateMenuItem(id, input, authToken) {
280
- return wpMutate(`/wp/v2/menu-items/${id}`, input, "PATCH", authToken);
375
+ return wpMutate(`/wp-json/wp/v2/menu-items/${id}`, input, "PATCH", authToken);
281
376
  }
282
377
  async function deleteMenuItem(id, authToken, force = false) {
283
378
  return wpMutate(
284
- `/wp/v2/menu-items/${id}`,
379
+ `/wp-json/wp/v2/menu-items/${id}`,
285
380
  { force },
286
381
  "DELETE",
287
382
  authToken
@@ -295,7 +390,7 @@ function createMediaMutations(fetcher) {
295
390
  const { wpMutate, wpUpload } = fetcher;
296
391
  async function uploadMedia(input, authToken) {
297
392
  const media = await wpUpload(
298
- "/wp/v2/media",
393
+ "/wp-json/wp/v2/media",
299
394
  input.file,
300
395
  input.filename,
301
396
  input.mimeType,
@@ -303,7 +398,7 @@ function createMediaMutations(fetcher) {
303
398
  );
304
399
  if (input.title || input.caption || input.alt_text) {
305
400
  return wpMutate(
306
- `/wp/v2/media/${media.id}`,
401
+ `/wp-json/wp/v2/media/${media.id}`,
307
402
  {
308
403
  ...input.title && { title: input.title },
309
404
  ...input.caption && { caption: input.caption },
@@ -316,11 +411,11 @@ function createMediaMutations(fetcher) {
316
411
  return media;
317
412
  }
318
413
  async function updateMedia(id, input, authToken) {
319
- return wpMutate(`/wp/v2/media/${id}`, input, "PATCH", authToken);
414
+ return wpMutate(`/wp-json/wp/v2/media/${id}`, input, "PATCH", authToken);
320
415
  }
321
416
  async function deleteMedia(id, authToken, force = true) {
322
417
  return wpMutate(
323
- `/wp/v2/media/${id}`,
418
+ `/wp-json/wp/v2/media/${id}`,
324
419
  { force },
325
420
  "DELETE",
326
421
  authToken
@@ -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;