@wordpress/abilities 0.2.1-next.8b30e05b0.0 → 0.3.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/CHANGELOG.md +2 -0
- package/README.md +32 -40
- package/build/api.js +19 -66
- package/build/api.js.map +3 -3
- package/build/index.js.map +2 -2
- package/build/store/actions.js +41 -31
- package/build/store/actions.js.map +2 -2
- package/build/store/constants.js +6 -15
- package/build/store/constants.js.map +2 -2
- package/build/store/index.js +1 -27
- package/build/store/index.js.map +3 -3
- package/build/store/reducer.js +2 -22
- package/build/store/reducer.js.map +2 -2
- package/build/store/selectors.js +1 -1
- package/build/store/selectors.js.map +2 -2
- package/build/store/types.js +19 -0
- package/build/store/types.js.map +7 -0
- package/build/types.js.map +1 -1
- package/build-module/api.js +20 -57
- package/build-module/api.js.map +2 -2
- package/build-module/index.js.map +2 -2
- package/build-module/store/actions.js +43 -32
- package/build-module/store/actions.js.map +2 -2
- package/build-module/store/constants.js +4 -10
- package/build-module/store/constants.js.map +2 -2
- package/build-module/store/index.js +3 -34
- package/build-module/store/index.js.map +2 -2
- package/build-module/store/reducer.js +2 -24
- package/build-module/store/reducer.js.map +2 -2
- package/build-module/store/selectors.js +1 -1
- package/build-module/store/selectors.js.map +2 -2
- package/build-module/store/types.js +1 -0
- package/build-module/store/types.js.map +7 -0
- package/build-types/api.d.ts +16 -23
- package/build-types/api.d.ts.map +1 -1
- package/build-types/index.d.ts +1 -1
- package/build-types/index.d.ts.map +1 -1
- package/build-types/store/actions.d.ts +5 -27
- package/build-types/store/actions.d.ts.map +1 -1
- package/build-types/store/constants.d.ts +2 -5
- package/build-types/store/constants.d.ts.map +1 -1
- package/build-types/store/index.d.ts.map +1 -1
- package/build-types/store/reducer.d.ts +21 -11
- package/build-types/store/reducer.d.ts.map +1 -1
- package/build-types/store/selectors.d.ts +2 -1
- package/build-types/store/selectors.d.ts.map +1 -1
- package/build-types/store/types.d.ts +18 -0
- package/build-types/store/types.d.ts.map +1 -0
- package/build-types/types.d.ts +14 -24
- package/build-types/types.d.ts.map +1 -1
- package/package.json +4 -7
- package/src/api.ts +43 -123
- package/src/index.ts +0 -1
- package/src/store/actions.ts +61 -46
- package/src/store/constants.ts +4 -5
- package/src/store/index.ts +2 -34
- package/src/store/reducer.ts +27 -33
- package/src/store/selectors.ts +3 -7
- package/src/store/tests/actions.test.ts +100 -244
- package/src/store/tests/reducer.test.ts +0 -451
- package/src/store/tests/selectors.test.ts +1 -1
- package/src/store/types.ts +19 -0
- package/src/tests/api.test.ts +112 -158
- package/src/types.ts +14 -26
- package/build/store/resolvers.js +0 -107
- package/build/store/resolvers.js.map +0 -7
- package/build-module/store/resolvers.js +0 -84
- package/build-module/store/resolvers.js.map +0 -7
- package/build-types/store/resolvers.d.ts +0 -47
- package/build-types/store/resolvers.d.ts.map +0 -1
- package/src/store/resolvers.ts +0 -151
- package/src/store/tests/resolvers.test.ts +0 -642
package/build/store/resolvers.js
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
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/store/resolvers.ts
|
|
21
|
-
var resolvers_exports = {};
|
|
22
|
-
__export(resolvers_exports, {
|
|
23
|
-
getAbilities: () => getAbilities,
|
|
24
|
-
getAbility: () => getAbility,
|
|
25
|
-
getAbilityCategories: () => getAbilityCategories,
|
|
26
|
-
getAbilityCategory: () => getAbilityCategory
|
|
27
|
-
});
|
|
28
|
-
module.exports = __toCommonJS(resolvers_exports);
|
|
29
|
-
var import_core_data = require("@wordpress/core-data");
|
|
30
|
-
var import_constants = require("./constants");
|
|
31
|
-
var import_actions = require("./actions");
|
|
32
|
-
function getAbilities() {
|
|
33
|
-
return async ({ dispatch, registry, select }) => {
|
|
34
|
-
const existingAbilities = select.getAbilities();
|
|
35
|
-
const hasServerAbilities = existingAbilities.some(
|
|
36
|
-
(ability) => !ability.callback
|
|
37
|
-
);
|
|
38
|
-
if (hasServerAbilities) {
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
const abilities = await registry.resolveSelect(import_core_data.store).getEntityRecords(import_constants.ENTITY_KIND, import_constants.ENTITY_NAME, {
|
|
42
|
-
per_page: -1
|
|
43
|
-
});
|
|
44
|
-
dispatch((0, import_actions.receiveAbilities)(abilities || []));
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
function getAbility(name) {
|
|
48
|
-
return async ({ dispatch, registry, select }) => {
|
|
49
|
-
const existingAbility = select.getAbility(name);
|
|
50
|
-
if (existingAbility) {
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
try {
|
|
54
|
-
const ability = await registry.resolveSelect(import_core_data.store).getEntityRecord(import_constants.ENTITY_KIND, import_constants.ENTITY_NAME, name);
|
|
55
|
-
if (ability) {
|
|
56
|
-
await dispatch((0, import_actions.registerAbility)(ability));
|
|
57
|
-
}
|
|
58
|
-
} catch (error) {
|
|
59
|
-
console.debug(`Ability not found: ${name}`);
|
|
60
|
-
}
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
function getAbilityCategories() {
|
|
64
|
-
return async ({ dispatch, registry, select }) => {
|
|
65
|
-
const existingCategories = select.getAbilityCategories();
|
|
66
|
-
const hasServerCategories = existingCategories.some(
|
|
67
|
-
(category) => !category.meta?._clientRegistered
|
|
68
|
-
);
|
|
69
|
-
if (hasServerCategories) {
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
const categories = await registry.resolveSelect(import_core_data.store).getEntityRecords(import_constants.ENTITY_KIND, import_constants.ENTITY_NAME_CATEGORIES, {
|
|
73
|
-
per_page: -1
|
|
74
|
-
});
|
|
75
|
-
dispatch((0, import_actions.receiveCategories)(categories || []));
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
function getAbilityCategory(slug) {
|
|
79
|
-
return async ({ dispatch, registry, select }) => {
|
|
80
|
-
const existingCategory = select.getAbilityCategory(slug);
|
|
81
|
-
if (existingCategory) {
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
try {
|
|
85
|
-
const category = await registry.resolveSelect(import_core_data.store).getEntityRecord(import_constants.ENTITY_KIND, import_constants.ENTITY_NAME_CATEGORIES, slug);
|
|
86
|
-
if (category) {
|
|
87
|
-
await dispatch(
|
|
88
|
-
(0, import_actions.registerAbilityCategory)(category.slug, {
|
|
89
|
-
label: category.label,
|
|
90
|
-
description: category.description,
|
|
91
|
-
meta: category.meta
|
|
92
|
-
})
|
|
93
|
-
);
|
|
94
|
-
}
|
|
95
|
-
} catch (error) {
|
|
96
|
-
console.debug(`Category not found: ${slug}`);
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
101
|
-
0 && (module.exports = {
|
|
102
|
-
getAbilities,
|
|
103
|
-
getAbility,
|
|
104
|
-
getAbilityCategories,
|
|
105
|
-
getAbilityCategory
|
|
106
|
-
});
|
|
107
|
-
//# sourceMappingURL=resolvers.js.map
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../src/store/resolvers.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { store as coreStore } from '@wordpress/core-data';\n\n/**\n * Internal dependencies\n */\nimport type { Ability, AbilityCategory } from '../types';\nimport { ENTITY_KIND, ENTITY_NAME, ENTITY_NAME_CATEGORIES } from './constants';\nimport {\n\treceiveAbilities,\n\treceiveCategories,\n\tregisterAbility,\n\tregisterAbilityCategory,\n} from './actions';\n\n/**\n * Resolver for getAbilities selector.\n * Fetches all abilities from the server.\n *\n * The resolver only fetches once (without query args filter) and stores all abilities.\n * Query args filtering handled client-side by the selector for better performance\n * and to avoid multiple API requests when filtering by different categories.\n */\nexport function getAbilities() {\n\t// @ts-expect-error - registry types are not yet available\n\treturn async ( { dispatch, registry, select } ) => {\n\t\tconst existingAbilities = select.getAbilities();\n\n\t\t// Check if we have any server-side abilities (abilities without callbacks)\n\t\t// Client abilities have callbacks and are registered immediately on page load\n\t\t// We only want to skip fetching if we've already fetched server abilities\n\t\tconst hasServerAbilities = existingAbilities.some(\n\t\t\t( ability: Ability ) => ! ability.callback\n\t\t);\n\n\t\tif ( hasServerAbilities ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst abilities = await registry\n\t\t\t.resolveSelect( coreStore )\n\t\t\t.getEntityRecords( ENTITY_KIND, ENTITY_NAME, {\n\t\t\t\tper_page: -1,\n\t\t\t} );\n\n\t\tdispatch( receiveAbilities( abilities || [] ) );\n\t};\n}\n\n/**\n * Resolver for getAbility selector.\n * Fetches a specific ability from the server if not already in store.\n *\n * @param name Ability name.\n */\nexport function getAbility( name: string ) {\n\t// @ts-expect-error - registry types are not yet available\n\treturn async ( { dispatch, registry, select } ) => {\n\t\t// Check if ability already exists in store (i.e. client ability or already fetched)\n\t\tconst existingAbility = select.getAbility( name );\n\t\tif ( existingAbility ) {\n\t\t\treturn;\n\t\t}\n\n\t\ttry {\n\t\t\tconst ability = await registry\n\t\t\t\t.resolveSelect( coreStore )\n\t\t\t\t.getEntityRecord( ENTITY_KIND, ENTITY_NAME, name );\n\n\t\t\tif ( ability ) {\n\t\t\t\tawait dispatch( registerAbility( ability ) );\n\t\t\t}\n\t\t} catch ( error ) {\n\t\t\t// If ability doesn't exist or error, we'll return undefined from the selector\n\t\t\t// eslint-disable-next-line no-console\n\t\t\tconsole.debug( `Ability not found: ${ name }` );\n\t\t}\n\t};\n}\n\n/**\n * Resolver for getAbilityCategories selector.\n * Fetches all categories from the server.\n *\n * The resolver only fetches once and stores all categories.\n */\nexport function getAbilityCategories() {\n\t// @ts-expect-error - registry types are not yet available\n\treturn async ( { dispatch, registry, select } ) => {\n\t\tconst existingCategories = select.getAbilityCategories();\n\n\t\t// Check if we have any server-side categories (categories without meta._clientRegistered flag)\n\t\t// Client categories have meta._clientRegistered=true and might be registered immediately\n\t\t// We only want to skip fetching if we've already fetched server categories\n\t\tconst hasServerCategories = existingCategories.some(\n\t\t\t( category: AbilityCategory ) => ! category.meta?._clientRegistered\n\t\t);\n\n\t\tif ( hasServerCategories ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst categories = await registry\n\t\t\t.resolveSelect( coreStore )\n\t\t\t.getEntityRecords( ENTITY_KIND, ENTITY_NAME_CATEGORIES, {\n\t\t\t\tper_page: -1,\n\t\t\t} );\n\n\t\tdispatch( receiveCategories( categories || [] ) );\n\t};\n}\n\n/**\n * Resolver for getAbilityCategory selector.\n * Fetches a specific category from the server if not already in store.\n *\n * @param slug Category slug.\n */\nexport function getAbilityCategory( slug: string ) {\n\t// @ts-expect-error - registry types are not yet available\n\treturn async ( { dispatch, registry, select } ) => {\n\t\t// Check if category already exists in store (either client-registered or server-fetched).\n\t\t// This prevents unnecessary network requests while allowing client-side categories\n\t\t// to be retrieved immediately without hitting the API.\n\t\tconst existingCategory = select.getAbilityCategory( slug );\n\t\tif ( existingCategory ) {\n\t\t\treturn;\n\t\t}\n\n\t\ttry {\n\t\t\tconst category = await registry\n\t\t\t\t.resolveSelect( coreStore )\n\t\t\t\t.getEntityRecord( ENTITY_KIND, ENTITY_NAME_CATEGORIES, slug );\n\n\t\t\tif ( category ) {\n\t\t\t\tawait dispatch(\n\t\t\t\t\tregisterAbilityCategory( category.slug, {\n\t\t\t\t\t\tlabel: category.label,\n\t\t\t\t\t\tdescription: category.description,\n\t\t\t\t\t\tmeta: category.meta,\n\t\t\t\t\t} )\n\t\t\t\t);\n\t\t\t}\n\t\t} catch ( error ) {\n\t\t\t// eslint-disable-next-line no-console\n\t\t\tconsole.debug( `Category not found: ${ slug }` );\n\t\t}\n\t};\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,uBAAmC;AAMnC,uBAAiE;AACjE,qBAKO;AAUA,SAAS,eAAe;AAE9B,SAAO,OAAQ,EAAE,UAAU,UAAU,OAAO,MAAO;AAClD,UAAM,oBAAoB,OAAO,aAAa;AAK9C,UAAM,qBAAqB,kBAAkB;AAAA,MAC5C,CAAE,YAAsB,CAAE,QAAQ;AAAA,IACnC;AAEA,QAAK,oBAAqB;AACzB;AAAA,IACD;AAEA,UAAM,YAAY,MAAM,SACtB,cAAe,iBAAAA,KAAU,EACzB,iBAAkB,8BAAa,8BAAa;AAAA,MAC5C,UAAU;AAAA,IACX,CAAE;AAEH,iBAAU,iCAAkB,aAAa,CAAC,CAAE,CAAE;AAAA,EAC/C;AACD;AAQO,SAAS,WAAY,MAAe;AAE1C,SAAO,OAAQ,EAAE,UAAU,UAAU,OAAO,MAAO;AAElD,UAAM,kBAAkB,OAAO,WAAY,IAAK;AAChD,QAAK,iBAAkB;AACtB;AAAA,IACD;AAEA,QAAI;AACH,YAAM,UAAU,MAAM,SACpB,cAAe,iBAAAA,KAAU,EACzB,gBAAiB,8BAAa,8BAAa,IAAK;AAElD,UAAK,SAAU;AACd,cAAM,aAAU,gCAAiB,OAAQ,CAAE;AAAA,MAC5C;AAAA,IACD,SAAU,OAAQ;AAGjB,cAAQ,MAAO,sBAAuB,IAAK,EAAG;AAAA,IAC/C;AAAA,EACD;AACD;AAQO,SAAS,uBAAuB;AAEtC,SAAO,OAAQ,EAAE,UAAU,UAAU,OAAO,MAAO;AAClD,UAAM,qBAAqB,OAAO,qBAAqB;AAKvD,UAAM,sBAAsB,mBAAmB;AAAA,MAC9C,CAAE,aAA+B,CAAE,SAAS,MAAM;AAAA,IACnD;AAEA,QAAK,qBAAsB;AAC1B;AAAA,IACD;AAEA,UAAM,aAAa,MAAM,SACvB,cAAe,iBAAAA,KAAU,EACzB,iBAAkB,8BAAa,yCAAwB;AAAA,MACvD,UAAU;AAAA,IACX,CAAE;AAEH,iBAAU,kCAAmB,cAAc,CAAC,CAAE,CAAE;AAAA,EACjD;AACD;AAQO,SAAS,mBAAoB,MAAe;AAElD,SAAO,OAAQ,EAAE,UAAU,UAAU,OAAO,MAAO;AAIlD,UAAM,mBAAmB,OAAO,mBAAoB,IAAK;AACzD,QAAK,kBAAmB;AACvB;AAAA,IACD;AAEA,QAAI;AACH,YAAM,WAAW,MAAM,SACrB,cAAe,iBAAAA,KAAU,EACzB,gBAAiB,8BAAa,yCAAwB,IAAK;AAE7D,UAAK,UAAW;AACf,cAAM;AAAA,cACL,wCAAyB,SAAS,MAAM;AAAA,YACvC,OAAO,SAAS;AAAA,YAChB,aAAa,SAAS;AAAA,YACtB,MAAM,SAAS;AAAA,UAChB,CAAE;AAAA,QACH;AAAA,MACD;AAAA,IACD,SAAU,OAAQ;AAEjB,cAAQ,MAAO,uBAAwB,IAAK,EAAG;AAAA,IAChD;AAAA,EACD;AACD;",
|
|
6
|
-
"names": ["coreStore"]
|
|
7
|
-
}
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
// packages/abilities/src/store/resolvers.ts
|
|
2
|
-
import { store as coreStore } from "@wordpress/core-data";
|
|
3
|
-
import { ENTITY_KIND, ENTITY_NAME, ENTITY_NAME_CATEGORIES } from "./constants";
|
|
4
|
-
import {
|
|
5
|
-
receiveAbilities,
|
|
6
|
-
receiveCategories,
|
|
7
|
-
registerAbility,
|
|
8
|
-
registerAbilityCategory
|
|
9
|
-
} from "./actions";
|
|
10
|
-
function getAbilities() {
|
|
11
|
-
return async ({ dispatch, registry, select }) => {
|
|
12
|
-
const existingAbilities = select.getAbilities();
|
|
13
|
-
const hasServerAbilities = existingAbilities.some(
|
|
14
|
-
(ability) => !ability.callback
|
|
15
|
-
);
|
|
16
|
-
if (hasServerAbilities) {
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
const abilities = await registry.resolveSelect(coreStore).getEntityRecords(ENTITY_KIND, ENTITY_NAME, {
|
|
20
|
-
per_page: -1
|
|
21
|
-
});
|
|
22
|
-
dispatch(receiveAbilities(abilities || []));
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
function getAbility(name) {
|
|
26
|
-
return async ({ dispatch, registry, select }) => {
|
|
27
|
-
const existingAbility = select.getAbility(name);
|
|
28
|
-
if (existingAbility) {
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
try {
|
|
32
|
-
const ability = await registry.resolveSelect(coreStore).getEntityRecord(ENTITY_KIND, ENTITY_NAME, name);
|
|
33
|
-
if (ability) {
|
|
34
|
-
await dispatch(registerAbility(ability));
|
|
35
|
-
}
|
|
36
|
-
} catch (error) {
|
|
37
|
-
console.debug(`Ability not found: ${name}`);
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
function getAbilityCategories() {
|
|
42
|
-
return async ({ dispatch, registry, select }) => {
|
|
43
|
-
const existingCategories = select.getAbilityCategories();
|
|
44
|
-
const hasServerCategories = existingCategories.some(
|
|
45
|
-
(category) => !category.meta?._clientRegistered
|
|
46
|
-
);
|
|
47
|
-
if (hasServerCategories) {
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
const categories = await registry.resolveSelect(coreStore).getEntityRecords(ENTITY_KIND, ENTITY_NAME_CATEGORIES, {
|
|
51
|
-
per_page: -1
|
|
52
|
-
});
|
|
53
|
-
dispatch(receiveCategories(categories || []));
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
function getAbilityCategory(slug) {
|
|
57
|
-
return async ({ dispatch, registry, select }) => {
|
|
58
|
-
const existingCategory = select.getAbilityCategory(slug);
|
|
59
|
-
if (existingCategory) {
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
try {
|
|
63
|
-
const category = await registry.resolveSelect(coreStore).getEntityRecord(ENTITY_KIND, ENTITY_NAME_CATEGORIES, slug);
|
|
64
|
-
if (category) {
|
|
65
|
-
await dispatch(
|
|
66
|
-
registerAbilityCategory(category.slug, {
|
|
67
|
-
label: category.label,
|
|
68
|
-
description: category.description,
|
|
69
|
-
meta: category.meta
|
|
70
|
-
})
|
|
71
|
-
);
|
|
72
|
-
}
|
|
73
|
-
} catch (error) {
|
|
74
|
-
console.debug(`Category not found: ${slug}`);
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
export {
|
|
79
|
-
getAbilities,
|
|
80
|
-
getAbility,
|
|
81
|
-
getAbilityCategories,
|
|
82
|
-
getAbilityCategory
|
|
83
|
-
};
|
|
84
|
-
//# sourceMappingURL=resolvers.js.map
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../src/store/resolvers.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { store as coreStore } from '@wordpress/core-data';\n\n/**\n * Internal dependencies\n */\nimport type { Ability, AbilityCategory } from '../types';\nimport { ENTITY_KIND, ENTITY_NAME, ENTITY_NAME_CATEGORIES } from './constants';\nimport {\n\treceiveAbilities,\n\treceiveCategories,\n\tregisterAbility,\n\tregisterAbilityCategory,\n} from './actions';\n\n/**\n * Resolver for getAbilities selector.\n * Fetches all abilities from the server.\n *\n * The resolver only fetches once (without query args filter) and stores all abilities.\n * Query args filtering handled client-side by the selector for better performance\n * and to avoid multiple API requests when filtering by different categories.\n */\nexport function getAbilities() {\n\t// @ts-expect-error - registry types are not yet available\n\treturn async ( { dispatch, registry, select } ) => {\n\t\tconst existingAbilities = select.getAbilities();\n\n\t\t// Check if we have any server-side abilities (abilities without callbacks)\n\t\t// Client abilities have callbacks and are registered immediately on page load\n\t\t// We only want to skip fetching if we've already fetched server abilities\n\t\tconst hasServerAbilities = existingAbilities.some(\n\t\t\t( ability: Ability ) => ! ability.callback\n\t\t);\n\n\t\tif ( hasServerAbilities ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst abilities = await registry\n\t\t\t.resolveSelect( coreStore )\n\t\t\t.getEntityRecords( ENTITY_KIND, ENTITY_NAME, {\n\t\t\t\tper_page: -1,\n\t\t\t} );\n\n\t\tdispatch( receiveAbilities( abilities || [] ) );\n\t};\n}\n\n/**\n * Resolver for getAbility selector.\n * Fetches a specific ability from the server if not already in store.\n *\n * @param name Ability name.\n */\nexport function getAbility( name: string ) {\n\t// @ts-expect-error - registry types are not yet available\n\treturn async ( { dispatch, registry, select } ) => {\n\t\t// Check if ability already exists in store (i.e. client ability or already fetched)\n\t\tconst existingAbility = select.getAbility( name );\n\t\tif ( existingAbility ) {\n\t\t\treturn;\n\t\t}\n\n\t\ttry {\n\t\t\tconst ability = await registry\n\t\t\t\t.resolveSelect( coreStore )\n\t\t\t\t.getEntityRecord( ENTITY_KIND, ENTITY_NAME, name );\n\n\t\t\tif ( ability ) {\n\t\t\t\tawait dispatch( registerAbility( ability ) );\n\t\t\t}\n\t\t} catch ( error ) {\n\t\t\t// If ability doesn't exist or error, we'll return undefined from the selector\n\t\t\t// eslint-disable-next-line no-console\n\t\t\tconsole.debug( `Ability not found: ${ name }` );\n\t\t}\n\t};\n}\n\n/**\n * Resolver for getAbilityCategories selector.\n * Fetches all categories from the server.\n *\n * The resolver only fetches once and stores all categories.\n */\nexport function getAbilityCategories() {\n\t// @ts-expect-error - registry types are not yet available\n\treturn async ( { dispatch, registry, select } ) => {\n\t\tconst existingCategories = select.getAbilityCategories();\n\n\t\t// Check if we have any server-side categories (categories without meta._clientRegistered flag)\n\t\t// Client categories have meta._clientRegistered=true and might be registered immediately\n\t\t// We only want to skip fetching if we've already fetched server categories\n\t\tconst hasServerCategories = existingCategories.some(\n\t\t\t( category: AbilityCategory ) => ! category.meta?._clientRegistered\n\t\t);\n\n\t\tif ( hasServerCategories ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst categories = await registry\n\t\t\t.resolveSelect( coreStore )\n\t\t\t.getEntityRecords( ENTITY_KIND, ENTITY_NAME_CATEGORIES, {\n\t\t\t\tper_page: -1,\n\t\t\t} );\n\n\t\tdispatch( receiveCategories( categories || [] ) );\n\t};\n}\n\n/**\n * Resolver for getAbilityCategory selector.\n * Fetches a specific category from the server if not already in store.\n *\n * @param slug Category slug.\n */\nexport function getAbilityCategory( slug: string ) {\n\t// @ts-expect-error - registry types are not yet available\n\treturn async ( { dispatch, registry, select } ) => {\n\t\t// Check if category already exists in store (either client-registered or server-fetched).\n\t\t// This prevents unnecessary network requests while allowing client-side categories\n\t\t// to be retrieved immediately without hitting the API.\n\t\tconst existingCategory = select.getAbilityCategory( slug );\n\t\tif ( existingCategory ) {\n\t\t\treturn;\n\t\t}\n\n\t\ttry {\n\t\t\tconst category = await registry\n\t\t\t\t.resolveSelect( coreStore )\n\t\t\t\t.getEntityRecord( ENTITY_KIND, ENTITY_NAME_CATEGORIES, slug );\n\n\t\t\tif ( category ) {\n\t\t\t\tawait dispatch(\n\t\t\t\t\tregisterAbilityCategory( category.slug, {\n\t\t\t\t\t\tlabel: category.label,\n\t\t\t\t\t\tdescription: category.description,\n\t\t\t\t\t\tmeta: category.meta,\n\t\t\t\t\t} )\n\t\t\t\t);\n\t\t\t}\n\t\t} catch ( error ) {\n\t\t\t// eslint-disable-next-line no-console\n\t\t\tconsole.debug( `Category not found: ${ slug }` );\n\t\t}\n\t};\n}\n"],
|
|
5
|
-
"mappings": ";AAGA,SAAS,SAAS,iBAAiB;AAMnC,SAAS,aAAa,aAAa,8BAA8B;AACjE;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACM;AAUA,SAAS,eAAe;AAE9B,SAAO,OAAQ,EAAE,UAAU,UAAU,OAAO,MAAO;AAClD,UAAM,oBAAoB,OAAO,aAAa;AAK9C,UAAM,qBAAqB,kBAAkB;AAAA,MAC5C,CAAE,YAAsB,CAAE,QAAQ;AAAA,IACnC;AAEA,QAAK,oBAAqB;AACzB;AAAA,IACD;AAEA,UAAM,YAAY,MAAM,SACtB,cAAe,SAAU,EACzB,iBAAkB,aAAa,aAAa;AAAA,MAC5C,UAAU;AAAA,IACX,CAAE;AAEH,aAAU,iBAAkB,aAAa,CAAC,CAAE,CAAE;AAAA,EAC/C;AACD;AAQO,SAAS,WAAY,MAAe;AAE1C,SAAO,OAAQ,EAAE,UAAU,UAAU,OAAO,MAAO;AAElD,UAAM,kBAAkB,OAAO,WAAY,IAAK;AAChD,QAAK,iBAAkB;AACtB;AAAA,IACD;AAEA,QAAI;AACH,YAAM,UAAU,MAAM,SACpB,cAAe,SAAU,EACzB,gBAAiB,aAAa,aAAa,IAAK;AAElD,UAAK,SAAU;AACd,cAAM,SAAU,gBAAiB,OAAQ,CAAE;AAAA,MAC5C;AAAA,IACD,SAAU,OAAQ;AAGjB,cAAQ,MAAO,sBAAuB,IAAK,EAAG;AAAA,IAC/C;AAAA,EACD;AACD;AAQO,SAAS,uBAAuB;AAEtC,SAAO,OAAQ,EAAE,UAAU,UAAU,OAAO,MAAO;AAClD,UAAM,qBAAqB,OAAO,qBAAqB;AAKvD,UAAM,sBAAsB,mBAAmB;AAAA,MAC9C,CAAE,aAA+B,CAAE,SAAS,MAAM;AAAA,IACnD;AAEA,QAAK,qBAAsB;AAC1B;AAAA,IACD;AAEA,UAAM,aAAa,MAAM,SACvB,cAAe,SAAU,EACzB,iBAAkB,aAAa,wBAAwB;AAAA,MACvD,UAAU;AAAA,IACX,CAAE;AAEH,aAAU,kBAAmB,cAAc,CAAC,CAAE,CAAE;AAAA,EACjD;AACD;AAQO,SAAS,mBAAoB,MAAe;AAElD,SAAO,OAAQ,EAAE,UAAU,UAAU,OAAO,MAAO;AAIlD,UAAM,mBAAmB,OAAO,mBAAoB,IAAK;AACzD,QAAK,kBAAmB;AACvB;AAAA,IACD;AAEA,QAAI;AACH,YAAM,WAAW,MAAM,SACrB,cAAe,SAAU,EACzB,gBAAiB,aAAa,wBAAwB,IAAK;AAE7D,UAAK,UAAW;AACf,cAAM;AAAA,UACL,wBAAyB,SAAS,MAAM;AAAA,YACvC,OAAO,SAAS;AAAA,YAChB,aAAa,SAAS;AAAA,YACtB,MAAM,SAAS;AAAA,UAChB,CAAE;AAAA,QACH;AAAA,MACD;AAAA,IACD,SAAU,OAAQ;AAEjB,cAAQ,MAAO,uBAAwB,IAAK,EAAG;AAAA,IAChD;AAAA,EACD;AACD;",
|
|
6
|
-
"names": []
|
|
7
|
-
}
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Resolver for getAbilities selector.
|
|
3
|
-
* Fetches all abilities from the server.
|
|
4
|
-
*
|
|
5
|
-
* The resolver only fetches once (without query args filter) and stores all abilities.
|
|
6
|
-
* Query args filtering handled client-side by the selector for better performance
|
|
7
|
-
* and to avoid multiple API requests when filtering by different categories.
|
|
8
|
-
*/
|
|
9
|
-
export declare function getAbilities(): ({ dispatch, registry, select }: {
|
|
10
|
-
dispatch: any;
|
|
11
|
-
registry: any;
|
|
12
|
-
select: any;
|
|
13
|
-
}) => Promise<void>;
|
|
14
|
-
/**
|
|
15
|
-
* Resolver for getAbility selector.
|
|
16
|
-
* Fetches a specific ability from the server if not already in store.
|
|
17
|
-
*
|
|
18
|
-
* @param name Ability name.
|
|
19
|
-
*/
|
|
20
|
-
export declare function getAbility(name: string): ({ dispatch, registry, select }: {
|
|
21
|
-
dispatch: any;
|
|
22
|
-
registry: any;
|
|
23
|
-
select: any;
|
|
24
|
-
}) => Promise<void>;
|
|
25
|
-
/**
|
|
26
|
-
* Resolver for getAbilityCategories selector.
|
|
27
|
-
* Fetches all categories from the server.
|
|
28
|
-
*
|
|
29
|
-
* The resolver only fetches once and stores all categories.
|
|
30
|
-
*/
|
|
31
|
-
export declare function getAbilityCategories(): ({ dispatch, registry, select }: {
|
|
32
|
-
dispatch: any;
|
|
33
|
-
registry: any;
|
|
34
|
-
select: any;
|
|
35
|
-
}) => Promise<void>;
|
|
36
|
-
/**
|
|
37
|
-
* Resolver for getAbilityCategory selector.
|
|
38
|
-
* Fetches a specific category from the server if not already in store.
|
|
39
|
-
*
|
|
40
|
-
* @param slug Category slug.
|
|
41
|
-
*/
|
|
42
|
-
export declare function getAbilityCategory(slug: string): ({ dispatch, registry, select }: {
|
|
43
|
-
dispatch: any;
|
|
44
|
-
registry: any;
|
|
45
|
-
select: any;
|
|
46
|
-
}) => Promise<void>;
|
|
47
|
-
//# sourceMappingURL=resolvers.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"resolvers.d.ts","sourceRoot":"","sources":["../../src/store/resolvers.ts"],"names":[],"mappings":"AAiBA;;;;;;;GAOG;AACH,wBAAgB,YAAY,KAEZ;;;;CAA8B,mBAsB7C;AAED;;;;;GAKG;AACH,wBAAgB,UAAU,CAAE,IAAI,EAAE,MAAM,IAExB;;;;CAA8B,mBAqB7C;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,KAEpB;;;;CAA8B,mBAsB7C;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAE,IAAI,EAAE,MAAM,IAEhC;;;;CAA8B,mBA4B7C"}
|
package/src/store/resolvers.ts
DELETED
|
@@ -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
|
-
}
|