@jtl-software/cloud-apps-core 0.0.0-dev.dce09c1 → 0.0.0-dev.eef23f5
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 +59 -29
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#  JTL-Platform
|
|
1
|
+
#  JTL-Platform Apps Core SDK
|
|
2
2
|
|
|
3
3
|
A lightweight, type-safe communication bridge for JTL Platform apps.
|
|
4
4
|
|
|
@@ -16,7 +16,7 @@ yarn add @jtl-software/cloud-apps-core
|
|
|
16
16
|
|
|
17
17
|
## 🔍 Overview
|
|
18
18
|
|
|
19
|
-
The JTL Platform
|
|
19
|
+
The JTL Platform Apps Core SDK provides a robust communication bridge between apps and the JTL Platform host application. It enables:
|
|
20
20
|
|
|
21
21
|
- Bidirectional method calling between apps and host
|
|
22
22
|
- Event-based publish/subscribe communication
|
|
@@ -27,25 +27,25 @@ This package is the foundation for building apps that integrate seamlessly with
|
|
|
27
27
|
|
|
28
28
|
## 🚀 Usage
|
|
29
29
|
|
|
30
|
-
### Creating a
|
|
30
|
+
### Creating a App Bridge
|
|
31
31
|
|
|
32
|
-
To use the
|
|
32
|
+
To use the AppBridge in your React components, you'll need to initialize it and maintain its instance in your component's state. Typically, this is done within a useEffect hook:
|
|
33
33
|
|
|
34
34
|
```typescript
|
|
35
|
-
import {
|
|
35
|
+
import { createAppBridge, AppBridge } from '@jtl-software/cloud-apps-core';
|
|
36
36
|
import { useState, useEffect } from 'react';
|
|
37
37
|
|
|
38
|
-
function
|
|
39
|
-
// Store the
|
|
40
|
-
const [appBridge,
|
|
38
|
+
function YourAppComponent() {
|
|
39
|
+
// Store the AppBridge instance in component state
|
|
40
|
+
const [appBridge, setAppBridge] = useState<AppBridge | undefined>(undefined);
|
|
41
41
|
|
|
42
42
|
useEffect((): void => {
|
|
43
43
|
console.info('Creating bridge...');
|
|
44
|
-
|
|
44
|
+
createAppBridge().then(bridge => {
|
|
45
45
|
console.log('Bridge created!');
|
|
46
46
|
|
|
47
47
|
// Store the bridge instance in state for use throughout your component
|
|
48
|
-
|
|
48
|
+
setAppBridge(bridge);
|
|
49
49
|
});
|
|
50
50
|
}, []);
|
|
51
51
|
|
|
@@ -59,10 +59,12 @@ The `subscribe` method allows your app to listen for specific events emitted by
|
|
|
59
59
|
|
|
60
60
|
```typescript
|
|
61
61
|
// Listen for inventory updates
|
|
62
|
-
appBridge.subscribe('inventory:updated', async data => {
|
|
62
|
+
const unsubscribe = appBridge.event.subscribe('inventory:updated', async data => {
|
|
63
63
|
console.info('Inventory updated:', data);
|
|
64
|
-
return { status: 'processed' };
|
|
65
64
|
});
|
|
65
|
+
|
|
66
|
+
// Later, you can unsubscribe
|
|
67
|
+
unsubscribe();
|
|
66
68
|
```
|
|
67
69
|
|
|
68
70
|
When the host system triggers an "inventory:updated" event, your callback function will execute with the provided data.
|
|
@@ -73,7 +75,7 @@ The `publish` method sends data to the host environment under a specific topic.
|
|
|
73
75
|
|
|
74
76
|
```typescript
|
|
75
77
|
// Notify the host about completed action
|
|
76
|
-
await appBridge.publish('order:verification:complete', {
|
|
78
|
+
await appBridge.event.publish('order:verification:complete', {
|
|
77
79
|
orderId: 'ORD-12345',
|
|
78
80
|
verifiedBy: 'app-user',
|
|
79
81
|
});
|
|
@@ -83,51 +85,75 @@ This is useful for informing the host about state changes or actions completed w
|
|
|
83
85
|
|
|
84
86
|
### Exposing Methods to the Host
|
|
85
87
|
|
|
86
|
-
The `
|
|
88
|
+
The `expose` function makes your app's functions callable by the host environment.
|
|
87
89
|
|
|
88
90
|
```typescript
|
|
89
91
|
// Make a function available to the host
|
|
90
|
-
appBridge.
|
|
92
|
+
const disposer = appBridge.method.expose('calculateShippingCost', (weight, destination) => {
|
|
91
93
|
const baseCost = 5.99;
|
|
92
94
|
const zoneRate = destination === 'international' ? 2.5 : 1;
|
|
93
95
|
return baseCost + 0.1 * weight * zoneRate;
|
|
94
96
|
});
|
|
95
97
|
|
|
96
98
|
// Check if a method is exposed
|
|
97
|
-
const isExposed =
|
|
99
|
+
const isExposed = appBridge.method.isExposed('calculateShippingCost'); // true
|
|
100
|
+
|
|
101
|
+
// Later, you can remove the exposed method
|
|
102
|
+
disposer();
|
|
98
103
|
```
|
|
99
104
|
|
|
100
105
|
This allows the host environment to directly use functionality implemented in your app.
|
|
101
106
|
|
|
102
107
|
### Calling Host Methods
|
|
103
108
|
|
|
104
|
-
The `
|
|
109
|
+
The `call` function allows your app to invoke functions provided by the host environment.
|
|
105
110
|
|
|
106
111
|
```typescript
|
|
107
112
|
// Get user information
|
|
108
|
-
const userProfile = await appBridge.
|
|
113
|
+
const userProfile = await appBridge.method.call('getUserProfile');
|
|
109
114
|
|
|
110
115
|
// Call a method with parameters
|
|
111
|
-
const orderDetails = await appBridge.
|
|
116
|
+
const orderDetails = await appBridge.method.call('getOrderDetails', 'ORD-5678');
|
|
112
117
|
```
|
|
113
118
|
|
|
114
119
|
## 📚 API Reference
|
|
115
120
|
|
|
116
|
-
###
|
|
121
|
+
### AppBridge
|
|
122
|
+
|
|
123
|
+
The main class that handles communication between the app and host. It provides access to different capabilities through the following properties:
|
|
117
124
|
|
|
118
|
-
|
|
125
|
+
- `method`: AppBridgeMethodCapability - Handles method calling functionality
|
|
126
|
+
- `event`: AppBridgeEventCapability - Handles event publish/subscribe functionality
|
|
119
127
|
|
|
120
128
|
---
|
|
121
129
|
|
|
122
|
-
|
|
130
|
+
### AppBridgeEventCapability
|
|
131
|
+
|
|
132
|
+
Handles event-based communication between the app and host.
|
|
133
|
+
|
|
134
|
+
#### `subscribe<TPayload>(topic: string, callback: EventListener<TPayload>): () => void`
|
|
123
135
|
|
|
124
136
|
**Description:**
|
|
125
137
|
Registers a listener for a specific event emitted by the host environment. When the event is triggered, the callback is executed with the event data.
|
|
126
138
|
|
|
127
139
|
**Parameters:**
|
|
128
140
|
|
|
129
|
-
- `
|
|
130
|
-
- `callback` (function): A function that handles the event data.
|
|
141
|
+
- `topic` (string): The name of the event to listen for.
|
|
142
|
+
- `callback` (function): A function that handles the event data. Can be sync or async.
|
|
143
|
+
|
|
144
|
+
**Returns:** `() => void` - A function that, when called, unsubscribes the listener.
|
|
145
|
+
|
|
146
|
+
---
|
|
147
|
+
|
|
148
|
+
#### `unsubscribe<TPayload>(topic: string, callback: EventListener<TPayload>): void`
|
|
149
|
+
|
|
150
|
+
**Description:**
|
|
151
|
+
Removes a specific callback from the event listeners for a topic.
|
|
152
|
+
|
|
153
|
+
**Parameters:**
|
|
154
|
+
|
|
155
|
+
- `topic` (string): The topic name to unsubscribe from.
|
|
156
|
+
- `callback` (function): The specific callback function to remove.
|
|
131
157
|
|
|
132
158
|
**Returns:** `void`
|
|
133
159
|
|
|
@@ -147,7 +173,11 @@ Sends data to the host under a specific topic, typically to notify about an acti
|
|
|
147
173
|
|
|
148
174
|
---
|
|
149
175
|
|
|
150
|
-
|
|
176
|
+
### AppBridgeMethodCapability
|
|
177
|
+
|
|
178
|
+
Handles method calling functionality between the app and host.
|
|
179
|
+
|
|
180
|
+
#### `expose<TMethod>(methodName: string, method: TMethod): () => void`
|
|
151
181
|
|
|
152
182
|
**Description:**
|
|
153
183
|
Makes a app method available for the host to call directly.
|
|
@@ -157,11 +187,11 @@ Makes a app method available for the host to call directly.
|
|
|
157
187
|
- `methodName` (string): The name under which the method should be exposed.
|
|
158
188
|
- `method` (`TMethod`): The actual method implementation.
|
|
159
189
|
|
|
160
|
-
**Returns:** `void`
|
|
190
|
+
**Returns:** `() => void` - A disposer function that removes the exposed method when called.
|
|
161
191
|
|
|
162
192
|
---
|
|
163
193
|
|
|
164
|
-
#### `
|
|
194
|
+
#### `isExposed(methodName: string): boolean`
|
|
165
195
|
|
|
166
196
|
**Description:**
|
|
167
197
|
Checks whether a method with the given name has already been exposed to the host.
|
|
@@ -174,7 +204,7 @@ Checks whether a method with the given name has already been exposed to the host
|
|
|
174
204
|
|
|
175
205
|
---
|
|
176
206
|
|
|
177
|
-
#### `
|
|
207
|
+
#### `call<TResponse>(methodName: string, ...args: unknown[]): Promise<TResponse>`
|
|
178
208
|
|
|
179
209
|
**Description:**
|
|
180
210
|
Calls a method provided by the host environment and returns the result.
|
|
@@ -188,7 +218,7 @@ Calls a method provided by the host environment and returns the result.
|
|
|
188
218
|
|
|
189
219
|
### Factories
|
|
190
220
|
|
|
191
|
-
- `
|
|
221
|
+
- `createAppBridge()`: Creates and initializes a AppBridge instance
|
|
192
222
|
|
|
193
223
|
## 📦 Dependencies
|
|
194
224
|
|