-
Notifications
You must be signed in to change notification settings - Fork 26
Add client-side package for Abilities API #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
bddf4e3
to
594458f
Compare
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## trunk #60 +/- ##
============================================
- Coverage 87.45% 84.84% -2.61%
- Complexity 95 102 +7
============================================
Files 8 16 +8
Lines 518 772 +254
Branches 0 85 +85
============================================
+ Hits 453 655 +202
- Misses 65 117 +52
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't like that we're muddying up the global namespace unnecessarily, or that this file is nested inside includes/abilities-api
. Also it's breaks the current repo CS.
IMO, either this should be moved up into includes/*
or the plugin-specific needs should be sanitized out of the WordPress-core needs into a class-assets-init.php
or similar (following the pattern established in class-wp-rest-abilities-init.php
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might leave a similar feedback in another PR. This will stay local, similar to WP_REST_Abilities_Init
. So maybe in both cases, the name shouldn't be prefixed with wp
, but in this PR it's fine to change the newly proposed name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In dce8f8c, I have moved these from the global namespace and created a WP_Abilities_Assets_Init
class.
* Client only abilities * fix: Add proper server ability validation in registerAbility * feat: Add ClientAbility and ServerAbility types for better type safety * refactor: Remove location field, use callback presence for execution type * feat: add permissionCallback on the client * update: check if ability is already registered in registerAbility * fix: wrap client errors in i18n * Add client library tests and code quality infrastructure (#70) * Add testing & linting infa * Remove uncessary combined checks * Add client tests * test: Use proper TypeScript types instead of 'as any' in tests * Fix typescript check for extra server check * test: update tests for client-only abilities API changes * chore: add build:client and dev:client scripts to root package.json * fix: formatting * fix: prettier issues * fix: apply Prettier formatting to README and validation.ts * deps: remove @types/wordpress__api-fetch since types are already shipped * fix: move validation to the store * fix: validate ability name uses same format as server * tests: clean up CI checks * deps: fix dev dependencies and remove extra package-lock.json * fix: rename main dev and build commands * fix: better match server validation rules for schema * Fix remaining validation issues Co-authored-by: emdashcodes <[email protected]> Co-authored-by: gziolo <[email protected]> Co-authored-by: justlevine <[email protected]> Co-authored-by: jonathanbossenger <[email protected]> --------- Co-authored-by: emdashcodes <[email protected]> Co-authored-by: gziolo <[email protected]> Co-authored-by: justlevine <[email protected]> Co-authored-by: jonathanbossenger <[email protected]> Co-authored-by: swissspidy <[email protected]>
448075d
to
f697d20
Compare
I took this branch for a spin and identified a few issues that I tried to fix. First, I rebased the branch with the latest changes from Next, I covered in 17dfa62:
Next, I noticed some issues with the Prettier config when running |
I also tested the following commands and they worked correctly with my latest changes applied:
The only task left for me that I have to perform later is ensuring that everything works correctly in the browser. In the long run, we might want to introduce some form of e2e testing with |
Thanks, I believe I messed up the import somewhere along the way, but via https://github.com/ajv-validator/ajv-draft-04
I also cleaned up the two jest dependencies you noted. |
I followed the instructions from #69 to see how things change when there are also abilities registered on the client. Everything works correctly. ![]() ![]() The only nitpick I have is that I still see I can confirm that it isn't possible to register an ability using the same name twice. It is possible to unregister an ability (even when registered on the server), and register it again on the client. The only remaining low-priority task would be aligning the shapes of abilities between the server-side representation and client-side representation. Namely, obsolete A separate story is to coninue discussing whether some follow-up work would be required after landing this PR to optimize the abilities data store's design as discussed in #60 (comment). |
This was coming from my example abilities and not the code itself, but I agree invalid fields should be stripped alongside things like _links from the server response. I added some code for this and tests in |
Closes #41
This PR introduces a client library that provides access to abilities registered on the server, making it easy to discover and execute WordPress abilities from JavaScript code.
getAbilities()
- Fetches all available abilitiesgetAbility(name)
- Retrieves a specific ability by nameexecuteAbility(name, input)
- Executes abilitiesIn addition to the client API, this also includes automatic script registration when used as a plugin and a helper function for Composer-based installations. The API is exposed in the
wp.abilities
namespace when enqueued.It consolidates other aspects of client-side registry work for Abilities API:
Usage Example
Testing
I recommend registering at least 1 tool and 1 resource, here are 2 dummy ones that I will use in the examples below: https://gist.github.com/emdashcodes/a2f2d2fe440d52d9736fac4ab535cef8
Open the developer console and paste the following:
await wp.abilities.getAbilities()
You should see both abilities (and any others you registered) listed.
Paste in the following:
You should see a list of pages returned.
Paste in:
See that info for the
demo/get-pages
ability is called.Finally, paste in the following:
See that a demo draft post has been created.