@telemetryos/root-sdk 1.0.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.
@@ -0,0 +1,45 @@
1
+ import { z } from 'zod';
2
+ import { clientMessageValidator } from './client-message-validator.js';
3
+ import { bridgeMessageValidator } from './bridge-message-validator.js';
4
+ /**
5
+ * Defines the structure of a client message as defined in the
6
+ * clientMessageValidator.
7
+ */
8
+ export type ClientMessage = z.infer<typeof clientMessageValidator>;
9
+ /**
10
+ * Defines the structure of a bridge message as defined in the
11
+ * bridgeMessageValidator.
12
+ */
13
+ export type BridgeMessage = z.infer<typeof bridgeMessageValidator>;
14
+ /**
15
+ * The Bridge class is provides a way for host applications to communicate with
16
+ * TelemetryOS applications. It listens for window message events and sends
17
+ * messages to all frames.
18
+ *
19
+ * SDK Clients will be listening for these messages, discriminating for messages
20
+ * specifically for them.
21
+ */
22
+ export declare class Bridge {
23
+ /**
24
+ * Must be provided you the host application when using the Bridge. This
25
+ * function will be called when a message is received. The message will be
26
+ * passed to the function.
27
+ */
28
+ onMessage?: (message: ClientMessage) => void;
29
+ _windowMessageHandler?: (event: MessageEvent) => void;
30
+ /**
31
+ * Binds the Bridge to the window message event. This will allow the Bridge
32
+ * to listen for messages from the host application.
33
+ */
34
+ bind(): void;
35
+ /**
36
+ * Unbinds the Bridge from the window message event. Useful if the host
37
+ * application no longer needs to communicate with SDK clients.
38
+ */
39
+ unbind(): void;
40
+ /**
41
+ * Sends a message to SDK clients.
42
+ * @param message The message to send.
43
+ */
44
+ send(message: BridgeMessage): void;
45
+ }
package/dist/bridge.js ADDED
@@ -0,0 +1,46 @@
1
+ import { z as e } from "./index-B98VDFRY.js";
2
+ const t = e.object({
3
+ telemetrySdkVersion: e.string(),
4
+ applicationName: e.string(),
5
+ name: e.string(),
6
+ data: e.any(),
7
+ responseName: e.string().optional(),
8
+ subscriptionName: e.string().optional(),
9
+ unsubscribeName: e.string().optional()
10
+ });
11
+ class d {
12
+ /**
13
+ * Binds the Bridge to the window message event. This will allow the Bridge
14
+ * to listen for messages from the host application.
15
+ */
16
+ bind() {
17
+ this._windowMessageHandler = (n) => {
18
+ var s;
19
+ if (n.source === window)
20
+ return;
21
+ const i = t.safeParse(n.data);
22
+ if (!i.success)
23
+ return;
24
+ const a = i.data;
25
+ (s = this.onMessage) === null || s === void 0 || s.call(this, a);
26
+ }, window.addEventListener("message", this._windowMessageHandler);
27
+ }
28
+ /**
29
+ * Unbinds the Bridge from the window message event. Useful if the host
30
+ * application no longer needs to communicate with SDK clients.
31
+ */
32
+ unbind() {
33
+ this._windowMessageHandler && window.removeEventListener("message", this._windowMessageHandler);
34
+ }
35
+ /**
36
+ * Sends a message to SDK clients.
37
+ * @param message The message to send.
38
+ */
39
+ send(n) {
40
+ for (let s = 0; s < window.frames.length; s += 1)
41
+ window.frames[s].postMessage(n, "*");
42
+ }
43
+ }
44
+ export {
45
+ d as Bridge
46
+ };
@@ -0,0 +1,12 @@
1
+ import { z } from 'zod';
2
+ import { clientMessageValidator } from './client-message-validator.js';
3
+ export type Message = z.infer<typeof clientMessageValidator>;
4
+ export declare function formatClientMessage(data: Message): {
5
+ telemetrySdkVersion: string;
6
+ applicationName: string;
7
+ name: string;
8
+ data?: any;
9
+ responseName?: string | undefined;
10
+ subscriptionName?: string | undefined;
11
+ unsubscribeName?: string | undefined;
12
+ };
@@ -0,0 +1,26 @@
1
+ import { z } from 'zod';
2
+ export declare const clientMessageValidator: z.ZodObject<{
3
+ telemetrySdkVersion: z.ZodString;
4
+ applicationName: z.ZodString;
5
+ name: z.ZodString;
6
+ data: z.ZodAny;
7
+ responseName: z.ZodOptional<z.ZodString>;
8
+ subscriptionName: z.ZodOptional<z.ZodString>;
9
+ unsubscribeName: z.ZodOptional<z.ZodString>;
10
+ }, "strip", z.ZodTypeAny, {
11
+ telemetrySdkVersion: string;
12
+ applicationName: string;
13
+ name: string;
14
+ data?: any;
15
+ responseName?: string | undefined;
16
+ subscriptionName?: string | undefined;
17
+ unsubscribeName?: string | undefined;
18
+ }, {
19
+ telemetrySdkVersion: string;
20
+ applicationName: string;
21
+ name: string;
22
+ data?: any;
23
+ responseName?: string | undefined;
24
+ subscriptionName?: string | undefined;
25
+ unsubscribeName?: string | undefined;
26
+ }>;
@@ -0,0 +1,293 @@
1
+ import { Applications } from './applications.js';
2
+ import { Media } from './media.js';
3
+ import { Store } from './store.js';
4
+ import { RootSettingsNavigation } from './root-settings-navigation.js';
5
+ import { Accounts } from './accounts.js';
6
+ import { Users } from './users.js';
7
+ /**
8
+ * The maximum time in milliseconds to wait for a response to a request call.
9
+ *
10
+ * When using the request() or subscribe() methods, if no response is received within
11
+ * this time period, the promise will reject with a timeout error. The default timeout
12
+ * is 30 seconds (30,000 milliseconds).
13
+ *
14
+ * This timeout helps prevent promises from hanging indefinitely if the TelemetryOS
15
+ * platform doesn't respond or if network connectivity is lost.
16
+ */
17
+ export declare const requestResponseTimeout: number;
18
+ /**
19
+ * A callback function type for handling messages received from the TelemetryOS platform.
20
+ *
21
+ * Message handlers are registered using the on(), once(), or subscribe() methods and are
22
+ * invoked when messages of the corresponding type are received. The handler receives
23
+ * the data payload from the message.
24
+ *
25
+ * @template D The type of data expected in the message
26
+ * @param data The data payload from the received message
27
+ */
28
+ export type MessageHandler<D> = (data: D) => void;
29
+ export type SubscriptionResult<D = void> = {
30
+ success: boolean;
31
+ data: D;
32
+ };
33
+ /**
34
+ * Client is the core class that powers communication with the TelemetryOS platform.
35
+ *
36
+ * This class establishes the communication channel with the TelemetryOS platform
37
+ * and provides messaging functionality. It manages message sending, request/response
38
+ * patterns, subscriptions, and event handling.
39
+ *
40
+ * For most use cases, you should use the convenience functions exported by the main module
41
+ * rather than creating your own Client instances.
42
+ *
43
+ * Advanced use cases might require direct usage of the Client class, such as
44
+ * when an application needs to manage multiple independent communication channels.
45
+ */
46
+ export declare class Client {
47
+ _applicationName: string;
48
+ _applicationId: string;
49
+ _onHandlers: Map<string, MessageHandler<any>[]>;
50
+ _onceHandlers: Map<string, MessageHandler<any>[]>;
51
+ _subscriptionNamesByHandler: Map<MessageHandler<any>, string>;
52
+ _subscriptionNamesBySubjectName: Map<string, string[]>;
53
+ _windowMessageHandler?: (event: MessageEvent) => void;
54
+ /**
55
+ * Creates a new Client instance for communicating with the TelemetryOS platform.
56
+ *
57
+ * Note that creating a Client instance alone is not sufficient to begin communication.
58
+ * You must also call the bind() method to initialize event listeners and extract
59
+ * the application ID from the URL.
60
+ *
61
+ * @param applicationName The name of your application - must match the 'name' property
62
+ * in your application's telemetry.config.json file
63
+ */
64
+ constructor(applicationName: string);
65
+ /**
66
+ * Provides access to the accounts API for retrieving TelemetryOS account information.
67
+ *
68
+ * This property returns a new Accounts instance that allows querying information
69
+ * about the current TelemetryOS account.
70
+ *
71
+ * NOTE: Most application developers should use the global accounts() function
72
+ * instead of accessing this property directly.
73
+ *
74
+ * @returns An Accounts instance bound to this client
75
+ */
76
+ get accounts(): Accounts;
77
+ /**
78
+ * Provides access to the users API for retrieving TelemetryOS user information.
79
+ *
80
+ * This property returns a new Users instance that allows querying information
81
+ * about the current TelemetryOS user.
82
+ *
83
+ * NOTE: Most application developers should use the global users() function
84
+ * instead of accessing this property directly.
85
+ *
86
+ * @returns A Users instance bound to this client
87
+ */
88
+ get users(): Users;
89
+ /**
90
+ * Provides access to the store API for data persistence with multiple storage scopes.
91
+ *
92
+ * This property returns a new Store instance that allows saving, retrieving, and
93
+ * subscribing to data changes across different scopes (global, local, deviceLocal, shared).
94
+ *
95
+ * NOTE: Most application developers should use the global store() function
96
+ * instead of accessing this property directly.
97
+ *
98
+ * @returns A Store instance bound to this client
99
+ */
100
+ get store(): Store;
101
+ /**
102
+ * Provides access to the applications API for discovering and embedding other TelemetryOS applications.
103
+ *
104
+ * This property returns a new Applications instance that allows querying for applications
105
+ * by name or mount point, and generating URLs for embedding applications in iframes.
106
+ *
107
+ * NOTE: Most application developers should use the global applications() function
108
+ * instead of accessing this property directly.
109
+ *
110
+ * @returns An Applications instance bound to this client
111
+ */
112
+ get applications(): Applications;
113
+ /**
114
+ * Provides access to the media API for working with content hosted on the TelemetryOS platform.
115
+ *
116
+ * This property returns a new Media instance that allows applications to browse and access
117
+ * media content uploaded to TelemetryOS. Applications can query folders, retrieve content,
118
+ * and access media files.
119
+ *
120
+ * NOTE: Most application developers should use the global media() function
121
+ * instead of accessing this property directly.
122
+ *
123
+ * @returns A Media instance bound to this client
124
+ */
125
+ get media(): Media;
126
+ /**
127
+ * Provides access to the root settings navigation API for TelemetryOS administration UI integration.
128
+ *
129
+ * NOTE: This API is not intended for most application developers. It is specifically designed
130
+ * for root applications that need to integrate with the TelemetryOS administration UI.
131
+ *
132
+ * This property returns a new RootSettingsNavigation instance that allows root applications
133
+ * to register sidebar navigation entries in the TelemetryOS administration UI.
134
+ *
135
+ * Most root application developers should use the global rootSettingsNavigation() function
136
+ * instead of accessing this property directly.
137
+ *
138
+ * @returns A RootSettingsNavigation instance bound to this client
139
+ * @throws {Error} If used by an application not mounted at the 'rootSettingsNavigation' mount point
140
+ */
141
+ get rootSettingsNavigation(): RootSettingsNavigation;
142
+ /**
143
+ * Initializes the client by setting up message listeners and extracting the application ID.
144
+ *
145
+ * This method must be called after creating a Client instance and before using any
146
+ * of its communication methods. It performs two important tasks:
147
+ *
148
+ * 1. Sets up an event listener for window 'message' events to receive messages
149
+ * from the TelemetryOS platform
150
+ * 2. Extracts the application ID from the URL's query parameters
151
+ *
152
+ * The application ID is used by several APIs, including the store's local and deviceLocal scopes.
153
+ *
154
+ * NOTE: Most application developers should use the global configure() function instead
155
+ * of creating and binding their own Client instances.
156
+ */
157
+ bind(): void;
158
+ /**
159
+ * Removes the message event listener and cleans up resources.
160
+ *
161
+ * Call this method when you're done with the client to prevent memory leaks
162
+ * and ensure proper cleanup. After calling unbind(), the client will no longer
163
+ * receive messages from the TelemetryOS platform.
164
+ *
165
+ * Note that this does not cancel any active subscriptions or server-side resources.
166
+ * You should explicitly unsubscribe from any subscriptions before unbinding.
167
+ *
168
+ * NOTE: Most application developers should use the global destroy() function instead
169
+ * of managing their own Client instances.
170
+ */
171
+ unbind(): void;
172
+ /**
173
+ * Sends a one-way message to the TelemetryOS platform.
174
+ *
175
+ * Use this method for fire-and-forget messages where no response is expected.
176
+ * The message is sent to the parent window using the postMessage API and includes
177
+ * metadata such as the SDK version and application name.
178
+ *
179
+ * NOTE: Most application developers should use the resource-specific APIs or the global
180
+ * send() function instead of using this method directly.
181
+ *
182
+ * @param name The name of the message type to send
183
+ * @param data The data payload to include with the message
184
+ */
185
+ send(name: string, data: any): void;
186
+ /**
187
+ * Sends a message to the TelemetryOS platform and waits for a response.
188
+ *
189
+ * This method implements a request-response pattern over the postMessage API.
190
+ * It generates a unique correlation ID and sets up a listener for the response.
191
+ * If no response is received within the timeout period (30 seconds by default),
192
+ * the promise will reject.
193
+ *
194
+ * NOTE: Most application developers should use the resource-specific APIs or the global
195
+ * request() function instead of using this method directly.
196
+ *
197
+ * @template D The expected type of the response data
198
+ * @param name The name of the message type (endpoint) to request
199
+ * @param data The data payload to include with the request
200
+ * @returns A promise that resolves with the response data when received
201
+ * @throws {Error} If the request times out
202
+ */
203
+ request<D>(name: string, data: any): Promise<Awaited<D>>;
204
+ /**
205
+ * Sets up a persistent subscription to messages from the TelemetryOS platform.
206
+ *
207
+ * This method sends an initial subscription message and registers a handler for
208
+ * a specific message type. The handler will be called each time a message with the
209
+ * matching subscription name is received. Unlike the on() method, this establishes
210
+ * a formal subscription that can be managed with unsubscribe().
211
+ *
212
+ * NOTE: Most application developers should use the resource-specific APIs or the global
213
+ * subscribe() function instead of using this method directly.
214
+ *
215
+ * @template D The type of response data in the subscription response
216
+ * @template S The expected type of the message data
217
+ * @param name The name of the subscription endpoint
218
+ * @param data data used in the subscription process
219
+ * @param handler The callback function that will be invoked when messages are received
220
+ * @returns A promise that resolves with the subscription result ({success: boolean})
221
+ * @throws {Error} If the subscription request times out
222
+ */
223
+ subscribe<S, D = void>(name: string, handler: MessageHandler<S>): Promise<SubscriptionResult<D>>;
224
+ subscribe<S, D = void>(name: string, data: any, handler: MessageHandler<S>): Promise<SubscriptionResult<D>>;
225
+ /**
226
+ * Cancels a subscription previously created with subscribe().
227
+ *
228
+ * This method removes a subscription and stops the handler from receiving messages.
229
+ * It sends an unsubscribe message to the platform to clean up server-side resources
230
+ * and removes the local message handler.
231
+ *
232
+ * NOTE: Most application developers should use the resource-specific APIs or the global
233
+ * unsubscribe() function instead of using this method directly.
234
+ *
235
+ * @template D The expected type of the unsubscribe response data.
236
+ * @param name The name of the subscription endpoint (same as used in subscribe)
237
+ * @param key The identifier or parameters for the subscription to cancel
238
+ * @param handler Optional. The specific handler to unsubscribe. If omitted, all handlers for this key will be unsubscribed.
239
+ * @returns A promise that resolves with the unsubscribe result ({success: boolean})
240
+ * @throws {Error} If the unsubscribe request times out
241
+ */
242
+ unsubscribe<D = void>(name: string, handler?: MessageHandler<any>): Promise<SubscriptionResult<D>>;
243
+ unsubscribe<D = void>(name: string, data: any, handler?: MessageHandler<any>): Promise<SubscriptionResult<D>>;
244
+ /**
245
+ * Registers a handler function for a specific message type.
246
+ *
247
+ * The handler will be called each time a message with the specified name is received.
248
+ * You can register multiple handlers for the same message type, and all will be executed
249
+ * when that message is received.
250
+ *
251
+ * Unlike subscribe(), this method only sets up a local event listener and doesn't
252
+ * notify the platform of your interest in a particular message type.
253
+ *
254
+ * NOTE: Most application developers should use the resource-specific APIs or the global
255
+ * on() function instead of using this method directly.
256
+ *
257
+ * @template T The expected type of the message data
258
+ * @param name The name of the message type to listen for
259
+ * @param handler The callback function to execute when messages are received
260
+ */
261
+ on<T>(name: string, handler: MessageHandler<T>): void;
262
+ /**
263
+ * Registers a one-time handler for a specific message type.
264
+ *
265
+ * Similar to the on() method, but the handler will be automatically removed
266
+ * after it is called once. This is useful for initialization events or operations
267
+ * that should only happen once in response to a particular message.
268
+ *
269
+ * NOTE: Most application developers should use the resource-specific APIs or the global
270
+ * once() function instead of using this method directly.
271
+ *
272
+ * @template T The expected type of the message data
273
+ * @param name The name of the message type to listen for
274
+ * @param handler The callback function to execute when the message is received
275
+ */
276
+ once<T>(name: string, handler: MessageHandler<T>): void;
277
+ /**
278
+ * Removes previously registered message handlers.
279
+ *
280
+ * Use this method to stop receiving messages of a specific type or to remove
281
+ * specific handler functions when they're no longer needed. This applies to
282
+ * handlers registered with both on() and once() methods.
283
+ *
284
+ * NOTE: Most application developers should use the resource-specific APIs or the global
285
+ * off() function instead of using this method directly.
286
+ *
287
+ * @template T The expected type of the message data
288
+ * @param name The name of the message type to stop listening for
289
+ * @param handler Optional. The specific handler function to remove. If omitted,
290
+ * all handlers for this message type will be removed.
291
+ */
292
+ off<T>(name: string, handler?: MessageHandler<T>): void;
293
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export declare function doAsync(fn: () => Promise<void>, onRejected?: (err: any) => void): void;
@@ -0,0 +1,10 @@
1
+ import { Client } from './index.js';
2
+ type ColorScheme = 'light' | 'dark' | 'system';
3
+ export declare class Environment {
4
+ _client: Client;
5
+ constructor(client: Client);
6
+ getColorScheme(): Promise<ColorScheme>;
7
+ subscribeColorScheme(handler: (colorScheme: ColorScheme) => void): void;
8
+ unsubscribeColorScheme(handler: (colorScheme: ColorScheme) => void): void;
9
+ }
10
+ export {};