@arkeytyp/valu-api 1.1.1 → 1.1.2

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
@@ -225,6 +225,102 @@ const reply = await valuApi.runConsoleCommand(
225
225
  console.log(reply);
226
226
  ```
227
227
 
228
+ # Routing in Valu iFrame Applications
229
+
230
+ The application is embedded inside Valuverse and controlled by the host.
231
+
232
+ - Valuverse owns the global routing context
233
+ - Your app receives route updates from Valu
234
+ - Your app should push navigation changes back to Valu
235
+
236
+ ---
237
+
238
+ ## Important Concept: Route Visibility
239
+
240
+ There are **two different routing scopes**:
241
+
242
+ ### Local (App-only) Routing
243
+ Routing handled only inside your application (e.g. React Router).
244
+
245
+ - ✅ App works normally
246
+ - ❌ Routes are **not visible** to Valuverse
247
+ - ❌ No deep linking from the main application
248
+
249
+ ### Valu Routing (Host-aware)
250
+ Routing handled through Valu API.
251
+
252
+ - ✅ Routes appear in the main application
253
+ - ✅ Deep linking works
254
+ - ✅ Navigation state is shared with Valuverse
255
+
256
+ ---
257
+
258
+ ## Using React Router in an iFrame App
259
+
260
+ Using **React Router inside an iFrame Valu app is totally fine**.
261
+
262
+ Your application will:
263
+ - render pages correctly
264
+ - navigate normally
265
+ - function as a self-contained UI
266
+
267
+ However:
268
+
269
+ - those routes are **internal only**
270
+ - they **do not propagate** to Valuverse
271
+ - the main application will not see or control them
272
+
273
+ This approach is acceptable for:
274
+ - demo applications
275
+ - prototypes
276
+ - isolated tools
277
+ - apps that do not need host-level routing
278
+
279
+ ---
280
+
281
+ ## Best Practice for Valuverse Applications
282
+
283
+ If your application is built to behave like a **native Valuverse app**, the recommended approach is:
284
+
285
+ > **Use Valu routing instead of (or in addition to) local routing.**
286
+
287
+ This ensures:
288
+ - consistent navigation behavior
289
+ - correct deep linking
290
+ - visibility in the main application router
291
+ - proper docking and context switching
292
+
293
+ ---
294
+
295
+ ## Valu Routing API
296
+
297
+ ### Subscribing to Route Changes
298
+
299
+ Valu notifies your application when the route changes:
300
+
301
+ ```ts
302
+ valuApi.addEventListener(ValuApi.ON_ROUTE, (route) => {
303
+ // route example:
304
+ // "/console"
305
+ // "/api/users/id/123"
306
+ });
307
+ ```
308
+
309
+ ### Pushing a New Route
310
+
311
+ Navigate forward:
312
+
313
+ ```ts
314
+ valuApi.pushRoute("/documentation");
315
+ ```
316
+
317
+ Replacing the Current Route
318
+ Redirect without adding a history entry:
319
+
320
+ ```ts
321
+ valuApi.replaceRoute("/console");
322
+ ```
323
+
228
324
  ## Sample Project
229
325
 
230
326
  We've created a sample application integrated with Valu API.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arkeytyp/valu-api",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
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
@@ -17,6 +17,7 @@ export { APIPointer } from "./APIPointer.js";
17
17
  export class ValuApi {
18
18
 
19
19
  static API_READY = 'api:ready'
20
+ static ON_ROUTE = `on_route`;
20
21
 
21
22
  #eventEmitter;
22
23
  #valuApplication = {};
@@ -184,6 +185,40 @@ export class ValuApi {
184
185
  return deferredPromise.promise;
185
186
  }
186
187
 
188
+
189
+ #runCommand(name, data) {
190
+ this.#postToValuApp('api:run-command', {
191
+ command: name,
192
+ data: data,
193
+ });
194
+ }
195
+
196
+ /**
197
+ * Pushes a new route onto the navigation stack.
198
+ *
199
+ * Use this when:
200
+ * navigating forward
201
+ * opening a new view
202
+ * preserving back-navigation history
203
+ * @param path
204
+ */
205
+ pushRoute = (path) => {
206
+ this.#runCommand('pushRoute', path);
207
+ }
208
+
209
+ /**
210
+ * Replaces the current route without adding a new history entry.
211
+ * Use this when:
212
+ * redirecting
213
+ * normalizing URLs
214
+ * preventing back-navigation to the previous route
215
+ * @param {string }path
216
+ */
217
+ replaceRoute = (path) => {
218
+ this.#runCommand('replaceRoute', path);
219
+ }
220
+
221
+
187
222
  #createDeferred() {
188
223
  let resolve, reject;
189
224
  const promise = new Promise((res, rej) => {
@@ -219,9 +254,20 @@ export class ValuApi {
219
254
  break;
220
255
  }
221
256
 
257
+ case 'api:trigger': {
258
+ switch (message.action) {
259
+ case ValuApi.ON_ROUTE: {
260
+ this.#eventEmitter.emit(ValuApi.ON_ROUTE, message.data);
261
+ this.#applicationInstance?.onUpdateRouterContext(message.data);
262
+ }
263
+ }
264
+ break;
265
+ }
266
+
222
267
  case 'api:new-intent': {
223
268
  const intent = new Intent(message.applicationId, message.action, message.params);
224
269
  this.#applicationInstance?.onNewIntent(intent);
270
+ break;
225
271
  }
226
272
 
227
273
  case 'api:run-console-completed': {
@@ -28,4 +28,15 @@ export class ValuApplication {
28
28
  * Called when the app is about to be destroyed.
29
29
  */
30
30
  async onDestroy() {}
31
+
32
+
33
+ /**
34
+ * Called when the application’s router context changes.
35
+ *
36
+ * This typically happens when:
37
+ * the app moves between main / side / modal containers
38
+ * the host updates routing or layout state
39
+ * @param {string} context
40
+ */
41
+ onUpdateRouterContext(context) {};
31
42
  }
@@ -1,6 +1,7 @@
1
1
  declare module '@arkeytyp/valu-api' {
2
2
  export class ValuApi {
3
3
  static API_READY: string;
4
+ static ON_ROUTE : string;
4
5
 
5
6
  get connected(): boolean;
6
7
 
@@ -22,6 +23,27 @@ declare module '@arkeytyp/valu-api' {
22
23
  removeEventListener(event: string, callback: (data: any) => void): void;
23
24
  getApi(apiName: string, version?: number): Promise<APIPointer>;
24
25
  runConsoleCommand(command: string): Promise<any | string>;
26
+
27
+ /**
28
+ * Pushes a new route onto the navigation stack.
29
+ *
30
+ * Use this when:
31
+ * navigating forward
32
+ * opening a new view
33
+ * preserving back-navigation history
34
+ * @param path
35
+ */
36
+ pushRoute(path: string): void;
37
+
38
+ /**
39
+ * Replaces the current route without adding a new history entry.
40
+ * Use this when:
41
+ * redirecting
42
+ * normalizing URLs
43
+ * preventing back-navigation to the previous route
44
+ * @param {string }path
45
+ */
46
+ replaceRoute(path: string): void;
25
47
  }
26
48
 
27
49
  export class APIPointer {
@@ -107,5 +129,15 @@ declare module '@arkeytyp/valu-api' {
107
129
  * Use this to clean up resources (e.g., closing connections, clearing timers).
108
130
  */
109
131
  onDestroy(): void;
132
+
133
+ /**
134
+ * Called when the application’s router context changes.
135
+ *
136
+ * This typically happens when:
137
+ * the app moves between main / side / modal containers
138
+ * the host updates routing or layout state
139
+ * @param context - updated application route
140
+ */
141
+ onUpdateRouterContext(context: string): void;
110
142
  }
111
143
  }