@arkeytyp/valu-api 1.1.0 → 1.1.1

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/README.md CHANGED
@@ -1,62 +1,107 @@
1
- The `valu-api` package enables developers to create custom iframe applications for Valu Social. It provides tools to invoke functions of registered Valu applications and subscribe to their events. With features like API versioning, event handling, and console command testing, developers can seamlessly integrate and extend functionality within the Valu Social ecosystem.
1
+ The `valu-api` package enables developers to build custom iframe applications for Valu Social.
2
+ It provides tools to invoke functions on registered Valu applications, subscribe to their events, and communicate via intents.
3
+ With features like API versioning, event handling, and console command execution, you can seamlessly integrate and extend functionality within the Valu Social ecosystem.
2
4
 
3
- # Installation
5
+ ## Installation
4
6
 
5
7
  ```bash
6
8
  npm install @arkeytyp/valu-api
7
9
  ```
8
10
 
9
- # Usage
11
+ ## Usage
10
12
 
11
- ## Initialize ValuApi
13
+ ### Initialize ValuApi
12
14
 
13
- On your application startup, create an instance of `ValuApi` and subscribe to the `API_READY` event. This event will be triggered only when your application is launched as an iframe within the Valu Verse application.
15
+ On application startup, create an instance of `ValuApi` and subscribe to the `API_READY` event.
16
+ This event is triggered only when your application is launched as an iframe within the Valu Verse application.
14
17
 
15
18
  ```javascript
16
- import { ValuApi } from 'valu-api';
19
+ import { ValuApi } from "@arkeytyp/valu-api";
17
20
 
18
21
  const valuApi = new ValuApi();
19
- valuApi.current.addEventListener(ValuApi.API_READY, async (e) => {
20
- console.log("API IS READY!!!");
22
+ valuApi.addEventListener(ValuApi.API_READY, async (e) => {
23
+ console.log("API IS READY!");
21
24
  });
22
25
  ```
23
26
 
24
- # Running Application Intents
25
27
 
26
- Intents are a powerful way to communicate with other applications inside the Valu Social ecosystem. They allow your application to request actions from other registered Valu apps in a standardized way — for example, opening a chat, joining a meeting, or performing any predefined operation supported by another app.
28
+ ## Running Application Intents
29
+
30
+ Intents are a powerful way to communicate with other applications inside Valu Social.
31
+ They allow your application to request actions from other registered apps in a standardized way — for example, opening a chat, joining a meeting, or performing any supported operation.
27
32
 
28
33
  Each Intent contains:
29
34
 
30
- * **applicationId:** The target application’s ID.
31
- * **action:** The action you want to perform (e.g., `open`, `connect-to-meeting`).
32
- * **params:** Optional parameters for the action (e.g., room IDs, configuration data).
35
+ - **applicationId:** The target application’s ID.
36
+ - **action:** The action to perform (e.g., `open`, `connect-to-meeting`).
37
+ - **params:** Optional parameters for the action (e.g., room IDs, configuration data).
33
38
 
34
- Here’s an example of creating and running an intent to connect to a video meeting:
39
+ ### Example: Open a Video Chat
35
40
 
36
41
  ```javascript
37
- import { Intent } from 'valu-api';
38
-
39
- const intent = new Intent(
40
- 'videochat', // Target application ID
41
- 'connect-to-meeting', // Action to perform
42
- {
43
- roomId: '<room-id>', // Additional parameters
44
- withLocalTracks: true
45
- }
46
- );
42
+ import { Intent } from "@arkeytyp/valu-api";
43
+
44
+ const intent = new Intent('videochat');
45
+ await valuApi.sendIntent(intent);
46
+ ```
47
+
48
+ ### Example: Open a Text Channel for the Current User
49
+
50
+ First, get the current user ID using the `users` API:
51
+
52
+ ```javascript
53
+ import { Intent } from "@arkeytyp/valu-api";
54
+
55
+ const usersApi = await valuApi.getApi('users');
56
+ const currentUser = await usersApi.run('current');
57
+ if (!currentUser) {
58
+ console.error('Something went wrong');
59
+ return;
60
+ }
47
61
 
62
+ const intent = new Intent('textchat', 'open-channel', { userId: currentUser.id });
48
63
  await valuApi.sendIntent(intent);
49
64
  ```
65
+ ## Invoking Services
66
+
67
+ Invoking a Service works almost the same way as running an Application Intent.
68
+ You still use the same `Intent` object with `applicationId`, `action`, and optional `params` — the key difference is **what the `applicationId` points to and how the call affects the UI**.
69
+
70
+ When calling a **Service Intent**:
71
+
72
+ - **`applicationId`** refers to the **service name** (e.g., `ApplicationStorage`), not a visible UI application.
73
+ - Services run entirely **in the background**.
74
+ - Invoking a service **does not change** the currently opened applications.
75
+ - Service Intents are ideal for performing background logic such as:
76
+ - Running searches
77
+ - Fetching or processing data
78
+ - Triggering non-visual workflows
79
+ - Performing system-level operations
80
+
81
+ This makes Services a parallel mechanism to Application Intents, with the difference that they target **non-UI services** instead of interactive apps.
82
+
83
+ ---
50
84
 
51
- # Handling Application Lifecycle
85
+ ### Example: Querying the `ApplicationStorage` Service
86
+
87
+ Below is an example of using an `Intent` to query the `ApplicationStorage` service to search for resources:
88
+
89
+ ```ts
90
+ const intent = new Intent('ApplicationStorage', 'resource-search', {
91
+ size: 10,
92
+ });
93
+
94
+ const result = await valuApi.callService(intent);
95
+ ```
52
96
 
53
- The `valu-api` package provides a way to handle **application lifecycle events** inside your iframe application. By implementing a `ValuApplication` class and registering it with `ValuApi`, you can easily respond to events such as when your app is created, receives a new intent, or is destroyed.
97
+ ## Handling Application Lifecycle
54
98
 
55
- This allows you to **separate application logic from API wiring** and makes it simple to handle incoming intents and return values back to the caller.
99
+ The `valu-api` package lets your iframe app handle **application lifecycle events**.
100
+ By extending `ValuApplication` and registering it with `ValuApi`, you can respond when your app is created, receives a new intent, or is destroyed.
56
101
 
57
- ## 1. Create Your Application Class
102
+ This helps you **separate application logic from API wiring** and makes handling incoming intents straightforward.
58
103
 
59
- Extend the `ValuApplication` class and override its lifecycle methods:
104
+ ### 1. Create Your Application Class
60
105
 
61
106
  ```javascript
62
107
  import { ValuApplication } from '@arkeytyp/valu-api';
@@ -72,15 +117,13 @@ class MyApp extends ValuApplication {
72
117
  return { handled: true, data: { message: 'Processed successfully' } };
73
118
  }
74
119
 
75
- async onDestroy() {
120
+ onDestroy() {
76
121
  console.log('App is shutting down');
77
122
  }
78
123
  }
79
124
  ```
80
125
 
81
- ## 2. Register Your Application with ValuApi
82
-
83
- Set your application instance within `ValuApi`:
126
+ ### 2. Register Your Application with ValuApi
84
127
 
85
128
  ```javascript
86
129
  import { ValuApi } from '@arkeytyp/valu-api';
@@ -89,101 +132,103 @@ const valuApi = new ValuApi();
89
132
  valuApi.setApplication(new MyApp());
90
133
  ```
91
134
 
92
- Once registered, the `ValuApi` will call the appropriate lifecycle methods when events are received from the host (e.g., app launch, new intent, app close).
93
-
94
- ## Lifecycle Methods
95
-
96
- * **`onCreate(intent)`** – Triggered when the application is first launched with an Intent. You can return an optional value to send back to the caller.
97
- * **`onNewIntent(intent)`** – Triggered when a new Intent is sent to the application while it is already running. The return value will be sent back to whoever triggered the Intent.
98
- * **`onDestroy()`** – Triggered when the application is about to be destroyed. Use this to clean up resources.
135
+ **Lifecycle Methods:**
136
+ - `onCreate(intent)` — Triggered when the application is first launched with an intent.
137
+ - `onNewIntent(intent)` — Triggered when a new intent is sent while the application is already running.
138
+ - `onDestroy()` — Triggered when the application is about to be destroyed.
99
139
 
100
- ### Lifecycle Flow
140
+ #### Lifecycle Flow
101
141
 
102
142
  ```
103
- [onCreate] → [onNewIntent] (many times) → [onDestroy]
143
+ [onCreate] → [onNewIntent] (0..N times) → [onDestroy]
104
144
  ```
105
145
 
106
- # Using System API
107
146
 
108
- The **System API** allows your iframe application to interact directly with the Valu Social platform and its internal applications.
147
+ ## Using the System API
109
148
 
110
- This API provides a unified way to:
149
+ The **System API** allows your iframe app to interact directly with the Valu Social platform and its internal applications.
150
+ It provides a unified way to:
111
151
 
112
- * Access core platform features (apps, chat, etc.)
113
- * Call specific commands on these features
114
- * Subscribe to real-time events emitted by the platform
115
- * Run and test commands from the console for debugging
152
+ - Access core platform features (apps, chat, etc.)
153
+ - Call commands on these features
154
+ - Subscribe to real-time events from the platform
155
+ - Run and test commands from the console for debugging
116
156
 
117
- This makes it easy to extend the functionality of Valu Social and integrate your iframe application with other parts of the ecosystem.
157
+ ### 1. Get an API Pointer
118
158
 
119
- ## 1. Get an API Pointer
120
-
121
- Once the API is ready, you can create an `APIPointer` instance by specifying the name and version of the API you want to use. The version parameter is optional, and if omitted, the latest version will be used.
159
+ Once the API is ready, you can get an `APIPointer` by specifying the API name and (optionally) the version.
122
160
 
123
161
  ```javascript
124
- const appApi = valuApi.getApi('app', 1); // Specific version
125
- const appApiLatest = valuApi.current.getApi('app'); // Latest version
162
+ const appApi = await valuApi.getApi('app', 1); // Specific version
163
+ const appApiLatest = await valuApi.getApi('app'); // Latest version
126
164
  ```
127
165
 
128
- ## 2. Invoke API Commands
166
+ ### 2. Invoke API Commands
129
167
 
130
- After obtaining the API pointer, you can invoke commands on the API. Here's an example of opening the `text_chat` on the app API:
168
+ After obtaining the API pointer, you can invoke commands.
169
+ For example, to get the current network id:
131
170
 
132
171
  ```javascript
133
- await appApi.run('open', 'text_chat');
172
+ const networkApi = await valuApi.getApi('network');
173
+ const networkId = await networkApi.run('id');
174
+ console.log(networkId);
134
175
  ```
135
176
 
136
- For interacting with other APIs, like the chat API:
177
+ ### 3. Subscribe to Events
137
178
 
138
- ```javascript
139
- const chatApi = valuApi.current.getApi('chat');
140
- await chatApi.run('open-channel', { roomId: 'room123', propId: 'prop456' });
141
- ```
142
-
143
- ## 3. Subscribe to Events
144
-
145
- You can subscribe to events emitted by the API. For example, if you want to listen for the `app-open` event:
179
+ You can subscribe to events emitted by the API.
180
+ For example, listen for the `app-open` event:
146
181
 
147
182
  ```javascript
183
+ const appApi = await valuApi.getApi('app');
148
184
  appApi.addEventListener('app-open', (event) => {
149
185
  console.log(event);
150
186
  });
151
187
  ```
152
188
 
153
- ## 4. Run Console Commands (For Testing)
189
+ ### 4. Run Console Commands (For Testing)
154
190
 
155
- You can use the `runConsoleCommand` method to execute commands directly in the console environment. This method processes the output and returns a resolved promise on success or an error message if the command fails.
191
+ Use `runConsoleCommand` to execute commands directly in the console environment.
156
192
 
157
193
  ```javascript
158
- let command = 'app open text_chat';
159
- let reply = await valuApi.current.runConsoleCommand(command);
160
- console.log(reply);
161
-
162
- command = 'chat open-channel roomId xz21wd31tx83kk propId 812t26xbq5424b';
163
- reply = await valuApi.current.runConsoleCommand(command);
194
+ const reply = await valuApi.runConsoleCommand('network id');
164
195
  console.log(reply);
165
196
  ```
166
197
 
167
- ## Example Workflow
198
+ #### Run Intents via Console Commands
199
+
200
+ You can also use the console to run intents — the following two examples achieve the same result:
201
+
202
+ **Via API:**
168
203
 
169
204
  ```javascript
170
- import { ValuApi } from 'valu-api';
205
+ import { Intent } from "@arkeytyp/valu-api";
171
206
 
172
- const valuApi = new ValuApi();
207
+ const usersApi = await valuApi.getApi('users');
208
+ const currentUser = await usersApi.run('current');
209
+ if (!currentUser) {
210
+ console.error('Something went wrong');
211
+ return;
212
+ }
173
213
 
174
- valuApi.current.addEventListener(ValuApi.API_READY, async () => {
175
- console.log("API IS READY!!!");
214
+ const intent = new Intent('textchat', 'open-channel', { userId: currentUser.id });
215
+ await valuApi.sendIntent(intent);
216
+ ```
176
217
 
177
- const appApi = valuApi.current.getApi('app');
218
+ **Via Console:**
178
219
 
179
- await appApi.run('open', 'text_chat');
220
+ ```javascript
221
+ const currentUser = await valuApi.runConsoleCommand('users current');
222
+ const reply = await valuApi.runConsoleCommand(
223
+ `app run -applicationId textchat -action open-channel -userId ${currentUser.id}`
224
+ );
225
+ console.log(reply);
226
+ ```
180
227
 
181
- appApi.addEventListener('app-open', (event) => {
182
- console.log('App opened:', event);
183
- });
228
+ ## Sample Project
184
229
 
185
- let command = 'app open text_chat';
186
- let reply = await valuApi.current.runConsoleCommand(command);
187
- console.log(reply);
188
- });
189
- ```
230
+ We've created a sample application integrated with Valu API.
231
+ Check out the repository here and feel free to leave comments or feedback:
232
+
233
+ [https://github.com/Roomful/ValuSampleApp](https://github.com/Roomful/ValuSampleApp)
234
+ know if you want a **quick start** section, more real-world samples, or even a troubleshooting/FAQ block!
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arkeytyp/valu-api",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "A package for developing iframe applications for Valu Social. Allows invoking functions of registered Valu applications and subscribing to events, as well as other features that enable developers creating own ifram apps for the value social",
5
5
  "main": "src/ValuApi.js",
6
6
  "scripts": {
package/src/ValuApi.js CHANGED
@@ -145,6 +145,20 @@ export class ValuApi {
145
145
  return deferredPromise.promise;
146
146
  }
147
147
 
148
+ async callService(intent) {
149
+ let deferredPromise = this.#createDeferred();
150
+
151
+ this.#postToValuApp('api:service-intent', {
152
+ applicationId: intent.applicationId,
153
+ action: intent.action,
154
+ params: intent.params,
155
+ requestId: deferredPromise.id,
156
+ });
157
+
158
+ this.#requests[deferredPromise.id] = deferredPromise;
159
+ return deferredPromise.promise;
160
+ }
161
+
148
162
  /**
149
163
  * Executes a given console command and returns the result of an API function.
150
164
  *
@@ -187,7 +201,7 @@ export class ValuApi {
187
201
  }
188
202
 
189
203
  const message = event.data.message;
190
- // console.log('Message From Valu: ', event.data.name, ' ', message);
204
+ //console.log('Message From Valu: ', event.data.name, ' ', message);
191
205
 
192
206
  switch (event.data.name) {
193
207
  case 'api:ready': {
@@ -213,6 +227,7 @@ export class ValuApi {
213
227
  case 'api:run-console-completed': {
214
228
  const requestId = event.data.requestId;
215
229
  const deferred = this.#requests[requestId];
230
+
216
231
  if(deferred) {
217
232
  deferred.resolve(message);
218
233
  } else {
@@ -16,6 +16,7 @@ declare module '@arkeytyp/valu-api' {
16
16
  * @returns A promise resolving to the response from the target application.
17
17
  */
18
18
  sendIntent(intent: Intent): Promise<any>;
19
+ callService(intent: Intent): Promise<any>;
19
20
 
20
21
  addEventListener(event: string, callback: (data: any) => void): void;
21
22
  removeEventListener(event: string, callback: (data: any) => void): void;