@jtl-software/cloud-apps-core 0.0.0-dev.71f72e3
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 +197 -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,197 @@
|
|
|
1
|
+
#  JTL-Platform Plugins 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 Plugins 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 Plugin Bridge
|
|
31
|
+
|
|
32
|
+
To use the PluginBridge 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 { createPluginBridge, PluginBridge } from '@jtl-software/cloud-apps-core';
|
|
36
|
+
import { useState, useEffect } from 'react';
|
|
37
|
+
|
|
38
|
+
function YourPluginComponent() {
|
|
39
|
+
// Store the PluginBridge instance in component state
|
|
40
|
+
const [appBridge, setPluginBridge] = useState<PluginBridge | undefined>(undefined);
|
|
41
|
+
|
|
42
|
+
useEffect((): void => {
|
|
43
|
+
console.info('Creating bridge...');
|
|
44
|
+
createPluginBridge().then(bridge => {
|
|
45
|
+
console.log('Bridge created!');
|
|
46
|
+
|
|
47
|
+
// Store the bridge instance in state for use throughout your component
|
|
48
|
+
setPluginBridge(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
|
+
appBridge.subscribe('inventory:updated', async data => {
|
|
63
|
+
console.info('Inventory updated:', data);
|
|
64
|
+
return { status: 'processed' };
|
|
65
|
+
});
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
When the host system triggers an "inventory:updated" event, your callback function will execute with the provided data.
|
|
69
|
+
|
|
70
|
+
### Publish events to the host
|
|
71
|
+
|
|
72
|
+
The `publish` method sends data to the host environment under a specific topic.
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
// Notify the host about completed action
|
|
76
|
+
await appBridge.publish('order:verification:complete', {
|
|
77
|
+
orderId: 'ORD-12345',
|
|
78
|
+
verifiedBy: 'app-user',
|
|
79
|
+
});
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
This is useful for informing the host about state changes or actions completed within your app.
|
|
83
|
+
|
|
84
|
+
### Exposing Methods to the Host
|
|
85
|
+
|
|
86
|
+
The `exposeMethod` function makes your app's functions callable by the host environment.
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
// Make a function available to the host
|
|
90
|
+
appBridge.exposeMethod('calculateShippingCost', (weight, destination) => {
|
|
91
|
+
const baseCost = 5.99;
|
|
92
|
+
const zoneRate = destination === 'international' ? 2.5 : 1;
|
|
93
|
+
return baseCost + 0.1 * weight * zoneRate;
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
// Check if a method is exposed
|
|
97
|
+
const isExposed = bridge.isMethodExposed('calculateShippingCost'); // true
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
This allows the host environment to directly use functionality implemented in your app.
|
|
101
|
+
|
|
102
|
+
### Calling Host Methods
|
|
103
|
+
|
|
104
|
+
The `callMethod` function allows your app to invoke functions provided by the host environment.
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
// Get user information
|
|
108
|
+
const userProfile = await appBridge.callMethod('getUserProfile');
|
|
109
|
+
|
|
110
|
+
// Call a method with parameters
|
|
111
|
+
const orderDetails = await appBridge.callMethod('getOrderDetails', 'ORD-5678');
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## 📚 API Reference
|
|
115
|
+
|
|
116
|
+
### PluginBridge
|
|
117
|
+
|
|
118
|
+
The main class that handles communication between the app and host, it provides the following methods.
|
|
119
|
+
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
#### `subscribe(event: string, callback: (data: unknown) => Promise<unknown>): void`
|
|
123
|
+
|
|
124
|
+
**Description:**
|
|
125
|
+
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
|
+
|
|
127
|
+
**Parameters:**
|
|
128
|
+
|
|
129
|
+
- `event` (string): The name of the event to listen for.
|
|
130
|
+
- `callback` (function): A function that handles the event data.
|
|
131
|
+
|
|
132
|
+
**Returns:** `void`
|
|
133
|
+
|
|
134
|
+
---
|
|
135
|
+
|
|
136
|
+
#### `publish<TPayload>(topic: string, payload: TPayload): Promise<void>`
|
|
137
|
+
|
|
138
|
+
**Description:**
|
|
139
|
+
Sends data to the host under a specific topic, typically to notify about an action or a state change.
|
|
140
|
+
|
|
141
|
+
**Parameters:**
|
|
142
|
+
|
|
143
|
+
- `topic` (string): The topic name under which the data should be published.
|
|
144
|
+
- `payload` (`TPayload`): The data to be sent to the host.
|
|
145
|
+
|
|
146
|
+
**Returns:** `Promise<void>`
|
|
147
|
+
|
|
148
|
+
---
|
|
149
|
+
|
|
150
|
+
#### `exposeMethod<TMethod>(methodName: string, method: TMethod): void`
|
|
151
|
+
|
|
152
|
+
**Description:**
|
|
153
|
+
Makes a app method available for the host to call directly.
|
|
154
|
+
|
|
155
|
+
**Parameters:**
|
|
156
|
+
|
|
157
|
+
- `methodName` (string): The name under which the method should be exposed.
|
|
158
|
+
- `method` (`TMethod`): The actual method implementation.
|
|
159
|
+
|
|
160
|
+
**Returns:** `void`
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
#### `isMethodExposed(methodName: string): boolean`
|
|
165
|
+
|
|
166
|
+
**Description:**
|
|
167
|
+
Checks whether a method with the given name has already been exposed to the host.
|
|
168
|
+
|
|
169
|
+
**Parameters:**
|
|
170
|
+
|
|
171
|
+
- `methodName` (string): The name of the method to check.
|
|
172
|
+
|
|
173
|
+
**Returns:** `boolean`
|
|
174
|
+
|
|
175
|
+
---
|
|
176
|
+
|
|
177
|
+
#### `callMethod<TResponse>(methodName: string, ...args: unknown[]): Promise<TResponse>`
|
|
178
|
+
|
|
179
|
+
**Description:**
|
|
180
|
+
Calls a method provided by the host environment and returns the result.
|
|
181
|
+
|
|
182
|
+
**Parameters:**
|
|
183
|
+
|
|
184
|
+
- `methodName` (string): The name of the host method to call.
|
|
185
|
+
- `...args` (unknown[]): The arguments to pass to the host method.
|
|
186
|
+
|
|
187
|
+
**Returns:** `Promise<TResponse>`
|
|
188
|
+
|
|
189
|
+
### Factories
|
|
190
|
+
|
|
191
|
+
- `createPluginBridge()`: Creates and initializes a PluginBridge instance
|
|
192
|
+
|
|
193
|
+
## 📦 Dependencies
|
|
194
|
+
|
|
195
|
+
This package has minimal dependencies:
|
|
196
|
+
|
|
197
|
+
- `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