@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/hooks/index.cjs +170 -31
- package/dist/hooks/index.cjs.map +1 -1
- package/dist/hooks/index.d.cts +1 -1
- package/dist/hooks/index.d.ts +1 -1
- package/dist/hooks/index.js +170 -31
- package/dist/hooks/index.js.map +1 -1
- package/dist/index.cjs +230 -59
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +225 -60
- package/dist/index.js.map +1 -1
- package/dist/{types-Be60n09c.d.cts → types-Dxb6tuW_.d.cts} +53 -4
- package/dist/{types-Be60n09c.d.ts → types-Dxb6tuW_.d.ts} +53 -4
- package/package.json +15 -5
package/dist/hooks/index.cjs
CHANGED
|
@@ -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,
|
|
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
|
-
|
|
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(
|
|
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
|
-
|
|
146
|
-
|
|
147
|
-
if (
|
|
148
|
-
|
|
149
|
-
|
|
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, {
|
|
@@ -201,7 +279,7 @@ function createFetcher(config) {
|
|
|
201
279
|
"User-Agent": USER_AGENT
|
|
202
280
|
};
|
|
203
281
|
if (authToken) {
|
|
204
|
-
headers["Authorization"] = authToken
|
|
282
|
+
headers["Authorization"] = `Bearer ${authToken}`;
|
|
205
283
|
}
|
|
206
284
|
const res = await doFetch(url, {
|
|
207
285
|
method,
|
|
@@ -219,7 +297,7 @@ function createFetcher(config) {
|
|
|
219
297
|
"Content-Disposition": `attachment; filename="${filename}"`,
|
|
220
298
|
"Content-Type": mimeType,
|
|
221
299
|
"User-Agent": USER_AGENT,
|
|
222
|
-
"Authorization": authToken
|
|
300
|
+
"Authorization": `Bearer ${authToken}`
|
|
223
301
|
},
|
|
224
302
|
body: file,
|
|
225
303
|
cache: "no-store"
|
|
@@ -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;
|
|
@@ -423,6 +537,18 @@ function createWooCommerceFetcher(config) {
|
|
|
423
537
|
const base = typeof window !== "undefined" && config.clientURL ? config.clientURL : config.serverURL;
|
|
424
538
|
return `${base}${path}?${toQueryString(withAuth(query))}`;
|
|
425
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
|
+
);
|
|
551
|
+
}
|
|
426
552
|
async function wcFetch(path, query, tags = ["woocommerce"], options) {
|
|
427
553
|
const url = buildUrl2(path, query);
|
|
428
554
|
const isMutation = options?.method && options.method !== "GET";
|
|
@@ -437,9 +563,7 @@ function createWooCommerceFetcher(config) {
|
|
|
437
563
|
next: isMutation || noStore ? void 0 : { tags, revalidate: cacheTTL }
|
|
438
564
|
});
|
|
439
565
|
if (!response.ok) {
|
|
440
|
-
|
|
441
|
-
const detail = body.message ? ` \u2013 ${body.message}` : "";
|
|
442
|
-
throw new Error(`WooCommerce API ${response.status}: ${response.statusText}${detail} [${url}]`);
|
|
566
|
+
await throwWooError(response, url);
|
|
443
567
|
}
|
|
444
568
|
return response.json();
|
|
445
569
|
}
|
|
@@ -458,7 +582,7 @@ function createWooCommerceFetcher(config) {
|
|
|
458
582
|
next: { tags, revalidate: cacheTTL }
|
|
459
583
|
});
|
|
460
584
|
if (!response.ok) {
|
|
461
|
-
|
|
585
|
+
await throwWooError(response, url);
|
|
462
586
|
}
|
|
463
587
|
return {
|
|
464
588
|
data: await response.json(),
|
|
@@ -924,8 +1048,9 @@ function useCustomer() {
|
|
|
924
1048
|
|
|
925
1049
|
// src/integrations/wpGraphQL/client/types.ts
|
|
926
1050
|
var WPGraphQLError = class extends Error {
|
|
927
|
-
constructor(
|
|
1051
|
+
constructor(code, status, endpoint, message, gqlErrors) {
|
|
928
1052
|
super(message);
|
|
1053
|
+
__publicField(this, "code", code);
|
|
929
1054
|
__publicField(this, "status", status);
|
|
930
1055
|
__publicField(this, "endpoint", endpoint);
|
|
931
1056
|
__publicField(this, "gqlErrors", gqlErrors);
|
|
@@ -950,22 +1075,29 @@ function createWPGraphQLFetcher(config) {
|
|
|
950
1075
|
});
|
|
951
1076
|
if (!response.ok) {
|
|
952
1077
|
throw new WPGraphQLError(
|
|
953
|
-
|
|
1078
|
+
ErrorCode.GQL_REQUEST_FAILED,
|
|
954
1079
|
response.status,
|
|
955
|
-
url
|
|
1080
|
+
url,
|
|
1081
|
+
resolveMessage(ErrorCode.GQL_REQUEST_FAILED, config.errorMessages)
|
|
956
1082
|
);
|
|
957
1083
|
}
|
|
958
1084
|
const parsed = await response.json();
|
|
959
1085
|
if (parsed.errors && parsed.errors.length > 0) {
|
|
960
1086
|
throw new WPGraphQLError(
|
|
961
|
-
|
|
1087
|
+
ErrorCode.GQL_ERRORS_IN_RESPONSE,
|
|
962
1088
|
200,
|
|
963
1089
|
url,
|
|
1090
|
+
resolveMessage(ErrorCode.GQL_ERRORS_IN_RESPONSE, config.errorMessages),
|
|
964
1091
|
parsed.errors
|
|
965
1092
|
);
|
|
966
1093
|
}
|
|
967
1094
|
if (parsed.data === void 0) {
|
|
968
|
-
throw new WPGraphQLError(
|
|
1095
|
+
throw new WPGraphQLError(
|
|
1096
|
+
ErrorCode.GQL_NO_DATA,
|
|
1097
|
+
200,
|
|
1098
|
+
url,
|
|
1099
|
+
resolveMessage(ErrorCode.GQL_NO_DATA, config.errorMessages)
|
|
1100
|
+
);
|
|
969
1101
|
}
|
|
970
1102
|
return parsed.data;
|
|
971
1103
|
}
|
|
@@ -993,22 +1125,29 @@ function createWPGraphQLFetcher(config) {
|
|
|
993
1125
|
});
|
|
994
1126
|
if (!response.ok) {
|
|
995
1127
|
throw new WPGraphQLError(
|
|
996
|
-
|
|
1128
|
+
ErrorCode.GQL_REQUEST_FAILED,
|
|
997
1129
|
response.status,
|
|
998
|
-
url
|
|
1130
|
+
url,
|
|
1131
|
+
resolveMessage(ErrorCode.GQL_REQUEST_FAILED, config.errorMessages)
|
|
999
1132
|
);
|
|
1000
1133
|
}
|
|
1001
1134
|
const parsed = await response.json();
|
|
1002
1135
|
if (parsed.errors && parsed.errors.length > 0) {
|
|
1003
1136
|
throw new WPGraphQLError(
|
|
1004
|
-
|
|
1137
|
+
ErrorCode.GQL_ERRORS_IN_RESPONSE,
|
|
1005
1138
|
200,
|
|
1006
1139
|
url,
|
|
1140
|
+
resolveMessage(ErrorCode.GQL_ERRORS_IN_RESPONSE, config.errorMessages),
|
|
1007
1141
|
parsed.errors
|
|
1008
1142
|
);
|
|
1009
1143
|
}
|
|
1010
1144
|
if (parsed.data === void 0) {
|
|
1011
|
-
throw new WPGraphQLError(
|
|
1145
|
+
throw new WPGraphQLError(
|
|
1146
|
+
ErrorCode.GQL_NO_DATA,
|
|
1147
|
+
200,
|
|
1148
|
+
url,
|
|
1149
|
+
resolveMessage(ErrorCode.GQL_NO_DATA, config.errorMessages)
|
|
1150
|
+
);
|
|
1012
1151
|
}
|
|
1013
1152
|
return parsed.data;
|
|
1014
1153
|
}
|