@jtl-software/cloud-apps-core 0.0.0-dev.05de610
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 +227 -0
- package/dist/AppBridge.d.ts +7 -0
- package/dist/capabilities/event/AppBridgeEventCapability.d.ts +10 -0
- package/dist/capabilities/event/AppBridgeEventCapability.spec.d.ts +1 -0
- package/dist/capabilities/event/index.d.ts +2 -0
- package/dist/capabilities/event/types/EventListener.d.ts +2 -0
- package/dist/capabilities/event/types/EventObject.d.ts +13 -0
- package/dist/capabilities/event/types/index.d.ts +2 -0
- package/dist/capabilities/index.d.ts +2 -0
- package/dist/capabilities/method/AppBridgeMethodCapability.d.ts +10 -0
- package/dist/capabilities/method/AppBridgeMethodCapability.spec.d.ts +1 -0
- package/dist/capabilities/method/index.d.ts +1 -0
- package/dist/capabilities/method/types/MethodCallBody.d.ts +13 -0
- package/dist/capabilities/method/types/index.d.ts +1 -0
- package/dist/core/AppBridgeMessagePort.d.ts +11 -0
- package/dist/core/AppBridgeMessagePort.spec.d.ts +1 -0
- package/dist/core/abstractions/AppBridgeCapability.d.ts +9 -0
- package/dist/core/abstractions/AppBridgeInfrastructure.d.ts +9 -0
- package/dist/core/abstractions/IAppBridgeCapability.d.ts +2 -0
- package/dist/core/abstractions/index.d.ts +3 -0
- package/dist/core/index.d.ts +3 -0
- package/dist/core/types/ChannelMessage.d.ts +19 -0
- package/dist/core/types/ChannelMessageHandler.d.ts +3 -0
- package/dist/core/types/Disposer.d.ts +2 -0
- package/dist/core/types/IMessageLookup.d.ts +6 -0
- package/dist/core/types/MessageType.d.ts +4 -0
- package/dist/core/types/SendAppBridgeMessageOptions.d.ts +8 -0
- package/dist/core/types/index.d.ts +6 -0
- package/dist/factories/createAppBridge.d.ts +2 -0
- package/dist/factories/createAppBridge.spec.d.ts +1 -0
- package/dist/factories/index.d.ts +1 -0
- package/dist/main.d.ts +4 -0
- package/dist/main.js +4298 -0
- package/dist/main.js.map +1 -0
- package/package.json +45 -0
package/README.md
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
#  JTL-Platform Apps Core SDK
|
|
2
|
+
|
|
3
|
+
A lightweight, type-safe communication bridge for JTL Platform apps.
|
|
4
|
+
|
|
5
|
+
## 📦 Installation
|
|
6
|
+
|
|
7
|
+
Install the package using npm or yarn:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
# Using npm
|
|
11
|
+
npm install @jtl-software/cloud-apps-core
|
|
12
|
+
|
|
13
|
+
# Using yarn
|
|
14
|
+
yarn add @jtl-software/cloud-apps-core
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## 🔍 Overview
|
|
18
|
+
|
|
19
|
+
The JTL Platform Apps Core SDK provides a robust communication bridge between apps and the JTL Platform host application. It enables:
|
|
20
|
+
|
|
21
|
+
- Bidirectional method calling between apps and host
|
|
22
|
+
- Event-based publish/subscribe communication
|
|
23
|
+
- Type-safe messaging with Zod validation
|
|
24
|
+
- Promise-based async communication
|
|
25
|
+
|
|
26
|
+
This package is the foundation for building apps that integrate seamlessly with the JTL Platform ecosystem.
|
|
27
|
+
|
|
28
|
+
## 🚀 Usage
|
|
29
|
+
|
|
30
|
+
### Creating a App Bridge
|
|
31
|
+
|
|
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
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
import { createAppBridge, AppBridge } from '@jtl-software/cloud-apps-core';
|
|
36
|
+
import { useState, useEffect } from 'react';
|
|
37
|
+
|
|
38
|
+
function YourAppComponent() {
|
|
39
|
+
// Store the AppBridge instance in component state
|
|
40
|
+
const [appBridge, setAppBridge] = useState<AppBridge | undefined>(undefined);
|
|
41
|
+
|
|
42
|
+
useEffect((): void => {
|
|
43
|
+
console.info('Creating bridge...');
|
|
44
|
+
createAppBridge().then(bridge => {
|
|
45
|
+
console.log('Bridge created!');
|
|
46
|
+
|
|
47
|
+
// Store the bridge instance in state for use throughout your component
|
|
48
|
+
setAppBridge(bridge);
|
|
49
|
+
});
|
|
50
|
+
}, []);
|
|
51
|
+
|
|
52
|
+
// Rest of your component...
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Subscribe to Host Events
|
|
57
|
+
|
|
58
|
+
The `subscribe` method allows your app to listen for specific events emitted by the host environment.
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
// Listen for inventory updates
|
|
62
|
+
const unsubscribe = appBridge.event.subscribe('inventory:updated', async data => {
|
|
63
|
+
console.info('Inventory updated:', data);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
// Later, you can unsubscribe
|
|
67
|
+
unsubscribe();
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
When the host system triggers an "inventory:updated" event, your callback function will execute with the provided data.
|
|
71
|
+
|
|
72
|
+
### Publish events to the host
|
|
73
|
+
|
|
74
|
+
The `publish` method sends data to the host environment under a specific topic.
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
// Notify the host about completed action
|
|
78
|
+
await appBridge.event.publish('order:verification:complete', {
|
|
79
|
+
orderId: 'ORD-12345',
|
|
80
|
+
verifiedBy: 'app-user',
|
|
81
|
+
});
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
This is useful for informing the host about state changes or actions completed within your app.
|
|
85
|
+
|
|
86
|
+
### Exposing Methods to the Host
|
|
87
|
+
|
|
88
|
+
The `expose` function makes your app's functions callable by the host environment.
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
// Make a function available to the host
|
|
92
|
+
const disposer = appBridge.method.expose('calculateShippingCost', (weight, destination) => {
|
|
93
|
+
const baseCost = 5.99;
|
|
94
|
+
const zoneRate = destination === 'international' ? 2.5 : 1;
|
|
95
|
+
return baseCost + 0.1 * weight * zoneRate;
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
// Check if a method is exposed
|
|
99
|
+
const isExposed = appBridge.method.isExposed('calculateShippingCost'); // true
|
|
100
|
+
|
|
101
|
+
// Later, you can remove the exposed method
|
|
102
|
+
disposer();
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
This allows the host environment to directly use functionality implemented in your app.
|
|
106
|
+
|
|
107
|
+
### Calling Host Methods
|
|
108
|
+
|
|
109
|
+
The `call` function allows your app to invoke functions provided by the host environment.
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
// Get user information
|
|
113
|
+
const userProfile = await appBridge.method.call('getUserProfile');
|
|
114
|
+
|
|
115
|
+
// Call a method with parameters
|
|
116
|
+
const orderDetails = await appBridge.method.call('getOrderDetails', 'ORD-5678');
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## 📚 API Reference
|
|
120
|
+
|
|
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:
|
|
124
|
+
|
|
125
|
+
- `method`: AppBridgeMethodCapability - Handles method calling functionality
|
|
126
|
+
- `event`: AppBridgeEventCapability - Handles event publish/subscribe functionality
|
|
127
|
+
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
### AppBridgeEventCapability
|
|
131
|
+
|
|
132
|
+
Handles event-based communication between the app and host.
|
|
133
|
+
|
|
134
|
+
#### `subscribe<TPayload>(topic: string, callback: EventListener<TPayload>): () => void`
|
|
135
|
+
|
|
136
|
+
**Description:**
|
|
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.
|
|
138
|
+
|
|
139
|
+
**Parameters:**
|
|
140
|
+
|
|
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.
|
|
157
|
+
|
|
158
|
+
**Returns:** `void`
|
|
159
|
+
|
|
160
|
+
---
|
|
161
|
+
|
|
162
|
+
#### `publish<TPayload>(topic: string, payload: TPayload): Promise<void>`
|
|
163
|
+
|
|
164
|
+
**Description:**
|
|
165
|
+
Sends data to the host under a specific topic, typically to notify about an action or a state change.
|
|
166
|
+
|
|
167
|
+
**Parameters:**
|
|
168
|
+
|
|
169
|
+
- `topic` (string): The topic name under which the data should be published.
|
|
170
|
+
- `payload` (`TPayload`): The data to be sent to the host.
|
|
171
|
+
|
|
172
|
+
**Returns:** `Promise<void>`
|
|
173
|
+
|
|
174
|
+
---
|
|
175
|
+
|
|
176
|
+
### AppBridgeMethodCapability
|
|
177
|
+
|
|
178
|
+
Handles method calling functionality between the app and host.
|
|
179
|
+
|
|
180
|
+
#### `expose<TMethod>(methodName: string, method: TMethod): () => void`
|
|
181
|
+
|
|
182
|
+
**Description:**
|
|
183
|
+
Makes a app method available for the host to call directly.
|
|
184
|
+
|
|
185
|
+
**Parameters:**
|
|
186
|
+
|
|
187
|
+
- `methodName` (string): The name under which the method should be exposed.
|
|
188
|
+
- `method` (`TMethod`): The actual method implementation.
|
|
189
|
+
|
|
190
|
+
**Returns:** `() => void` - A disposer function that removes the exposed method when called.
|
|
191
|
+
|
|
192
|
+
---
|
|
193
|
+
|
|
194
|
+
#### `isExposed(methodName: string): boolean`
|
|
195
|
+
|
|
196
|
+
**Description:**
|
|
197
|
+
Checks whether a method with the given name has already been exposed to the host.
|
|
198
|
+
|
|
199
|
+
**Parameters:**
|
|
200
|
+
|
|
201
|
+
- `methodName` (string): The name of the method to check.
|
|
202
|
+
|
|
203
|
+
**Returns:** `boolean`
|
|
204
|
+
|
|
205
|
+
---
|
|
206
|
+
|
|
207
|
+
#### `call<TResponse>(methodName: string, ...args: unknown[]): Promise<TResponse>`
|
|
208
|
+
|
|
209
|
+
**Description:**
|
|
210
|
+
Calls a method provided by the host environment and returns the result.
|
|
211
|
+
|
|
212
|
+
**Parameters:**
|
|
213
|
+
|
|
214
|
+
- `methodName` (string): The name of the host method to call.
|
|
215
|
+
- `...args` (unknown[]): The arguments to pass to the host method.
|
|
216
|
+
|
|
217
|
+
**Returns:** `Promise<TResponse>`
|
|
218
|
+
|
|
219
|
+
### Factories
|
|
220
|
+
|
|
221
|
+
- `createAppBridge()`: Creates and initializes a AppBridge instance
|
|
222
|
+
|
|
223
|
+
## 📦 Dependencies
|
|
224
|
+
|
|
225
|
+
This package has minimal dependencies:
|
|
226
|
+
|
|
227
|
+
- `zod`: For runtime type validation
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { AppBridgeEventCapability, AppBridgeMethodCapability } from './capabilities';
|
|
2
|
+
import { AppBridgeInfrastructure, AppBridgeMessagePort } from './core';
|
|
3
|
+
export default class AppBridge extends AppBridgeInfrastructure {
|
|
4
|
+
readonly method: AppBridgeMethodCapability;
|
|
5
|
+
readonly event: AppBridgeEventCapability;
|
|
6
|
+
constructor(appBridgeMessagePort: AppBridgeMessagePort);
|
|
7
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { AppBridgeCapability, AppBridgeMessagePort } from '../../core';
|
|
2
|
+
import { EventListener } from './types';
|
|
3
|
+
export default class AppBridgeEventCapability extends AppBridgeCapability<'event'> {
|
|
4
|
+
private readonly _eventListeners;
|
|
5
|
+
constructor(appBridgeMessagePort: AppBridgeMessagePort);
|
|
6
|
+
subscribe<TPayload>(topic: string, callback: EventListener<TPayload>): () => void;
|
|
7
|
+
unsubscribe<TPayload>(topic: string, callback: EventListener<TPayload>): void;
|
|
8
|
+
publish<TPayload>(topic: string, payload: TPayload): Promise<void>;
|
|
9
|
+
private handlePublishedEvent;
|
|
10
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
declare const EventObject: z.ZodObject<{
|
|
3
|
+
topic: z.ZodString;
|
|
4
|
+
payload: z.ZodUnknown;
|
|
5
|
+
}, "strip", z.ZodTypeAny, {
|
|
6
|
+
topic: string;
|
|
7
|
+
payload?: unknown;
|
|
8
|
+
}, {
|
|
9
|
+
topic: string;
|
|
10
|
+
payload?: unknown;
|
|
11
|
+
}>;
|
|
12
|
+
type EventObject = z.infer<typeof EventObject>;
|
|
13
|
+
export default EventObject;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Disposer, AppBridgeCapability, AppBridgeMessagePort } from '../../core';
|
|
2
|
+
export default class AppBridgeMethodCapability extends AppBridgeCapability<'method'> {
|
|
3
|
+
private readonly _exposedMethods;
|
|
4
|
+
constructor(appBridgeMessagePort: AppBridgeMessagePort);
|
|
5
|
+
expose<TMethod>(methodName: string, method: TMethod): Disposer;
|
|
6
|
+
removeExposedMethod(methodName: string): void;
|
|
7
|
+
isExposed(methodName: string): boolean;
|
|
8
|
+
call<TResponse>(methodName: string, ...args: unknown[]): Promise<TResponse>;
|
|
9
|
+
private handleMethodCall;
|
|
10
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as AppBridgeMethodCapability } from './AppBridgeMethodCapability';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
declare const MethodCallBody: z.ZodObject<{
|
|
3
|
+
methodName: z.ZodString;
|
|
4
|
+
args: z.ZodArray<z.ZodUnknown, "many">;
|
|
5
|
+
}, "strip", z.ZodTypeAny, {
|
|
6
|
+
methodName: string;
|
|
7
|
+
args: unknown[];
|
|
8
|
+
}, {
|
|
9
|
+
methodName: string;
|
|
10
|
+
args: unknown[];
|
|
11
|
+
}>;
|
|
12
|
+
type MethodCallBody = z.infer<typeof MethodCallBody>;
|
|
13
|
+
export default MethodCallBody;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as MethodCallBody } from './MethodCallBody';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ChannelMessageHandler, SendAppBridgeMessageOptions } from './types';
|
|
2
|
+
export default class AppBridgeMessagePort {
|
|
3
|
+
private readonly messagePort;
|
|
4
|
+
private static readonly DEFAULT_SEND_MESSAGE_TIMEOUT;
|
|
5
|
+
private readonly channelMessageHandlers;
|
|
6
|
+
private _messageLookup;
|
|
7
|
+
constructor(messagePort: MessagePort);
|
|
8
|
+
sendMessage<TBody, TResponse = void>(subject: string, body?: TBody, options?: SendAppBridgeMessageOptions): Promise<TResponse>;
|
|
9
|
+
registerMessageHandler(subject: string, handler: ChannelMessageHandler): void;
|
|
10
|
+
private handleMessage;
|
|
11
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { default as IAppBridgeCapability } from './IAppBridgeCapability';
|
|
2
|
+
import { default as AppBridgeMessagePort } from '../AppBridgeMessagePort';
|
|
3
|
+
import { ChannelMessageHandler, SendAppBridgeMessageOptions } from '../types';
|
|
4
|
+
export default abstract class AppBridgeCapability<NAME extends string> implements IAppBridgeCapability {
|
|
5
|
+
private readonly appBridgeMessagePort;
|
|
6
|
+
protected constructor(appBridgeMessagePort: AppBridgeMessagePort);
|
|
7
|
+
protected registerMessageHandler(subject: `${NAME}:${string}`, messageHandler: ChannelMessageHandler): void;
|
|
8
|
+
protected sendMessage<TBody, TResponse = void>(subject: `${NAME}:${string}`, body: TBody, options?: SendAppBridgeMessageOptions): Promise<TResponse>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { default as IAppBridgeCapability } from './IAppBridgeCapability';
|
|
2
|
+
import { default as AppBridgeMessagePort } from '../AppBridgeMessagePort';
|
|
3
|
+
export default abstract class AppBridgeInfrastructure {
|
|
4
|
+
private readonly appBridgeMessagePort;
|
|
5
|
+
protected constructor(appBridgeMessagePort: AppBridgeMessagePort);
|
|
6
|
+
protected registerCapability<T>(capability: IAppBridgeCapability & {
|
|
7
|
+
new (appBridgeMessagePort: AppBridgeMessagePort): T;
|
|
8
|
+
}): T;
|
|
9
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
declare const ChannelMessage: z.ZodObject<{
|
|
3
|
+
id: z.ZodString;
|
|
4
|
+
type: z.ZodEnum<["HANDSHAKE", "REQUEST", "RESPONSE", "ERROR"]>;
|
|
5
|
+
subject: z.ZodString;
|
|
6
|
+
body: z.ZodOptional<z.ZodUnknown>;
|
|
7
|
+
}, "strip", z.ZodTypeAny, {
|
|
8
|
+
type: "HANDSHAKE" | "REQUEST" | "RESPONSE" | "ERROR";
|
|
9
|
+
id: string;
|
|
10
|
+
subject: string;
|
|
11
|
+
body?: unknown;
|
|
12
|
+
}, {
|
|
13
|
+
type: "HANDSHAKE" | "REQUEST" | "RESPONSE" | "ERROR";
|
|
14
|
+
id: string;
|
|
15
|
+
subject: string;
|
|
16
|
+
body?: unknown;
|
|
17
|
+
}>;
|
|
18
|
+
type ChannelMessage = z.infer<typeof ChannelMessage>;
|
|
19
|
+
export default ChannelMessage;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { default as ChannelMessage } from './ChannelMessage';
|
|
2
|
+
export { default as MessageType } from './MessageType';
|
|
3
|
+
export type { default as ChannelMessageHandler } from './ChannelMessageHandler';
|
|
4
|
+
export type { default as Disposer } from './Disposer';
|
|
5
|
+
export type { default as IMessageLookup } from './IMessageLookup';
|
|
6
|
+
export type { default as SendAppBridgeMessageOptions } from './SendAppBridgeMessageOptions';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as createAppBridge } from './createAppBridge';
|
package/dist/main.d.ts
ADDED