@szymonpiatek/nextwordpress 0.0.6 → 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/hooks/index.cjs +170 -30
- 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 -30
- package/dist/hooks/index.js.map +1 -1
- package/dist/index.cjs +934 -31
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +577 -3
- package/dist/index.d.ts +577 -3
- package/dist/index.js +929 -32
- package/dist/index.js.map +1 -1
- package/dist/{types-soq-mA1E.d.cts → types-Dxb6tuW_.d.cts} +54 -4
- package/dist/{types-soq-mA1E.d.ts → types-Dxb6tuW_.d.ts} +54 -4
- package/package.json +15 -5
package/dist/hooks/index.js
CHANGED
|
@@ -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,
|
|
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
|
-
|
|
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(
|
|
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
|
-
|
|
140
|
-
|
|
141
|
-
if (
|
|
142
|
-
|
|
143
|
-
|
|
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;
|
|
@@ -414,7 +528,20 @@ function createWooCommerceFetcher(config) {
|
|
|
414
528
|
return { ...query, consumer_key: config.consumerKey, consumer_secret: config.consumerSecret };
|
|
415
529
|
}
|
|
416
530
|
function buildUrl2(path, query) {
|
|
417
|
-
|
|
531
|
+
const base = typeof window !== "undefined" && config.clientURL ? config.clientURL : config.serverURL;
|
|
532
|
+
return `${base}${path}?${toQueryString(withAuth(query))}`;
|
|
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
|
+
);
|
|
418
545
|
}
|
|
419
546
|
async function wcFetch(path, query, tags = ["woocommerce"], options) {
|
|
420
547
|
const url = buildUrl2(path, query);
|
|
@@ -430,9 +557,7 @@ function createWooCommerceFetcher(config) {
|
|
|
430
557
|
next: isMutation || noStore ? void 0 : { tags, revalidate: cacheTTL }
|
|
431
558
|
});
|
|
432
559
|
if (!response.ok) {
|
|
433
|
-
|
|
434
|
-
const detail = body.message ? ` \u2013 ${body.message}` : "";
|
|
435
|
-
throw new Error(`WooCommerce API ${response.status}: ${response.statusText}${detail} [${url}]`);
|
|
560
|
+
await throwWooError(response, url);
|
|
436
561
|
}
|
|
437
562
|
return response.json();
|
|
438
563
|
}
|
|
@@ -451,7 +576,7 @@ function createWooCommerceFetcher(config) {
|
|
|
451
576
|
next: { tags, revalidate: cacheTTL }
|
|
452
577
|
});
|
|
453
578
|
if (!response.ok) {
|
|
454
|
-
|
|
579
|
+
await throwWooError(response, url);
|
|
455
580
|
}
|
|
456
581
|
return {
|
|
457
582
|
data: await response.json(),
|
|
@@ -917,8 +1042,9 @@ function useCustomer() {
|
|
|
917
1042
|
|
|
918
1043
|
// src/integrations/wpGraphQL/client/types.ts
|
|
919
1044
|
var WPGraphQLError = class extends Error {
|
|
920
|
-
constructor(
|
|
1045
|
+
constructor(code, status, endpoint, message, gqlErrors) {
|
|
921
1046
|
super(message);
|
|
1047
|
+
__publicField(this, "code", code);
|
|
922
1048
|
__publicField(this, "status", status);
|
|
923
1049
|
__publicField(this, "endpoint", endpoint);
|
|
924
1050
|
__publicField(this, "gqlErrors", gqlErrors);
|
|
@@ -943,22 +1069,29 @@ function createWPGraphQLFetcher(config) {
|
|
|
943
1069
|
});
|
|
944
1070
|
if (!response.ok) {
|
|
945
1071
|
throw new WPGraphQLError(
|
|
946
|
-
|
|
1072
|
+
ErrorCode.GQL_REQUEST_FAILED,
|
|
947
1073
|
response.status,
|
|
948
|
-
url
|
|
1074
|
+
url,
|
|
1075
|
+
resolveMessage(ErrorCode.GQL_REQUEST_FAILED, config.errorMessages)
|
|
949
1076
|
);
|
|
950
1077
|
}
|
|
951
1078
|
const parsed = await response.json();
|
|
952
1079
|
if (parsed.errors && parsed.errors.length > 0) {
|
|
953
1080
|
throw new WPGraphQLError(
|
|
954
|
-
|
|
1081
|
+
ErrorCode.GQL_ERRORS_IN_RESPONSE,
|
|
955
1082
|
200,
|
|
956
1083
|
url,
|
|
1084
|
+
resolveMessage(ErrorCode.GQL_ERRORS_IN_RESPONSE, config.errorMessages),
|
|
957
1085
|
parsed.errors
|
|
958
1086
|
);
|
|
959
1087
|
}
|
|
960
1088
|
if (parsed.data === void 0) {
|
|
961
|
-
throw new WPGraphQLError(
|
|
1089
|
+
throw new WPGraphQLError(
|
|
1090
|
+
ErrorCode.GQL_NO_DATA,
|
|
1091
|
+
200,
|
|
1092
|
+
url,
|
|
1093
|
+
resolveMessage(ErrorCode.GQL_NO_DATA, config.errorMessages)
|
|
1094
|
+
);
|
|
962
1095
|
}
|
|
963
1096
|
return parsed.data;
|
|
964
1097
|
}
|
|
@@ -986,22 +1119,29 @@ function createWPGraphQLFetcher(config) {
|
|
|
986
1119
|
});
|
|
987
1120
|
if (!response.ok) {
|
|
988
1121
|
throw new WPGraphQLError(
|
|
989
|
-
|
|
1122
|
+
ErrorCode.GQL_REQUEST_FAILED,
|
|
990
1123
|
response.status,
|
|
991
|
-
url
|
|
1124
|
+
url,
|
|
1125
|
+
resolveMessage(ErrorCode.GQL_REQUEST_FAILED, config.errorMessages)
|
|
992
1126
|
);
|
|
993
1127
|
}
|
|
994
1128
|
const parsed = await response.json();
|
|
995
1129
|
if (parsed.errors && parsed.errors.length > 0) {
|
|
996
1130
|
throw new WPGraphQLError(
|
|
997
|
-
|
|
1131
|
+
ErrorCode.GQL_ERRORS_IN_RESPONSE,
|
|
998
1132
|
200,
|
|
999
1133
|
url,
|
|
1134
|
+
resolveMessage(ErrorCode.GQL_ERRORS_IN_RESPONSE, config.errorMessages),
|
|
1000
1135
|
parsed.errors
|
|
1001
1136
|
);
|
|
1002
1137
|
}
|
|
1003
1138
|
if (parsed.data === void 0) {
|
|
1004
|
-
throw new WPGraphQLError(
|
|
1139
|
+
throw new WPGraphQLError(
|
|
1140
|
+
ErrorCode.GQL_NO_DATA,
|
|
1141
|
+
200,
|
|
1142
|
+
url,
|
|
1143
|
+
resolveMessage(ErrorCode.GQL_NO_DATA, config.errorMessages)
|
|
1144
|
+
);
|
|
1005
1145
|
}
|
|
1006
1146
|
return parsed.data;
|
|
1007
1147
|
}
|