@codenotch/codenotch.react 2.0.3 → 2.0.5
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/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +75 -46
- package/dist/models/Codenotch.d.ts +21 -5
- package/dist/models/Codenotch.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +82 -48
- package/src/models/Codenotch.ts +26 -5
package/dist/index.d.ts
CHANGED
|
@@ -21,7 +21,8 @@ declare const env: ICodenotchEnv;
|
|
|
21
21
|
* - anything else — copied as-is onto {@link env}.
|
|
22
22
|
*
|
|
23
23
|
* The UI theme is resolved from the `_theme` URL parameter, or from
|
|
24
|
-
* `prefers-color-scheme` as a fallback
|
|
24
|
+
* `prefers-color-scheme` as a fallback, and applied as the `light`/`dark`
|
|
25
|
+
* class on `<html>`.
|
|
25
26
|
*
|
|
26
27
|
* @param envVariables An object containing the Codenotch environment variables as key-value pairs.
|
|
27
28
|
* @example
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuC,KAAK,aAAa,EAAE,KAAK,cAAc,EAAE,KAAK,iBAAiB,EAAkB,KAAK,aAAa,EAAE,MAAM,OAAO,CAAC;AAGjK,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAoB,aAAa,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuC,KAAK,aAAa,EAAE,KAAK,cAAc,EAAE,KAAK,iBAAiB,EAAkB,KAAK,aAAa,EAAE,MAAM,OAAO,CAAC;AAGjK,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAoB,a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— copied as-is onto {@link env}.
|
|
77
77
|
*
|
|
78
78
|
* The UI theme is resolved from the `_theme` URL parameter, or from
|
|
79
|
-
* `prefers-color-scheme` as a fallback
|
|
79
|
+
* `prefers-color-scheme` as a fallback, and applied as the `light`/`dark`
|
|
80
|
+
* class on `<html>`.
|
|
80
81
|
*
|
|
81
82
|
* @param envVariables An object containing the Codenotch environment variables as key-value pairs.
|
|
82
83
|
* @example
|
|
@@ -134,6 +135,13 @@ function init(envVariables) {
|
|
|
134
135
|
else {
|
|
135
136
|
env.theme = 'light';
|
|
136
137
|
}
|
|
138
|
+
// Apply the resolved theme to <html> like setTheme does. Without this, getTheme()
|
|
139
|
+
// and the DOM disagree until the first setTheme call, which can then be a visual
|
|
140
|
+
// no-op (early return when env.theme already equals the requested theme).
|
|
141
|
+
if (env.theme) {
|
|
142
|
+
document.documentElement.classList.remove(env.theme === 'dark' ? 'light' : 'dark');
|
|
143
|
+
document.documentElement.classList.add(env.theme);
|
|
144
|
+
}
|
|
137
145
|
}
|
|
138
146
|
catch {
|
|
139
147
|
}
|
|
@@ -194,7 +202,10 @@ function createApi() {
|
|
|
194
202
|
}
|
|
195
203
|
return await response.json();
|
|
196
204
|
},
|
|
197
|
-
startProcess: async (
|
|
205
|
+
startProcess: async (p, inputs) => {
|
|
206
|
+
return api.startProcessFromId(p.id, inputs);
|
|
207
|
+
},
|
|
208
|
+
startProcessFromId: async (processId, inputs) => {
|
|
198
209
|
if (env.clusterUrl === undefined)
|
|
199
210
|
throw new Error(MISSING_CLUSTER_URL_ERROR);
|
|
200
211
|
if (env.serviceName === undefined)
|
|
@@ -239,54 +250,72 @@ function createApi() {
|
|
|
239
250
|
errorMessage: `Failed to start process: invalid response data`
|
|
240
251
|
};
|
|
241
252
|
}
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
"
|
|
251
|
-
|
|
252
|
-
|
|
253
|
+
try {
|
|
254
|
+
const getProcessResult = async () => {
|
|
255
|
+
// Instance get: http://127.0.0.1:5005/instances/826bd498-e410-4c48-b29a-e79a76b75ef1
|
|
256
|
+
// Token header: Codenotch-Instance-Token
|
|
257
|
+
/*
|
|
258
|
+
{
|
|
259
|
+
"id": "826bd498-e410-4c48-b29a-e79a76b75ef1",
|
|
260
|
+
"state": "SUCCESS",
|
|
261
|
+
"output": {
|
|
262
|
+
"result": "Hello, World!"
|
|
263
|
+
},
|
|
264
|
+
"errors": []
|
|
265
|
+
}
|
|
266
|
+
*/
|
|
267
|
+
const instanceResp = await fetch(`${env.clusterUrl}/instances/${instanceData.instanceId}`, {
|
|
268
|
+
method: 'GET',
|
|
269
|
+
headers: {
|
|
270
|
+
'Codenotch-Instance-Token': instanceData.instanceToken
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
const instanceDataResp = await instanceResp.json();
|
|
274
|
+
// Process success
|
|
275
|
+
if (instanceDataResp.state === 'SUCCESS') {
|
|
276
|
+
return instanceDataResp.output;
|
|
253
277
|
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
'Codenotch-Instance-Token': instanceData.instanceToken
|
|
278
|
+
// Process crash
|
|
279
|
+
if (instanceDataResp.state === 'ERROR') {
|
|
280
|
+
console.error("Codenotch startProcess: process failed", instanceDataResp.errors);
|
|
281
|
+
throw new Error(`Process failed: ${instanceDataResp.errors.map(e => `${e.code}: ${e.message}`).join('; ')}`);
|
|
259
282
|
}
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
return
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
};
|
|
283
|
+
if (instanceDataResp.state === 'WAITING') {
|
|
284
|
+
// Just continue polling
|
|
285
|
+
}
|
|
286
|
+
return undefined;
|
|
287
|
+
};
|
|
288
|
+
const timeout = 30000; // 30 seconds
|
|
289
|
+
const pollInterval = 500; // 0.5 second
|
|
290
|
+
const startTime = Date.now();
|
|
291
|
+
while (Date.now() - startTime < timeout) {
|
|
292
|
+
const result = await getProcessResult();
|
|
293
|
+
if (result !== undefined) {
|
|
294
|
+
return {
|
|
295
|
+
isError: false,
|
|
296
|
+
output: result,
|
|
297
|
+
processInstanceId: instanceData.instanceId,
|
|
298
|
+
errorMessage: ''
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
await new Promise(resolve => setTimeout(resolve, pollInterval));
|
|
280
302
|
}
|
|
281
|
-
|
|
303
|
+
console.warn(`Codenotch startProcess: process ${processId} timed out after ${timeout} ms`);
|
|
304
|
+
return {
|
|
305
|
+
isError: true,
|
|
306
|
+
output: undefined,
|
|
307
|
+
processInstanceId: instanceData.instanceId,
|
|
308
|
+
errorMessage: `Process timed out after ${timeout} ms`
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
catch (error) {
|
|
312
|
+
return {
|
|
313
|
+
isError: true,
|
|
314
|
+
output: undefined,
|
|
315
|
+
processInstanceId: instanceData.instanceId,
|
|
316
|
+
errorMessage: `Failed to get process result: ${error}`
|
|
317
|
+
};
|
|
282
318
|
}
|
|
283
|
-
console.warn(`Codenotch startProcess: process ${processId} timed out after ${timeout} ms`);
|
|
284
|
-
return {
|
|
285
|
-
isError: true,
|
|
286
|
-
output: undefined,
|
|
287
|
-
processInstanceId: instanceData.instanceId,
|
|
288
|
-
errorMessage: `Process timed out after ${timeout} ms`
|
|
289
|
-
};
|
|
290
319
|
},
|
|
291
320
|
getUrlParams: () => {
|
|
292
321
|
let result = {};
|
|
@@ -31,7 +31,7 @@ export interface ICodenotchEnv {
|
|
|
31
31
|
[key: string]: any;
|
|
32
32
|
}
|
|
33
33
|
/**
|
|
34
|
-
* Result of a
|
|
34
|
+
* Result of a process execution
|
|
35
35
|
*/
|
|
36
36
|
export interface IProcessResult<T = any> {
|
|
37
37
|
/** Unique id of this execution of the process. */
|
|
@@ -43,6 +43,23 @@ export interface IProcessResult<T = any> {
|
|
|
43
43
|
/** Error details when {@link isError} is `true`. */
|
|
44
44
|
errorMessage: string;
|
|
45
45
|
}
|
|
46
|
+
type Json = string | number | boolean | null | Json[] | {
|
|
47
|
+
[key: string]: Json;
|
|
48
|
+
};
|
|
49
|
+
type Serializable = Json | {
|
|
50
|
+
toJSON(): Json;
|
|
51
|
+
} | undefined | Serializable[] | {
|
|
52
|
+
[key: string]: Serializable;
|
|
53
|
+
};
|
|
54
|
+
type EventMap = Record<string, Serializable>;
|
|
55
|
+
export interface Process<In = unknown, Out = unknown, E extends EventMap = {}> {
|
|
56
|
+
readonly id: string;
|
|
57
|
+
readonly __types?: {
|
|
58
|
+
input: In;
|
|
59
|
+
output: Out;
|
|
60
|
+
events: E;
|
|
61
|
+
};
|
|
62
|
+
}
|
|
46
63
|
/**
|
|
47
64
|
* The Codenotch client API.
|
|
48
65
|
*
|
|
@@ -55,10 +72,8 @@ export interface IProcessResult<T = any> {
|
|
|
55
72
|
* listens to real-time signals and manages theme/language.
|
|
56
73
|
*/
|
|
57
74
|
export interface ICodenotchApi {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
*/
|
|
61
|
-
startProcess<TInput, TOutput>(processId: string, inputs: TInput): Promise<IProcessResult<TOutput>>;
|
|
75
|
+
startProcessFromId<TInput, TOutput>(processId: string, inputs: TInput): Promise<IProcessResult<TOutput>>;
|
|
76
|
+
startProcess<In, Out>(p: Process<In, Out, any>, inputs: In): Promise<IProcessResult<Out>>;
|
|
62
77
|
/** Runtime environment (cluster, service, language, theme dictionaries, manifests…). Populated by `init()`. */
|
|
63
78
|
readonly env: ICodenotchEnv;
|
|
64
79
|
/**
|
|
@@ -149,4 +164,5 @@ export interface ICodenotchDialog {
|
|
|
149
164
|
/** Close the dialog and remove it from the DOM. */
|
|
150
165
|
close: () => void;
|
|
151
166
|
}
|
|
167
|
+
export {};
|
|
152
168
|
//# sourceMappingURL=Codenotch.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Codenotch.d.ts","sourceRoot":"","sources":["../../src/models/Codenotch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AACvC,OAAO,EAAE,YAAY,EAAE,yBAAyB,EAAE,MAAM,2BAA2B,CAAC;AAGpF;;;;;GAKG;AACH,MAAM,WAAW,aAAa;IAC1B,uCAAuC;IACvC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,iGAAiG;IACjG,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,oHAAoH;IACpH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,iGAAiG;IACjG,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,yGAAyG;IACzG,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,2CAA2C;IAC3C,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,2CAA2C;IAC3C,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,2GAA2G;IAC3G,KAAK,CAAC,EAAE,OAAO,GAAG,MAAM,CAAC;IAEzB,gGAAgG;IAChG,WAAW,CAAC,EAAE,YAAY,CAAC;IAC3B,8EAA8E;IAC9E,eAAe,CAAC,EAAE,yBAAyB,CAAC;IAE5C,8DAA8D;IAC9D,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACtB;AAED;;GAEG;AACH,MAAM,WAAW,cAAc,CAAC,CAAC,GAAG,GAAG;IAEnC,kDAAkD;IAClD,iBAAiB,EAAE,MAAM,CAAC;IAC1B,iEAAiE;IACjE,MAAM,EAAE,CAAC,CAAC;IACV,oFAAoF;IACpF,OAAO,EAAE,OAAO,CAAC;IACjB,oDAAoD;IACpD,YAAY,EAAE,MAAM,CAAC;CACxB;
|
|
1
|
+
{"version":3,"file":"Coden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
2
|
"name": "@codenotch/codenotch.react",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.5",
|
|
4
4
|
"description": "React bindings for Codenotch applications: start BPMN processes, run cnql queries, listen to real-time signals, manage theme and language.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"codenotch",
|
package/src/index.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { createElement, useSyncExternalStore, type ComponentType, type Component
|
|
|
2
2
|
import { flushSync } from "react-dom";
|
|
3
3
|
import { createRoot, Root } from "react-dom/client";
|
|
4
4
|
import { IDisposable } from "./models/Misc";
|
|
5
|
-
import { ICodenotchApi, ICodenotchDialog, ICodenotchEnv, IProcessResult, WithCodenotchProps } from "./models/Codenotch";
|
|
5
|
+
import { ICodenotchApi, ICodenotchDialog, ICodenotchEnv, IProcessResult, Process, WithCodenotchProps } from "./models/Codenotch";
|
|
6
6
|
import { v4 } from "uuid";
|
|
7
7
|
import CodeEditor from "./components/CodeEditor";
|
|
8
8
|
|
|
@@ -53,7 +53,8 @@ function subscribe(listener: () => void): () => void {
|
|
|
53
53
|
* - anything else — copied as-is onto {@link env}.
|
|
54
54
|
*
|
|
55
55
|
* The UI theme is resolved from the `_theme` URL parameter, or from
|
|
56
|
-
* `prefers-color-scheme` as a fallback
|
|
56
|
+
* `prefers-color-scheme` as a fallback, and applied as the `light`/`dark`
|
|
57
|
+
* class on `<html>`.
|
|
57
58
|
*
|
|
58
59
|
* @param envVariables An object containing the Codenotch environment variables as key-value pairs.
|
|
59
60
|
* @example
|
|
@@ -117,6 +118,14 @@ function init(envVariables: any): void {
|
|
|
117
118
|
else {
|
|
118
119
|
env.theme = 'light';
|
|
119
120
|
}
|
|
121
|
+
|
|
122
|
+
// Apply the resolved theme to <html> like setTheme does. Without this, getTheme()
|
|
123
|
+
// and the DOM disagree until the first setTheme call, which can then be a visual
|
|
124
|
+
// no-op (early return when env.theme already equals the requested theme).
|
|
125
|
+
if (env.theme) {
|
|
126
|
+
document.documentElement.classList.remove(env.theme === 'dark' ? 'light' : 'dark');
|
|
127
|
+
document.documentElement.classList.add(env.theme);
|
|
128
|
+
}
|
|
120
129
|
}
|
|
121
130
|
catch {
|
|
122
131
|
}
|
|
@@ -177,7 +186,10 @@ function createApi(): ICodenotchApi {
|
|
|
177
186
|
|
|
178
187
|
return await response.json();
|
|
179
188
|
},
|
|
180
|
-
startProcess: async <
|
|
189
|
+
startProcess: async <In, Out>(p: Process<In, Out, any>, inputs: In): Promise<IProcessResult<Out>> => {
|
|
190
|
+
return api.startProcessFromId<In, Out>(p.id, inputs);
|
|
191
|
+
},
|
|
192
|
+
startProcessFromId: async <TInput, TOutput>(processId: string, inputs: TInput): Promise<IProcessResult<TOutput>> => {
|
|
181
193
|
if (env.clusterUrl === undefined) throw new Error(MISSING_CLUSTER_URL_ERROR);
|
|
182
194
|
if (env.serviceName === undefined) throw new Error(MISSING_SERVICE_NAME_ERROR);
|
|
183
195
|
|
|
@@ -226,59 +238,81 @@ function createApi(): ICodenotchApi {
|
|
|
226
238
|
};
|
|
227
239
|
}
|
|
228
240
|
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
"
|
|
238
|
-
|
|
239
|
-
|
|
241
|
+
try {
|
|
242
|
+
const getProcessResult = async () => {
|
|
243
|
+
// Instance get: http://127.0.0.1:5005/instances/826bd498-e410-4c48-b29a-e79a76b75ef1
|
|
244
|
+
// Token header: Codenotch-Instance-Token
|
|
245
|
+
/*
|
|
246
|
+
{
|
|
247
|
+
"id": "826bd498-e410-4c48-b29a-e79a76b75ef1",
|
|
248
|
+
"state": "SUCCESS",
|
|
249
|
+
"output": {
|
|
250
|
+
"result": "Hello, World!"
|
|
251
|
+
},
|
|
252
|
+
"errors": []
|
|
253
|
+
}
|
|
254
|
+
*/
|
|
255
|
+
const instanceResp = await fetch(`${env.clusterUrl}/instances/${instanceData.instanceId}`, {
|
|
256
|
+
method: 'GET',
|
|
257
|
+
headers: {
|
|
258
|
+
'Codenotch-Instance-Token': instanceData.instanceToken
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
const instanceDataResp: { state: string; output: any; errors: {code: number, message: string}[] } = await instanceResp.json();
|
|
262
|
+
|
|
263
|
+
// Process success
|
|
264
|
+
if (instanceDataResp.state === 'SUCCESS') {
|
|
265
|
+
return instanceDataResp.output;
|
|
240
266
|
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
267
|
+
|
|
268
|
+
// Process crash
|
|
269
|
+
if (instanceDataResp.state === 'ERROR') {
|
|
270
|
+
console.error("Codenotch startProcess: process failed", instanceDataResp.errors);
|
|
271
|
+
throw new Error(`Process failed: ${instanceDataResp.errors.map(e => `${e.code}: ${e.message}`).join('; ')}`);
|
|
246
272
|
}
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
};
|
|
273
|
+
|
|
274
|
+
if (instanceDataResp.state === 'WAITING') {
|
|
275
|
+
// Just continue polling
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
return undefined;
|
|
279
|
+
};
|
|
255
280
|
|
|
256
281
|
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
282
|
+
const timeout = 30000; // 30 seconds
|
|
283
|
+
const pollInterval = 500; // 0.5 second
|
|
284
|
+
const startTime = Date.now();
|
|
285
|
+
while (Date.now() - startTime < timeout) {
|
|
286
|
+
const result = await getProcessResult();
|
|
287
|
+
if (result !== undefined) {
|
|
288
|
+
return {
|
|
289
|
+
isError: false,
|
|
290
|
+
output: result as TOutput,
|
|
291
|
+
processInstanceId: instanceData.instanceId,
|
|
292
|
+
errorMessage: ''
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
await new Promise(resolve => setTimeout(resolve, pollInterval));
|
|
269
296
|
}
|
|
270
|
-
|
|
297
|
+
|
|
298
|
+
console.warn(`Codenotch startProcess: process ${processId} timed out after ${timeout} ms`);
|
|
299
|
+
return {
|
|
300
|
+
isError: true,
|
|
301
|
+
output: undefined as any,
|
|
302
|
+
processInstanceId: instanceData.instanceId,
|
|
303
|
+
errorMessage: `Process timed out after ${timeout} ms`
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
catch (error) {
|
|
307
|
+
return {
|
|
308
|
+
isError: true,
|
|
309
|
+
output: undefined as any,
|
|
310
|
+
processInstanceId: instanceData.instanceId,
|
|
311
|
+
errorMessage: `Failed to get process result: ${error}`
|
|
312
|
+
};
|
|
271
313
|
}
|
|
272
314
|
|
|
273
|
-
console.warn(`Codenotch startProcess: process ${processId} timed out after ${timeout} ms`);
|
|
274
|
-
return {
|
|
275
|
-
isError: true,
|
|
276
|
-
output: undefined as any,
|
|
277
|
-
processInstanceId: instanceData.instanceId,
|
|
278
|
-
errorMessage: `Process timed out after ${timeout} ms`
|
|
279
|
-
};
|
|
280
315
|
|
|
281
|
-
|
|
282
316
|
},
|
|
283
317
|
getUrlParams: () => {
|
|
284
318
|
let result: { [key: string]: string } = {};
|
package/src/models/Codenotch.ts
CHANGED
|
@@ -36,7 +36,7 @@ export interface ICodenotchEnv {
|
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
/**
|
|
39
|
-
* Result of a
|
|
39
|
+
* Result of a process execution
|
|
40
40
|
*/
|
|
41
41
|
export interface IProcessResult<T = any> {
|
|
42
42
|
|
|
@@ -50,6 +50,28 @@ export interface IProcessResult<T = any> {
|
|
|
50
50
|
errorMessage: string;
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
+
|
|
54
|
+
/// @CODENOTCH/PROCESS
|
|
55
|
+
type Json = string | number | boolean | null | Json[] | {
|
|
56
|
+
[key: string]: Json;
|
|
57
|
+
};
|
|
58
|
+
type Serializable = Json | {
|
|
59
|
+
toJSON(): Json;
|
|
60
|
+
} | undefined | Serializable[] | {
|
|
61
|
+
[key: string]: Serializable;
|
|
62
|
+
};
|
|
63
|
+
type EventMap = Record<string, Serializable>;
|
|
64
|
+
export interface Process<In = unknown, Out = unknown, E extends EventMap = {}> {
|
|
65
|
+
readonly id: string;
|
|
66
|
+
readonly __types?: {
|
|
67
|
+
input: In;
|
|
68
|
+
output: Out;
|
|
69
|
+
events: E;
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
/// @CODENOTCH/PROCESS
|
|
73
|
+
|
|
74
|
+
|
|
53
75
|
/**
|
|
54
76
|
* The Codenotch client API.
|
|
55
77
|
*
|
|
@@ -63,10 +85,9 @@ export interface IProcessResult<T = any> {
|
|
|
63
85
|
*/
|
|
64
86
|
export interface ICodenotchApi {
|
|
65
87
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
startProcess<TInput, TOutput>(processId: string, inputs: TInput): Promise<IProcessResult<TOutput>>;
|
|
88
|
+
startProcessFromId<TInput, TOutput>(processId: string, inputs: TInput): Promise<IProcessResult<TOutput>>;
|
|
89
|
+
|
|
90
|
+
startProcess<In, Out>(p: Process<In, Out, any>, inputs: In): Promise<IProcessResult<Out>>;
|
|
70
91
|
|
|
71
92
|
/** Runtime environment (cluster, service, language, theme dictionaries, manifests…). Populated by `init()`. */
|
|
72
93
|
readonly env: ICodenotchEnv;
|