@wordpress/abilities 0.24.0 → 0.26.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 (41) hide show
  1. package/build/api.cjs +176 -122
  2. package/build/api.cjs.map +1 -7
  3. package/build/index.cjs +73 -52
  4. package/build/store/actions.cjs +119 -187
  5. package/build/store/actions.cjs.map +1 -7
  6. package/build/store/constants.cjs +23 -47
  7. package/build/store/constants.cjs.map +1 -7
  8. package/build/store/index.cjs +36 -40
  9. package/build/store/index.cjs.map +1 -7
  10. package/build/store/reducer.cjs +102 -94
  11. package/build/store/reducer.cjs.map +1 -7
  12. package/build/store/selectors.cjs +49 -58
  13. package/build/store/selectors.cjs.map +1 -7
  14. package/build/store/types.cjs +4 -18
  15. package/build/types.cjs +4 -18
  16. package/build/validation.cjs +122 -137
  17. package/build/validation.cjs.map +1 -7
  18. package/build-module/api.mjs +161 -87
  19. package/build-module/api.mjs.map +1 -7
  20. package/build-module/index.mjs +2 -26
  21. package/build-module/store/actions.mjs +112 -167
  22. package/build-module/store/actions.mjs.map +1 -7
  23. package/build-module/store/constants.mjs +13 -17
  24. package/build-module/store/constants.mjs.map +1 -7
  25. package/build-module/store/index.mjs +11 -9
  26. package/build-module/store/index.mjs.map +1 -7
  27. package/build-module/store/reducer.mjs +97 -78
  28. package/build-module/store/reducer.mjs.map +1 -7
  29. package/build-module/store/selectors.mjs +42 -31
  30. package/build-module/store/selectors.mjs.map +1 -7
  31. package/build-module/store/types.mjs +0 -1
  32. package/build-module/types.mjs +0 -1
  33. package/build-module/validation.mjs +101 -109
  34. package/build-module/validation.mjs.map +1 -7
  35. package/package.json +5 -5
  36. package/build/index.cjs.map +0 -7
  37. package/build/store/types.cjs.map +0 -7
  38. package/build/types.cjs.map +0 -7
  39. package/build-module/index.mjs.map +0 -7
  40. package/build-module/store/types.mjs.map +0 -7
  41. package/build-module/types.mjs.map +0 -7
package/build/api.cjs CHANGED
@@ -1,142 +1,196 @@
1
1
  "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // packages/abilities/src/api.ts
21
- var api_exports = {};
22
- __export(api_exports, {
23
- executeAbility: () => executeAbility,
24
- getAbilities: () => getAbilities,
25
- getAbility: () => getAbility,
26
- getAbilityCategories: () => getAbilityCategories,
27
- getAbilityCategory: () => getAbilityCategory,
28
- registerAbility: () => registerAbility,
29
- registerAbilityCategory: () => registerAbilityCategory,
30
- unregisterAbility: () => unregisterAbility,
31
- unregisterAbilityCategory: () => unregisterAbilityCategory
2
+ Object.defineProperties(exports, {
3
+ __esModule: { value: true },
4
+ [Symbol.toStringTag]: { value: "Module" }
32
5
  });
33
- module.exports = __toCommonJS(api_exports);
34
- var import_data = require("@wordpress/data");
35
- var import_i18n = require("@wordpress/i18n");
36
- var import_store = require("./store/index.cjs");
37
- var import_validation = require("./validation.cjs");
6
+ let _wordpress_data = require("@wordpress/data");
7
+ let _wordpress_i18n = require("@wordpress/i18n");
8
+ let __store_index_ts = require("./store/index.cjs");
9
+ let __validation_ts = require("./validation.cjs");
10
+ // packages/abilities/src/api.ts
11
+ /**
12
+ * Get all available abilities with optional filtering.
13
+ *
14
+ * @param args Optional query arguments for filtering abilities.
15
+ * @return Array of matching abilities.
16
+ */
38
17
  function getAbilities(args = {}) {
39
- return (0, import_data.select)(import_store.store).getAbilities(args);
18
+ return (0, _wordpress_data.select)(__store_index_ts.store).getAbilities(args);
40
19
  }
20
+ /**
21
+ * Get a specific ability by name.
22
+ *
23
+ * @param name The ability name.
24
+ * @return The ability or undefined if not found.
25
+ */
41
26
  function getAbility(name) {
42
- return (0, import_data.select)(import_store.store).getAbility(name);
27
+ return (0, _wordpress_data.select)(__store_index_ts.store).getAbility(name);
43
28
  }
29
+ /**
30
+ * Get all available ability categories.
31
+ *
32
+ * @return Array of categories.
33
+ */
44
34
  function getAbilityCategories() {
45
- return (0, import_data.select)(import_store.store).getAbilityCategories();
35
+ return (0, _wordpress_data.select)(__store_index_ts.store).getAbilityCategories();
46
36
  }
37
+ /**
38
+ * Get a specific ability category by slug.
39
+ *
40
+ * @param slug The category slug.
41
+ * @return The category or undefined if not found.
42
+ */
47
43
  function getAbilityCategory(slug) {
48
- return (0, import_data.select)(import_store.store).getAbilityCategory(slug);
44
+ return (0, _wordpress_data.select)(__store_index_ts.store).getAbilityCategory(slug);
49
45
  }
46
+ /**
47
+ * Register a client-side ability.
48
+ *
49
+ * Client-side abilities are executed locally in the browser and must include
50
+ * a callback function. The ability's category must already be registered.
51
+ *
52
+ * @param ability The ability definition including callback.
53
+ * @throws {Error} If the ability fails validation.
54
+ *
55
+ * @example
56
+ * ```js
57
+ * registerAbility({
58
+ * name: 'my-plugin/navigate',
59
+ * label: 'Navigate to URL',
60
+ * description: 'Navigates to a URL within WordPress admin',
61
+ * category: 'navigation',
62
+ * input_schema: {
63
+ * type: 'object',
64
+ * properties: {
65
+ * url: { type: 'string' }
66
+ * },
67
+ * required: ['url']
68
+ * },
69
+ * callback: async ({ url }) => {
70
+ * window.location.href = url;
71
+ * return { success: true };
72
+ * }
73
+ * });
74
+ * ```
75
+ */
50
76
  function registerAbility(ability) {
51
- (0, import_data.dispatch)(import_store.store).registerAbility(ability);
77
+ (0, _wordpress_data.dispatch)(__store_index_ts.store).registerAbility(ability);
52
78
  }
79
+ /**
80
+ * Unregister a client-side ability from the store.
81
+ *
82
+ * @param name The ability name to unregister.
83
+ * @throws {Error} If the ability is server-side and cannot be unregistered.
84
+ */
53
85
  function unregisterAbility(name) {
54
- (0, import_data.dispatch)(import_store.store).unregisterAbility(name);
86
+ (0, _wordpress_data.dispatch)(__store_index_ts.store).unregisterAbility(name);
55
87
  }
88
+ /**
89
+ * Register a client-side ability category.
90
+ *
91
+ * Use this when registering client-side abilities that belong to a category
92
+ * not already defined by the server. Client-side categories are stored
93
+ * alongside server-side categories in the same store.
94
+ *
95
+ * @param slug Category slug (lowercase alphanumeric with dashes only).
96
+ * @param args Category arguments (label, description, optional meta).
97
+ * @throws {Error} If the category fails validation.
98
+ *
99
+ * @example
100
+ * ```js
101
+ * // Register a new category for block editor abilities
102
+ * registerAbilityCategory('block-editor', {
103
+ * label: 'Block Editor',
104
+ * description: 'Abilities for interacting with the WordPress block editor'
105
+ * });
106
+ *
107
+ * // Then register abilities using this category
108
+ * registerAbility({
109
+ * name: 'my-plugin/insert-block',
110
+ * label: 'Insert Block',
111
+ * description: 'Inserts a block into the editor',
112
+ * category: 'block-editor',
113
+ * callback: async ({ blockType }) => {
114
+ * // Implementation
115
+ * return { success: true };
116
+ * }
117
+ * });
118
+ * ```
119
+ */
56
120
  function registerAbilityCategory(slug, args) {
57
- (0, import_data.dispatch)(import_store.store).registerAbilityCategory(slug, args);
121
+ (0, _wordpress_data.dispatch)(__store_index_ts.store).registerAbilityCategory(slug, args);
58
122
  }
123
+ /**
124
+ * Unregister an ability category.
125
+ *
126
+ * @param slug The category slug to unregister.
127
+ *
128
+ * @example
129
+ * ```js
130
+ * unregisterAbilityCategory('block-editor');
131
+ * ```
132
+ */
59
133
  function unregisterAbilityCategory(slug) {
60
- (0, import_data.dispatch)(import_store.store).unregisterAbilityCategory(slug);
134
+ (0, _wordpress_data.dispatch)(__store_index_ts.store).unregisterAbilityCategory(slug);
61
135
  }
136
+ /**
137
+ * Execute an ability.
138
+ *
139
+ * Validates input and output against their schemas when defined. For
140
+ * server-side abilities, input is validated on the client first to avoid
141
+ * unnecessary network roundtrips, then both input and output are validated on
142
+ * the server. The client also re-validates the output to ensure data
143
+ * compatibility between server and client.
144
+ *
145
+ * @param name The ability name.
146
+ * @param input Optional input parameters for the ability.
147
+ * @return Promise resolving to the ability execution result.
148
+ * @throws {Error} If the ability is not found, permission is denied, input or output validation fails, or execution throws.
149
+ */
62
150
  async function executeAbility(name, input) {
63
- const ability = getAbility(name);
64
- if (!ability) {
65
- throw new Error((0, import_i18n.sprintf)("Ability not found: %s", name));
66
- }
67
- if (!ability.callback) {
68
- throw new Error(
69
- (0, import_i18n.sprintf)(
70
- 'Ability "%s" is missing callback. Please ensure the ability is properly registered.',
71
- ability.name
72
- )
73
- );
74
- }
75
- if (ability.permissionCallback) {
76
- const hasPermission = await ability.permissionCallback(input);
77
- if (!hasPermission) {
78
- const error = new Error(
79
- (0, import_i18n.sprintf)("Permission denied for ability: %s", ability.name)
80
- );
81
- error.code = "ability_permission_denied";
82
- throw error;
83
- }
84
- }
85
- if (ability.input_schema) {
86
- const inputValidation = (0, import_validation.validateValueFromSchema)(
87
- input,
88
- ability.input_schema,
89
- "input"
90
- );
91
- if (inputValidation !== true) {
92
- const error = new Error(
93
- (0, import_i18n.sprintf)(
94
- 'Ability "%1$s" has invalid input. Reason: %2$s',
95
- ability.name,
96
- inputValidation
97
- )
98
- );
99
- error.code = "ability_invalid_input";
100
- throw error;
101
- }
102
- }
103
- let result;
104
- try {
105
- result = await ability.callback(input);
106
- } catch (error) {
107
- console.error(`Error executing ability ${ability.name}:`, error);
108
- throw error;
109
- }
110
- if (ability.output_schema) {
111
- const outputValidation = (0, import_validation.validateValueFromSchema)(
112
- result,
113
- ability.output_schema,
114
- "output"
115
- );
116
- if (outputValidation !== true) {
117
- const error = new Error(
118
- (0, import_i18n.sprintf)(
119
- 'Ability "%1$s" has invalid output. Reason: %2$s',
120
- ability.name,
121
- outputValidation
122
- )
123
- );
124
- error.code = "ability_invalid_output";
125
- throw error;
126
- }
127
- }
128
- return result;
151
+ const ability = getAbility(name);
152
+ if (!ability) throw new Error((0, _wordpress_i18n.sprintf)("Ability not found: %s", name));
153
+ if (!ability.callback) throw new Error((0, _wordpress_i18n.sprintf)("Ability \"%s\" is missing callback. Please ensure the ability is properly registered.", ability.name));
154
+ if (ability.permissionCallback) {
155
+ if (!await ability.permissionCallback(input)) {
156
+ const error = new Error((0, _wordpress_i18n.sprintf)("Permission denied for ability: %s", ability.name));
157
+ error.code = "ability_permission_denied";
158
+ throw error;
159
+ }
160
+ }
161
+ if (ability.input_schema) {
162
+ const inputValidation = (0, __validation_ts.validateValueFromSchema)(input, ability.input_schema, "input");
163
+ if (inputValidation !== true) {
164
+ const error = new Error((0, _wordpress_i18n.sprintf)("Ability \"%1$s\" has invalid input. Reason: %2$s", ability.name, inputValidation));
165
+ error.code = "ability_invalid_input";
166
+ throw error;
167
+ }
168
+ }
169
+ let result;
170
+ try {
171
+ result = await ability.callback(input);
172
+ } catch (error) {
173
+ console.error(`Error executing ability ${ability.name}:`, error);
174
+ throw error;
175
+ }
176
+ if (ability.output_schema) {
177
+ const outputValidation = (0, __validation_ts.validateValueFromSchema)(result, ability.output_schema, "output");
178
+ if (outputValidation !== true) {
179
+ const error = new Error((0, _wordpress_i18n.sprintf)("Ability \"%1$s\" has invalid output. Reason: %2$s", ability.name, outputValidation));
180
+ error.code = "ability_invalid_output";
181
+ throw error;
182
+ }
183
+ }
184
+ return result;
129
185
  }
130
- // Annotate the CommonJS export names for ESM import in node:
131
- 0 && (module.exports = {
132
- executeAbility,
133
- getAbilities,
134
- getAbility,
135
- getAbilityCategories,
136
- getAbilityCategory,
137
- registerAbility,
138
- registerAbilityCategory,
139
- unregisterAbility,
140
- unregisterAbilityCategory
141
- });
142
- //# sourceMappingURL=api.cjs.map
186
+ exports.executeAbility = executeAbility;
187
+ exports.getAbilities = getAbilities;
188
+ exports.getAbility = getAbility;
189
+ exports.getAbilityCategories = getAbilityCategories;
190
+ exports.getAbilityCategory = getAbilityCategory;
191
+ exports.registerAbility = registerAbility;
192
+ exports.registerAbilityCategory = registerAbilityCategory;
193
+ exports.unregisterAbility = unregisterAbility;
194
+ exports.unregisterAbilityCategory = unregisterAbilityCategory;
195
+
196
+ //# sourceMappingURL=api.cjs.map
package/build/api.cjs.map CHANGED
@@ -1,7 +1 @@
1
- {
2
- "version": 3,
3
- "sources": ["../src/api.ts"],
4
- "sourcesContent": ["import { dispatch, select } from '@wordpress/data';\nimport { sprintf } from '@wordpress/i18n';\nimport { store } from './store';\nimport type {\n\tAbility,\n\tAbilityCategory,\n\tAbilityCategoryArgs,\n\tAbilitiesQueryArgs,\n\tAbilityInput,\n\tAbilityOutput,\n} from './types';\nimport { validateValueFromSchema } from './validation';\n\n/**\n * Get all available abilities with optional filtering.\n *\n * @param args Optional query arguments for filtering abilities.\n * @return Array of matching abilities.\n */\nexport function getAbilities( args: AbilitiesQueryArgs = {} ): Ability[] {\n\treturn select( store ).getAbilities( args );\n}\n\n/**\n * Get a specific ability by name.\n *\n * @param name The ability name.\n * @return The ability or undefined if not found.\n */\nexport function getAbility( name: string ): Ability | undefined {\n\treturn select( store ).getAbility( name );\n}\n\n/**\n * Get all available ability categories.\n *\n * @return Array of categories.\n */\nexport function getAbilityCategories(): AbilityCategory[] {\n\treturn select( store ).getAbilityCategories();\n}\n\n/**\n * Get a specific ability category by slug.\n *\n * @param slug The category slug.\n * @return The category or undefined if not found.\n */\nexport function getAbilityCategory(\n\tslug: string\n): AbilityCategory | undefined {\n\treturn select( store ).getAbilityCategory( slug );\n}\n\n/**\n * Register a client-side ability.\n *\n * Client-side abilities are executed locally in the browser and must include\n * a callback function. The ability's category must already be registered.\n *\n * @param ability The ability definition including callback.\n * @throws {Error} If the ability fails validation.\n *\n * @example\n * ```js\n * registerAbility({\n * name: 'my-plugin/navigate',\n * label: 'Navigate to URL',\n * description: 'Navigates to a URL within WordPress admin',\n * category: 'navigation',\n * input_schema: {\n * type: 'object',\n * properties: {\n * url: { type: 'string' }\n * },\n * required: ['url']\n * },\n * callback: async ({ url }) => {\n * window.location.href = url;\n * return { success: true };\n * }\n * });\n * ```\n */\nexport function registerAbility( ability: Ability ): void {\n\tdispatch( store ).registerAbility( ability );\n}\n\n/**\n * Unregister a client-side ability from the store.\n *\n * @param name The ability name to unregister.\n * @throws {Error} If the ability is server-side and cannot be unregistered.\n */\nexport function unregisterAbility( name: string ): void {\n\tdispatch( store ).unregisterAbility( name );\n}\n\n/**\n * Register a client-side ability category.\n *\n * Use this when registering client-side abilities that belong to a category\n * not already defined by the server. Client-side categories are stored\n * alongside server-side categories in the same store.\n *\n * @param slug Category slug (lowercase alphanumeric with dashes only).\n * @param args Category arguments (label, description, optional meta).\n * @throws {Error} If the category fails validation.\n *\n * @example\n * ```js\n * // Register a new category for block editor abilities\n * registerAbilityCategory('block-editor', {\n * label: 'Block Editor',\n * description: 'Abilities for interacting with the WordPress block editor'\n * });\n *\n * // Then register abilities using this category\n * registerAbility({\n * name: 'my-plugin/insert-block',\n * label: 'Insert Block',\n * description: 'Inserts a block into the editor',\n * category: 'block-editor',\n * callback: async ({ blockType }) => {\n * // Implementation\n * return { success: true };\n * }\n * });\n * ```\n */\nexport function registerAbilityCategory(\n\tslug: string,\n\targs: AbilityCategoryArgs\n): void {\n\tdispatch( store ).registerAbilityCategory( slug, args );\n}\n\n/**\n * Unregister an ability category.\n *\n * @param slug The category slug to unregister.\n *\n * @example\n * ```js\n * unregisterAbilityCategory('block-editor');\n * ```\n */\nexport function unregisterAbilityCategory( slug: string ): void {\n\tdispatch( store ).unregisterAbilityCategory( slug );\n}\n\n/**\n * Execute an ability.\n *\n * Validates input and output against their schemas when defined. For\n * server-side abilities, input is validated on the client first to avoid\n * unnecessary network roundtrips, then both input and output are validated on\n * the server. The client also re-validates the output to ensure data\n * compatibility between server and client.\n *\n * @param name The ability name.\n * @param input Optional input parameters for the ability.\n * @return Promise resolving to the ability execution result.\n * @throws {Error} If the ability is not found, permission is denied, input or output validation fails, or execution throws.\n */\nexport async function executeAbility(\n\tname: string,\n\tinput?: AbilityInput\n): Promise< AbilityOutput > {\n\tconst ability = getAbility( name );\n\tif ( ! ability ) {\n\t\tthrow new Error( sprintf( 'Ability not found: %s', name ) );\n\t}\n\n\tif ( ! ability.callback ) {\n\t\tthrow new Error(\n\t\t\tsprintf(\n\t\t\t\t'Ability \"%s\" is missing callback. Please ensure the ability is properly registered.',\n\t\t\t\tability.name\n\t\t\t)\n\t\t);\n\t}\n\n\t// Check permission callback if defined\n\tif ( ability.permissionCallback ) {\n\t\tconst hasPermission = await ability.permissionCallback( input );\n\t\tif ( ! hasPermission ) {\n\t\t\tconst error = new Error(\n\t\t\t\tsprintf( 'Permission denied for ability: %s', ability.name )\n\t\t\t);\n\t\t\t( error as any ).code = 'ability_permission_denied';\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\t// Validate input\n\tif ( ability.input_schema ) {\n\t\tconst inputValidation = validateValueFromSchema(\n\t\t\tinput,\n\t\t\tability.input_schema,\n\t\t\t'input'\n\t\t);\n\t\tif ( inputValidation !== true ) {\n\t\t\tconst error = new Error(\n\t\t\t\tsprintf(\n\t\t\t\t\t'Ability \"%1$s\" has invalid input. Reason: %2$s',\n\t\t\t\t\tability.name,\n\t\t\t\t\tinputValidation\n\t\t\t\t)\n\t\t\t);\n\t\t\t( error as any ).code = 'ability_invalid_input';\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\t// Execute the ability\n\tlet result: AbilityOutput;\n\ttry {\n\t\tresult = await ability.callback( input );\n\t} catch ( error ) {\n\t\t// eslint-disable-next-line no-console\n\t\tconsole.error( `Error executing ability ${ ability.name }:`, error );\n\t\tthrow error;\n\t}\n\n\t// Validate output\n\tif ( ability.output_schema ) {\n\t\tconst outputValidation = validateValueFromSchema(\n\t\t\tresult,\n\t\t\tability.output_schema,\n\t\t\t'output'\n\t\t);\n\t\tif ( outputValidation !== true ) {\n\t\t\tconst error = new Error(\n\t\t\t\tsprintf(\n\t\t\t\t\t'Ability \"%1$s\" has invalid output. Reason: %2$s',\n\t\t\t\t\tability.name,\n\t\t\t\t\toutputValidation\n\t\t\t\t)\n\t\t\t);\n\t\t\t( error as any ).code = 'ability_invalid_output';\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\treturn result;\n}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,kBAAiC;AACjC,kBAAwB;AACxB,mBAAsB;AAStB,wBAAwC;AAQjC,SAAS,aAAc,OAA2B,CAAC,GAAe;AACxE,aAAO,oBAAQ,kBAAM,EAAE,aAAc,IAAK;AAC3C;AAQO,SAAS,WAAY,MAAoC;AAC/D,aAAO,oBAAQ,kBAAM,EAAE,WAAY,IAAK;AACzC;AAOO,SAAS,uBAA0C;AACzD,aAAO,oBAAQ,kBAAM,EAAE,qBAAqB;AAC7C;AAQO,SAAS,mBACf,MAC8B;AAC9B,aAAO,oBAAQ,kBAAM,EAAE,mBAAoB,IAAK;AACjD;AAgCO,SAAS,gBAAiB,SAAyB;AACzD,4BAAU,kBAAM,EAAE,gBAAiB,OAAQ;AAC5C;AAQO,SAAS,kBAAmB,MAAqB;AACvD,4BAAU,kBAAM,EAAE,kBAAmB,IAAK;AAC3C;AAkCO,SAAS,wBACf,MACA,MACO;AACP,4BAAU,kBAAM,EAAE,wBAAyB,MAAM,IAAK;AACvD;AAYO,SAAS,0BAA2B,MAAqB;AAC/D,4BAAU,kBAAM,EAAE,0BAA2B,IAAK;AACnD;AAgBA,eAAsB,eACrB,MACA,OAC2B;AAC3B,QAAM,UAAU,WAAY,IAAK;AACjC,MAAK,CAAE,SAAU;AAChB,UAAM,IAAI,UAAO,qBAAS,yBAAyB,IAAK,CAAE;AAAA,EAC3D;AAEA,MAAK,CAAE,QAAQ,UAAW;AACzB,UAAM,IAAI;AAAA,UACT;AAAA,QACC;AAAA,QACA,QAAQ;AAAA,MACT;AAAA,IACD;AAAA,EACD;AAGA,MAAK,QAAQ,oBAAqB;AACjC,UAAM,gBAAgB,MAAM,QAAQ,mBAAoB,KAAM;AAC9D,QAAK,CAAE,eAAgB;AACtB,YAAM,QAAQ,IAAI;AAAA,YACjB,qBAAS,qCAAqC,QAAQ,IAAK;AAAA,MAC5D;AACA,MAAE,MAAe,OAAO;AACxB,YAAM;AAAA,IACP;AAAA,EACD;AAGA,MAAK,QAAQ,cAAe;AAC3B,UAAM,sBAAkB;AAAA,MACvB;AAAA,MACA,QAAQ;AAAA,MACR;AAAA,IACD;AACA,QAAK,oBAAoB,MAAO;AAC/B,YAAM,QAAQ,IAAI;AAAA,YACjB;AAAA,UACC;AAAA,UACA,QAAQ;AAAA,UACR;AAAA,QACD;AAAA,MACD;AACA,MAAE,MAAe,OAAO;AACxB,YAAM;AAAA,IACP;AAAA,EACD;AAGA,MAAI;AACJ,MAAI;AACH,aAAS,MAAM,QAAQ,SAAU,KAAM;AAAA,EACxC,SAAU,OAAQ;AAEjB,YAAQ,MAAO,2BAA4B,QAAQ,IAAK,KAAK,KAAM;AACnE,UAAM;AAAA,EACP;AAGA,MAAK,QAAQ,eAAgB;AAC5B,UAAM,uBAAmB;AAAA,MACxB;AAAA,MACA,QAAQ;AAAA,MACR;AAAA,IACD;AACA,QAAK,qBAAqB,MAAO;AAChC,YAAM,QAAQ,IAAI;AAAA,YACjB;AAAA,UACC;AAAA,UACA,QAAQ;AAAA,UACR;AAAA,QACD;AAAA,MACD;AACA,MAAE,MAAe,OAAO;AACxB,YAAM;AAAA,IACP;AAAA,EACD;AAEA,SAAO;AACR;",
6
- "names": []
7
- }
1
+ {"version":3,"file":"api.cjs","names":["select","store","sprintf","validateValueFromSchema"],"sources":["../src/api.ts"],"sourcesContent":["import { dispatch, select } from '@wordpress/data';\nimport { sprintf } from '@wordpress/i18n';\nimport { store } from './store';\nimport type {\n\tAbility,\n\tAbilityCategory,\n\tAbilityCategoryArgs,\n\tAbilitiesQueryArgs,\n\tAbilityInput,\n\tAbilityOutput,\n} from './types';\nimport { validateValueFromSchema } from './validation';\n\n/**\n * Get all available abilities with optional filtering.\n *\n * @param args Optional query arguments for filtering abilities.\n * @return Array of matching abilities.\n */\nexport function getAbilities( args: AbilitiesQueryArgs = {} ): Ability[] {\n\treturn select( store ).getAbilities( args );\n}\n\n/**\n * Get a specific ability by name.\n *\n * @param name The ability name.\n * @return The ability or undefined if not found.\n */\nexport function getAbility( name: string ): Ability | undefined {\n\treturn select( store ).getAbility( name );\n}\n\n/**\n * Get all available ability categories.\n *\n * @return Array of categories.\n */\nexport function getAbilityCategories(): AbilityCategory[] {\n\treturn select( store ).getAbilityCategories();\n}\n\n/**\n * Get a specific ability category by slug.\n *\n * @param slug The category slug.\n * @return The category or undefined if not found.\n */\nexport function getAbilityCategory(\n\tslug: string\n): AbilityCategory | undefined {\n\treturn select( store ).getAbilityCategory( slug );\n}\n\n/**\n * Register a client-side ability.\n *\n * Client-side abilities are executed locally in the browser and must include\n * a callback function. The ability's category must already be registered.\n *\n * @param ability The ability definition including callback.\n * @throws {Error} If the ability fails validation.\n *\n * @example\n * ```js\n * registerAbility({\n * name: 'my-plugin/navigate',\n * label: 'Navigate to URL',\n * description: 'Navigates to a URL within WordPress admin',\n * category: 'navigation',\n * input_schema: {\n * type: 'object',\n * properties: {\n * url: { type: 'string' }\n * },\n * required: ['url']\n * },\n * callback: async ({ url }) => {\n * window.location.href = url;\n * return { success: true };\n * }\n * });\n * ```\n */\nexport function registerAbility( ability: Ability ): void {\n\tdispatch( store ).registerAbility( ability );\n}\n\n/**\n * Unregister a client-side ability from the store.\n *\n * @param name The ability name to unregister.\n * @throws {Error} If the ability is server-side and cannot be unregistered.\n */\nexport function unregisterAbility( name: string ): void {\n\tdispatch( store ).unregisterAbility( name );\n}\n\n/**\n * Register a client-side ability category.\n *\n * Use this when registering client-side abilities that belong to a category\n * not already defined by the server. Client-side categories are stored\n * alongside server-side categories in the same store.\n *\n * @param slug Category slug (lowercase alphanumeric with dashes only).\n * @param args Category arguments (label, description, optional meta).\n * @throws {Error} If the category fails validation.\n *\n * @example\n * ```js\n * // Register a new category for block editor abilities\n * registerAbilityCategory('block-editor', {\n * label: 'Block Editor',\n * description: 'Abilities for interacting with the WordPress block editor'\n * });\n *\n * // Then register abilities using this category\n * registerAbility({\n * name: 'my-plugin/insert-block',\n * label: 'Insert Block',\n * description: 'Inserts a block into the editor',\n * category: 'block-editor',\n * callback: async ({ blockType }) => {\n * // Implementation\n * return { success: true };\n * }\n * });\n * ```\n */\nexport function registerAbilityCategory(\n\tslug: string,\n\targs: AbilityCategoryArgs\n): void {\n\tdispatch( store ).registerAbilityCategory( slug, args );\n}\n\n/**\n * Unregister an ability category.\n *\n * @param slug The category slug to unregister.\n *\n * @example\n * ```js\n * unregisterAbilityCategory('block-editor');\n * ```\n */\nexport function unregisterAbilityCategory( slug: string ): void {\n\tdispatch( store ).unregisterAbilityCategory( slug );\n}\n\n/**\n * Execute an ability.\n *\n * Validates input and output against their schemas when defined. For\n * server-side abilities, input is validated on the client first to avoid\n * unnecessary network roundtrips, then both input and output are validated on\n * the server. The client also re-validates the output to ensure data\n * compatibility between server and client.\n *\n * @param name The ability name.\n * @param input Optional input parameters for the ability.\n * @return Promise resolving to the ability execution result.\n * @throws {Error} If the ability is not found, permission is denied, input or output validation fails, or execution throws.\n */\nexport async function executeAbility(\n\tname: string,\n\tinput?: AbilityInput\n): Promise< AbilityOutput > {\n\tconst ability = getAbility( name );\n\tif ( ! ability ) {\n\t\tthrow new Error( sprintf( 'Ability not found: %s', name ) );\n\t}\n\n\tif ( ! ability.callback ) {\n\t\tthrow new Error(\n\t\t\tsprintf(\n\t\t\t\t'Ability \"%s\" is missing callback. Please ensure the ability is properly registered.',\n\t\t\t\tability.name\n\t\t\t)\n\t\t);\n\t}\n\n\t// Check permission callback if defined\n\tif ( ability.permissionCallback ) {\n\t\tconst hasPermission = await ability.permissionCallback( input );\n\t\tif ( ! hasPermission ) {\n\t\t\tconst error = new Error(\n\t\t\t\tsprintf( 'Permission denied for ability: %s', ability.name )\n\t\t\t);\n\t\t\t( error as any ).code = 'ability_permission_denied';\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\t// Validate input\n\tif ( ability.input_schema ) {\n\t\tconst inputValidation = validateValueFromSchema(\n\t\t\tinput,\n\t\t\tability.input_schema,\n\t\t\t'input'\n\t\t);\n\t\tif ( inputValidation !== true ) {\n\t\t\tconst error = new Error(\n\t\t\t\tsprintf(\n\t\t\t\t\t'Ability \"%1$s\" has invalid input. Reason: %2$s',\n\t\t\t\t\tability.name,\n\t\t\t\t\tinputValidation\n\t\t\t\t)\n\t\t\t);\n\t\t\t( error as any ).code = 'ability_invalid_input';\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\t// Execute the ability\n\tlet result: AbilityOutput;\n\ttry {\n\t\tresult = await ability.callback( input );\n\t} catch ( error ) {\n\t\t// eslint-disable-next-line no-console\n\t\tconsole.error( `Error executing ability ${ ability.name }:`, error );\n\t\tthrow error;\n\t}\n\n\t// Validate output\n\tif ( ability.output_schema ) {\n\t\tconst outputValidation = validateValueFromSchema(\n\t\t\tresult,\n\t\t\tability.output_schema,\n\t\t\t'output'\n\t\t);\n\t\tif ( outputValidation !== true ) {\n\t\t\tconst error = new Error(\n\t\t\t\tsprintf(\n\t\t\t\t\t'Ability \"%1$s\" has invalid output. Reason: %2$s',\n\t\t\t\t\tability.name,\n\t\t\t\t\toutputValidation\n\t\t\t\t)\n\t\t\t);\n\t\t\t( error as any ).code = 'ability_invalid_output';\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\treturn result;\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAmBA,SAAgB,aAAc,OAA2B,CAAC,GAAe;CACxE,QAAA,GAAOA,gBAAAA,OAAAA,CAAQC,iBAAAA,KAAM,CAAC,CAAC,aAAc,IAAK;AAC3C;;;;;;;AAQA,SAAgB,WAAY,MAAoC;CAC/D,QAAA,GAAOD,gBAAAA,OAAAA,CAAQC,iBAAAA,KAAM,CAAC,CAAC,WAAY,IAAK;AACzC;;;;;;AAOA,SAAgB,uBAA0C;CACzD,QAAA,GAAOD,gBAAAA,OAAAA,CAAQC,iBAAAA,KAAM,CAAC,CAAC,qBAAqB;AAC7C;;;;;;;AAQA,SAAgB,mBACf,MAC8B;CAC9B,QAAA,GAAOD,gBAAAA,OAAAA,CAAQC,iBAAAA,KAAM,CAAC,CAAC,mBAAoB,IAAK;AACjD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgCA,SAAgB,gBAAiB,SAAyB;CACzD,CAAA,GAAA,gBAAA,SAAA,CAAUA,iBAAAA,KAAM,CAAC,CAAC,gBAAiB,OAAQ;AAC5C;;;;;;;AAQA,SAAgB,kBAAmB,MAAqB;CACvD,CAAA,GAAA,gBAAA,SAAA,CAAUA,iBAAAA,KAAM,CAAC,CAAC,kBAAmB,IAAK;AAC3C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkCA,SAAgB,wBACf,MACA,MACO;CACP,CAAA,GAAA,gBAAA,SAAA,CAAUA,iBAAAA,KAAM,CAAC,CAAC,wBAAyB,MAAM,IAAK;AACvD;;;;;;;;;;;AAYA,SAAgB,0BAA2B,MAAqB;CAC/D,CAAA,GAAA,gBAAA,SAAA,CAAUA,iBAAAA,KAAM,CAAC,CAAC,0BAA2B,IAAK;AACnD;;;;;;;;;;;;;;;AAgBA,eAAsB,eACrB,MACA,OAC2B;CAC3B,MAAM,UAAU,WAAY,IAAK;CACjC,IAAK,CAAE,SACN,MAAM,IAAI,OAAA,GAAOC,gBAAAA,QAAAA,CAAS,yBAAyB,IAAK,CAAE;CAG3D,IAAK,CAAE,QAAQ,UACd,MAAM,IAAI,OAAA,GACTA,gBAAAA,QAAAA,CACC,yFACA,QAAQ,IACT,CACD;CAID,IAAK,QAAQ,oBAEP;MAAA,CAAE,MADqB,QAAQ,mBAAoB,KAAM,GACvC;GACtB,MAAM,QAAQ,IAAI,OAAA,GACjBA,gBAAAA,QAAAA,CAAS,qCAAqC,QAAQ,IAAK,CAC5D;GACA,MAAiB,OAAO;GACxB,MAAM;EACP;;CAID,IAAK,QAAQ,cAAe;EAC3B,MAAM,mBAAA,GAAkBC,gBAAAA,wBAAAA,CACvB,OACA,QAAQ,cACR,OACD;EACA,IAAK,oBAAoB,MAAO;GAC/B,MAAM,QAAQ,IAAI,OAAA,GACjBD,gBAAAA,QAAAA,CACC,oDACA,QAAQ,MACR,eACD,CACD;GACA,MAAiB,OAAO;GACxB,MAAM;EACP;CACD;CAGA,IAAI;CACJ,IAAI;EACH,SAAS,MAAM,QAAQ,SAAU,KAAM;CACxC,SAAU,OAAQ;EAEjB,QAAQ,MAAO,2BAA4B,QAAQ,KAAM,IAAI,KAAM;EACnE,MAAM;CACP;CAGA,IAAK,QAAQ,eAAgB;EAC5B,MAAM,oBAAA,GAAmBC,gBAAAA,wBAAAA,CACxB,QACA,QAAQ,eACR,QACD;EACA,IAAK,qBAAqB,MAAO;GAChC,MAAM,QAAQ,IAAI,OAAA,GACjBD,gBAAAA,QAAAA,CACC,qDACA,QAAQ,MACR,gBACD,CACD;GACA,MAAiB,OAAO;GACxB,MAAM;EACP;CACD;CAEA,OAAO;AACR"}
package/build/index.cjs CHANGED
@@ -1,53 +1,74 @@
1
1
  "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // packages/abilities/src/index.ts
21
- var index_exports = {};
22
- __export(index_exports, {
23
- executeAbility: () => import_api.executeAbility,
24
- getAbilities: () => import_api.getAbilities,
25
- getAbility: () => import_api.getAbility,
26
- getAbilityCategories: () => import_api.getAbilityCategories,
27
- getAbilityCategory: () => import_api.getAbilityCategory,
28
- registerAbility: () => import_api.registerAbility,
29
- registerAbilityCategory: () => import_api.registerAbilityCategory,
30
- store: () => import_store.store,
31
- unregisterAbility: () => import_api.unregisterAbility,
32
- unregisterAbilityCategory: () => import_api.unregisterAbilityCategory,
33
- validateValueFromSchema: () => import_validation.validateValueFromSchema
34
- });
35
- module.exports = __toCommonJS(index_exports);
36
- var import_api = require("./api.cjs");
37
- var import_store = require("./store/index.cjs");
38
- var import_validation = require("./validation.cjs");
39
- // Annotate the CommonJS export names for ESM import in node:
40
- 0 && (module.exports = {
41
- executeAbility,
42
- getAbilities,
43
- getAbility,
44
- getAbilityCategories,
45
- getAbilityCategory,
46
- registerAbility,
47
- registerAbilityCategory,
48
- store,
49
- unregisterAbility,
50
- unregisterAbilityCategory,
51
- validateValueFromSchema
52
- });
53
- //# sourceMappingURL=index.cjs.map
2
+ Object.defineProperties(exports, {
3
+ __esModule: { value: true },
4
+ [Symbol.toStringTag]: { value: "Module" }
5
+ });
6
+ let __api_ts = require("./api.cjs");
7
+ let __store_index_ts = require("./store/index.cjs");
8
+ let __validation_ts = require("./validation.cjs");
9
+ Object.defineProperty(exports, "executeAbility", {
10
+ enumerable: true,
11
+ get: function() {
12
+ return __api_ts.executeAbility;
13
+ }
14
+ });
15
+ Object.defineProperty(exports, "getAbilities", {
16
+ enumerable: true,
17
+ get: function() {
18
+ return __api_ts.getAbilities;
19
+ }
20
+ });
21
+ Object.defineProperty(exports, "getAbility", {
22
+ enumerable: true,
23
+ get: function() {
24
+ return __api_ts.getAbility;
25
+ }
26
+ });
27
+ Object.defineProperty(exports, "getAbilityCategories", {
28
+ enumerable: true,
29
+ get: function() {
30
+ return __api_ts.getAbilityCategories;
31
+ }
32
+ });
33
+ Object.defineProperty(exports, "getAbilityCategory", {
34
+ enumerable: true,
35
+ get: function() {
36
+ return __api_ts.getAbilityCategory;
37
+ }
38
+ });
39
+ Object.defineProperty(exports, "registerAbility", {
40
+ enumerable: true,
41
+ get: function() {
42
+ return __api_ts.registerAbility;
43
+ }
44
+ });
45
+ Object.defineProperty(exports, "registerAbilityCategory", {
46
+ enumerable: true,
47
+ get: function() {
48
+ return __api_ts.registerAbilityCategory;
49
+ }
50
+ });
51
+ Object.defineProperty(exports, "store", {
52
+ enumerable: true,
53
+ get: function() {
54
+ return __store_index_ts.store;
55
+ }
56
+ });
57
+ Object.defineProperty(exports, "unregisterAbility", {
58
+ enumerable: true,
59
+ get: function() {
60
+ return __api_ts.unregisterAbility;
61
+ }
62
+ });
63
+ Object.defineProperty(exports, "unregisterAbilityCategory", {
64
+ enumerable: true,
65
+ get: function() {
66
+ return __api_ts.unregisterAbilityCategory;
67
+ }
68
+ });
69
+ Object.defineProperty(exports, "validateValueFromSchema", {
70
+ enumerable: true,
71
+ get: function() {
72
+ return __validation_ts.validateValueFromSchema;
73
+ }
74
+ });