@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.d.cts CHANGED
@@ -1,5 +1,5 @@
1
- import { N as NextWordpressConfig, J as JwtAuthCredentials, k as JwtAuthResponse, a as WooCommerceConfig, d as WooCommerceResponse, W as WordPressResponse, T as Taxonomy, l as WPEntity, R as RenderedTitle, m as RenderedContent, P as Post, A as Author, F as FeaturedMedia, n as WCImage, o as WCProductVariation, b as WCProduct, g as WCCustomer, p as WCAddress, U as UpdateCustomerInput, e as WCCreateOrderInput, f as WCOrder, c as WCProductFilterParams, h as WPGraphQLConfig, j as GQLConnection, i as GQLPostFilter, G as GQLPost } from './types-Be60n09c.cjs';
2
- export { q as AuthenticationError, B as BlockType, E as EditorBlock, r as EmbeddedAuthor, s as EmbeddedTerm, t as GQLError, u as GQLPageInfo, M as MediaDetails, v as MediaSize, w as PostEmbedded, S as SearchResult, x as TemplatePart, y as WCDimensions, z as WCOrderLineItem, C as WCProductAttribute, D as WCProductDefaultAttribute, H as WCShippingLine, I as WPGraphQLError, K as WooCommercePaginationHeaders } from './types-Be60n09c.cjs';
1
+ import { N as NextWordpressConfig, J as JwtAuthCredentials, k as JwtAuthResponse, a as WooCommerceConfig, d as WooCommerceResponse, W as WordPressResponse, T as Taxonomy, l as WPEntity, R as RenderedTitle, m as RenderedContent, P as Post, A as Author, F as FeaturedMedia, n as WCImage, o as WCProductVariation, b as WCProduct, g as WCCustomer, p as WCAddress, U as UpdateCustomerInput, e as WCCreateOrderInput, f as WCOrder, c as WCProductFilterParams, h as WPGraphQLConfig, j as GQLConnection, i as GQLPostFilter, G as GQLPost } from './types-Dxb6tuW_.cjs';
2
+ export { q as AuthenticationError, B as BlockType, E as EditorBlock, r as EmbeddedAuthor, s as EmbeddedTerm, t as ErrorCode, u as GQLError, v as GQLPageInfo, M as MediaDetails, w as MediaSize, x as PostEmbedded, S as SearchResult, y as TemplatePart, z as WCDimensions, C as WCOrderLineItem, D as WCProductAttribute, H as WCProductDefaultAttribute, I as WCShippingLine, K as WPGraphQLError, L as WooCommerceError, O as WooCommercePaginationHeaders, Q as WordPressAPIError, V as defaultMessages, X as defaultMessagesPl, Y as resolveMessage } from './types-Dxb6tuW_.cjs';
3
3
 
4
4
  declare function createApplicationPasswordToken(username: string, appPassword: string): string;
5
5
 
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { N as NextWordpressConfig, J as JwtAuthCredentials, k as JwtAuthResponse, a as WooCommerceConfig, d as WooCommerceResponse, W as WordPressResponse, T as Taxonomy, l as WPEntity, R as RenderedTitle, m as RenderedContent, P as Post, A as Author, F as FeaturedMedia, n as WCImage, o as WCProductVariation, b as WCProduct, g as WCCustomer, p as WCAddress, U as UpdateCustomerInput, e as WCCreateOrderInput, f as WCOrder, c as WCProductFilterParams, h as WPGraphQLConfig, j as GQLConnection, i as GQLPostFilter, G as GQLPost } from './types-Be60n09c.js';
2
- export { q as AuthenticationError, B as BlockType, E as EditorBlock, r as EmbeddedAuthor, s as EmbeddedTerm, t as GQLError, u as GQLPageInfo, M as MediaDetails, v as MediaSize, w as PostEmbedded, S as SearchResult, x as TemplatePart, y as WCDimensions, z as WCOrderLineItem, C as WCProductAttribute, D as WCProductDefaultAttribute, H as WCShippingLine, I as WPGraphQLError, K as WooCommercePaginationHeaders } from './types-Be60n09c.js';
1
+ import { N as NextWordpressConfig, J as JwtAuthCredentials, k as JwtAuthResponse, a as WooCommerceConfig, d as WooCommerceResponse, W as WordPressResponse, T as Taxonomy, l as WPEntity, R as RenderedTitle, m as RenderedContent, P as Post, A as Author, F as FeaturedMedia, n as WCImage, o as WCProductVariation, b as WCProduct, g as WCCustomer, p as WCAddress, U as UpdateCustomerInput, e as WCCreateOrderInput, f as WCOrder, c as WCProductFilterParams, h as WPGraphQLConfig, j as GQLConnection, i as GQLPostFilter, G as GQLPost } from './types-Dxb6tuW_.js';
2
+ export { q as AuthenticationError, B as BlockType, E as EditorBlock, r as EmbeddedAuthor, s as EmbeddedTerm, t as ErrorCode, u as GQLError, v as GQLPageInfo, M as MediaDetails, w as MediaSize, x as PostEmbedded, S as SearchResult, y as TemplatePart, z as WCDimensions, C as WCOrderLineItem, D as WCProductAttribute, H as WCProductDefaultAttribute, I as WCShippingLine, K as WPGraphQLError, L as WooCommerceError, O as WooCommercePaginationHeaders, Q as WordPressAPIError, V as defaultMessages, X as defaultMessagesPl, Y as resolveMessage } from './types-Dxb6tuW_.js';
3
3
 
4
4
  declare function createApplicationPasswordToken(username: string, appPassword: string): string;
5
5
 
package/dist/index.js CHANGED
@@ -7,10 +7,96 @@ function resolveBaseUrl(config) {
7
7
  return typeof window === "undefined" ? config.serverURL : config.clientURL;
8
8
  }
9
9
 
10
+ // src/shared/errors/codes.ts
11
+ var ErrorCode = {
12
+ // WordPress REST API
13
+ WP_REQUEST_FAILED: "WP_REQUEST_FAILED",
14
+ WP_NOT_FOUND: "WP_NOT_FOUND",
15
+ WP_UNAUTHORIZED: "WP_UNAUTHORIZED",
16
+ WP_FORBIDDEN: "WP_FORBIDDEN",
17
+ // WPGraphQL
18
+ GQL_REQUEST_FAILED: "GQL_REQUEST_FAILED",
19
+ GQL_NO_DATA: "GQL_NO_DATA",
20
+ GQL_ERRORS_IN_RESPONSE: "GQL_ERRORS_IN_RESPONSE",
21
+ // WooCommerce — generic
22
+ WOO_REQUEST_FAILED: "WOO_REQUEST_FAILED",
23
+ WOO_NOT_FOUND: "WOO_NOT_FOUND",
24
+ WOO_UNAUTHORIZED: "WOO_UNAUTHORIZED",
25
+ WOO_FORBIDDEN: "WOO_FORBIDDEN",
26
+ // WooCommerce — resource-specific
27
+ WOO_COUPON_NOT_FOUND: "WOO_COUPON_NOT_FOUND",
28
+ WOO_PRODUCT_NOT_FOUND: "WOO_PRODUCT_NOT_FOUND",
29
+ WOO_ORDER_NOT_FOUND: "WOO_ORDER_NOT_FOUND",
30
+ WOO_CUSTOMER_NOT_FOUND: "WOO_CUSTOMER_NOT_FOUND",
31
+ WOO_CATEGORY_NOT_FOUND: "WOO_CATEGORY_NOT_FOUND",
32
+ WOO_TAG_NOT_FOUND: "WOO_TAG_NOT_FOUND",
33
+ WOO_REVIEW_NOT_FOUND: "WOO_REVIEW_NOT_FOUND",
34
+ WOO_WEBHOOK_NOT_FOUND: "WOO_WEBHOOK_NOT_FOUND",
35
+ WOO_SHIPPING_ZONE_NOT_FOUND: "WOO_SHIPPING_ZONE_NOT_FOUND",
36
+ // Auth
37
+ AUTH_JWT_FAILED: "AUTH_JWT_FAILED",
38
+ AUTH_CREDENTIALS_INVALID: "AUTH_CREDENTIALS_INVALID",
39
+ AUTH_TOKEN_INVALID: "AUTH_TOKEN_INVALID"
40
+ };
41
+ var defaultMessages = {
42
+ WP_REQUEST_FAILED: "WordPress API request failed",
43
+ WP_NOT_FOUND: "WordPress resource not found",
44
+ WP_UNAUTHORIZED: "WordPress authentication required",
45
+ WP_FORBIDDEN: "WordPress access denied",
46
+ GQL_REQUEST_FAILED: "WPGraphQL request failed",
47
+ GQL_NO_DATA: "No data returned from WPGraphQL",
48
+ GQL_ERRORS_IN_RESPONSE: "WPGraphQL returned errors",
49
+ WOO_REQUEST_FAILED: "WooCommerce API request failed",
50
+ WOO_NOT_FOUND: "WooCommerce resource not found",
51
+ WOO_UNAUTHORIZED: "WooCommerce authentication required",
52
+ WOO_FORBIDDEN: "WooCommerce access denied",
53
+ WOO_COUPON_NOT_FOUND: "Coupon not found",
54
+ WOO_PRODUCT_NOT_FOUND: "Product not found",
55
+ WOO_ORDER_NOT_FOUND: "Order not found",
56
+ WOO_CUSTOMER_NOT_FOUND: "Customer not found",
57
+ WOO_CATEGORY_NOT_FOUND: "Category not found",
58
+ WOO_TAG_NOT_FOUND: "Tag not found",
59
+ WOO_REVIEW_NOT_FOUND: "Product review not found",
60
+ WOO_WEBHOOK_NOT_FOUND: "Webhook not found",
61
+ WOO_SHIPPING_ZONE_NOT_FOUND: "Shipping zone not found",
62
+ AUTH_JWT_FAILED: "JWT authentication failed",
63
+ AUTH_CREDENTIALS_INVALID: "Invalid credentials",
64
+ AUTH_TOKEN_INVALID: "JWT token is invalid or expired"
65
+ };
66
+ var defaultMessagesPl = {
67
+ WP_REQUEST_FAILED: "B\u0142\u0105d \u017C\u0105dania do WordPress API",
68
+ WP_NOT_FOUND: "Zas\xF3b WordPress nie istnieje",
69
+ WP_UNAUTHORIZED: "Wymagane uwierzytelnienie WordPress",
70
+ WP_FORBIDDEN: "Brak dost\u0119pu do zasobu WordPress",
71
+ GQL_REQUEST_FAILED: "B\u0142\u0105d \u017C\u0105dania WPGraphQL",
72
+ GQL_NO_DATA: "WPGraphQL nie zwr\xF3ci\u0142o danych",
73
+ GQL_ERRORS_IN_RESPONSE: "WPGraphQL zwr\xF3ci\u0142o b\u0142\u0119dy",
74
+ WOO_REQUEST_FAILED: "B\u0142\u0105d \u017C\u0105dania do WooCommerce API",
75
+ WOO_NOT_FOUND: "Zas\xF3b WooCommerce nie istnieje",
76
+ WOO_UNAUTHORIZED: "Wymagane uwierzytelnienie WooCommerce",
77
+ WOO_FORBIDDEN: "Brak dost\u0119pu do zasobu WooCommerce",
78
+ WOO_COUPON_NOT_FOUND: "Kupon nie istnieje",
79
+ WOO_PRODUCT_NOT_FOUND: "Produkt nie istnieje",
80
+ WOO_ORDER_NOT_FOUND: "Zam\xF3wienie nie istnieje",
81
+ WOO_CUSTOMER_NOT_FOUND: "Klient nie istnieje",
82
+ WOO_CATEGORY_NOT_FOUND: "Kategoria produkt\xF3w nie istnieje",
83
+ WOO_TAG_NOT_FOUND: "Tag produkt\xF3w nie istnieje",
84
+ WOO_REVIEW_NOT_FOUND: "Opinia o produkcie nie istnieje",
85
+ WOO_WEBHOOK_NOT_FOUND: "Webhook nie istnieje",
86
+ WOO_SHIPPING_ZONE_NOT_FOUND: "Strefa wysy\u0142ki nie istnieje",
87
+ AUTH_JWT_FAILED: "B\u0142\u0105d uwierzytelnienia JWT",
88
+ AUTH_CREDENTIALS_INVALID: "Nieprawid\u0142owe dane logowania",
89
+ AUTH_TOKEN_INVALID: "Token JWT jest nieprawid\u0142owy lub wygas\u0142"
90
+ };
91
+ function resolveMessage(code, overrides) {
92
+ return overrides?.[code] ?? defaultMessages[code];
93
+ }
94
+
10
95
  // src/integrations/restApi/core/client/types.ts
11
96
  var WordPressAPIError = class extends Error {
12
- constructor(message, status, endpoint) {
13
- super(message);
97
+ constructor(code, status, endpoint, message, detail) {
98
+ super(detail ? `${message}: ${detail}` : message);
99
+ __publicField(this, "code", code);
14
100
  __publicField(this, "status", status);
15
101
  __publicField(this, "endpoint", endpoint);
16
102
  this.name = "WordPressAPIError";
@@ -33,19 +119,28 @@ function buildUrl(config, path, query) {
33
119
 
34
120
  // src/integrations/restApi/core/client/fetcher.ts
35
121
  var USER_AGENT = "NextWordpress Client";
36
- async function doFetch(url, init = {}) {
37
- const response = await fetch(url, init);
38
- if (!response.ok) {
39
- throw new WordPressAPIError(
40
- `WordPress API request failed: ${response.statusText}`,
41
- response.status,
42
- url
43
- );
44
- }
45
- return response;
122
+ function resolveWpErrorCode(status) {
123
+ if (status === 401) return ErrorCode.WP_UNAUTHORIZED;
124
+ if (status === 403) return ErrorCode.WP_FORBIDDEN;
125
+ if (status === 404) return ErrorCode.WP_NOT_FOUND;
126
+ return ErrorCode.WP_REQUEST_FAILED;
46
127
  }
47
128
  function createFetcher(config) {
48
129
  const cacheTtl = config.cacheTTL ?? 300;
130
+ async function doFetch(url, init = {}) {
131
+ const response = await fetch(url, init);
132
+ if (!response.ok) {
133
+ const code = resolveWpErrorCode(response.status);
134
+ throw new WordPressAPIError(
135
+ code,
136
+ response.status,
137
+ url,
138
+ resolveMessage(code, config.errorMessages),
139
+ response.statusText
140
+ );
141
+ }
142
+ return response;
143
+ }
49
144
  async function wpFetch(path, query, tags = ["wordpress"]) {
50
145
  const url = buildUrl(config, path, query);
51
146
  const res = await doFetch(url, {
@@ -92,7 +187,7 @@ function createFetcher(config) {
92
187
  "User-Agent": USER_AGENT
93
188
  };
94
189
  if (authToken) {
95
- headers["Authorization"] = authToken;
190
+ headers["Authorization"] = `Bearer ${authToken}`;
96
191
  }
97
192
  const res = await doFetch(url, {
98
193
  method,
@@ -110,7 +205,7 @@ function createFetcher(config) {
110
205
  "Content-Disposition": `attachment; filename="${filename}"`,
111
206
  "Content-Type": mimeType,
112
207
  "User-Agent": USER_AGENT,
113
- "Authorization": authToken
208
+ "Authorization": `Bearer ${authToken}`
114
209
  },
115
210
  body: file,
116
211
  cache: "no-store"
@@ -138,14 +233,14 @@ function createCF7Queries(config) {
138
233
  function createPostsMutations(fetcher) {
139
234
  const { wpMutate } = fetcher;
140
235
  async function createPost(input, authToken) {
141
- return wpMutate("/wp/v2/posts", input, "POST", authToken);
236
+ return wpMutate("/wp-json/wp/v2/posts", input, "POST", authToken);
142
237
  }
143
238
  async function updatePost(id, input, authToken) {
144
- return wpMutate(`/wp/v2/posts/${id}`, input, "PATCH", authToken);
239
+ return wpMutate(`/wp-json/wp/v2/posts/${id}`, input, "PATCH", authToken);
145
240
  }
146
241
  async function deletePost(id, authToken, force = false) {
147
242
  return wpMutate(
148
- `/wp/v2/posts/${id}`,
243
+ `/wp-json/wp/v2/posts/${id}`,
149
244
  { force },
150
245
  "DELETE",
151
246
  authToken
@@ -158,14 +253,14 @@ function createPostsMutations(fetcher) {
158
253
  function createCommentsMutations(fetcher) {
159
254
  const { wpMutate } = fetcher;
160
255
  async function createComment(input, authToken) {
161
- return wpMutate("/wp/v2/comments", input, "POST", authToken);
256
+ return wpMutate("/wp-json/wp/v2/comments", input, "POST", authToken);
162
257
  }
163
258
  async function updateComment(id, input, authToken) {
164
- return wpMutate(`/wp/v2/comments/${id}`, input, "PATCH", authToken);
259
+ return wpMutate(`/wp-json/wp/v2/comments/${id}`, input, "PATCH", authToken);
165
260
  }
166
261
  async function deleteComment(id, authToken, force = false) {
167
262
  return wpMutate(
168
- `/wp/v2/comments/${id}`,
263
+ `/wp-json/wp/v2/comments/${id}`,
169
264
  { force },
170
265
  "DELETE",
171
266
  authToken
@@ -178,14 +273,14 @@ function createCommentsMutations(fetcher) {
178
273
  function createPagesMutations(fetcher) {
179
274
  const { wpMutate } = fetcher;
180
275
  async function createPage(input, authToken) {
181
- return wpMutate("/wp/v2/pages", input, "POST", authToken);
276
+ return wpMutate("/wp-json/wp/v2/pages", input, "POST", authToken);
182
277
  }
183
278
  async function updatePage(id, input, authToken) {
184
- return wpMutate(`/wp/v2/pages/${id}`, input, "PATCH", authToken);
279
+ return wpMutate(`/wp-json/wp/v2/pages/${id}`, input, "PATCH", authToken);
185
280
  }
186
281
  async function deletePage(id, authToken, force = false) {
187
282
  return wpMutate(
188
- `/wp/v2/pages/${id}`,
283
+ `/wp-json/wp/v2/pages/${id}`,
189
284
  { force },
190
285
  "DELETE",
191
286
  authToken
@@ -198,14 +293,14 @@ function createPagesMutations(fetcher) {
198
293
  function createCategoriesMutations(fetcher) {
199
294
  const { wpMutate } = fetcher;
200
295
  async function createCategory(input, authToken) {
201
- return wpMutate("/wp/v2/categories", input, "POST", authToken);
296
+ return wpMutate("/wp-json/wp/v2/categories", input, "POST", authToken);
202
297
  }
203
298
  async function updateCategory(id, input, authToken) {
204
- return wpMutate(`/wp/v2/categories/${id}`, input, "PATCH", authToken);
299
+ return wpMutate(`/wp-json/wp/v2/categories/${id}`, input, "PATCH", authToken);
205
300
  }
206
301
  async function deleteCategory(id, authToken, force = false) {
207
302
  return wpMutate(
208
- `/wp/v2/categories/${id}`,
303
+ `/wp-json/wp/v2/categories/${id}`,
209
304
  { force },
210
305
  "DELETE",
211
306
  authToken
@@ -218,14 +313,14 @@ function createCategoriesMutations(fetcher) {
218
313
  function createTagsMutations(fetcher) {
219
314
  const { wpMutate } = fetcher;
220
315
  async function createTag(input, authToken) {
221
- return wpMutate("/wp/v2/tags", input, "POST", authToken);
316
+ return wpMutate("/wp-json/wp/v2/tags", input, "POST", authToken);
222
317
  }
223
318
  async function updateTag(id, input, authToken) {
224
- return wpMutate(`/wp/v2/tags/${id}`, input, "PATCH", authToken);
319
+ return wpMutate(`/wp-json/wp/v2/tags/${id}`, input, "PATCH", authToken);
225
320
  }
226
321
  async function deleteTag(id, authToken, force = false) {
227
322
  return wpMutate(
228
- `/wp/v2/tags/${id}`,
323
+ `/wp-json/wp/v2/tags/${id}`,
229
324
  { force },
230
325
  "DELETE",
231
326
  authToken
@@ -238,14 +333,14 @@ function createTagsMutations(fetcher) {
238
333
  function createAuthorsMutations(fetcher) {
239
334
  const { wpMutate } = fetcher;
240
335
  async function createAuthor(input, authToken) {
241
- return wpMutate("/wp/v2/users", input, "POST", authToken);
336
+ return wpMutate("/wp-json/wp/v2/users", input, "POST", authToken);
242
337
  }
243
338
  async function updateAuthor(id, input, authToken) {
244
- return wpMutate(`/wp/v2/users/${id}`, input, "PATCH", authToken);
339
+ return wpMutate(`/wp-json/wp/v2/users/${id}`, input, "PATCH", authToken);
245
340
  }
246
341
  async function deleteAuthor(id, authToken, reassign) {
247
342
  return wpMutate(
248
- `/wp/v2/users/${id}`,
343
+ `/wp-json/wp/v2/users/${id}`,
249
344
  { force: true, reassign },
250
345
  "DELETE",
251
346
  authToken
@@ -258,28 +353,28 @@ function createAuthorsMutations(fetcher) {
258
353
  function createMenusMutations(fetcher) {
259
354
  const { wpMutate } = fetcher;
260
355
  async function createMenu(input, authToken) {
261
- return wpMutate("/wp/v2/menus", input, "POST", authToken);
356
+ return wpMutate("/wp-json/wp/v2/menus", input, "POST", authToken);
262
357
  }
263
358
  async function updateMenu(id, input, authToken) {
264
- return wpMutate(`/wp/v2/menus/${id}`, input, "PATCH", authToken);
359
+ return wpMutate(`/wp-json/wp/v2/menus/${id}`, input, "PATCH", authToken);
265
360
  }
266
361
  async function deleteMenu(id, authToken) {
267
362
  return wpMutate(
268
- `/wp/v2/menus/${id}`,
363
+ `/wp-json/wp/v2/menus/${id}`,
269
364
  {},
270
365
  "DELETE",
271
366
  authToken
272
367
  );
273
368
  }
274
369
  async function createMenuItem(input, authToken) {
275
- return wpMutate("/wp/v2/menu-items", input, "POST", authToken);
370
+ return wpMutate("/wp-json/wp/v2/menu-items", input, "POST", authToken);
276
371
  }
277
372
  async function updateMenuItem(id, input, authToken) {
278
- return wpMutate(`/wp/v2/menu-items/${id}`, input, "PATCH", authToken);
373
+ return wpMutate(`/wp-json/wp/v2/menu-items/${id}`, input, "PATCH", authToken);
279
374
  }
280
375
  async function deleteMenuItem(id, authToken, force = false) {
281
376
  return wpMutate(
282
- `/wp/v2/menu-items/${id}`,
377
+ `/wp-json/wp/v2/menu-items/${id}`,
283
378
  { force },
284
379
  "DELETE",
285
380
  authToken
@@ -293,7 +388,7 @@ function createMediaMutations(fetcher) {
293
388
  const { wpMutate, wpUpload } = fetcher;
294
389
  async function uploadMedia(input, authToken) {
295
390
  const media = await wpUpload(
296
- "/wp/v2/media",
391
+ "/wp-json/wp/v2/media",
297
392
  input.file,
298
393
  input.filename,
299
394
  input.mimeType,
@@ -301,7 +396,7 @@ function createMediaMutations(fetcher) {
301
396
  );
302
397
  if (input.title || input.caption || input.alt_text) {
303
398
  return wpMutate(
304
- `/wp/v2/media/${media.id}`,
399
+ `/wp-json/wp/v2/media/${media.id}`,
305
400
  {
306
401
  ...input.title && { title: input.title },
307
402
  ...input.caption && { caption: input.caption },
@@ -314,11 +409,11 @@ function createMediaMutations(fetcher) {
314
409
  return media;
315
410
  }
316
411
  async function updateMedia(id, input, authToken) {
317
- return wpMutate(`/wp/v2/media/${id}`, input, "PATCH", authToken);
412
+ return wpMutate(`/wp-json/wp/v2/media/${id}`, input, "PATCH", authToken);
318
413
  }
319
414
  async function deleteMedia(id, authToken, force = true) {
320
415
  return wpMutate(
321
- `/wp/v2/media/${id}`,
416
+ `/wp-json/wp/v2/media/${id}`,
322
417
  { force },
323
418
  "DELETE",
324
419
  authToken
@@ -708,6 +803,42 @@ function withOmnibusVariation(variation) {
708
803
  };
709
804
  }
710
805
 
806
+ // src/integrations/restApi/woocommerce/client/types.ts
807
+ var WooCommerceError = class extends Error {
808
+ constructor(code, status, endpoint, message, upstreamCode, detail) {
809
+ super(detail ? `${message}: ${detail}` : message);
810
+ __publicField(this, "code", code);
811
+ __publicField(this, "status", status);
812
+ __publicField(this, "endpoint", endpoint);
813
+ __publicField(this, "upstreamCode", upstreamCode);
814
+ this.name = "WooCommerceError";
815
+ }
816
+ };
817
+
818
+ // src/integrations/restApi/woocommerce/client/errorMap.ts
819
+ var upstreamToErrorCode = {
820
+ woocommerce_rest_coupon_invalid_id: ErrorCode.WOO_COUPON_NOT_FOUND,
821
+ woocommerce_rest_product_invalid_id: ErrorCode.WOO_PRODUCT_NOT_FOUND,
822
+ woocommerce_rest_order_invalid_id: ErrorCode.WOO_ORDER_NOT_FOUND,
823
+ woocommerce_rest_customer_invalid_id: ErrorCode.WOO_CUSTOMER_NOT_FOUND,
824
+ woocommerce_rest_term_invalid: ErrorCode.WOO_CATEGORY_NOT_FOUND,
825
+ woocommerce_rest_review_invalid_id: ErrorCode.WOO_REVIEW_NOT_FOUND,
826
+ woocommerce_rest_webhook_invalid_id: ErrorCode.WOO_WEBHOOK_NOT_FOUND,
827
+ woocommerce_rest_shipping_zone_invalid_id: ErrorCode.WOO_SHIPPING_ZONE_NOT_FOUND
828
+ };
829
+ function resolveWooErrorCode(status, upstreamCode) {
830
+ if (status === 401) return ErrorCode.WOO_UNAUTHORIZED;
831
+ if (status === 403) return ErrorCode.WOO_FORBIDDEN;
832
+ if (status === 404) {
833
+ if (upstreamCode) {
834
+ const mapped = upstreamToErrorCode[upstreamCode];
835
+ if (mapped) return mapped;
836
+ }
837
+ return ErrorCode.WOO_NOT_FOUND;
838
+ }
839
+ return ErrorCode.WOO_REQUEST_FAILED;
840
+ }
841
+
711
842
  // src/integrations/restApi/woocommerce/client/fetcher.ts
712
843
  var USER_AGENT2 = "NextWordpress WooCommerce Client";
713
844
  var DEFAULT_CACHE_TTL = 3600;
@@ -728,6 +859,18 @@ function createWooCommerceFetcher(config) {
728
859
  const base = typeof window !== "undefined" && config.clientURL ? config.clientURL : config.serverURL;
729
860
  return `${base}${path}?${toQueryString(withAuth(query))}`;
730
861
  }
862
+ async function throwWooError(response, url) {
863
+ const body = await response.json().catch(() => ({}));
864
+ const errorCode = resolveWooErrorCode(response.status, body.code);
865
+ throw new WooCommerceError(
866
+ errorCode,
867
+ response.status,
868
+ url,
869
+ resolveMessage(errorCode, config.errorMessages),
870
+ body.code,
871
+ body.message
872
+ );
873
+ }
731
874
  async function wcFetch(path, query, tags = ["woocommerce"], options) {
732
875
  const url = buildUrl2(path, query);
733
876
  const isMutation = options?.method && options.method !== "GET";
@@ -742,9 +885,7 @@ function createWooCommerceFetcher(config) {
742
885
  next: isMutation || noStore ? void 0 : { tags, revalidate: cacheTTL }
743
886
  });
744
887
  if (!response.ok) {
745
- const body = await response.json().catch(() => ({}));
746
- const detail = body.message ? ` \u2013 ${body.message}` : "";
747
- throw new Error(`WooCommerce API ${response.status}: ${response.statusText}${detail} [${url}]`);
888
+ await throwWooError(response, url);
748
889
  }
749
890
  return response.json();
750
891
  }
@@ -763,7 +904,7 @@ function createWooCommerceFetcher(config) {
763
904
  next: { tags, revalidate: cacheTTL }
764
905
  });
765
906
  if (!response.ok) {
766
- throw new Error(`WooCommerce API ${response.status}: ${response.statusText} [${url}]`);
907
+ await throwWooError(response, url);
767
908
  }
768
909
  return {
769
910
  data: await response.json(),
@@ -1960,8 +2101,9 @@ function createWooCommerceClient(config) {
1960
2101
 
1961
2102
  // src/integrations/wpGraphQL/client/types.ts
1962
2103
  var WPGraphQLError = class extends Error {
1963
- constructor(message, status, endpoint, gqlErrors) {
2104
+ constructor(code, status, endpoint, message, gqlErrors) {
1964
2105
  super(message);
2106
+ __publicField(this, "code", code);
1965
2107
  __publicField(this, "status", status);
1966
2108
  __publicField(this, "endpoint", endpoint);
1967
2109
  __publicField(this, "gqlErrors", gqlErrors);
@@ -1986,22 +2128,29 @@ function createWPGraphQLFetcher(config) {
1986
2128
  });
1987
2129
  if (!response.ok) {
1988
2130
  throw new WPGraphQLError(
1989
- `WPGraphQL request failed: ${response.statusText}`,
2131
+ ErrorCode.GQL_REQUEST_FAILED,
1990
2132
  response.status,
1991
- url
2133
+ url,
2134
+ resolveMessage(ErrorCode.GQL_REQUEST_FAILED, config.errorMessages)
1992
2135
  );
1993
2136
  }
1994
2137
  const parsed = await response.json();
1995
2138
  if (parsed.errors && parsed.errors.length > 0) {
1996
2139
  throw new WPGraphQLError(
1997
- parsed.errors[0].message,
2140
+ ErrorCode.GQL_ERRORS_IN_RESPONSE,
1998
2141
  200,
1999
2142
  url,
2143
+ resolveMessage(ErrorCode.GQL_ERRORS_IN_RESPONSE, config.errorMessages),
2000
2144
  parsed.errors
2001
2145
  );
2002
2146
  }
2003
2147
  if (parsed.data === void 0) {
2004
- throw new WPGraphQLError("No data returned from WPGraphQL", 200, url);
2148
+ throw new WPGraphQLError(
2149
+ ErrorCode.GQL_NO_DATA,
2150
+ 200,
2151
+ url,
2152
+ resolveMessage(ErrorCode.GQL_NO_DATA, config.errorMessages)
2153
+ );
2005
2154
  }
2006
2155
  return parsed.data;
2007
2156
  }
@@ -2029,22 +2178,29 @@ function createWPGraphQLFetcher(config) {
2029
2178
  });
2030
2179
  if (!response.ok) {
2031
2180
  throw new WPGraphQLError(
2032
- `WPGraphQL mutation failed: ${response.statusText}`,
2181
+ ErrorCode.GQL_REQUEST_FAILED,
2033
2182
  response.status,
2034
- url
2183
+ url,
2184
+ resolveMessage(ErrorCode.GQL_REQUEST_FAILED, config.errorMessages)
2035
2185
  );
2036
2186
  }
2037
2187
  const parsed = await response.json();
2038
2188
  if (parsed.errors && parsed.errors.length > 0) {
2039
2189
  throw new WPGraphQLError(
2040
- parsed.errors[0].message,
2190
+ ErrorCode.GQL_ERRORS_IN_RESPONSE,
2041
2191
  200,
2042
2192
  url,
2193
+ resolveMessage(ErrorCode.GQL_ERRORS_IN_RESPONSE, config.errorMessages),
2043
2194
  parsed.errors
2044
2195
  );
2045
2196
  }
2046
2197
  if (parsed.data === void 0) {
2047
- throw new WPGraphQLError("No data returned from WPGraphQL mutation", 200, url);
2198
+ throw new WPGraphQLError(
2199
+ ErrorCode.GQL_NO_DATA,
2200
+ 200,
2201
+ url,
2202
+ resolveMessage(ErrorCode.GQL_NO_DATA, config.errorMessages)
2203
+ );
2048
2204
  }
2049
2205
  return parsed.data;
2050
2206
  }
@@ -4027,14 +4183,20 @@ function createApplicationPasswordToken(username, appPassword) {
4027
4183
 
4028
4184
  // src/auth/types.ts
4029
4185
  var AuthenticationError = class extends Error {
4030
- constructor(message, status) {
4031
- super(message);
4186
+ constructor(code, status, message, detail) {
4187
+ super(detail ? `${message}: ${detail}` : message);
4188
+ __publicField(this, "code", code);
4032
4189
  __publicField(this, "status", status);
4033
4190
  this.name = "AuthenticationError";
4034
4191
  }
4035
4192
  };
4036
4193
 
4037
4194
  // src/auth/jwt.ts
4195
+ function resolveAuthErrorCode(status) {
4196
+ if (status === 401) return ErrorCode.AUTH_CREDENTIALS_INVALID;
4197
+ if (status === 403) return ErrorCode.AUTH_TOKEN_INVALID;
4198
+ return ErrorCode.AUTH_JWT_FAILED;
4199
+ }
4038
4200
  async function authenticateJwt(config, credentials) {
4039
4201
  const url = `${resolveBaseUrl(config)}/wp-json/jwt-auth/v1/token`;
4040
4202
  const response = await fetch(url, {
@@ -4044,9 +4206,12 @@ async function authenticateJwt(config, credentials) {
4044
4206
  cache: "no-store"
4045
4207
  });
4046
4208
  if (!response.ok) {
4209
+ const code = resolveAuthErrorCode(response.status);
4047
4210
  throw new AuthenticationError(
4048
- `JWT authentication failed: ${response.statusText}`,
4049
- response.status
4211
+ code,
4212
+ response.status,
4213
+ resolveMessage(code, config.errorMessages),
4214
+ response.statusText
4050
4215
  );
4051
4216
  }
4052
4217
  return response.json();
@@ -4061,6 +4226,6 @@ async function validateJwtToken(config, token) {
4061
4226
  return response.ok;
4062
4227
  }
4063
4228
 
4064
- export { AuthenticationError, WPGraphQLError, authenticateJwt, buildACFFragment, buildPageWithACFQuery, buildPostWithACFQuery, buildPostsWithACFQuery, createApplicationPasswordToken, createAuthorsMutations, createCF7Queries, createCPTQueries, createCategoriesMutations, createCommentsMutations, createFaustAuthHandler, createMediaMutations, createMenusMutations, createPagesMutations, createPostsMutations, createPreviewHandler, createRevalidationHandler, createTagsMutations, createWPGraphQLCoreClient as createWPGraphQLClient, createWPGraphQLCoreClient, createWPGraphQLFetcher, createWPGraphQLMutationsClient, createWPGraphQLWooCommerceClient, createWooCommerceClient, createWooCommerceFetcher, createWordPressClient, createWordPressMutationsClient, createYoastQueries, extractOmnibusData, resolveBaseUrl, validateJwtToken, withOmnibus, withOmnibusVariation };
4229
+ export { AuthenticationError, ErrorCode, WPGraphQLError, WooCommerceError, WordPressAPIError, authenticateJwt, buildACFFragment, buildPageWithACFQuery, buildPostWithACFQuery, buildPostsWithACFQuery, createApplicationPasswordToken, createAuthorsMutations, createCF7Queries, createCPTQueries, createCategoriesMutations, createCommentsMutations, createFaustAuthHandler, createMediaMutations, createMenusMutations, createPagesMutations, createPostsMutations, createPreviewHandler, createRevalidationHandler, createTagsMutations, createWPGraphQLCoreClient as createWPGraphQLClient, createWPGraphQLCoreClient, createWPGraphQLFetcher, createWPGraphQLMutationsClient, createWPGraphQLWooCommerceClient, createWooCommerceClient, createWooCommerceFetcher, createWordPressClient, createWordPressMutationsClient, createYoastQueries, defaultMessages, defaultMessagesPl, extractOmnibusData, resolveBaseUrl, resolveMessage, validateJwtToken, withOmnibus, withOmnibusVariation };
4065
4230
  //# sourceMappingURL=index.js.map
4066
4231
  //# sourceMappingURL=index.js.map