@wordpress/abilities 0.1.1-next.dc3f6d3c1.0 → 0.2.1-next.6deb34194.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.
Files changed (97) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/README.md +32 -40
  3. package/build/api.js +19 -66
  4. package/build/api.js.map +3 -3
  5. package/build/index.js.map +2 -2
  6. package/build/store/actions.js +41 -31
  7. package/build/store/actions.js.map +2 -2
  8. package/build/store/constants.js +6 -15
  9. package/build/store/constants.js.map +2 -2
  10. package/build/store/index.js +1 -27
  11. package/build/store/index.js.map +3 -3
  12. package/build/store/reducer.js +2 -22
  13. package/build/store/reducer.js.map +2 -2
  14. package/build/store/selectors.js +1 -1
  15. package/build/store/selectors.js.map +2 -2
  16. package/build/store/types.js +19 -0
  17. package/build/store/types.js.map +7 -0
  18. package/build/types.js.map +1 -1
  19. package/build-module/api.js +20 -57
  20. package/build-module/api.js.map +2 -2
  21. package/build-module/index.js.map +2 -2
  22. package/build-module/store/actions.js +43 -32
  23. package/build-module/store/actions.js.map +2 -2
  24. package/build-module/store/constants.js +4 -10
  25. package/build-module/store/constants.js.map +2 -2
  26. package/build-module/store/index.js +3 -34
  27. package/build-module/store/index.js.map +2 -2
  28. package/build-module/store/reducer.js +2 -24
  29. package/build-module/store/reducer.js.map +2 -2
  30. package/build-module/store/selectors.js +1 -1
  31. package/build-module/store/selectors.js.map +2 -2
  32. package/build-module/store/types.js +1 -0
  33. package/build-module/store/types.js.map +7 -0
  34. package/build-types/api.d.ts +16 -23
  35. package/build-types/api.d.ts.map +1 -1
  36. package/build-types/index.d.ts +1 -1
  37. package/build-types/index.d.ts.map +1 -1
  38. package/build-types/store/actions.d.ts +5 -27
  39. package/build-types/store/actions.d.ts.map +1 -1
  40. package/build-types/store/constants.d.ts +2 -5
  41. package/build-types/store/constants.d.ts.map +1 -1
  42. package/build-types/store/index.d.ts +1 -1
  43. package/build-types/store/index.d.ts.map +1 -1
  44. package/build-types/store/reducer.d.ts +21 -11
  45. package/build-types/store/reducer.d.ts.map +1 -1
  46. package/build-types/store/selectors.d.ts +2 -1
  47. package/build-types/store/selectors.d.ts.map +1 -1
  48. package/build-types/store/types.d.ts +18 -0
  49. package/build-types/store/types.d.ts.map +1 -0
  50. package/build-types/types.d.ts +14 -24
  51. package/build-types/types.d.ts.map +1 -1
  52. package/package.json +15 -9
  53. package/src/api.ts +43 -123
  54. package/src/index.ts +0 -1
  55. package/src/store/actions.ts +61 -46
  56. package/src/store/constants.ts +4 -5
  57. package/src/store/index.ts +2 -34
  58. package/src/store/reducer.ts +27 -33
  59. package/src/store/selectors.ts +3 -7
  60. package/src/store/tests/actions.test.ts +100 -244
  61. package/src/store/tests/reducer.test.ts +0 -451
  62. package/src/store/tests/selectors.test.ts +1 -1
  63. package/src/store/types.ts +19 -0
  64. package/src/tests/api.test.ts +112 -158
  65. package/src/types.ts +14 -26
  66. package/build/store/resolvers.js +0 -107
  67. package/build/store/resolvers.js.map +0 -7
  68. package/build/store/tests/actions.test.js +0 -792
  69. package/build/store/tests/actions.test.js.map +0 -7
  70. package/build/store/tests/reducer.test.js +0 -743
  71. package/build/store/tests/reducer.test.js.map +0 -7
  72. package/build/store/tests/resolvers.test.js +0 -520
  73. package/build/store/tests/resolvers.test.js.map +0 -7
  74. package/build/store/tests/selectors.test.js +0 -349
  75. package/build/store/tests/selectors.test.js.map +0 -7
  76. package/build/tests/api.test.js +0 -546
  77. package/build/tests/api.test.js.map +0 -7
  78. package/build/tests/validation.test.js +0 -453
  79. package/build/tests/validation.test.js.map +0 -7
  80. package/build-module/store/resolvers.js +0 -84
  81. package/build-module/store/resolvers.js.map +0 -7
  82. package/build-module/store/tests/actions.test.js +0 -804
  83. package/build-module/store/tests/actions.test.js.map +0 -7
  84. package/build-module/store/tests/reducer.test.js +0 -726
  85. package/build-module/store/tests/reducer.test.js.map +0 -7
  86. package/build-module/store/tests/resolvers.test.js +0 -523
  87. package/build-module/store/tests/resolvers.test.js.map +0 -7
  88. package/build-module/store/tests/selectors.test.js +0 -352
  89. package/build-module/store/tests/selectors.test.js.map +0 -7
  90. package/build-module/tests/api.test.js +0 -530
  91. package/build-module/tests/api.test.js.map +0 -7
  92. package/build-module/tests/validation.test.js +0 -451
  93. package/build-module/tests/validation.test.js.map +0 -7
  94. package/build-types/store/resolvers.d.ts +0 -47
  95. package/build-types/store/resolvers.d.ts.map +0 -1
  96. package/src/store/resolvers.ts +0 -151
  97. package/src/store/tests/resolvers.test.ts +0 -642
@@ -1,151 +0,0 @@
1
- /**
2
- * WordPress dependencies
3
- */
4
- import { store as coreStore } from '@wordpress/core-data';
5
-
6
- /**
7
- * Internal dependencies
8
- */
9
- import type { Ability, AbilityCategory } from '../types';
10
- import { ENTITY_KIND, ENTITY_NAME, ENTITY_NAME_CATEGORIES } from './constants';
11
- import {
12
- receiveAbilities,
13
- receiveCategories,
14
- registerAbility,
15
- registerAbilityCategory,
16
- } from './actions';
17
-
18
- /**
19
- * Resolver for getAbilities selector.
20
- * Fetches all abilities from the server.
21
- *
22
- * The resolver only fetches once (without query args filter) and stores all abilities.
23
- * Query args filtering handled client-side by the selector for better performance
24
- * and to avoid multiple API requests when filtering by different categories.
25
- */
26
- export function getAbilities() {
27
- // @ts-expect-error - registry types are not yet available
28
- return async ( { dispatch, registry, select } ) => {
29
- const existingAbilities = select.getAbilities();
30
-
31
- // Check if we have any server-side abilities (abilities without callbacks)
32
- // Client abilities have callbacks and are registered immediately on page load
33
- // We only want to skip fetching if we've already fetched server abilities
34
- const hasServerAbilities = existingAbilities.some(
35
- ( ability: Ability ) => ! ability.callback
36
- );
37
-
38
- if ( hasServerAbilities ) {
39
- return;
40
- }
41
-
42
- const abilities = await registry
43
- .resolveSelect( coreStore )
44
- .getEntityRecords( ENTITY_KIND, ENTITY_NAME, {
45
- per_page: -1,
46
- } );
47
-
48
- dispatch( receiveAbilities( abilities || [] ) );
49
- };
50
- }
51
-
52
- /**
53
- * Resolver for getAbility selector.
54
- * Fetches a specific ability from the server if not already in store.
55
- *
56
- * @param name Ability name.
57
- */
58
- export function getAbility( name: string ) {
59
- // @ts-expect-error - registry types are not yet available
60
- return async ( { dispatch, registry, select } ) => {
61
- // Check if ability already exists in store (i.e. client ability or already fetched)
62
- const existingAbility = select.getAbility( name );
63
- if ( existingAbility ) {
64
- return;
65
- }
66
-
67
- try {
68
- const ability = await registry
69
- .resolveSelect( coreStore )
70
- .getEntityRecord( ENTITY_KIND, ENTITY_NAME, name );
71
-
72
- if ( ability ) {
73
- await dispatch( registerAbility( ability ) );
74
- }
75
- } catch ( error ) {
76
- // If ability doesn't exist or error, we'll return undefined from the selector
77
- // eslint-disable-next-line no-console
78
- console.debug( `Ability not found: ${ name }` );
79
- }
80
- };
81
- }
82
-
83
- /**
84
- * Resolver for getAbilityCategories selector.
85
- * Fetches all categories from the server.
86
- *
87
- * The resolver only fetches once and stores all categories.
88
- */
89
- export function getAbilityCategories() {
90
- // @ts-expect-error - registry types are not yet available
91
- return async ( { dispatch, registry, select } ) => {
92
- const existingCategories = select.getAbilityCategories();
93
-
94
- // Check if we have any server-side categories (categories without meta._clientRegistered flag)
95
- // Client categories have meta._clientRegistered=true and might be registered immediately
96
- // We only want to skip fetching if we've already fetched server categories
97
- const hasServerCategories = existingCategories.some(
98
- ( category: AbilityCategory ) => ! category.meta?._clientRegistered
99
- );
100
-
101
- if ( hasServerCategories ) {
102
- return;
103
- }
104
-
105
- const categories = await registry
106
- .resolveSelect( coreStore )
107
- .getEntityRecords( ENTITY_KIND, ENTITY_NAME_CATEGORIES, {
108
- per_page: -1,
109
- } );
110
-
111
- dispatch( receiveCategories( categories || [] ) );
112
- };
113
- }
114
-
115
- /**
116
- * Resolver for getAbilityCategory selector.
117
- * Fetches a specific category from the server if not already in store.
118
- *
119
- * @param slug Category slug.
120
- */
121
- export function getAbilityCategory( slug: string ) {
122
- // @ts-expect-error - registry types are not yet available
123
- return async ( { dispatch, registry, select } ) => {
124
- // Check if category already exists in store (either client-registered or server-fetched).
125
- // This prevents unnecessary network requests while allowing client-side categories
126
- // to be retrieved immediately without hitting the API.
127
- const existingCategory = select.getAbilityCategory( slug );
128
- if ( existingCategory ) {
129
- return;
130
- }
131
-
132
- try {
133
- const category = await registry
134
- .resolveSelect( coreStore )
135
- .getEntityRecord( ENTITY_KIND, ENTITY_NAME_CATEGORIES, slug );
136
-
137
- if ( category ) {
138
- await dispatch(
139
- registerAbilityCategory( category.slug, {
140
- label: category.label,
141
- description: category.description,
142
- meta: category.meta,
143
- } )
144
- );
145
- }
146
- } catch ( error ) {
147
- // eslint-disable-next-line no-console
148
- console.debug( `Category not found: ${ slug }` );
149
- }
150
- };
151
- }