@wordpress/abilities 0.2.0 → 0.2.1-next.738bb1424.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 (84) hide show
  1. package/README.md +32 -40
  2. package/build/{api.js → api.cjs} +22 -69
  3. package/build/api.cjs.map +7 -0
  4. package/build/{index.js → index.cjs} +4 -4
  5. package/build/{index.js.map → index.cjs.map} +2 -2
  6. package/build/store/{actions.js → actions.cjs} +43 -33
  7. package/build/store/actions.cjs.map +7 -0
  8. package/build/store/{constants.js → constants.cjs} +7 -16
  9. package/build/store/constants.cjs.map +7 -0
  10. package/build/store/{index.js → index.cjs} +6 -32
  11. package/build/store/index.cjs.map +7 -0
  12. package/build/store/{reducer.js → reducer.cjs} +4 -24
  13. package/build/store/reducer.cjs.map +7 -0
  14. package/build/store/{selectors.js → selectors.cjs} +2 -2
  15. package/build/store/selectors.cjs.map +7 -0
  16. package/build/store/types.cjs +19 -0
  17. package/build/store/types.cjs.map +7 -0
  18. package/build/{types.js → types.cjs} +1 -1
  19. package/build/types.cjs.map +7 -0
  20. package/build/{validation.js → validation.cjs} +3 -3
  21. package/build-module/api.js +22 -59
  22. package/build-module/api.js.map +2 -2
  23. package/build-module/index.js +3 -3
  24. package/build-module/index.js.map +2 -2
  25. package/build-module/store/actions.js +44 -33
  26. package/build-module/store/actions.js.map +2 -2
  27. package/build-module/store/constants.js +4 -10
  28. package/build-module/store/constants.js.map +2 -2
  29. package/build-module/store/index.js +6 -37
  30. package/build-module/store/index.js.map +2 -2
  31. package/build-module/store/reducer.js +3 -25
  32. package/build-module/store/reducer.js.map +2 -2
  33. package/build-module/store/selectors.js +1 -1
  34. package/build-module/store/selectors.js.map +2 -2
  35. package/build-module/store/types.js +1 -0
  36. package/build-module/store/types.js.map +7 -0
  37. package/build-types/api.d.ts +16 -23
  38. package/build-types/api.d.ts.map +1 -1
  39. package/build-types/index.d.ts +1 -1
  40. package/build-types/index.d.ts.map +1 -1
  41. package/build-types/store/actions.d.ts +5 -27
  42. package/build-types/store/actions.d.ts.map +1 -1
  43. package/build-types/store/constants.d.ts +2 -5
  44. package/build-types/store/constants.d.ts.map +1 -1
  45. package/build-types/store/index.d.ts +1 -1
  46. package/build-types/store/index.d.ts.map +1 -1
  47. package/build-types/store/reducer.d.ts +21 -11
  48. package/build-types/store/reducer.d.ts.map +1 -1
  49. package/build-types/store/selectors.d.ts +2 -1
  50. package/build-types/store/selectors.d.ts.map +1 -1
  51. package/build-types/store/types.d.ts +18 -0
  52. package/build-types/store/types.d.ts.map +1 -0
  53. package/build-types/types.d.ts +14 -24
  54. package/build-types/types.d.ts.map +1 -1
  55. package/package.json +8 -10
  56. package/src/api.ts +43 -123
  57. package/src/index.ts +0 -1
  58. package/src/store/actions.ts +61 -46
  59. package/src/store/constants.ts +4 -5
  60. package/src/store/index.ts +2 -34
  61. package/src/store/reducer.ts +27 -33
  62. package/src/store/selectors.ts +3 -7
  63. package/src/store/tests/actions.test.ts +100 -244
  64. package/src/store/tests/reducer.test.ts +0 -451
  65. package/src/store/tests/selectors.test.ts +1 -1
  66. package/src/store/types.ts +19 -0
  67. package/src/tests/api.test.ts +112 -158
  68. package/src/types.ts +14 -26
  69. package/build/api.js.map +0 -7
  70. package/build/store/actions.js.map +0 -7
  71. package/build/store/constants.js.map +0 -7
  72. package/build/store/index.js.map +0 -7
  73. package/build/store/reducer.js.map +0 -7
  74. package/build/store/resolvers.js +0 -107
  75. package/build/store/resolvers.js.map +0 -7
  76. package/build/store/selectors.js.map +0 -7
  77. package/build/types.js.map +0 -7
  78. package/build-module/store/resolvers.js +0 -84
  79. package/build-module/store/resolvers.js.map +0 -7
  80. package/build-types/store/resolvers.d.ts +0 -47
  81. package/build-types/store/resolvers.d.ts.map +0 -1
  82. package/src/store/resolvers.ts +0 -151
  83. package/src/store/tests/resolvers.test.ts +0 -642
  84. /package/build/{validation.js.map → validation.cjs.map} +0 -0
@@ -2,14 +2,13 @@
2
2
  * Store constants
3
3
  */
4
4
  export const STORE_NAME = 'core/abilities';
5
- export const ENTITY_KIND = 'root';
6
- export const ENTITY_NAME = 'abilities';
7
- export const ENTITY_NAME_CATEGORIES = 'ability-categories';
5
+
6
+ // Validation patterns
7
+ export const ABILITY_NAME_PATTERN = /^[a-z0-9-]+\/[a-z0-9-]+$/;
8
+ export const CATEGORY_SLUG_PATTERN = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
8
9
 
9
10
  // Action types
10
- export const RECEIVE_ABILITIES = 'RECEIVE_ABILITIES';
11
11
  export const REGISTER_ABILITY = 'REGISTER_ABILITY';
12
12
  export const UNREGISTER_ABILITY = 'UNREGISTER_ABILITY';
13
- export const RECEIVE_CATEGORIES = 'RECEIVE_CATEGORIES';
14
13
  export const REGISTER_ABILITY_CATEGORY = 'REGISTER_ABILITY_CATEGORY';
15
14
  export const UNREGISTER_ABILITY_CATEGORY = 'UNREGISTER_ABILITY_CATEGORY';
@@ -1,9 +1,7 @@
1
1
  /**
2
2
  * WordPress dependencies
3
3
  */
4
- import { createReduxStore, register, dispatch } from '@wordpress/data';
5
- import { store as coreStore } from '@wordpress/core-data';
6
- import { __ } from '@wordpress/i18n';
4
+ import { createReduxStore, register } from '@wordpress/data';
7
5
 
8
6
  /**
9
7
  * Internal dependencies
@@ -11,13 +9,7 @@ import { __ } from '@wordpress/i18n';
11
9
  import reducer from './reducer';
12
10
  import * as actions from './actions';
13
11
  import * as selectors from './selectors';
14
- import * as resolvers from './resolvers';
15
- import {
16
- STORE_NAME,
17
- ENTITY_KIND,
18
- ENTITY_NAME,
19
- ENTITY_NAME_CATEGORIES,
20
- } from './constants';
12
+ import { STORE_NAME } from './constants';
21
13
 
22
14
  /**
23
15
  * The abilities store definition.
@@ -26,30 +18,6 @@ export const store = createReduxStore( STORE_NAME, {
26
18
  reducer,
27
19
  actions,
28
20
  selectors,
29
- resolvers,
30
21
  } );
31
22
 
32
23
  register( store );
33
-
34
- dispatch( coreStore ).addEntities( [
35
- {
36
- name: ENTITY_NAME,
37
- kind: ENTITY_KIND,
38
- key: 'name',
39
- baseURL: '/wp-abilities/v1/abilities',
40
- baseURLParams: { context: 'edit' },
41
- plural: 'abilities',
42
- label: __( 'Abilities' ),
43
- supportsPagination: true,
44
- },
45
- {
46
- name: ENTITY_NAME_CATEGORIES,
47
- kind: ENTITY_KIND,
48
- key: 'slug',
49
- baseURL: '/wp-abilities/v1/categories',
50
- baseURLParams: { context: 'edit' },
51
- plural: 'ability-categories',
52
- label: __( 'Ability Categories' ),
53
- supportsPagination: true,
54
- },
55
- ] );
@@ -8,10 +8,8 @@ import { combineReducers } from '@wordpress/data';
8
8
  */
9
9
  import type { Ability, AbilityCategory } from '../types';
10
10
  import {
11
- RECEIVE_ABILITIES,
12
11
  REGISTER_ABILITY,
13
12
  UNREGISTER_ABILITY,
14
- RECEIVE_CATEGORIES,
15
13
  REGISTER_ABILITY_CATEGORY,
16
14
  UNREGISTER_ABILITY_CATEGORY,
17
15
  } from './constants';
@@ -78,16 +76,32 @@ function sanitizeCategory( category: any ): AbilityCategory {
78
76
  );
79
77
  }
80
78
 
81
- interface AbilitiesAction {
82
- type: string;
83
- abilities?: Ability[];
84
- ability?: Ability;
85
- categories?: AbilityCategory[];
86
- category?: AbilityCategory;
87
- name?: string;
88
- slug?: string;
79
+ interface RegisterAbilityAction {
80
+ type: typeof REGISTER_ABILITY;
81
+ ability: Ability;
89
82
  }
90
83
 
84
+ interface UnregisterAbilityAction {
85
+ type: typeof UNREGISTER_ABILITY;
86
+ name: string;
87
+ }
88
+
89
+ interface RegisterAbilityCategoryAction {
90
+ type: typeof REGISTER_ABILITY_CATEGORY;
91
+ category: AbilityCategory;
92
+ }
93
+
94
+ interface UnregisterAbilityCategoryAction {
95
+ type: typeof UNREGISTER_ABILITY_CATEGORY;
96
+ slug: string;
97
+ }
98
+
99
+ type AbilitiesAction = RegisterAbilityAction | UnregisterAbilityAction;
100
+
101
+ type AbilitiesCategoryAction =
102
+ | RegisterAbilityCategoryAction
103
+ | UnregisterAbilityCategoryAction;
104
+
91
105
  const DEFAULT_STATE: Record< string, Ability > = {};
92
106
 
93
107
  /**
@@ -102,16 +116,6 @@ function abilitiesByName(
102
116
  action: AbilitiesAction
103
117
  ): Record< string, Ability > {
104
118
  switch ( action.type ) {
105
- case RECEIVE_ABILITIES: {
106
- if ( ! action.abilities ) {
107
- return state;
108
- }
109
- const newState: Record< string, Ability > = {};
110
- action.abilities.forEach( ( ability ) => {
111
- newState[ ability.name ] = sanitizeAbility( ability );
112
- } );
113
- return newState;
114
- }
115
119
  case REGISTER_ABILITY: {
116
120
  if ( ! action.ability ) {
117
121
  return state;
@@ -122,7 +126,7 @@ function abilitiesByName(
122
126
  };
123
127
  }
124
128
  case UNREGISTER_ABILITY: {
125
- if ( ! action.name || ! state[ action.name ] ) {
129
+ if ( ! state[ action.name ] ) {
126
130
  return state;
127
131
  }
128
132
  const { [ action.name ]: _, ...newState } = state;
@@ -144,19 +148,9 @@ const DEFAULT_CATEGORIES_STATE: Record< string, AbilityCategory > = {};
144
148
  */
145
149
  function categoriesBySlug(
146
150
  state: Record< string, AbilityCategory > = DEFAULT_CATEGORIES_STATE,
147
- action: AbilitiesAction
151
+ action: AbilitiesCategoryAction
148
152
  ): Record< string, AbilityCategory > {
149
153
  switch ( action.type ) {
150
- case RECEIVE_CATEGORIES: {
151
- if ( ! action.categories ) {
152
- return state;
153
- }
154
- const newState: Record< string, AbilityCategory > = {};
155
- action.categories.forEach( ( category ) => {
156
- newState[ category.slug ] = sanitizeCategory( category );
157
- } );
158
- return newState;
159
- }
160
154
  case REGISTER_ABILITY_CATEGORY: {
161
155
  if ( ! action.category ) {
162
156
  return state;
@@ -167,7 +161,7 @@ function categoriesBySlug(
167
161
  };
168
162
  }
169
163
  case UNREGISTER_ABILITY_CATEGORY: {
170
- if ( ! action.slug || ! state[ action.slug ] ) {
164
+ if ( ! state[ action.slug ] ) {
171
165
  return state;
172
166
  }
173
167
  const { [ action.slug ]: _, ...newState } = state;
@@ -6,12 +6,8 @@ import { createSelector } from '@wordpress/data';
6
6
  /**
7
7
  * Internal dependencies
8
8
  */
9
- import type {
10
- Ability,
11
- AbilityCategory,
12
- AbilitiesQueryArgs,
13
- AbilitiesState,
14
- } from '../types';
9
+ import type { Ability, AbilityCategory, AbilitiesQueryArgs } from '../types';
10
+ import type { AbilitiesState } from './types';
15
11
 
16
12
  /**
17
13
  * Returns all registered abilities.
@@ -34,7 +30,7 @@ export const getAbilities = createSelector(
34
30
  }
35
31
  return abilities;
36
32
  },
37
- ( state: AbilitiesState, category?: string ) => [
33
+ ( state: AbilitiesState, { category }: AbilitiesQueryArgs = {} ) => [
38
34
  state.abilitiesByName,
39
35
  category,
40
36
  ]