wp-native-client 0.0.1

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.
Files changed (59) hide show
  1. package/LICENSE +23 -0
  2. package/README.md +41 -0
  3. package/dist/abilities/catalog.d.ts +49 -0
  4. package/dist/abilities/catalog.d.ts.map +1 -0
  5. package/dist/abilities/catalog.js +65 -0
  6. package/dist/abilities/catalog.js.map +1 -0
  7. package/dist/abilities/discovery.d.ts +43 -0
  8. package/dist/abilities/discovery.d.ts.map +1 -0
  9. package/dist/abilities/discovery.js +82 -0
  10. package/dist/abilities/discovery.js.map +1 -0
  11. package/dist/abilities/index.d.ts +7 -0
  12. package/dist/abilities/index.d.ts.map +1 -0
  13. package/dist/abilities/index.js +6 -0
  14. package/dist/abilities/index.js.map +1 -0
  15. package/dist/abilities/types.d.ts +88 -0
  16. package/dist/abilities/types.d.ts.map +1 -0
  17. package/dist/abilities/types.js +15 -0
  18. package/dist/abilities/types.js.map +1 -0
  19. package/dist/client.d.ts +110 -0
  20. package/dist/client.d.ts.map +1 -0
  21. package/dist/client.js +142 -0
  22. package/dist/client.js.map +1 -0
  23. package/dist/index.d.ts +16 -0
  24. package/dist/index.d.ts.map +1 -0
  25. package/dist/index.js +13 -0
  26. package/dist/index.js.map +1 -0
  27. package/dist/transports/auth-fetch.d.ts +132 -0
  28. package/dist/transports/auth-fetch.d.ts.map +1 -0
  29. package/dist/transports/auth-fetch.js +217 -0
  30. package/dist/transports/auth-fetch.js.map +1 -0
  31. package/dist/transports/fetch.d.ts +42 -0
  32. package/dist/transports/fetch.d.ts.map +1 -0
  33. package/dist/transports/fetch.js +68 -0
  34. package/dist/transports/fetch.js.map +1 -0
  35. package/dist/transports/types.d.ts +22 -0
  36. package/dist/transports/types.d.ts.map +1 -0
  37. package/dist/transports/types.js +9 -0
  38. package/dist/transports/types.js.map +1 -0
  39. package/dist/transports/wp-api-fetch.d.ts +25 -0
  40. package/dist/transports/wp-api-fetch.d.ts.map +1 -0
  41. package/dist/transports/wp-api-fetch.js +40 -0
  42. package/dist/transports/wp-api-fetch.js.map +1 -0
  43. package/dist/wordpress.d.ts +8 -0
  44. package/dist/wordpress.d.ts.map +1 -0
  45. package/dist/wordpress.js +8 -0
  46. package/dist/wordpress.js.map +1 -0
  47. package/package.json +45 -0
  48. package/src/__smoke__/discovery.smoke.ts +66 -0
  49. package/src/abilities/catalog.ts +75 -0
  50. package/src/abilities/discovery.ts +111 -0
  51. package/src/abilities/index.ts +18 -0
  52. package/src/abilities/types.ts +98 -0
  53. package/src/client.ts +191 -0
  54. package/src/index.ts +32 -0
  55. package/src/transports/auth-fetch.ts +310 -0
  56. package/src/transports/fetch.ts +107 -0
  57. package/src/transports/types.ts +24 -0
  58. package/src/transports/wp-api-fetch.ts +58 -0
  59. package/src/wordpress.ts +8 -0
@@ -0,0 +1,58 @@
1
+ /**
2
+ * WordPress api-fetch transport.
3
+ *
4
+ * Wraps @wordpress/api-fetch for use inside WordPress blocks.
5
+ * Nonce handling, root URL, and middleware are all managed by WP core.
6
+ *
7
+ * This transport is in a separate entry point (wordpress.ts) so the main
8
+ * bundle has zero WordPress dependencies. Only import this in WP blocks.
9
+ */
10
+
11
+ import type { Transport, TransportRequest } from './types';
12
+ import { ApiError } from './fetch';
13
+
14
+ type ApiFetchOptions = {
15
+ path: string;
16
+ method: string;
17
+ data?: Record<string, unknown>;
18
+ body?: FormData;
19
+ headers?: Record<string, string>;
20
+ };
21
+
22
+ type ApiFetchFn = <T>(options: ApiFetchOptions) => Promise<T>;
23
+
24
+ export class WpApiFetchTransport implements Transport {
25
+ private apiFetch: ApiFetchFn;
26
+
27
+ constructor(apiFetch: ApiFetchFn) {
28
+ this.apiFetch = apiFetch;
29
+ }
30
+
31
+ async request<T>(req: TransportRequest): Promise<T> {
32
+ const isFormData = typeof FormData !== 'undefined' && req.body instanceof FormData;
33
+
34
+ try {
35
+ const result = await this.apiFetch<T>({
36
+ path: req.path,
37
+ method: req.method,
38
+ ...(isFormData
39
+ ? { body: req.body as FormData }
40
+ : req.body
41
+ ? { data: req.body as Record<string, unknown> }
42
+ : {}),
43
+ ...(req.headers ? { headers: req.headers } : {}),
44
+ });
45
+ return result;
46
+ } catch (error: unknown) {
47
+ if (error && typeof error === 'object' && 'code' in error) {
48
+ const wpError = error as { code: string; message: string; data?: { status: number } };
49
+ throw new ApiError(
50
+ wpError.message,
51
+ wpError.code,
52
+ wpError.data?.status || 500
53
+ );
54
+ }
55
+ throw error;
56
+ }
57
+ }
58
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * WordPress entry point — only import from inside Gutenberg blocks.
3
+ *
4
+ * Keeps @wordpress/api-fetch out of the main bundle so React Native /
5
+ * Node consumers don't pull in WP-specific deps.
6
+ */
7
+
8
+ export { WpApiFetchTransport } from './transports/wp-api-fetch';