Skip to content

Commit 818224d

Browse files
Updates to API examples
1 parent f45c9d1 commit 818224d

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

docs/7.javascript-client.md

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
# 7. JavaScript/TypeScript Client
22

3-
The JavaScript client provides a simple interface for discovering and executing WordPress Abilities from frontend applications, block editor plugins, and external systems.
3+
The JavaScript client provides an interface for discovering and executing WordPress Abilities from the browser.
44

55
## Overview
66

7-
The JavaScript client enables frontend code to interact with the PHP Abilities API system. It can:
7+
The JavaScript client enables frontend code to interact with the Abilities API system. It can:
88

99
- Discover all registered abilities on your WordPress site
10-
- Execute server-side PHP abilities via REST API calls
10+
- Execute server-side PHP abilities
1111
- Register and execute client-side JavaScript abilities
12-
- Handle input validation and permission checking
12+
13+
You can read more about installation and setup in the [package readme](../packages/client/README.md).
1314

1415
## Core API Functions
1516

@@ -31,10 +32,6 @@ console.log(`Found ${abilities.length} abilities`);
3132
abilities.forEach(ability => {
3233
console.log(`${ability.name}: ${ability.description}`);
3334
});
34-
35-
// Filter by type
36-
const tools = abilities.filter(ability => ability.meta?.type === 'tool');
37-
const resources = abilities.filter(ability => ability.meta?.type === 'resource');
3835
```
3936

4037
### getAbility(name)
@@ -80,13 +77,6 @@ const posts = await executeAbility('my-plugin/get-posts', {
8077
limit: 5
8178
});
8279
posts.forEach(post => console.log(post.title));
83-
84-
// Execute with complex input
85-
const result = await executeAbility('my-plugin/update-option', {
86-
option_name: 'blogname',
87-
option_value: 'My New Site Title'
88-
});
89-
console.log('Success:', result.success);
9080
```
9181

9282
### registerAbility(ability)
@@ -101,7 +91,13 @@ Registers a client-side ability that runs in the browser.
10191
**Example:**
10292

10393
```javascript
104-
// Register a simple notification ability
94+
// showNotification function
95+
const showNotification = (message) => {
96+
new Notification(message);
97+
return { success: true, displayed: message };
98+
}
99+
100+
// Register a notification ability which calls the showNotification function
105101
registerAbility({
106102
name: 'my-plugin/show-notification',
107103
label: 'Show Notification',
@@ -115,11 +111,19 @@ registerAbility({
115111
required: ['message']
116112
},
117113
callback: async ({ message, type = 'info' }) => {
118-
// Show browser notification
119-
if (Notification.permission === 'granted') {
120-
new Notification(message);
121-
}
122-
return { success: true, displayed: message };
114+
// Show browser notification
115+
if (!("Notification" in window)) {
116+
alert("This browser does not support desktop notification");
117+
return { success: false, error: 'Browser does not support notifications' };
118+
}
119+
if (Notification.permission !== 'granted') {
120+
Notification.requestPermission().then((permission) => {
121+
if (permission === "granted") {
122+
return showNotification(message);
123+
}
124+
});
125+
}
126+
return showNotification(message);
123127
},
124128
permissionCallback: () => {
125129
return !!wp.data.select('core').getCurrentUser();

0 commit comments

Comments
 (0)