@schoolpalm/message-bridge 0.1.0
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/AUTHORS +7 -0
- package/LICENSE +21 -0
- package/README.md +198 -0
- package/TODO.md +13 -0
- package/dist/__tests__/bridge.test.d.ts +6 -0
- package/dist/__tests__/bridge.test.d.ts.map +1 -0
- package/dist/__tests__/bridge.test.js +84 -0
- package/dist/__tests__/bridge.test.js.map +1 -0
- package/dist/bridgeBase.d.ts +70 -0
- package/dist/bridgeBase.d.ts.map +1 -0
- package/dist/bridgeBase.js +76 -0
- package/dist/bridgeBase.js.map +1 -0
- package/dist/hostBridge.d.ts +52 -0
- package/dist/hostBridge.d.ts.map +1 -0
- package/dist/hostBridge.js +59 -0
- package/dist/hostBridge.js.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/messageTypes.d.ts +38 -0
- package/dist/messageTypes.d.ts.map +1 -0
- package/dist/messageTypes.js +40 -0
- package/dist/messageTypes.js.map +1 -0
- package/dist/moduleBridge.d.ts +61 -0
- package/dist/moduleBridge.d.ts.map +1 -0
- package/dist/moduleBridge.js +70 -0
- package/dist/moduleBridge.js.map +1 -0
- package/dist/payloadSchemas.d.ts +97 -0
- package/dist/payloadSchemas.d.ts.map +1 -0
- package/dist/payloadSchemas.js +6 -0
- package/dist/payloadSchemas.js.map +1 -0
- package/package.json +36 -0
- package/src/__tests__/bridge.test.ts +100 -0
- package/src/bridgeBase.ts +93 -0
- package/src/hostBridge.ts +63 -0
- package/src/index.ts +20 -0
- package/src/messageTypes.ts +40 -0
- package/src/moduleBridge.ts +79 -0
- package/src/payloadSchemas.ts +115 -0
- package/tsconfig.json +18 -0
- package/vitest.config.ts +8 -0
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Message type definitions for bridge communication.
|
|
3
|
+
* @module @schoolpalm/message-bridge/messageTypes
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// src/messageTypes.ts
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Enumeration of all allowed message types for Host ↔ Module communication.
|
|
10
|
+
*
|
|
11
|
+
* These message types define the protocol for communication between the host
|
|
12
|
+
* application and embedded modules. Each type indicates the direction and
|
|
13
|
+
* purpose of the message.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* import { MessageType } from '@schoolpalm/message-bridge';
|
|
18
|
+
*
|
|
19
|
+
* // Listen for UI updates from modules
|
|
20
|
+
* bridge.on(MessageType.UI_UPDATE, (payload) => {
|
|
21
|
+
* console.log('UI updated:', payload);
|
|
22
|
+
* });
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export enum MessageType {
|
|
26
|
+
/** Module → Host: Indicates the module is ready for communication. */
|
|
27
|
+
HANDSHAKE_READY = 'handshake:ready',
|
|
28
|
+
/** Host → Module: Signals the module to start with provided context. */
|
|
29
|
+
MODULE_START = 'module:start',
|
|
30
|
+
/** Module → Host: Updates the host UI (title, breadcrumb, theme). */
|
|
31
|
+
UI_UPDATE = 'ui:update',
|
|
32
|
+
/** Module → Host: Requests data from the host. */
|
|
33
|
+
DATA_REQUEST = 'data:request',
|
|
34
|
+
/** Host → Module: Responds to a data request. */
|
|
35
|
+
DATA_RESPONSE = 'data:response',
|
|
36
|
+
/** Module → Host: Reports an error to the host. */
|
|
37
|
+
ERROR = 'error',
|
|
38
|
+
/** Host → Module: Signals the module to exit and clean up. */
|
|
39
|
+
MODULE_EXIT = 'module:exit'
|
|
40
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Module-side bridge for communication with the host application.
|
|
3
|
+
* @module @schoolpalm/message-bridge/moduleBridge
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// src/moduleBridge.ts
|
|
7
|
+
import { BridgeBase } from './bridgeBase';
|
|
8
|
+
import { MessageType } from './messageTypes';
|
|
9
|
+
import {
|
|
10
|
+
HandshakeReadyPayload,
|
|
11
|
+
UIUpdatePayload,
|
|
12
|
+
ErrorPayload
|
|
13
|
+
} from './payloadSchemas';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Module-side bridge for communication with the host application.
|
|
17
|
+
*
|
|
18
|
+
* This class extends BridgeBase to provide module-specific functionality for
|
|
19
|
+
* communicating with the parent host application. It handles handshake,
|
|
20
|
+
* UI updates, and error reporting from the embedded module.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* const moduleBridge = new ModuleBridge('https://host.example.com');
|
|
25
|
+
*
|
|
26
|
+
* // Send handshake when module is ready
|
|
27
|
+
* moduleBridge.sendHandshake({
|
|
28
|
+
* version: '1.0.0',
|
|
29
|
+
* timestamp: Date.now()
|
|
30
|
+
* });
|
|
31
|
+
*
|
|
32
|
+
* // Update UI in the host
|
|
33
|
+
* moduleBridge.sendUIUpdate({
|
|
34
|
+
* title: 'Dashboard',
|
|
35
|
+
* breadcrumb: ['Home', 'Dashboard'],
|
|
36
|
+
* theme: 'dark'
|
|
37
|
+
* });
|
|
38
|
+
*
|
|
39
|
+
* // Listen for module start from host
|
|
40
|
+
* moduleBridge.on(MessageType.MODULE_START, (payload) => {
|
|
41
|
+
* console.log('Module started:', payload.route);
|
|
42
|
+
* });
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export class ModuleBridge extends BridgeBase {
|
|
46
|
+
/**
|
|
47
|
+
* Creates a new ModuleBridge instance.
|
|
48
|
+
* @param targetOrigin - The origin to restrict messages to (default: '*').
|
|
49
|
+
*/
|
|
50
|
+
constructor(targetOrigin: string = '*') {
|
|
51
|
+
super(window.parent, targetOrigin);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Notifies the host application that the module is ready.
|
|
56
|
+
* This should be called after the module has initialized.
|
|
57
|
+
* @param payload - The handshake payload containing version and timestamp.
|
|
58
|
+
*/
|
|
59
|
+
sendHandshake(payload: HandshakeReadyPayload) {
|
|
60
|
+
this.send(MessageType.HANDSHAKE_READY, payload);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Notifies the host application of UI updates.
|
|
65
|
+
* This includes changes to title, breadcrumb, and theme.
|
|
66
|
+
* @param payload - The UI update payload.
|
|
67
|
+
*/
|
|
68
|
+
sendUIUpdate(payload: UIUpdatePayload) {
|
|
69
|
+
this.send(MessageType.UI_UPDATE, payload);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Notifies the host application of an error.
|
|
74
|
+
* @param payload - The error payload containing error details.
|
|
75
|
+
*/
|
|
76
|
+
sendError(payload: ErrorPayload) {
|
|
77
|
+
this.send(MessageType.ERROR, payload);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Payload schema definitions for message-based communication.
|
|
3
|
+
* @module @schoolpalm/message-bridge/payloadSchemas
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// src/payloadSchemas.ts
|
|
7
|
+
import { MessageType } from './messageTypes';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Payload interfaces for each message type in the bridge communication protocol.
|
|
11
|
+
*
|
|
12
|
+
* These interfaces define the structure of data exchanged between host applications
|
|
13
|
+
* and embedded modules. Each payload corresponds to a specific message type and
|
|
14
|
+
* contains the necessary information for that communication scenario.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Payload for handshake ready message.
|
|
19
|
+
* Sent by modules to indicate they are ready for communication.
|
|
20
|
+
*/
|
|
21
|
+
export interface HandshakeReadyPayload {
|
|
22
|
+
/** Version of the module or SDK */
|
|
23
|
+
version: string;
|
|
24
|
+
/** Timestamp when the handshake was initiated */
|
|
25
|
+
timestamp: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Payload for module start message.
|
|
30
|
+
* Sent by host to initialize a module with context.
|
|
31
|
+
*/
|
|
32
|
+
export interface ModuleStartPayload {
|
|
33
|
+
/** Route or path to navigate to in the module */
|
|
34
|
+
route: string;
|
|
35
|
+
/** Context data passed to the module (user info, permissions, etc.) */
|
|
36
|
+
context: Record<string, any>;
|
|
37
|
+
/** Timestamp when the module start was requested */
|
|
38
|
+
timestamp: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Payload for UI update message.
|
|
43
|
+
* Sent by modules to update the host application's UI.
|
|
44
|
+
*/
|
|
45
|
+
export interface UIUpdatePayload {
|
|
46
|
+
/** Title to display in the host UI */
|
|
47
|
+
title: string;
|
|
48
|
+
/** Breadcrumb navigation path */
|
|
49
|
+
breadcrumb: string[];
|
|
50
|
+
/** Optional theme to apply to the host UI */
|
|
51
|
+
theme?: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Payload for data request message.
|
|
56
|
+
* Sent by modules to request data from the host.
|
|
57
|
+
*/
|
|
58
|
+
export interface DataRequestPayload {
|
|
59
|
+
/** Unique identifier for tracking the request */
|
|
60
|
+
requestId: string;
|
|
61
|
+
/** Action or endpoint to request data from */
|
|
62
|
+
action: string;
|
|
63
|
+
/** Optional payload data for the request */
|
|
64
|
+
payload?: Record<string, any>;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Payload for data response message.
|
|
69
|
+
* Sent by host in response to data requests.
|
|
70
|
+
*/
|
|
71
|
+
export interface DataResponsePayload {
|
|
72
|
+
/** Unique identifier matching the original request */
|
|
73
|
+
requestId: string;
|
|
74
|
+
/** Status of the response */
|
|
75
|
+
status: 'success' | 'error';
|
|
76
|
+
/** Response data (present on success) */
|
|
77
|
+
payload?: any;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Payload for error message.
|
|
82
|
+
* Sent by modules to report errors to the host.
|
|
83
|
+
*/
|
|
84
|
+
export interface ErrorPayload {
|
|
85
|
+
/** Error code for categorization */
|
|
86
|
+
code: string;
|
|
87
|
+
/** Human-readable error message */
|
|
88
|
+
message: string;
|
|
89
|
+
/** Optional additional error details */
|
|
90
|
+
details?: any;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Payload for module exit message.
|
|
95
|
+
* Sent by host to signal module termination.
|
|
96
|
+
*/
|
|
97
|
+
export interface ModuleExitPayload {
|
|
98
|
+
/** Optional reason for the module exit */
|
|
99
|
+
reason?: string;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Union type representing all possible message payloads.
|
|
104
|
+
*
|
|
105
|
+
* This type is used internally for type safety when handling messages
|
|
106
|
+
* of any type in the bridge classes.
|
|
107
|
+
*/
|
|
108
|
+
export type MessagePayload =
|
|
109
|
+
| HandshakeReadyPayload
|
|
110
|
+
| ModuleStartPayload
|
|
111
|
+
| UIUpdatePayload
|
|
112
|
+
| DataRequestPayload
|
|
113
|
+
| DataResponsePayload
|
|
114
|
+
| ErrorPayload
|
|
115
|
+
| ModuleExitPayload;
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "node",
|
|
6
|
+
"outDir": "dist",
|
|
7
|
+
"rootDir": "src",
|
|
8
|
+
"declaration": true,
|
|
9
|
+
"declarationMap": true,
|
|
10
|
+
"sourceMap": true,
|
|
11
|
+
"strict": true,
|
|
12
|
+
"esModuleInterop": true,
|
|
13
|
+
"skipLibCheck": true,
|
|
14
|
+
"forceConsistentCasingInFileNames": true
|
|
15
|
+
},
|
|
16
|
+
"include": ["src/**/*"],
|
|
17
|
+
"exclude": ["node_modules", "dist"]
|
|
18
|
+
}
|