@kemu-io/hs-react 0.2.36 → 0.2.37

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.
Files changed (37) hide show
  1. package/cjs/WidgetWrapper.d.ts +0 -2
  2. package/cjs/components/ResizableContainer.d.ts +71 -0
  3. package/cjs/components/WidgetContainer.d.ts +20 -0
  4. package/cjs/components/index.d.ts +85 -0
  5. package/cjs/constants.d.ts +14 -0
  6. package/cjs/hooks/index.d.ts +695 -0
  7. package/cjs/hooks/useOnBroadcastEvent.d.ts +552 -0
  8. package/cjs/hooks/useOnParentEvent.d.ts +545 -0
  9. package/cjs/hooks/useOnSetOutputsEvent.d.ts +554 -0
  10. package/cjs/hooks/useReactiveWidgetState.d.ts +46 -0
  11. package/cjs/hooks/useResizableWidgetDimensions.d.ts +39 -0
  12. package/cjs/hooks/useWidgetState.d.ts +46 -0
  13. package/cjs/lib/InstanceContext.d.ts +523 -0
  14. package/cjs/lib/cache.d.ts +14 -0
  15. package/cjs/lib/globalContext.d.ts +429 -0
  16. package/cjs/types/context_t.d.ts +427 -0
  17. package/cjs/types/hooks_t.d.ts +44 -0
  18. package/cjs/types/widgetUI_t.d.ts +519 -0
  19. package/mjs/WidgetWrapper.d.ts +0 -2
  20. package/mjs/components/ResizableContainer.d.ts +71 -0
  21. package/mjs/components/WidgetContainer.d.ts +20 -0
  22. package/mjs/components/index.d.ts +85 -0
  23. package/mjs/constants.d.ts +14 -0
  24. package/mjs/hooks/index.d.ts +695 -0
  25. package/mjs/hooks/useOnBroadcastEvent.d.ts +552 -0
  26. package/mjs/hooks/useOnParentEvent.d.ts +545 -0
  27. package/mjs/hooks/useOnSetOutputsEvent.d.ts +554 -0
  28. package/mjs/hooks/useReactiveWidgetState.d.ts +46 -0
  29. package/mjs/hooks/useResizableWidgetDimensions.d.ts +39 -0
  30. package/mjs/hooks/useWidgetState.d.ts +46 -0
  31. package/mjs/lib/InstanceContext.d.ts +523 -0
  32. package/mjs/lib/cache.d.ts +14 -0
  33. package/mjs/lib/globalContext.d.ts +429 -0
  34. package/mjs/types/context_t.d.ts +427 -0
  35. package/mjs/types/hooks_t.d.ts +44 -0
  36. package/mjs/types/widgetUI_t.d.ts +519 -0
  37. package/package.json +1 -1
@@ -0,0 +1,427 @@
1
+ export type Rect = {
2
+ width: number;
3
+ height: number;
4
+ top: number;
5
+ left: number;
6
+ };
7
+ export type Point = {
8
+ x: number;
9
+ y: number;
10
+ };
11
+ export type BinaryFile<D extends ArrayBuffer | Uint8Array | Uint8ClampedArray | Buffer = ArrayBuffer> = {
12
+ format: string;
13
+ data: D;
14
+ };
15
+ declare enum DataType {
16
+ Number = 0,
17
+ String = 1,
18
+ ArrayBuffer = 2,
19
+ Array = 3,
20
+ Boolean = 4,
21
+ /** a javascript object */
22
+ JsonObj = 5,
23
+ /**
24
+ * Does not care; mostly used by actions to indicate they accept anything since they probably won't use the
25
+ * value.
26
+ **/
27
+ Anything = 6,
28
+ /** Raw image data, produced in browser environments */
29
+ ImageData = 7,
30
+ /** Raw audio data fraction */
31
+ AudioBuffer = 8,
32
+ Rect = 9,
33
+ Point = 10,
34
+ ImageBitmap = 11,
35
+ /**
36
+ * Custom binary data. Use the `format` property to specify the type of data, i.e. 'image/png', 'audio/mp3', etc.
37
+ **/
38
+ BinaryFile = 12
39
+ }
40
+ export type JsonType = {
41
+ [key: string]: SupportedTypes;
42
+ };
43
+ export type KemuType = number | string | ArrayBuffer | JsonType | boolean | ImageData | AudioBuffer | Rect | Point | ImageBitmap | BinaryFile;
44
+ export type SupportedTypes = KemuType | KemuType[];
45
+ /**
46
+ * Describe the shape of objects. For example:
47
+ *
48
+ * Given the following object:
49
+ * ```
50
+ * const obj = {
51
+ * property1: 12,
52
+ * property2: 'Hello'
53
+ * };
54
+ * ```
55
+ *
56
+ * The shape definition would be:
57
+ * ```
58
+ * {
59
+ * 'property1': DataType.Number,
60
+ * 'property2': DataType.String,
61
+ * }
62
+ * ```
63
+ *
64
+ * Use a JsonPrimitiveFormat (`$$primitive_{x}`) to specify the type of an array of primitive objects,
65
+ * where 'x' is the index in the array the value of the property belongs to. For example:
66
+ *
67
+ * An event with `[1, 'hi', {other: 'yes}]` would produce the following jsonShape:
68
+ *
69
+ * ```
70
+ * {
71
+ * '$$primitive_0': DataType.Number,
72
+ * '$$primitive_1': DataType.String,
73
+ * '$$primitive_2': DataType.JsonObj,
74
+ * }
75
+ * ```
76
+ *
77
+ * If no primitive index is specified, the system assumes all the items in the array
78
+ * will have the given primitive type. For example:
79
+ *
80
+ * An array of `[1, 2, 3, 4]` should be described as:
81
+ * ```
82
+ * {
83
+ * '$$primitive': DataType.Number
84
+ * }
85
+ * ```
86
+ */
87
+ export type JsonTypeShape = {
88
+ [key: string]: DataType;
89
+ };
90
+ /**
91
+ * Used by Widget (formerly known as Gates) modules when defining their
92
+ * input/output port list
93
+ */
94
+ export type WidgetPort = {
95
+ /** a unique identifier (in the context of the widget) for this port. */
96
+ name: string;
97
+ /** alternative label used to replace the name for presentation purposes only */
98
+ label?: string;
99
+ /** Type of data the port accepts or produces, an array indicates the gate can receive multiple types */
100
+ type: DataType | DataType[];
101
+ /**
102
+ * If true, processors won't be invoked until at least an event is received in this port.
103
+ */
104
+ required?: boolean;
105
+ /**
106
+ * Describes the shape of a JSON object when dataType == JsonObj. Or the shape of items
107
+ * when dataType == Array
108
+ */
109
+ jsonShape?: JsonTypeShape;
110
+ /** help description markdown */
111
+ description?: string;
112
+ };
113
+ export type WidgetState<T extends Record<string, any> = Record<string, unknown>> = T;
114
+ export type TargetOutput = {
115
+ /**
116
+ * the name of the port the `data` will be sent from.
117
+ * This name MUST match one of the ports
118
+ * returned by the `getOutputs` function or defined in the widget
119
+ * manifest. If the name does not match, the data will not be sent.
120
+ **/
121
+ name: string;
122
+ /**
123
+ * The data to send to the output port.
124
+ * If the value is `undefined` or `null`, no data will be sent.
125
+ * Also, the data type MUST match the type defined in the widget manifest.
126
+ */
127
+ value: SupportedTypes | undefined | null;
128
+ /**
129
+ * the data type of the value being sent.
130
+ * This value is automatically inferred from the widget manifest.
131
+ **/
132
+ type: DataType;
133
+ /** id of the variant that defined the output */
134
+ variantId?: string;
135
+ };
136
+ export type WidgetPortStr = Omit<WidgetPort, "type"> & {
137
+ type: string;
138
+ };
139
+ declare enum ProcessorType {
140
+ Javascript = "js",
141
+ Python = "py",
142
+ Executable = "exe"
143
+ }
144
+ export type ServiceSecret = {
145
+ /** a markdown description of the secret */
146
+ description: string;
147
+ /** if true, the secret is optional and the service will still start if the secret is not provided */
148
+ optional?: boolean;
149
+ };
150
+ export type ServiceWidgetVariant<P extends WidgetPort | WidgetPortStr> = {
151
+ /** unique identifier for the variant */
152
+ id: string;
153
+ name: string;
154
+ description: string;
155
+ /**
156
+ * custom color for the variant. If not provided, the main
157
+ * color defined in the manifest will be used.
158
+ **/
159
+ color?: string;
160
+ /** SVG icon data */
161
+ svgIcon?: string;
162
+ inputs?: P[];
163
+ outputs?: P[];
164
+ };
165
+ export type ServiceManifest<P extends WidgetPort | WidgetPortStr> = {
166
+ /** the unique name defined by the service */
167
+ name: string;
168
+ /** a unique identifier given during startup */
169
+ version: string;
170
+ /** a service title for UI purposes */
171
+ title?: string;
172
+ /** a shorten title to be used in the canvas */
173
+ shortTitle?: string;
174
+ description: string;
175
+ processor: ProcessorType;
176
+ /**
177
+ * if true, it indicates other services can subscribe
178
+ * to events emitted by this service. Only services
179
+ * with `eventEmitter` set to true can use `broadcast`
180
+ * to emit events.
181
+ */
182
+ eventEmitter?: boolean;
183
+ /**
184
+ * Specifies the default timeout in seconds when a parentEvent is received.
185
+ * This is used to discard events that take too long to process.
186
+ * Set to 0 to disable the timeout. Warning this may
187
+ * cause some parent widgets to hang indefinitely if the child
188
+ * widget does not respond to the event.
189
+ **/
190
+ processingTimeoutSec?: number;
191
+ /**
192
+ * if true, parent events sent to this widget will
193
+ * be resolved immediately, meaning the parent widget
194
+ * will not have to wait for this service to respond
195
+ * before continuing execution.
196
+ */
197
+ asyncParentEvents?: boolean;
198
+ /**
199
+ * if true, it indicates the service is only meant to be used in a
200
+ * web environment. The widget won't work when exported.
201
+ */
202
+ webOnly?: boolean;
203
+ /**
204
+ * if set the service will only receive env vars that start with this prefix.
205
+ * Use it to isolate the information services have access to.
206
+ * Eg:
207
+ * - `KEMU_{CUSTOM_PREFIX}_DB_URL`: Your service will receive this env var as `DB_URL`
208
+ *
209
+ * Remember that only variables that start with `KEMU_` are forwarded to services.
210
+ */
211
+ envVarPrefix?: string;
212
+ color?: string;
213
+ svgIcon?: string;
214
+ /**
215
+ * Flags this service as internal. Internal services are not
216
+ * meant to be used by the end-user. Attempts to use an internal service
217
+ * will likely result in an error.
218
+ */
219
+ internal?: boolean;
220
+ /**
221
+ * if true, the service is expected to have a `widgetUI.js` file in the root directory
222
+ * that will be used to mount a new UI onto the canvas.
223
+ **/
224
+ widgetUI?: boolean;
225
+ /**
226
+ * If true, the processor won't be forwarded any parent events.
227
+ * Use it when you have a widget with custom UI that performs all processing
228
+ * inside of its own component. Keep in mind that when exported, any processing
229
+ * logic that does not exist in the processor will not be available in the exported
230
+ * recipe.
231
+ */
232
+ ignoreParentEvents?: boolean;
233
+ /**
234
+ * A list of relative paths to `txt` files where custom widget clipboard data are stored. Eg:
235
+ * ```json
236
+ * {
237
+ * "customWidgets": [
238
+ * "customWidgets/widget1.txt",
239
+ * "customWidgets/widget2.txt",
240
+ * ]
241
+ * }
242
+ * ```
243
+ * This will cause the service to be rendered as a folder with the custom widgets as sub-widgets.
244
+ */
245
+ customWidgets?: string[];
246
+ /**
247
+ * A list of widget variants that can be selected by the user.
248
+ * All variants will invoke the same processor, but they can have different input/output ports
249
+ * and be visually distinct.
250
+ */
251
+ variants?: ServiceWidgetVariant<P>[];
252
+ /**
253
+ * A list of relative paths to folders containing extra services that are part of this service. Eg:
254
+ * ```json
255
+ * {
256
+ * "subServices": [
257
+ * "subServices/subService1",
258
+ * "subServices/subService2",
259
+ * ]
260
+ * }
261
+ * ```
262
+ *
263
+ * Each folder should contain all the required files that represent a service (manifest, processor, etc).
264
+ */
265
+ subServices?: string[];
266
+ /** the list of input ports. If not provided,
267
+ * `dynamicInputs` MUST be set to true, or loading the
268
+ * widget will fail.
269
+ */
270
+ inputs: P[];
271
+ /**
272
+ * the list of output ports. If not provided,
273
+ * `dynamicOutputs` MUST be set to true, or loading the
274
+ * widget will fail.
275
+ */
276
+ outputs: P[];
277
+ /**
278
+ * A map of secret names to their descriptions
279
+ */
280
+ requiredSecrets?: {
281
+ [name: string]: ServiceSecret;
282
+ };
283
+ };
284
+ export type ServiceWidgetInfo = {
285
+ name: string;
286
+ icon?: string;
287
+ color?: string;
288
+ description: string;
289
+ contents: string;
290
+ /**
291
+ * The id of the first root group in the widget.
292
+ * Keep in mind this id will be re-generated when the widget is added to the canvas.
293
+ */
294
+ rootGroupId: string;
295
+ protocolVersion: string;
296
+ };
297
+ /**
298
+ * This is the output of the service library after processing its own manifest file.
299
+ */
300
+ export type PartiallyProcessedManifest<B extends Buffer | ArrayBuffer = Buffer, SM extends WidgetPort | WidgetPortStr = WidgetPortStr> = ServiceManifest<SM> & {
301
+ widgetUIContents?: B;
302
+ uiContentChecksum?: string;
303
+ /** path to the root directory where the service resides */
304
+ path: string;
305
+ /** added by the service automatically when the service is running in dev mode */
306
+ readonly devMode?: boolean;
307
+ };
308
+ /**
309
+ * This is the output of the Hub after combining a PartiallyProcessedManifest and processing variants, sub-services and custom widgets.
310
+ */
311
+ export type ProcessedManifest<B extends Buffer | ArrayBuffer = Buffer> = Omit<PartiallyProcessedManifest<B, WidgetPort>, "customWidgets"> & {
312
+ customWidgets?: ServiceWidgetInfo[];
313
+ /** if present, it indicates this service is a sub-service of another service */
314
+ parentService?: {
315
+ name: string;
316
+ version: string;
317
+ };
318
+ };
319
+ export type RequireAtLeastOne<T, Keys extends keyof T = keyof T> = Pick<T, Exclude<keyof T, Keys>> & {
320
+ [K in Keys]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<Keys, K>>>;
321
+ }[Keys];
322
+ export type DefineDynamicPortsArgs = RequireAtLeastOne<{
323
+ /**
324
+ * Set to null to remove any previously defined ports or
325
+ * set to an empty array to remove ports from the widget.
326
+ */
327
+ outputs?: WidgetPort[] | null;
328
+ /**
329
+ * Set to null to remove any previously defined ports or
330
+ * set to an empty array to remove ports from the widget.
331
+ */
332
+ inputs?: WidgetPort[] | null;
333
+ }, "outputs" | "inputs"> & {
334
+ /** the id of the variant this definition applies to */
335
+ variantId?: string;
336
+ };
337
+ export type UseWidgetStateResponse<T extends WidgetState = WidgetState> = {
338
+ /**
339
+ * the current state of the widget at the time the hook was created.
340
+ * This state is not reactive and will not trigger a re-render when the state changes.
341
+ **/
342
+ state: Readonly<T>;
343
+ /**
344
+ * @returns the current state of the widget. This function is memoized and will always return the latest state.
345
+ */
346
+ getState: () => Readonly<T>;
347
+ /**
348
+ * Updates the state of the widget.
349
+ * @param newState the new state to set.
350
+ * @param triggerEvents if true, the widget will notify listeners that its state has changed. Default is true.
351
+ */
352
+ setState: (newState: T, triggerEvents?: boolean) => void;
353
+ };
354
+ /**
355
+ * @returns the current state and a memoized function to get the current state.
356
+ */
357
+ export type UseWidgetStateHook<T extends WidgetState = WidgetState> = () => UseWidgetStateResponse<T>;
358
+ export type LimitedManifest = Omit<ProcessedManifest, "widgetUIContents" | "settingsUIContents" | "path">;
359
+ export type ReactWidgetUtils = {
360
+ /**
361
+ * Shows a native dialog to choose a file.
362
+ * @returns a promise that resolves to the paths of the chosen files.
363
+ **/
364
+ showChooseFileDialog: (args: {
365
+ title?: string;
366
+ fileTypes?: string[];
367
+ allowMultiple?: boolean;
368
+ }) => Promise<string[]>;
369
+ /**
370
+ * Shows a native dialog to choose a directory.
371
+ * @returns a promise that resolves to the path of the chosen directory.
372
+ */
373
+ showChooseDirectoryDialog: (args: {
374
+ title?: string;
375
+ }) => Promise<string>;
376
+ browser: {
377
+ /**
378
+ * @returns the Cache Storage path for the given file
379
+ **/
380
+ getCacheFilePath: (fileName: string) => string;
381
+ /**
382
+ * Adds the given data to the service cache
383
+ * @param filePath the full path of the file, including the file name and extension
384
+ * @param data the raw data to store
385
+ * @param headers optional response headers
386
+ * @returns
387
+ */
388
+ cacheFile: (filePath: string, data: string | ArrayBuffer | Uint8Array | Uint8ClampedArray | Blob, headers?: Headers) => Promise<void>;
389
+ /**
390
+ * Attempts to load the file from the cache storage
391
+ * @param filePath full path of the file, including the file name and extension
392
+ * @returns the cached file or undefined if the file is not found
393
+ */
394
+ getCachedFile: (filePath: string) => Promise<Response | undefined>;
395
+ /**
396
+ * Removes the file from the cache storage. If path does not exist, nothing happens.
397
+ * @param filePath full path of the file, including the file name and extension
398
+ */
399
+ removeCachedFile: (filePath: string) => Promise<void>;
400
+ /**
401
+ * Removes all entries linked to this service
402
+ */
403
+ clearServiceCache: () => Promise<void>;
404
+ };
405
+ };
406
+ export type WidgetCanvasDimensions = {
407
+ width: number;
408
+ height: number;
409
+ };
410
+ export type Output = Omit<TargetOutput, "value"> & {
411
+ value: SupportedTypes;
412
+ };
413
+ export type GlobalContext<T extends WidgetState = WidgetState> = {
414
+ useWidgetState: UseWidgetStateHook<T>;
415
+ setOutputs: (outputs: Output[]) => Promise<void>;
416
+ defineDynamicPorts: (config: DefineDynamicPortsArgs) => Promise<void>;
417
+ callProcessorHandler: <R = any>(name: string, data?: any) => Promise<R>;
418
+ setWidgetDimensions: (width: number, height: number) => void;
419
+ getWidgetDimensions: () => WidgetCanvasDimensions;
420
+ utils: ReactWidgetUtils;
421
+ widgetId: string;
422
+ recipeId: string;
423
+ variantId?: string;
424
+ manifest: LimitedManifest;
425
+ };
426
+
427
+ export {};
@@ -0,0 +1,44 @@
1
+ export type WidgetState<T extends Record<string, any> = Record<string, unknown>> = T;
2
+ export type GetStateFunction<T extends WidgetState = WidgetState> = ((currentState: T) => T | Promise<T>);
3
+ export type NewStateOrFunction<T extends WidgetState = WidgetState> = (T) | GetStateFunction<T>;
4
+ /**
5
+ * A function that updates the widget state.
6
+ * @param newState the new state to set or a function that returns the new state.
7
+ * @param triggerEvents if true, the widget will notify listeners that its state has changed. Default is true.
8
+ */
9
+ export type SetStateFun<T extends WidgetState = WidgetState> = (newState: NewStateOrFunction<T>, triggerEvents?: boolean) => Promise<void>;
10
+ export type ReactiveWidgetStateResponse<T extends WidgetState = WidgetState> = [
11
+ currentState: Readonly<T>,
12
+ setState: SetStateFun<T>
13
+ ];
14
+ export type CompareFunction<T extends WidgetState> = (prevState: Readonly<T>, newState: Readonly<T>) => boolean | Promise<boolean>;
15
+ /**
16
+ * A hook that reacts to widget changes and provides a setState function to update the widget state.
17
+ * @param compareFun a function to call when a new state is available. The function
18
+ * should return true if the new state needs to be updated, otherwise the new state
19
+ * will be ignored.
20
+ */
21
+ export type UseReactiveWidgetStateHook<T extends WidgetState = WidgetState> = (defaultState?: T) => ReactiveWidgetStateResponse<T>;
22
+ export type UseWidgetStateResponse<T extends WidgetState = WidgetState> = {
23
+ /**
24
+ * the current state of the widget at the time the hook was created.
25
+ * This state is not reactive and will not trigger a re-render when the state changes.
26
+ **/
27
+ state: Readonly<T>;
28
+ /**
29
+ * @returns the current state of the widget. This function is memoized and will always return the latest state.
30
+ */
31
+ getState: () => Readonly<T>;
32
+ /**
33
+ * Updates the state of the widget.
34
+ * @param newState the new state to set.
35
+ * @param triggerEvents if true, the widget will notify listeners that its state has changed. Default is true.
36
+ */
37
+ setState: (newState: T, triggerEvents?: boolean) => void;
38
+ };
39
+ /**
40
+ * @returns the current state and a memoized function to get the current state.
41
+ */
42
+ export type UseWidgetStateHook<T extends WidgetState = WidgetState> = () => UseWidgetStateResponse<T>;
43
+
44
+ export {};