@atomic-solutions/wordpress-api-client 0.1.0
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/LICENSE +21 -0
- package/README.md +295 -0
- package/dist/calculatePagination-WUlN3OdM.d.cts +19 -0
- package/dist/calculatePagination-WUlN3OdM.d.ts +19 -0
- package/dist/client/index.cjs +1143 -0
- package/dist/client/index.cjs.map +1 -0
- package/dist/client/index.d.cts +10 -0
- package/dist/client/index.d.ts +10 -0
- package/dist/client/index.js +1137 -0
- package/dist/client/index.js.map +1 -0
- package/dist/http/index.cjs +674 -0
- package/dist/http/index.cjs.map +1 -0
- package/dist/http/index.d.cts +12 -0
- package/dist/http/index.d.ts +12 -0
- package/dist/http/index.js +667 -0
- package/dist/http/index.js.map +1 -0
- package/dist/index.cjs +1251 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +51 -0
- package/dist/index.d.ts +51 -0
- package/dist/index.js +1205 -0
- package/dist/index.js.map +1 -0
- package/dist/schemas/index.cjs +414 -0
- package/dist/schemas/index.cjs.map +1 -0
- package/dist/schemas/index.d.cts +1339 -0
- package/dist/schemas/index.d.ts +1339 -0
- package/dist/schemas/index.js +376 -0
- package/dist/schemas/index.js.map +1 -0
- package/dist/testing/index.cjs +1435 -0
- package/dist/testing/index.cjs.map +1 -0
- package/dist/testing/index.d.cts +50 -0
- package/dist/testing/index.d.ts +50 -0
- package/dist/testing/index.js +1426 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/types-8pbwmNdu.d.ts +154 -0
- package/dist/types-BTo088EY.d.cts +154 -0
- package/dist/user.schema-eeUHQ4sI.d.cts +10614 -0
- package/dist/user.schema-eeUHQ4sI.d.ts +10614 -0
- package/dist/utils/index.cjs +47 -0
- package/dist/utils/index.cjs.map +1 -0
- package/dist/utils/index.d.cts +10 -0
- package/dist/utils/index.d.ts +10 -0
- package/dist/utils/index.js +44 -0
- package/dist/utils/index.js.map +1 -0
- package/package.json +103 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,1251 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var axios = require('axios');
|
|
4
|
+
var zod = require('zod');
|
|
5
|
+
|
|
6
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
7
|
+
|
|
8
|
+
var axios__default = /*#__PURE__*/_interopDefault(axios);
|
|
9
|
+
|
|
10
|
+
// src/client/createClient.ts
|
|
11
|
+
|
|
12
|
+
// src/constants/endpoints.ts
|
|
13
|
+
var ENDPOINTS = {
|
|
14
|
+
// Posts
|
|
15
|
+
POSTS: "/wp/v2/posts",
|
|
16
|
+
POST: (id) => `/wp/v2/posts/${id}`,
|
|
17
|
+
// Categories
|
|
18
|
+
CATEGORIES: "/wp/v2/categories",
|
|
19
|
+
CATEGORY: (id) => `/wp/v2/categories/${id}`,
|
|
20
|
+
// Tags
|
|
21
|
+
TAGS: "/wp/v2/tags",
|
|
22
|
+
TAG: (id) => `/wp/v2/tags/${id}`,
|
|
23
|
+
// Media
|
|
24
|
+
MEDIA: "/wp/v2/media",
|
|
25
|
+
MEDIA_ITEM: (id) => `/wp/v2/media/${id}`,
|
|
26
|
+
// Users
|
|
27
|
+
USERS: "/wp/v2/users",
|
|
28
|
+
USER: (id) => `/wp/v2/users/${id}`,
|
|
29
|
+
USERS_ME: "/wp/v2/users/me",
|
|
30
|
+
// JWT Auth (requires JWT Authentication plugin)
|
|
31
|
+
JWT_TOKEN: "/jwt-auth/v1/token",
|
|
32
|
+
JWT_VALIDATE: "/jwt-auth/v1/token/validate",
|
|
33
|
+
// Pages
|
|
34
|
+
PAGES: "/wp/v2/pages",
|
|
35
|
+
PAGE: (id) => `/wp/v2/pages/${id}`,
|
|
36
|
+
// Comments
|
|
37
|
+
COMMENTS: "/wp/v2/comments",
|
|
38
|
+
COMMENT: (id) => `/wp/v2/comments/${id}`,
|
|
39
|
+
// Search
|
|
40
|
+
SEARCH: "/wp/v2/search"
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// src/errors/classes/BaseError.ts
|
|
44
|
+
var BaseError = class extends Error {
|
|
45
|
+
constructor(options) {
|
|
46
|
+
super(options.message);
|
|
47
|
+
this.options = options;
|
|
48
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
49
|
+
this.name = this.constructor.name;
|
|
50
|
+
if (Error.captureStackTrace) {
|
|
51
|
+
Error.captureStackTrace(this, this.constructor);
|
|
52
|
+
}
|
|
53
|
+
if (this.options.cause instanceof Error && this.options.cause.stack) {
|
|
54
|
+
this.stack = `${this.stack}
|
|
55
|
+
Caused by: ${this.options.cause.stack}`;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Get reportable data for error tracking/logging
|
|
60
|
+
* Subclasses should override this to add their own reportable fields
|
|
61
|
+
*/
|
|
62
|
+
getReportableData() {
|
|
63
|
+
const { code, operation, userMessage, retryable } = this.options;
|
|
64
|
+
return {
|
|
65
|
+
code,
|
|
66
|
+
operation,
|
|
67
|
+
userMessage,
|
|
68
|
+
retryable
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Custom JSON serialization
|
|
73
|
+
* Makes the error properly serializable for logging/reporting
|
|
74
|
+
*/
|
|
75
|
+
toJSON() {
|
|
76
|
+
return {
|
|
77
|
+
...this.getReportableData(),
|
|
78
|
+
name: this.name,
|
|
79
|
+
message: this.message,
|
|
80
|
+
stack: this.stack
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
// src/errors/mapping/auth.ts
|
|
86
|
+
var AUTH_CODE_TRANSFORMATIONS = {
|
|
87
|
+
// Authentication
|
|
88
|
+
jwt_auth_failed: "auth_failed",
|
|
89
|
+
jwt_auth_invalid_token: "token_expired",
|
|
90
|
+
jwt_auth_bad_request: "invalid_auth_request",
|
|
91
|
+
// Authorization
|
|
92
|
+
rest_forbidden: "forbidden",
|
|
93
|
+
rest_cannot_read: "cannot_read",
|
|
94
|
+
rest_cannot_edit: "cannot_edit",
|
|
95
|
+
rest_cannot_create: "cannot_create",
|
|
96
|
+
rest_cannot_delete: "cannot_delete"
|
|
97
|
+
};
|
|
98
|
+
var AUTH_ERROR_METADATA = {
|
|
99
|
+
// Authentication
|
|
100
|
+
auth_failed: {
|
|
101
|
+
operation: "authenticate",
|
|
102
|
+
retryable: false,
|
|
103
|
+
userMessage: "Invalid username or password"
|
|
104
|
+
},
|
|
105
|
+
token_expired: {
|
|
106
|
+
operation: "validate_token",
|
|
107
|
+
retryable: false,
|
|
108
|
+
userMessage: "Your session has expired. Please sign in again."
|
|
109
|
+
},
|
|
110
|
+
invalid_auth_request: {
|
|
111
|
+
operation: "authenticate",
|
|
112
|
+
retryable: false,
|
|
113
|
+
userMessage: "Invalid login request"
|
|
114
|
+
},
|
|
115
|
+
// Authorization
|
|
116
|
+
forbidden: {
|
|
117
|
+
operation: "authorize",
|
|
118
|
+
retryable: false,
|
|
119
|
+
userMessage: "You do not have permission to access this resource"
|
|
120
|
+
},
|
|
121
|
+
cannot_read: {
|
|
122
|
+
operation: "read_resource",
|
|
123
|
+
retryable: false,
|
|
124
|
+
userMessage: "You do not have permission to view this content"
|
|
125
|
+
},
|
|
126
|
+
cannot_edit: {
|
|
127
|
+
operation: "edit_resource",
|
|
128
|
+
retryable: false,
|
|
129
|
+
userMessage: "You do not have permission to edit this content"
|
|
130
|
+
},
|
|
131
|
+
cannot_create: {
|
|
132
|
+
operation: "create_resource",
|
|
133
|
+
retryable: false,
|
|
134
|
+
userMessage: "You do not have permission to create content"
|
|
135
|
+
},
|
|
136
|
+
cannot_delete: {
|
|
137
|
+
operation: "delete_resource",
|
|
138
|
+
retryable: false,
|
|
139
|
+
userMessage: "You do not have permission to delete this content"
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
// src/errors/mapping/resources.ts
|
|
144
|
+
var RESOURCE_CODE_TRANSFORMATIONS = {
|
|
145
|
+
rest_post_invalid_id: "post_not_found",
|
|
146
|
+
rest_term_invalid: "term_not_found",
|
|
147
|
+
rest_user_invalid_id: "user_not_found",
|
|
148
|
+
rest_no_route: "route_not_found"
|
|
149
|
+
};
|
|
150
|
+
var RESOURCE_ERROR_METADATA = {
|
|
151
|
+
post_not_found: {
|
|
152
|
+
operation: "get_post",
|
|
153
|
+
retryable: false,
|
|
154
|
+
userMessage: "Post not found"
|
|
155
|
+
},
|
|
156
|
+
term_not_found: {
|
|
157
|
+
operation: "get_term",
|
|
158
|
+
retryable: false,
|
|
159
|
+
userMessage: "Category or tag not found"
|
|
160
|
+
},
|
|
161
|
+
user_not_found: {
|
|
162
|
+
operation: "get_user",
|
|
163
|
+
retryable: false,
|
|
164
|
+
userMessage: "User not found"
|
|
165
|
+
},
|
|
166
|
+
route_not_found: {
|
|
167
|
+
operation: "api_request",
|
|
168
|
+
retryable: false,
|
|
169
|
+
userMessage: "The requested resource was not found"
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
// src/errors/mapping/server.ts
|
|
174
|
+
var SERVER_CODE_TRANSFORMATIONS = {
|
|
175
|
+
rest_cookie_invalid_nonce: "invalid_nonce",
|
|
176
|
+
internal_error: "server_error"
|
|
177
|
+
};
|
|
178
|
+
var SERVER_ERROR_METADATA = {
|
|
179
|
+
invalid_nonce: {
|
|
180
|
+
operation: "api_request",
|
|
181
|
+
retryable: true,
|
|
182
|
+
userMessage: "Session expired. Please try again."
|
|
183
|
+
},
|
|
184
|
+
server_error: {
|
|
185
|
+
operation: "api_request",
|
|
186
|
+
retryable: true,
|
|
187
|
+
userMessage: "An unexpected error occurred. Please try again."
|
|
188
|
+
},
|
|
189
|
+
unknown_error: {
|
|
190
|
+
operation: "api_request",
|
|
191
|
+
retryable: false,
|
|
192
|
+
userMessage: "An error occurred"
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
// src/errors/mapping/constants.ts
|
|
197
|
+
var VALIDATION_CODE_TRANSFORMATIONS = {
|
|
198
|
+
rest_invalid_param: "invalid_params",
|
|
199
|
+
rest_missing_callback_param: "missing_params"
|
|
200
|
+
};
|
|
201
|
+
var VALIDATION_ERROR_METADATA = {
|
|
202
|
+
invalid_params: {
|
|
203
|
+
operation: "api_request",
|
|
204
|
+
retryable: false,
|
|
205
|
+
userMessage: "Invalid request parameters"
|
|
206
|
+
},
|
|
207
|
+
missing_params: {
|
|
208
|
+
operation: "api_request",
|
|
209
|
+
retryable: false,
|
|
210
|
+
userMessage: "Missing required parameters"
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
var WORDPRESS_ERROR_METADATA = {
|
|
214
|
+
...AUTH_ERROR_METADATA,
|
|
215
|
+
...RESOURCE_ERROR_METADATA,
|
|
216
|
+
...VALIDATION_ERROR_METADATA,
|
|
217
|
+
...SERVER_ERROR_METADATA
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
// src/errors/mapping/index.ts
|
|
221
|
+
var CODE_TRANSFORMATIONS = {
|
|
222
|
+
...AUTH_CODE_TRANSFORMATIONS,
|
|
223
|
+
...RESOURCE_CODE_TRANSFORMATIONS,
|
|
224
|
+
...VALIDATION_CODE_TRANSFORMATIONS,
|
|
225
|
+
...SERVER_CODE_TRANSFORMATIONS
|
|
226
|
+
};
|
|
227
|
+
var ERROR_METADATA = {
|
|
228
|
+
...AUTH_ERROR_METADATA,
|
|
229
|
+
...RESOURCE_ERROR_METADATA,
|
|
230
|
+
...VALIDATION_ERROR_METADATA,
|
|
231
|
+
...SERVER_ERROR_METADATA
|
|
232
|
+
};
|
|
233
|
+
var mapWordPressCode = (apiCode, status) => {
|
|
234
|
+
const appCode = CODE_TRANSFORMATIONS[apiCode];
|
|
235
|
+
if (appCode) {
|
|
236
|
+
const metadata = ERROR_METADATA[appCode];
|
|
237
|
+
return {
|
|
238
|
+
code: appCode,
|
|
239
|
+
...metadata
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
if (apiCode.includes("invalid_id") || apiCode.includes("not_found")) {
|
|
243
|
+
return {
|
|
244
|
+
code: "route_not_found",
|
|
245
|
+
...ERROR_METADATA.route_not_found
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
if (apiCode.includes("cannot_") || apiCode.includes("forbidden")) {
|
|
249
|
+
return {
|
|
250
|
+
code: "forbidden",
|
|
251
|
+
...ERROR_METADATA.forbidden
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
if (apiCode.includes("invalid_param") || apiCode.includes("missing_")) {
|
|
255
|
+
return {
|
|
256
|
+
code: "invalid_params",
|
|
257
|
+
...ERROR_METADATA.invalid_params
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
if (apiCode.includes("nonce")) {
|
|
261
|
+
return {
|
|
262
|
+
code: "invalid_nonce",
|
|
263
|
+
...ERROR_METADATA.invalid_nonce
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
if (status === 401) {
|
|
267
|
+
return {
|
|
268
|
+
code: "auth_failed",
|
|
269
|
+
retryable: false,
|
|
270
|
+
userMessage: "Authentication required"
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
if (status === 403) {
|
|
274
|
+
return {
|
|
275
|
+
code: "forbidden",
|
|
276
|
+
...ERROR_METADATA.forbidden
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
if (status === 404) {
|
|
280
|
+
return {
|
|
281
|
+
code: "route_not_found",
|
|
282
|
+
...ERROR_METADATA.route_not_found
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
if (status >= 500) {
|
|
286
|
+
return {
|
|
287
|
+
code: "server_error",
|
|
288
|
+
...ERROR_METADATA.server_error
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
return {
|
|
292
|
+
code: "unknown_error",
|
|
293
|
+
...ERROR_METADATA.unknown_error
|
|
294
|
+
};
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
// src/errors/classes/WordPressApiError.ts
|
|
298
|
+
var WordPressApiError = class extends BaseError {
|
|
299
|
+
constructor(options) {
|
|
300
|
+
const metadata = WORDPRESS_ERROR_METADATA[options.code] ?? {
|
|
301
|
+
operation: "wordpress_api_request",
|
|
302
|
+
retryable: false
|
|
303
|
+
};
|
|
304
|
+
const enrichedOptions = {
|
|
305
|
+
...options,
|
|
306
|
+
operation: options.operation || metadata.operation,
|
|
307
|
+
userMessage: options.userMessage || metadata.userMessage,
|
|
308
|
+
retryable: options.retryable ?? metadata.retryable
|
|
309
|
+
};
|
|
310
|
+
super(enrichedOptions);
|
|
311
|
+
}
|
|
312
|
+
// Convenient getters for frequently accessed fields (no duplication)
|
|
313
|
+
get wpCode() {
|
|
314
|
+
return this.options.code;
|
|
315
|
+
}
|
|
316
|
+
get originalCode() {
|
|
317
|
+
return this.options.originalCode;
|
|
318
|
+
}
|
|
319
|
+
get statusCode() {
|
|
320
|
+
return this.options.statusCode;
|
|
321
|
+
}
|
|
322
|
+
get url() {
|
|
323
|
+
return this.options.url;
|
|
324
|
+
}
|
|
325
|
+
get method() {
|
|
326
|
+
return this.options.method;
|
|
327
|
+
}
|
|
328
|
+
get requestBody() {
|
|
329
|
+
return this.options.requestBody;
|
|
330
|
+
}
|
|
331
|
+
get responseBody() {
|
|
332
|
+
return this.options.responseBody;
|
|
333
|
+
}
|
|
334
|
+
get data() {
|
|
335
|
+
return this.options.data;
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Check if error is an authentication error
|
|
339
|
+
*/
|
|
340
|
+
get isAuthError() {
|
|
341
|
+
return this.statusCode === 401 || this.wpCode === "auth_failed" || this.wpCode === "token_expired" || this.wpCode === "invalid_auth_request";
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Check if error is a not found error
|
|
345
|
+
*/
|
|
346
|
+
get isNotFoundError() {
|
|
347
|
+
return this.statusCode === 404 || this.wpCode === "route_not_found" || this.wpCode === "post_not_found" || this.wpCode === "term_not_found" || this.wpCode === "user_not_found";
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Get reportable data for error tracking/logging
|
|
351
|
+
* Includes HTTP context and WordPress-specific fields
|
|
352
|
+
*/
|
|
353
|
+
getReportableData() {
|
|
354
|
+
return {
|
|
355
|
+
code: this.options.code,
|
|
356
|
+
operation: this.options.operation,
|
|
357
|
+
userMessage: this.options.userMessage,
|
|
358
|
+
retryable: this.options.retryable,
|
|
359
|
+
statusCode: this.options.statusCode,
|
|
360
|
+
url: this.options.url,
|
|
361
|
+
method: this.options.method,
|
|
362
|
+
wpCode: this.options.code,
|
|
363
|
+
originalCode: this.options.originalCode
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
// src/errors/classes/WordPressDataValidationError.ts
|
|
369
|
+
var WordPressDataValidationError = class _WordPressDataValidationError extends BaseError {
|
|
370
|
+
constructor(options) {
|
|
371
|
+
const fieldErrors = options.zodError ? _WordPressDataValidationError.extractFieldErrorsFromZod(options.zodError) : void 0;
|
|
372
|
+
super({
|
|
373
|
+
code: "validation_error",
|
|
374
|
+
message: options.message,
|
|
375
|
+
operation: options.operation,
|
|
376
|
+
userMessage: options.userMessage || "Received unexpected data from WordPress",
|
|
377
|
+
retryable: false,
|
|
378
|
+
cause: options.cause,
|
|
379
|
+
url: options.url,
|
|
380
|
+
zodError: options.zodError,
|
|
381
|
+
value: options.value,
|
|
382
|
+
fieldErrors
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
// Convenient getters for validation-specific fields (no duplication)
|
|
386
|
+
get url() {
|
|
387
|
+
return this.options.url;
|
|
388
|
+
}
|
|
389
|
+
get zodError() {
|
|
390
|
+
return this.options.zodError;
|
|
391
|
+
}
|
|
392
|
+
get invalidValue() {
|
|
393
|
+
return this.options.value;
|
|
394
|
+
}
|
|
395
|
+
get fieldErrors() {
|
|
396
|
+
return this.options.fieldErrors;
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* Get reportable data for error tracking/logging
|
|
400
|
+
* Includes validation-specific fields
|
|
401
|
+
*/
|
|
402
|
+
getReportableData() {
|
|
403
|
+
return {
|
|
404
|
+
code: this.options.code,
|
|
405
|
+
operation: this.options.operation,
|
|
406
|
+
userMessage: this.options.userMessage,
|
|
407
|
+
retryable: false,
|
|
408
|
+
fieldErrors: this.options.fieldErrors,
|
|
409
|
+
url: this.options.url
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
/**
|
|
413
|
+
* Extract field errors from Zod error
|
|
414
|
+
*/
|
|
415
|
+
static extractFieldErrorsFromZod(zodError) {
|
|
416
|
+
if (!zodError?.errors) {
|
|
417
|
+
return void 0;
|
|
418
|
+
}
|
|
419
|
+
const fieldErrors = {};
|
|
420
|
+
for (const issue of zodError.errors) {
|
|
421
|
+
const path = issue.path.join(".");
|
|
422
|
+
if (!fieldErrors[path]) {
|
|
423
|
+
fieldErrors[path] = [];
|
|
424
|
+
}
|
|
425
|
+
fieldErrors[path].push(issue.message);
|
|
426
|
+
}
|
|
427
|
+
return fieldErrors;
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* Get all error messages as a flat array
|
|
431
|
+
*/
|
|
432
|
+
getMessages() {
|
|
433
|
+
if (!this.fieldErrors) {
|
|
434
|
+
return [this.message];
|
|
435
|
+
}
|
|
436
|
+
const messages = [];
|
|
437
|
+
for (const fieldMessages of Object.values(this.fieldErrors)) {
|
|
438
|
+
messages.push(...fieldMessages);
|
|
439
|
+
}
|
|
440
|
+
return messages;
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* Check if a specific field has errors
|
|
444
|
+
*/
|
|
445
|
+
hasFieldError(field) {
|
|
446
|
+
return this.fieldErrors ? field in this.fieldErrors : false;
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* Get error messages for a specific field
|
|
450
|
+
*/
|
|
451
|
+
getFieldError(field) {
|
|
452
|
+
return this.fieldErrors?.[field];
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
|
|
456
|
+
// src/http/handleApiResponse.ts
|
|
457
|
+
var handleApiResponse = (response, schema, options) => {
|
|
458
|
+
const validationMode = options?.validationMode ?? "strict";
|
|
459
|
+
if (validationMode === "warn") {
|
|
460
|
+
const result = schema.safeParse(response.data);
|
|
461
|
+
if (!result.success) {
|
|
462
|
+
const validationError = new WordPressDataValidationError({
|
|
463
|
+
message: `WordPress API response validation failed for ${response.config.url || "unknown endpoint"}`,
|
|
464
|
+
url: response.config.url,
|
|
465
|
+
zodError: result.error,
|
|
466
|
+
value: response.data,
|
|
467
|
+
userMessage: "Received unexpected data from WordPress"
|
|
468
|
+
});
|
|
469
|
+
options?.onValidationError?.(validationError);
|
|
470
|
+
options?.errorReporter?.(validationError);
|
|
471
|
+
console.warn("[wordpress-utils] Validation warning:", validationError.message);
|
|
472
|
+
return response.data;
|
|
473
|
+
}
|
|
474
|
+
return result.data;
|
|
475
|
+
}
|
|
476
|
+
try {
|
|
477
|
+
return schema.parse(response.data);
|
|
478
|
+
} catch (error2) {
|
|
479
|
+
if (error2 && typeof error2 === "object" && "issues" in error2) {
|
|
480
|
+
const validationError = new WordPressDataValidationError({
|
|
481
|
+
message: `WordPress API response validation failed for ${response.config.url || "unknown endpoint"}`,
|
|
482
|
+
url: response.config.url,
|
|
483
|
+
zodError: error2,
|
|
484
|
+
value: response.data,
|
|
485
|
+
userMessage: "Received unexpected data from WordPress"
|
|
486
|
+
});
|
|
487
|
+
options?.onValidationError?.(validationError);
|
|
488
|
+
options?.errorReporter?.(validationError);
|
|
489
|
+
throw validationError;
|
|
490
|
+
}
|
|
491
|
+
throw error2;
|
|
492
|
+
}
|
|
493
|
+
};
|
|
494
|
+
|
|
495
|
+
// src/utils/calculatePagination.ts
|
|
496
|
+
var calculatePagination = (input) => {
|
|
497
|
+
const { page, perPage, total, totalPages } = input;
|
|
498
|
+
const hasNextPage = page < totalPages;
|
|
499
|
+
const hasPrevPage = page > 1;
|
|
500
|
+
return {
|
|
501
|
+
page,
|
|
502
|
+
perPage,
|
|
503
|
+
total,
|
|
504
|
+
totalPages,
|
|
505
|
+
hasNextPage,
|
|
506
|
+
hasPrevPage,
|
|
507
|
+
nextPage: hasNextPage ? page + 1 : null,
|
|
508
|
+
prevPage: hasPrevPage ? page - 1 : null
|
|
509
|
+
};
|
|
510
|
+
};
|
|
511
|
+
|
|
512
|
+
// src/utils/getPaginationMeta.ts
|
|
513
|
+
var getPaginationMeta = (headers) => {
|
|
514
|
+
if (!headers) {
|
|
515
|
+
return {
|
|
516
|
+
total: 0,
|
|
517
|
+
totalPages: 0
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
const wpTotal = headers["x-wp-total"];
|
|
521
|
+
const wpTotalPages = headers["x-wp-totalpages"];
|
|
522
|
+
if (!wpTotal || !wpTotalPages) {
|
|
523
|
+
return {
|
|
524
|
+
total: 0,
|
|
525
|
+
totalPages: 0
|
|
526
|
+
};
|
|
527
|
+
}
|
|
528
|
+
const total = parseInt(wpTotal, 10);
|
|
529
|
+
const totalPages = parseInt(wpTotalPages, 10);
|
|
530
|
+
return {
|
|
531
|
+
total,
|
|
532
|
+
totalPages
|
|
533
|
+
};
|
|
534
|
+
};
|
|
535
|
+
|
|
536
|
+
// src/http/handlePaginatedApiResponse.ts
|
|
537
|
+
var handlePaginatedApiResponse = (response, schema, params = {}, options) => {
|
|
538
|
+
const validationMode = options?.validationMode ?? "strict";
|
|
539
|
+
let data;
|
|
540
|
+
if (validationMode === "warn") {
|
|
541
|
+
const result = schema.safeParse(response.data);
|
|
542
|
+
if (!result.success) {
|
|
543
|
+
const validationError = new WordPressDataValidationError({
|
|
544
|
+
message: `WordPress API response validation failed for ${response.config.url || "unknown endpoint"}`,
|
|
545
|
+
url: response.config.url,
|
|
546
|
+
zodError: result.error,
|
|
547
|
+
value: response.data,
|
|
548
|
+
userMessage: "Received unexpected data from WordPress"
|
|
549
|
+
});
|
|
550
|
+
options?.onValidationError?.(validationError);
|
|
551
|
+
options?.errorReporter?.(validationError);
|
|
552
|
+
console.warn("[wordpress-utils] Validation warning:", validationError.message);
|
|
553
|
+
data = Array.isArray(response.data) ? response.data : [];
|
|
554
|
+
} else {
|
|
555
|
+
data = result.data;
|
|
556
|
+
}
|
|
557
|
+
} else {
|
|
558
|
+
try {
|
|
559
|
+
data = schema.parse(response.data);
|
|
560
|
+
} catch (error2) {
|
|
561
|
+
if (error2 && typeof error2 === "object" && "issues" in error2) {
|
|
562
|
+
const validationError = new WordPressDataValidationError({
|
|
563
|
+
message: `WordPress API response validation failed for ${response.config.url || "unknown endpoint"}`,
|
|
564
|
+
url: response.config.url,
|
|
565
|
+
zodError: error2,
|
|
566
|
+
value: response.data,
|
|
567
|
+
userMessage: "Received unexpected data from WordPress"
|
|
568
|
+
});
|
|
569
|
+
options?.onValidationError?.(validationError);
|
|
570
|
+
options?.errorReporter?.(validationError);
|
|
571
|
+
throw validationError;
|
|
572
|
+
}
|
|
573
|
+
throw error2;
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
const { total, totalPages } = getPaginationMeta(response.headers);
|
|
577
|
+
const page = params.page || 1;
|
|
578
|
+
const perPage = params.per_page || 10;
|
|
579
|
+
const pagination = calculatePagination({
|
|
580
|
+
page,
|
|
581
|
+
perPage,
|
|
582
|
+
total,
|
|
583
|
+
totalPages
|
|
584
|
+
});
|
|
585
|
+
return {
|
|
586
|
+
data,
|
|
587
|
+
pagination
|
|
588
|
+
};
|
|
589
|
+
};
|
|
590
|
+
|
|
591
|
+
// src/logging/logger.ts
|
|
592
|
+
var DEBUG_PREFIX = "[wordpress-utils]";
|
|
593
|
+
var isDebugEnabled = () => {
|
|
594
|
+
if (typeof process !== "undefined" && process.env) {
|
|
595
|
+
return process.env.DEBUG === "true" || process.env.DEBUG === "1";
|
|
596
|
+
}
|
|
597
|
+
return false;
|
|
598
|
+
};
|
|
599
|
+
var debug = (...args) => {
|
|
600
|
+
if (isDebugEnabled()) {
|
|
601
|
+
console.log(DEBUG_PREFIX, ...args);
|
|
602
|
+
}
|
|
603
|
+
};
|
|
604
|
+
var error = (...args) => {
|
|
605
|
+
console.error(DEBUG_PREFIX, ...args);
|
|
606
|
+
};
|
|
607
|
+
|
|
608
|
+
// src/http/interceptors/request.ts
|
|
609
|
+
var setupRequestInterceptor = (axiosInstance, config, client) => {
|
|
610
|
+
axiosInstance.interceptors.request.use(
|
|
611
|
+
async (requestConfig) => {
|
|
612
|
+
if (config.jwtToken) {
|
|
613
|
+
const token = await config.jwtToken.get();
|
|
614
|
+
if (token) {
|
|
615
|
+
requestConfig.headers.Authorization = `Bearer ${token}`;
|
|
616
|
+
debug("JWT token injected");
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
if (config.onRequest) {
|
|
620
|
+
return config.onRequest(requestConfig, client);
|
|
621
|
+
}
|
|
622
|
+
return requestConfig;
|
|
623
|
+
},
|
|
624
|
+
(error2) => {
|
|
625
|
+
return Promise.reject(error2);
|
|
626
|
+
}
|
|
627
|
+
);
|
|
628
|
+
};
|
|
629
|
+
|
|
630
|
+
// src/http/interceptors/response.ts
|
|
631
|
+
var setupResponseInterceptor = (axiosInstance, config, client) => {
|
|
632
|
+
axiosInstance.interceptors.response.use(
|
|
633
|
+
async (response) => {
|
|
634
|
+
debug("Response:", response.config.url, response.status);
|
|
635
|
+
if (config.onResponse) {
|
|
636
|
+
return config.onResponse(response, client);
|
|
637
|
+
}
|
|
638
|
+
return response;
|
|
639
|
+
},
|
|
640
|
+
(error2) => {
|
|
641
|
+
return Promise.reject(error2);
|
|
642
|
+
}
|
|
643
|
+
);
|
|
644
|
+
};
|
|
645
|
+
var setupErrorInterceptor = (axiosInstance, config, client) => {
|
|
646
|
+
axiosInstance.interceptors.response.use(
|
|
647
|
+
(response) => response,
|
|
648
|
+
async (axiosError) => {
|
|
649
|
+
const status = axiosError.response?.status || 500;
|
|
650
|
+
const errorData = axiosError.response?.data;
|
|
651
|
+
const apiCode = errorData?.code || "unknown_error";
|
|
652
|
+
const wpMessage = errorData?.message || axiosError.message || "Unknown error";
|
|
653
|
+
const mappedError = mapWordPressCode(apiCode, status);
|
|
654
|
+
const wpError = new WordPressApiError({
|
|
655
|
+
message: wpMessage,
|
|
656
|
+
statusCode: status,
|
|
657
|
+
code: mappedError.code,
|
|
658
|
+
// Transformed app-level code
|
|
659
|
+
originalCode: apiCode,
|
|
660
|
+
// Original WordPress API code
|
|
661
|
+
retryable: mappedError.retryable,
|
|
662
|
+
userMessage: mappedError.userMessage,
|
|
663
|
+
operation: axiosError.config?.url || "wordpress_api_request",
|
|
664
|
+
url: axiosError.config?.url || "",
|
|
665
|
+
method: axiosError.config?.method?.toUpperCase() || "GET",
|
|
666
|
+
requestBody: axiosError.config?.data,
|
|
667
|
+
responseBody: errorData,
|
|
668
|
+
cause: axiosError
|
|
669
|
+
});
|
|
670
|
+
if (config.debug) {
|
|
671
|
+
debug("WordPress error:", apiCode, status, wpMessage);
|
|
672
|
+
error(wpError);
|
|
673
|
+
}
|
|
674
|
+
if ((status === 401 || status === 403) && config.onAuthError && axios.isAxiosError(axiosError) && !axiosError.config?._retry) {
|
|
675
|
+
if (config.debug) {
|
|
676
|
+
debug("Auth error detected, calling onAuthError handler");
|
|
677
|
+
}
|
|
678
|
+
try {
|
|
679
|
+
const shouldRetry = await config.onAuthError(wpError, client);
|
|
680
|
+
if (shouldRetry) {
|
|
681
|
+
if (config.debug) {
|
|
682
|
+
debug("Retrying request after auth error");
|
|
683
|
+
}
|
|
684
|
+
axiosError.config._retry = true;
|
|
685
|
+
return axiosInstance.request(axiosError.config);
|
|
686
|
+
}
|
|
687
|
+
} catch (authErrorHandlerError) {
|
|
688
|
+
if (config.debug) {
|
|
689
|
+
debug("onAuthError handler threw error", authErrorHandlerError);
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
if (config.onError) {
|
|
694
|
+
config.onError(wpError, client);
|
|
695
|
+
}
|
|
696
|
+
config.errorReporter?.report(wpError);
|
|
697
|
+
return Promise.reject(wpError);
|
|
698
|
+
}
|
|
699
|
+
);
|
|
700
|
+
};
|
|
701
|
+
var addressSchema = zod.z.object({
|
|
702
|
+
/** First name */
|
|
703
|
+
first_name: zod.z.string().min(1, "First name is required"),
|
|
704
|
+
/** Last name */
|
|
705
|
+
last_name: zod.z.string().min(1, "Last name is required"),
|
|
706
|
+
/** Company name (optional) */
|
|
707
|
+
company: zod.z.string(),
|
|
708
|
+
/** Address line 1 */
|
|
709
|
+
address_1: zod.z.string().min(1, "Address is required"),
|
|
710
|
+
/** Address line 2 (optional) */
|
|
711
|
+
address_2: zod.z.string(),
|
|
712
|
+
/** City */
|
|
713
|
+
city: zod.z.string().min(1, "City is required"),
|
|
714
|
+
/** State/Province/Region */
|
|
715
|
+
state: zod.z.string(),
|
|
716
|
+
/** Postal code */
|
|
717
|
+
postcode: zod.z.string().min(1, "Postal code is required"),
|
|
718
|
+
/** Country code (ISO 3166-1 alpha-2) */
|
|
719
|
+
country: zod.z.string(),
|
|
720
|
+
/** Phone number */
|
|
721
|
+
phone: zod.z.string().min(1, "Phone number is required")
|
|
722
|
+
});
|
|
723
|
+
var addressSchemaNonMandatory = zod.z.object({
|
|
724
|
+
/** First name */
|
|
725
|
+
first_name: zod.z.string(),
|
|
726
|
+
/** Last name */
|
|
727
|
+
last_name: zod.z.string(),
|
|
728
|
+
/** Company name (optional) */
|
|
729
|
+
company: zod.z.string(),
|
|
730
|
+
/** Address line 1 */
|
|
731
|
+
address_1: zod.z.string(),
|
|
732
|
+
/** Address line 2 (optional) */
|
|
733
|
+
address_2: zod.z.string(),
|
|
734
|
+
/** City */
|
|
735
|
+
city: zod.z.string(),
|
|
736
|
+
/** State/Province/Region */
|
|
737
|
+
state: zod.z.string(),
|
|
738
|
+
/** Postal code */
|
|
739
|
+
postcode: zod.z.string(),
|
|
740
|
+
/** Country code (ISO 3166-1 alpha-2) */
|
|
741
|
+
country: zod.z.string(),
|
|
742
|
+
/** Phone number */
|
|
743
|
+
phone: zod.z.string()
|
|
744
|
+
});
|
|
745
|
+
addressSchema.extend({
|
|
746
|
+
/** Email address (required for billing) */
|
|
747
|
+
email: zod.z.string().email("Valid email is required")
|
|
748
|
+
});
|
|
749
|
+
addressSchemaNonMandatory.extend({
|
|
750
|
+
/** Email address (can be null for guest carts) */
|
|
751
|
+
email: zod.z.string().nullable()
|
|
752
|
+
});
|
|
753
|
+
var moneySchema = zod.z.object({
|
|
754
|
+
/** Currency code (e.g., 'USD', 'EUR', 'BAM') */
|
|
755
|
+
currency_code: zod.z.string(),
|
|
756
|
+
/** Currency symbol (e.g., '$', '€', 'KM') */
|
|
757
|
+
currency_symbol: zod.z.string(),
|
|
758
|
+
/** Number of decimal places (e.g., 2 for dollars/euros) */
|
|
759
|
+
currency_minor_unit: zod.z.number(),
|
|
760
|
+
/** Decimal separator character (e.g., '.', ',') */
|
|
761
|
+
currency_decimal_separator: zod.z.string(),
|
|
762
|
+
/** Thousands separator character (e.g., ',', '.') */
|
|
763
|
+
currency_thousand_separator: zod.z.string(),
|
|
764
|
+
/** Currency symbol prefix (empty if symbol is suffix) */
|
|
765
|
+
currency_prefix: zod.z.string(),
|
|
766
|
+
/** Currency symbol suffix (empty if symbol is prefix) */
|
|
767
|
+
currency_suffix: zod.z.string()
|
|
768
|
+
});
|
|
769
|
+
var paginationParamsSchema = zod.z.object({
|
|
770
|
+
/** Current page number */
|
|
771
|
+
page: zod.z.number().int().positive().optional(),
|
|
772
|
+
/** Items per page */
|
|
773
|
+
per_page: zod.z.number().int().positive().max(100).optional(),
|
|
774
|
+
/** Offset for pagination */
|
|
775
|
+
offset: zod.z.number().int().nonnegative().optional(),
|
|
776
|
+
/** Sort order */
|
|
777
|
+
order: zod.z.enum(["asc", "desc"]).optional(),
|
|
778
|
+
/** Field to sort by */
|
|
779
|
+
orderby: zod.z.string().optional()
|
|
780
|
+
});
|
|
781
|
+
zod.z.object({
|
|
782
|
+
/** Total number of items */
|
|
783
|
+
total: zod.z.number(),
|
|
784
|
+
/** Total number of pages */
|
|
785
|
+
totalPages: zod.z.number(),
|
|
786
|
+
/** Current page */
|
|
787
|
+
currentPage: zod.z.number(),
|
|
788
|
+
/** Items per page */
|
|
789
|
+
perPage: zod.z.number()
|
|
790
|
+
});
|
|
791
|
+
var JwtLoginInputSchema = zod.z.object({
|
|
792
|
+
username: zod.z.string(),
|
|
793
|
+
password: zod.z.string()
|
|
794
|
+
});
|
|
795
|
+
var JwtTokenResponseSchema = zod.z.object({
|
|
796
|
+
token: zod.z.string(),
|
|
797
|
+
user_email: zod.z.string(),
|
|
798
|
+
user_nicename: zod.z.string(),
|
|
799
|
+
user_display_name: zod.z.string()
|
|
800
|
+
});
|
|
801
|
+
var JwtValidateResponseSchema = zod.z.object({
|
|
802
|
+
code: zod.z.string(),
|
|
803
|
+
data: zod.z.object({
|
|
804
|
+
status: zod.z.number()
|
|
805
|
+
})
|
|
806
|
+
});
|
|
807
|
+
var JwtErrorResponseSchema = zod.z.object({
|
|
808
|
+
code: zod.z.string(),
|
|
809
|
+
message: zod.z.string(),
|
|
810
|
+
data: zod.z.object({
|
|
811
|
+
status: zod.z.number()
|
|
812
|
+
}).optional()
|
|
813
|
+
});
|
|
814
|
+
var RenderedContentSchema = zod.z.object({
|
|
815
|
+
rendered: zod.z.string(),
|
|
816
|
+
protected: zod.z.boolean().optional()
|
|
817
|
+
});
|
|
818
|
+
var GuidSchema = zod.z.object({
|
|
819
|
+
rendered: zod.z.string()
|
|
820
|
+
});
|
|
821
|
+
var PostStatusSchema = zod.z.enum([
|
|
822
|
+
"publish",
|
|
823
|
+
"future",
|
|
824
|
+
"draft",
|
|
825
|
+
"pending",
|
|
826
|
+
"private",
|
|
827
|
+
"trash",
|
|
828
|
+
"auto-draft",
|
|
829
|
+
"inherit"
|
|
830
|
+
]);
|
|
831
|
+
var HalLinkItemSchema = zod.z.object({
|
|
832
|
+
href: zod.z.string(),
|
|
833
|
+
embeddable: zod.z.boolean().optional(),
|
|
834
|
+
templated: zod.z.boolean().optional()
|
|
835
|
+
});
|
|
836
|
+
var HalLinksSchema = zod.z.object({
|
|
837
|
+
self: zod.z.array(HalLinkItemSchema).optional(),
|
|
838
|
+
collection: zod.z.array(HalLinkItemSchema).optional(),
|
|
839
|
+
about: zod.z.array(HalLinkItemSchema).optional(),
|
|
840
|
+
author: zod.z.array(HalLinkItemSchema).optional(),
|
|
841
|
+
replies: zod.z.array(HalLinkItemSchema).optional(),
|
|
842
|
+
"wp:featuredmedia": zod.z.array(HalLinkItemSchema).optional(),
|
|
843
|
+
"wp:attachment": zod.z.array(HalLinkItemSchema).optional(),
|
|
844
|
+
"wp:term": zod.z.array(HalLinkItemSchema).optional(),
|
|
845
|
+
"wp:post_type": zod.z.array(HalLinkItemSchema).optional(),
|
|
846
|
+
curies: zod.z.array(
|
|
847
|
+
zod.z.object({
|
|
848
|
+
name: zod.z.string(),
|
|
849
|
+
href: zod.z.string(),
|
|
850
|
+
templated: zod.z.boolean()
|
|
851
|
+
})
|
|
852
|
+
).optional()
|
|
853
|
+
}).passthrough();
|
|
854
|
+
var WordPressBaseSchema = zod.z.object({
|
|
855
|
+
id: zod.z.number(),
|
|
856
|
+
date: zod.z.string(),
|
|
857
|
+
date_gmt: zod.z.string(),
|
|
858
|
+
modified: zod.z.string(),
|
|
859
|
+
modified_gmt: zod.z.string(),
|
|
860
|
+
slug: zod.z.string(),
|
|
861
|
+
status: zod.z.string(),
|
|
862
|
+
type: zod.z.string(),
|
|
863
|
+
link: zod.z.string(),
|
|
864
|
+
_links: HalLinksSchema.optional()
|
|
865
|
+
});
|
|
866
|
+
var CategorySchema = zod.z.object({
|
|
867
|
+
id: zod.z.number(),
|
|
868
|
+
count: zod.z.number(),
|
|
869
|
+
description: zod.z.string().optional(),
|
|
870
|
+
link: zod.z.string(),
|
|
871
|
+
name: zod.z.string(),
|
|
872
|
+
slug: zod.z.string(),
|
|
873
|
+
taxonomy: zod.z.string(),
|
|
874
|
+
parent: zod.z.number(),
|
|
875
|
+
meta: zod.z.any().optional(),
|
|
876
|
+
_links: HalLinksSchema.optional()
|
|
877
|
+
});
|
|
878
|
+
var CategoriesListSchema = zod.z.array(CategorySchema);
|
|
879
|
+
var CategoryParamsSchema = paginationParamsSchema.extend({
|
|
880
|
+
context: zod.z.enum(["view", "embed", "edit"]).optional(),
|
|
881
|
+
search: zod.z.string().optional(),
|
|
882
|
+
exclude: zod.z.array(zod.z.number()).optional(),
|
|
883
|
+
include: zod.z.array(zod.z.number()).optional(),
|
|
884
|
+
hide_empty: zod.z.boolean().optional(),
|
|
885
|
+
parent: zod.z.number().optional(),
|
|
886
|
+
post: zod.z.number().optional(),
|
|
887
|
+
slug: zod.z.string().optional()
|
|
888
|
+
});
|
|
889
|
+
var TagSchema = zod.z.object({
|
|
890
|
+
id: zod.z.number(),
|
|
891
|
+
count: zod.z.number(),
|
|
892
|
+
description: zod.z.string().optional(),
|
|
893
|
+
link: zod.z.string(),
|
|
894
|
+
name: zod.z.string(),
|
|
895
|
+
slug: zod.z.string(),
|
|
896
|
+
taxonomy: zod.z.string(),
|
|
897
|
+
meta: zod.z.any().optional(),
|
|
898
|
+
_links: HalLinksSchema.optional()
|
|
899
|
+
});
|
|
900
|
+
var TagsListSchema = zod.z.array(TagSchema);
|
|
901
|
+
var MediaSizeSchema = zod.z.object({
|
|
902
|
+
file: zod.z.string(),
|
|
903
|
+
width: zod.z.number(),
|
|
904
|
+
height: zod.z.number(),
|
|
905
|
+
filesize: zod.z.number().optional(),
|
|
906
|
+
mime_type: zod.z.string(),
|
|
907
|
+
source_url: zod.z.string()
|
|
908
|
+
});
|
|
909
|
+
var MediaSizesSchema = zod.z.object({
|
|
910
|
+
thumbnail: MediaSizeSchema.optional(),
|
|
911
|
+
medium: MediaSizeSchema.optional(),
|
|
912
|
+
medium_large: MediaSizeSchema.optional(),
|
|
913
|
+
large: MediaSizeSchema.optional(),
|
|
914
|
+
full: MediaSizeSchema.optional()
|
|
915
|
+
}).passthrough();
|
|
916
|
+
var ImageMetaSchema = zod.z.object({
|
|
917
|
+
aperture: zod.z.string().optional(),
|
|
918
|
+
credit: zod.z.string().optional(),
|
|
919
|
+
camera: zod.z.string().optional(),
|
|
920
|
+
caption: zod.z.string().optional(),
|
|
921
|
+
created_timestamp: zod.z.string().optional(),
|
|
922
|
+
copyright: zod.z.string().optional(),
|
|
923
|
+
focal_length: zod.z.string().optional(),
|
|
924
|
+
iso: zod.z.string().optional(),
|
|
925
|
+
shutter_speed: zod.z.string().optional(),
|
|
926
|
+
title: zod.z.string().optional(),
|
|
927
|
+
orientation: zod.z.string().optional(),
|
|
928
|
+
keywords: zod.z.array(zod.z.string()).optional()
|
|
929
|
+
});
|
|
930
|
+
var MediaDetailsSchema = zod.z.object({
|
|
931
|
+
width: zod.z.number(),
|
|
932
|
+
height: zod.z.number(),
|
|
933
|
+
file: zod.z.string(),
|
|
934
|
+
filesize: zod.z.number().optional(),
|
|
935
|
+
sizes: MediaSizesSchema,
|
|
936
|
+
image_meta: ImageMetaSchema.optional()
|
|
937
|
+
});
|
|
938
|
+
var MediaSchema = zod.z.object({
|
|
939
|
+
id: zod.z.number(),
|
|
940
|
+
date: zod.z.string(),
|
|
941
|
+
date_gmt: zod.z.string(),
|
|
942
|
+
guid: GuidSchema,
|
|
943
|
+
modified: zod.z.string(),
|
|
944
|
+
modified_gmt: zod.z.string(),
|
|
945
|
+
slug: zod.z.string(),
|
|
946
|
+
status: zod.z.string(),
|
|
947
|
+
type: zod.z.string(),
|
|
948
|
+
link: zod.z.string(),
|
|
949
|
+
title: RenderedContentSchema,
|
|
950
|
+
author: zod.z.number(),
|
|
951
|
+
comment_status: zod.z.string(),
|
|
952
|
+
ping_status: zod.z.string(),
|
|
953
|
+
template: zod.z.string().optional(),
|
|
954
|
+
meta: zod.z.any().optional(),
|
|
955
|
+
description: RenderedContentSchema,
|
|
956
|
+
caption: RenderedContentSchema,
|
|
957
|
+
alt_text: zod.z.string(),
|
|
958
|
+
media_type: zod.z.string(),
|
|
959
|
+
mime_type: zod.z.string(),
|
|
960
|
+
media_details: MediaDetailsSchema,
|
|
961
|
+
post: zod.z.number().nullable(),
|
|
962
|
+
source_url: zod.z.string(),
|
|
963
|
+
_links: HalLinksSchema.optional()
|
|
964
|
+
});
|
|
965
|
+
var MediaListSchema = zod.z.array(MediaSchema);
|
|
966
|
+
var PostSchema = zod.z.object({
|
|
967
|
+
id: zod.z.number(),
|
|
968
|
+
date: zod.z.string(),
|
|
969
|
+
date_gmt: zod.z.string(),
|
|
970
|
+
guid: GuidSchema,
|
|
971
|
+
modified: zod.z.string(),
|
|
972
|
+
modified_gmt: zod.z.string(),
|
|
973
|
+
slug: zod.z.string(),
|
|
974
|
+
status: zod.z.string(),
|
|
975
|
+
type: zod.z.string(),
|
|
976
|
+
link: zod.z.string(),
|
|
977
|
+
title: RenderedContentSchema,
|
|
978
|
+
content: RenderedContentSchema,
|
|
979
|
+
excerpt: RenderedContentSchema,
|
|
980
|
+
author: zod.z.number(),
|
|
981
|
+
featured_media: zod.z.number(),
|
|
982
|
+
comment_status: zod.z.string(),
|
|
983
|
+
ping_status: zod.z.string(),
|
|
984
|
+
sticky: zod.z.boolean(),
|
|
985
|
+
template: zod.z.string(),
|
|
986
|
+
format: zod.z.string(),
|
|
987
|
+
meta: zod.z.any().optional(),
|
|
988
|
+
categories: zod.z.array(zod.z.number()),
|
|
989
|
+
tags: zod.z.array(zod.z.number()).optional(),
|
|
990
|
+
_links: HalLinksSchema.optional()
|
|
991
|
+
});
|
|
992
|
+
var PostsListSchema = zod.z.array(PostSchema);
|
|
993
|
+
var PostParamsSchema = paginationParamsSchema.extend({
|
|
994
|
+
context: zod.z.enum(["view", "embed", "edit"]).optional(),
|
|
995
|
+
search: zod.z.string().optional(),
|
|
996
|
+
after: zod.z.string().optional(),
|
|
997
|
+
before: zod.z.string().optional(),
|
|
998
|
+
author: zod.z.union([zod.z.number(), zod.z.array(zod.z.number())]).optional(),
|
|
999
|
+
author_exclude: zod.z.array(zod.z.number()).optional(),
|
|
1000
|
+
exclude: zod.z.array(zod.z.number()).optional(),
|
|
1001
|
+
include: zod.z.array(zod.z.number()).optional(),
|
|
1002
|
+
categories: zod.z.union([zod.z.number(), zod.z.array(zod.z.number())]).optional(),
|
|
1003
|
+
categories_exclude: zod.z.array(zod.z.number()).optional(),
|
|
1004
|
+
tags: zod.z.union([zod.z.number(), zod.z.array(zod.z.number())]).optional(),
|
|
1005
|
+
tags_exclude: zod.z.array(zod.z.number()).optional(),
|
|
1006
|
+
sticky: zod.z.boolean().optional()
|
|
1007
|
+
});
|
|
1008
|
+
var EmbeddedPostSchema = zod.z.object({
|
|
1009
|
+
id: zod.z.number(),
|
|
1010
|
+
title: zod.z.string(),
|
|
1011
|
+
content: zod.z.string(),
|
|
1012
|
+
featured_image: zod.z.string().optional(),
|
|
1013
|
+
published_date: zod.z.string(),
|
|
1014
|
+
categories: zod.z.array(
|
|
1015
|
+
zod.z.object({
|
|
1016
|
+
id: zod.z.number(),
|
|
1017
|
+
name: zod.z.string(),
|
|
1018
|
+
slug: zod.z.string(),
|
|
1019
|
+
taxonomy: zod.z.string().optional(),
|
|
1020
|
+
description: zod.z.string().optional(),
|
|
1021
|
+
parent: zod.z.number().optional(),
|
|
1022
|
+
count: zod.z.number().optional()
|
|
1023
|
+
})
|
|
1024
|
+
).optional()
|
|
1025
|
+
});
|
|
1026
|
+
var EmbeddedPostsListSchema = zod.z.array(EmbeddedPostSchema);
|
|
1027
|
+
var AvatarUrlsSchema = zod.z.object({
|
|
1028
|
+
"24": zod.z.string().optional(),
|
|
1029
|
+
"48": zod.z.string().optional(),
|
|
1030
|
+
"96": zod.z.string().optional()
|
|
1031
|
+
});
|
|
1032
|
+
var UserSchema = zod.z.object({
|
|
1033
|
+
id: zod.z.number(),
|
|
1034
|
+
username: zod.z.string().optional(),
|
|
1035
|
+
// Only in edit context
|
|
1036
|
+
name: zod.z.string(),
|
|
1037
|
+
first_name: zod.z.string().optional(),
|
|
1038
|
+
last_name: zod.z.string().optional(),
|
|
1039
|
+
email: zod.z.string().optional(),
|
|
1040
|
+
// Only for current user or admin
|
|
1041
|
+
url: zod.z.string(),
|
|
1042
|
+
description: zod.z.string(),
|
|
1043
|
+
link: zod.z.string(),
|
|
1044
|
+
locale: zod.z.string().optional(),
|
|
1045
|
+
// Only for current user
|
|
1046
|
+
nickname: zod.z.string().optional(),
|
|
1047
|
+
// Only in edit context
|
|
1048
|
+
slug: zod.z.string(),
|
|
1049
|
+
registered_date: zod.z.string().optional(),
|
|
1050
|
+
// Only in edit context
|
|
1051
|
+
roles: zod.z.array(zod.z.string()).optional(),
|
|
1052
|
+
// Only in edit context
|
|
1053
|
+
capabilities: zod.z.record(zod.z.boolean()).optional(),
|
|
1054
|
+
// Only in edit context
|
|
1055
|
+
extra_capabilities: zod.z.record(zod.z.boolean()).optional(),
|
|
1056
|
+
// Only in edit context
|
|
1057
|
+
avatar_urls: AvatarUrlsSchema.optional(),
|
|
1058
|
+
meta: zod.z.any().optional(),
|
|
1059
|
+
_links: HalLinksSchema.optional()
|
|
1060
|
+
});
|
|
1061
|
+
var UsersListSchema = zod.z.array(UserSchema);
|
|
1062
|
+
var CurrentUserSchema = UserSchema.extend({
|
|
1063
|
+
username: zod.z.string(),
|
|
1064
|
+
email: zod.z.string(),
|
|
1065
|
+
locale: zod.z.string(),
|
|
1066
|
+
nickname: zod.z.string(),
|
|
1067
|
+
registered_date: zod.z.string(),
|
|
1068
|
+
roles: zod.z.array(zod.z.string())
|
|
1069
|
+
});
|
|
1070
|
+
|
|
1071
|
+
// src/api/auth.ts
|
|
1072
|
+
var createAuthAPI = (axios2, options) => ({
|
|
1073
|
+
/**
|
|
1074
|
+
* Login with username/password and get JWT token
|
|
1075
|
+
*/
|
|
1076
|
+
async login(input) {
|
|
1077
|
+
const response = await axios2.post(ENDPOINTS.JWT_TOKEN, input);
|
|
1078
|
+
return handleApiResponse(response, JwtTokenResponseSchema, options);
|
|
1079
|
+
},
|
|
1080
|
+
/**
|
|
1081
|
+
* Validate current JWT token
|
|
1082
|
+
*
|
|
1083
|
+
* Requires Authorization header to be set
|
|
1084
|
+
*/
|
|
1085
|
+
async validateToken() {
|
|
1086
|
+
try {
|
|
1087
|
+
const response = await axios2.post(ENDPOINTS.JWT_VALIDATE);
|
|
1088
|
+
return response.data?.code === "jwt_auth_valid_token";
|
|
1089
|
+
} catch {
|
|
1090
|
+
return false;
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
1093
|
+
});
|
|
1094
|
+
|
|
1095
|
+
// src/api/categories.ts
|
|
1096
|
+
var createCategoriesAPI = (axios2, options) => ({
|
|
1097
|
+
/**
|
|
1098
|
+
* Get list of categories with pagination
|
|
1099
|
+
*/
|
|
1100
|
+
async list(params) {
|
|
1101
|
+
const response = await axios2.get(ENDPOINTS.CATEGORIES, { params });
|
|
1102
|
+
return handlePaginatedApiResponse(response, CategoriesListSchema, params, options);
|
|
1103
|
+
},
|
|
1104
|
+
/**
|
|
1105
|
+
* Get single category by ID
|
|
1106
|
+
*/
|
|
1107
|
+
async get(id) {
|
|
1108
|
+
const response = await axios2.get(ENDPOINTS.CATEGORY(id));
|
|
1109
|
+
return handleApiResponse(response, CategorySchema, options);
|
|
1110
|
+
}
|
|
1111
|
+
});
|
|
1112
|
+
|
|
1113
|
+
// src/api/media.ts
|
|
1114
|
+
var createMediaAPI = (axios2, options) => ({
|
|
1115
|
+
/**
|
|
1116
|
+
* Get single media item by ID
|
|
1117
|
+
*/
|
|
1118
|
+
async get(id) {
|
|
1119
|
+
const response = await axios2.get(ENDPOINTS.MEDIA_ITEM(id));
|
|
1120
|
+
return handleApiResponse(response, MediaSchema, options);
|
|
1121
|
+
}
|
|
1122
|
+
});
|
|
1123
|
+
|
|
1124
|
+
// src/api/posts.ts
|
|
1125
|
+
var createPostsAPI = (axios2, options) => ({
|
|
1126
|
+
/**
|
|
1127
|
+
* Get list of posts with pagination
|
|
1128
|
+
*/
|
|
1129
|
+
async list(params) {
|
|
1130
|
+
const response = await axios2.get(ENDPOINTS.POSTS, { params });
|
|
1131
|
+
return handlePaginatedApiResponse(response, PostsListSchema, params, options);
|
|
1132
|
+
},
|
|
1133
|
+
/**
|
|
1134
|
+
* Get single post by ID
|
|
1135
|
+
*/
|
|
1136
|
+
async get(id) {
|
|
1137
|
+
const response = await axios2.get(ENDPOINTS.POST(id));
|
|
1138
|
+
return handleApiResponse(response, PostSchema, options);
|
|
1139
|
+
},
|
|
1140
|
+
/**
|
|
1141
|
+
* Get single post by slug
|
|
1142
|
+
*/
|
|
1143
|
+
async getBySlug(slug) {
|
|
1144
|
+
const response = await axios2.get(ENDPOINTS.POSTS, {
|
|
1145
|
+
params: { slug, per_page: 1 }
|
|
1146
|
+
});
|
|
1147
|
+
const posts = handleApiResponse(response, PostsListSchema, options);
|
|
1148
|
+
return posts[0] || null;
|
|
1149
|
+
}
|
|
1150
|
+
});
|
|
1151
|
+
|
|
1152
|
+
// src/api/users.ts
|
|
1153
|
+
var createUsersAPI = (axios2, options) => ({
|
|
1154
|
+
/**
|
|
1155
|
+
* Get current authenticated user
|
|
1156
|
+
*/
|
|
1157
|
+
async me() {
|
|
1158
|
+
const response = await axios2.get(ENDPOINTS.USERS_ME);
|
|
1159
|
+
return handleApiResponse(response, CurrentUserSchema, options);
|
|
1160
|
+
},
|
|
1161
|
+
/**
|
|
1162
|
+
* Get user by ID
|
|
1163
|
+
*/
|
|
1164
|
+
async get(id) {
|
|
1165
|
+
const response = await axios2.get(ENDPOINTS.USER(id));
|
|
1166
|
+
return handleApiResponse(response, UserSchema, options);
|
|
1167
|
+
}
|
|
1168
|
+
});
|
|
1169
|
+
|
|
1170
|
+
// src/client/createClient.ts
|
|
1171
|
+
var DEFAULT_ERROR_REPORTER = {
|
|
1172
|
+
report: (error2) => {
|
|
1173
|
+
console.error("[wordpress-utils]", error2);
|
|
1174
|
+
}
|
|
1175
|
+
};
|
|
1176
|
+
var createClient = (config) => {
|
|
1177
|
+
const fullConfig = {
|
|
1178
|
+
...config,
|
|
1179
|
+
errorReporter: config.errorReporter ?? DEFAULT_ERROR_REPORTER
|
|
1180
|
+
};
|
|
1181
|
+
const axiosInstance = axios__default.default.create({
|
|
1182
|
+
baseURL: fullConfig.baseURL,
|
|
1183
|
+
timeout: fullConfig.timeout || 3e4,
|
|
1184
|
+
headers: {
|
|
1185
|
+
"Content-Type": "application/json",
|
|
1186
|
+
...fullConfig.headers
|
|
1187
|
+
}
|
|
1188
|
+
});
|
|
1189
|
+
const responseOptions = {
|
|
1190
|
+
errorReporter: fullConfig.errorReporter?.report,
|
|
1191
|
+
onValidationError: fullConfig.onValidationError,
|
|
1192
|
+
validationMode: fullConfig.validationMode
|
|
1193
|
+
};
|
|
1194
|
+
const client = {
|
|
1195
|
+
config: fullConfig,
|
|
1196
|
+
axios: axiosInstance,
|
|
1197
|
+
posts: createPostsAPI(axiosInstance, responseOptions),
|
|
1198
|
+
categories: createCategoriesAPI(axiosInstance, responseOptions),
|
|
1199
|
+
media: createMediaAPI(axiosInstance, responseOptions),
|
|
1200
|
+
users: createUsersAPI(axiosInstance, responseOptions),
|
|
1201
|
+
auth: createAuthAPI(axiosInstance, responseOptions)
|
|
1202
|
+
};
|
|
1203
|
+
setupRequestInterceptor(axiosInstance, fullConfig, client);
|
|
1204
|
+
setupResponseInterceptor(axiosInstance, fullConfig, client);
|
|
1205
|
+
setupErrorInterceptor(axiosInstance, fullConfig, client);
|
|
1206
|
+
return client;
|
|
1207
|
+
};
|
|
1208
|
+
|
|
1209
|
+
exports.AddressSchema = addressSchema;
|
|
1210
|
+
exports.AvatarUrlsSchema = AvatarUrlsSchema;
|
|
1211
|
+
exports.BaseError = BaseError;
|
|
1212
|
+
exports.CategoriesListSchema = CategoriesListSchema;
|
|
1213
|
+
exports.CategoryParamsSchema = CategoryParamsSchema;
|
|
1214
|
+
exports.CategorySchema = CategorySchema;
|
|
1215
|
+
exports.CurrentUserSchema = CurrentUserSchema;
|
|
1216
|
+
exports.ENDPOINTS = ENDPOINTS;
|
|
1217
|
+
exports.EmbeddedPostSchema = EmbeddedPostSchema;
|
|
1218
|
+
exports.EmbeddedPostsListSchema = EmbeddedPostsListSchema;
|
|
1219
|
+
exports.GuidSchema = GuidSchema;
|
|
1220
|
+
exports.HalLinkItemSchema = HalLinkItemSchema;
|
|
1221
|
+
exports.HalLinksSchema = HalLinksSchema;
|
|
1222
|
+
exports.ImageMetaSchema = ImageMetaSchema;
|
|
1223
|
+
exports.JwtErrorResponseSchema = JwtErrorResponseSchema;
|
|
1224
|
+
exports.JwtLoginInputSchema = JwtLoginInputSchema;
|
|
1225
|
+
exports.JwtTokenResponseSchema = JwtTokenResponseSchema;
|
|
1226
|
+
exports.JwtValidateResponseSchema = JwtValidateResponseSchema;
|
|
1227
|
+
exports.MediaDetailsSchema = MediaDetailsSchema;
|
|
1228
|
+
exports.MediaListSchema = MediaListSchema;
|
|
1229
|
+
exports.MediaSchema = MediaSchema;
|
|
1230
|
+
exports.MediaSizeSchema = MediaSizeSchema;
|
|
1231
|
+
exports.MediaSizesSchema = MediaSizesSchema;
|
|
1232
|
+
exports.MoneySchema = moneySchema;
|
|
1233
|
+
exports.PaginationParamsSchema = paginationParamsSchema;
|
|
1234
|
+
exports.PostParamsSchema = PostParamsSchema;
|
|
1235
|
+
exports.PostSchema = PostSchema;
|
|
1236
|
+
exports.PostStatusSchema = PostStatusSchema;
|
|
1237
|
+
exports.PostsListSchema = PostsListSchema;
|
|
1238
|
+
exports.RenderedContentSchema = RenderedContentSchema;
|
|
1239
|
+
exports.TagSchema = TagSchema;
|
|
1240
|
+
exports.TagsListSchema = TagsListSchema;
|
|
1241
|
+
exports.UserSchema = UserSchema;
|
|
1242
|
+
exports.UsersListSchema = UsersListSchema;
|
|
1243
|
+
exports.WordPressApiError = WordPressApiError;
|
|
1244
|
+
exports.WordPressBaseSchema = WordPressBaseSchema;
|
|
1245
|
+
exports.WordPressDataValidationError = WordPressDataValidationError;
|
|
1246
|
+
exports.calculatePagination = calculatePagination;
|
|
1247
|
+
exports.createClient = createClient;
|
|
1248
|
+
exports.getPaginationMeta = getPaginationMeta;
|
|
1249
|
+
exports.mapWordPressCode = mapWordPressCode;
|
|
1250
|
+
//# sourceMappingURL=index.cjs.map
|
|
1251
|
+
//# sourceMappingURL=index.cjs.map
|