@codenotch/codenotch.react 2.0.2 → 2.0.4
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.map +1 -1
- package/dist/index.js +69 -45
- package/dist/models/Codenotch.d.ts +19 -3
- package/dist/models/Codenotch.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +75 -47
- package/src/models/Codenotch.ts +24 -3
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,aAAa,EAA2B,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AAEjI,OAAO,UAAU,MAAM,yBAAyB,CAAC;AAEjD;;;GAGG;AACH,QAAA,MAAM,GAAG,EAAE,aAAkB,CAAC;AA6B9B;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,iBAAS,IAAI,CAAC,YAAY,EAAE,GAAG,GAAG,IAAI,CA0DrC;AAwSD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,iBAAS,YAAY,IAAI,aAAa,CAErC;AAED;;;;;;;;;;;;;GAaG;AACH,iBAAS,YAAY,IAAI,aAAa,CAErC;AAED;;;;;;;;;;;;;;GAcG;AACH,iBAAS,iBAAiB,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,WAAW,CAG5D;AAED,wFAAwF;AACxF,KAAK,qBAAqB,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,EAAE,MAAM,kBAAkB,CAAC,CAAC;AAElE,8FAA8F;AAC9F,KAAK,iBAAiB,CAAC,CAAC,IAAI,CAAC,SAAS,KAAK,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;AAElF;;;;;;;;;;GAUG;AACH,iBAAS,aAAa,CAAC,CAAC,SAAS,aAAa,CAAC,GAAG,CAAC,EAC/C,SAAS,EAAE,CAAC,GACb,iBAAiB,CAAC,qBAAqB,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,GAAG,aAAa,CAAC,iBAAiB,CAAC,CAAC,CAAC,CAAC,CAAC,CAQnG;AAED,OAAO,EACH,GAAG,EACH,YAAY,EACZ,YAAY,EACZ,aAAa,EACb,iBAAiB,EACjB,IAAI,EACJ,UAAU,EACb,CAAC;AAEF,cAAc,oBAAoB,CAAC;AACnC,cAAc,eAAe,CAAC;AAC9B,cAAc,2BAA2B,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -194,6 +194,9 @@ function createApi() {
|
|
|
194
194
|
}
|
|
195
195
|
return await response.json();
|
|
196
196
|
},
|
|
197
|
+
startProcessObj: async (p, inputs) => {
|
|
198
|
+
return api.startProcess(p.id, inputs);
|
|
199
|
+
},
|
|
197
200
|
startProcess: async (processId, inputs) => {
|
|
198
201
|
if (env.clusterUrl === undefined)
|
|
199
202
|
throw new Error(MISSING_CLUSTER_URL_ERROR);
|
|
@@ -214,7 +217,10 @@ function createApi() {
|
|
|
214
217
|
body: JSON.stringify({
|
|
215
218
|
"id": (0, uuid_1.v4)(),
|
|
216
219
|
"input": inputs
|
|
217
|
-
})
|
|
220
|
+
}),
|
|
221
|
+
headers: {
|
|
222
|
+
'Content-Type': 'application/json',
|
|
223
|
+
}
|
|
218
224
|
});
|
|
219
225
|
if (response.status.toString().startsWith('2') === false) {
|
|
220
226
|
console.warn("Codenotch startProcess: failed to start process", response.status, response.statusText);
|
|
@@ -236,54 +242,72 @@ function createApi() {
|
|
|
236
242
|
errorMessage: `Failed to start process: invalid response data`
|
|
237
243
|
};
|
|
238
244
|
}
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
"
|
|
248
|
-
|
|
249
|
-
|
|
245
|
+
try {
|
|
246
|
+
const getProcessResult = async () => {
|
|
247
|
+
// Instance get: http://127.0.0.1:5005/instances/826bd498-e410-4c48-b29a-e79a76b75ef1
|
|
248
|
+
// Token header: Codenotch-Instance-Token
|
|
249
|
+
/*
|
|
250
|
+
{
|
|
251
|
+
"id": "826bd498-e410-4c48-b29a-e79a76b75ef1",
|
|
252
|
+
"state": "SUCCESS",
|
|
253
|
+
"output": {
|
|
254
|
+
"result": "Hello, World!"
|
|
255
|
+
},
|
|
256
|
+
"errors": []
|
|
257
|
+
}
|
|
258
|
+
*/
|
|
259
|
+
const instanceResp = await fetch(`${env.clusterUrl}/instances/${instanceData.instanceId}`, {
|
|
260
|
+
method: 'GET',
|
|
261
|
+
headers: {
|
|
262
|
+
'Codenotch-Instance-Token': instanceData.instanceToken
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
const instanceDataResp = await instanceResp.json();
|
|
266
|
+
// Process success
|
|
267
|
+
if (instanceDataResp.state === 'SUCCESS') {
|
|
268
|
+
return instanceDataResp.output;
|
|
250
269
|
}
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
'Codenotch-Instance-Token': instanceData.instanceToken
|
|
270
|
+
// Process crash
|
|
271
|
+
if (instanceDataResp.state === 'ERROR') {
|
|
272
|
+
console.error("Codenotch startProcess: process failed", instanceDataResp.errors);
|
|
273
|
+
throw new Error(`Process failed: ${instanceDataResp.errors.map(e => `${e.code}: ${e.message}`).join('; ')}`);
|
|
256
274
|
}
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
return
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
};
|
|
275
|
+
if (instanceDataResp.state === 'WAITING') {
|
|
276
|
+
// Just continue polling
|
|
277
|
+
}
|
|
278
|
+
return undefined;
|
|
279
|
+
};
|
|
280
|
+
const timeout = 30000; // 30 seconds
|
|
281
|
+
const pollInterval = 500; // 0.5 second
|
|
282
|
+
const startTime = Date.now();
|
|
283
|
+
while (Date.now() - startTime < timeout) {
|
|
284
|
+
const result = await getProcessResult();
|
|
285
|
+
if (result !== undefined) {
|
|
286
|
+
return {
|
|
287
|
+
isError: false,
|
|
288
|
+
output: result,
|
|
289
|
+
processInstanceId: instanceData.instanceId,
|
|
290
|
+
errorMessage: ''
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
await new Promise(resolve => setTimeout(resolve, pollInterval));
|
|
277
294
|
}
|
|
278
|
-
|
|
295
|
+
console.warn(`Codenotch startProcess: process ${processId} timed out after ${timeout} ms`);
|
|
296
|
+
return {
|
|
297
|
+
isError: true,
|
|
298
|
+
output: undefined,
|
|
299
|
+
processInstanceId: instanceData.instanceId,
|
|
300
|
+
errorMessage: `Process timed out after ${timeout} ms`
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
catch (error) {
|
|
304
|
+
return {
|
|
305
|
+
isError: true,
|
|
306
|
+
output: undefined,
|
|
307
|
+
processInstanceId: instanceData.instanceId,
|
|
308
|
+
errorMessage: `Failed to get process result: ${error}`
|
|
309
|
+
};
|
|
279
310
|
}
|
|
280
|
-
console.warn(`Codenotch startProcess: process ${processId} timed out after ${timeout} ms`);
|
|
281
|
-
return {
|
|
282
|
-
isError: true,
|
|
283
|
-
output: undefined,
|
|
284
|
-
processInstanceId: instanceData.instanceId,
|
|
285
|
-
errorMessage: `Process timed out after ${timeout} ms`
|
|
286
|
-
};
|
|
287
311
|
},
|
|
288
312
|
getUrlParams: () => {
|
|
289
313
|
let result = {};
|
|
@@ -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
|
-
* Start a server-side BPMN process and wait for its completion.
|
|
60
|
-
*/
|
|
61
75
|
startProcess<TInput, TOutput>(processId: string, inputs: TInput): Promise<IProcessResult<TOutput>>;
|
|
76
|
+
startProcessObj<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,E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EAAE,MAAM,CAAC;IACX,mDAAmD;IACnD,KAAK,EAAE,MAAM,IAAI,CAAC;CACrB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codenotch/codenotch.react",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.4",
|
|
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
|
|
|
@@ -177,6 +177,9 @@ function createApi(): ICodenotchApi {
|
|
|
177
177
|
|
|
178
178
|
return await response.json();
|
|
179
179
|
},
|
|
180
|
+
startProcessObj: async <In, Out>(p: Process<In, Out, any>, inputs: In): Promise<IProcessResult<Out>> => {
|
|
181
|
+
return api.startProcess<In, Out>(p.id, inputs);
|
|
182
|
+
},
|
|
180
183
|
startProcess: async <TInput, TOutput>(processId: string, inputs: TInput): Promise<IProcessResult<TOutput>> => {
|
|
181
184
|
if (env.clusterUrl === undefined) throw new Error(MISSING_CLUSTER_URL_ERROR);
|
|
182
185
|
if (env.serviceName === undefined) throw new Error(MISSING_SERVICE_NAME_ERROR);
|
|
@@ -199,7 +202,10 @@ function createApi(): ICodenotchApi {
|
|
|
199
202
|
body: JSON.stringify({
|
|
200
203
|
"id": v4(),
|
|
201
204
|
"input": inputs
|
|
202
|
-
})
|
|
205
|
+
}),
|
|
206
|
+
headers: {
|
|
207
|
+
'Content-Type': 'application/json',
|
|
208
|
+
}
|
|
203
209
|
});
|
|
204
210
|
if (response.status.toString().startsWith('2') === false) {
|
|
205
211
|
console.warn("Codenotch startProcess: failed to start process", response.status, response.statusText);
|
|
@@ -223,59 +229,81 @@ function createApi(): ICodenotchApi {
|
|
|
223
229
|
};
|
|
224
230
|
}
|
|
225
231
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
"
|
|
235
|
-
|
|
236
|
-
|
|
232
|
+
try {
|
|
233
|
+
const getProcessResult = async () => {
|
|
234
|
+
// Instance get: http://127.0.0.1:5005/instances/826bd498-e410-4c48-b29a-e79a76b75ef1
|
|
235
|
+
// Token header: Codenotch-Instance-Token
|
|
236
|
+
/*
|
|
237
|
+
{
|
|
238
|
+
"id": "826bd498-e410-4c48-b29a-e79a76b75ef1",
|
|
239
|
+
"state": "SUCCESS",
|
|
240
|
+
"output": {
|
|
241
|
+
"result": "Hello, World!"
|
|
242
|
+
},
|
|
243
|
+
"errors": []
|
|
244
|
+
}
|
|
245
|
+
*/
|
|
246
|
+
const instanceResp = await fetch(`${env.clusterUrl}/instances/${instanceData.instanceId}`, {
|
|
247
|
+
method: 'GET',
|
|
248
|
+
headers: {
|
|
249
|
+
'Codenotch-Instance-Token': instanceData.instanceToken
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
const instanceDataResp: { state: string; output: any; errors: {code: number, message: string}[] } = await instanceResp.json();
|
|
253
|
+
|
|
254
|
+
// Process success
|
|
255
|
+
if (instanceDataResp.state === 'SUCCESS') {
|
|
256
|
+
return instanceDataResp.output;
|
|
237
257
|
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
258
|
+
|
|
259
|
+
// Process crash
|
|
260
|
+
if (instanceDataResp.state === 'ERROR') {
|
|
261
|
+
console.error("Codenotch startProcess: process failed", instanceDataResp.errors);
|
|
262
|
+
throw new Error(`Process failed: ${instanceDataResp.errors.map(e => `${e.code}: ${e.message}`).join('; ')}`);
|
|
243
263
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
};
|
|
264
|
+
|
|
265
|
+
if (instanceDataResp.state === 'WAITING') {
|
|
266
|
+
// Just continue polling
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
return undefined;
|
|
270
|
+
};
|
|
252
271
|
|
|
253
272
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
273
|
+
const timeout = 30000; // 30 seconds
|
|
274
|
+
const pollInterval = 500; // 0.5 second
|
|
275
|
+
const startTime = Date.now();
|
|
276
|
+
while (Date.now() - startTime < timeout) {
|
|
277
|
+
const result = await getProcessResult();
|
|
278
|
+
if (result !== undefined) {
|
|
279
|
+
return {
|
|
280
|
+
isError: false,
|
|
281
|
+
output: result as TOutput,
|
|
282
|
+
processInstanceId: instanceData.instanceId,
|
|
283
|
+
errorMessage: ''
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
await new Promise(resolve => setTimeout(resolve, pollInterval));
|
|
266
287
|
}
|
|
267
|
-
|
|
288
|
+
|
|
289
|
+
console.warn(`Codenotch startProcess: process ${processId} timed out after ${timeout} ms`);
|
|
290
|
+
return {
|
|
291
|
+
isError: true,
|
|
292
|
+
output: undefined as any,
|
|
293
|
+
processInstanceId: instanceData.instanceId,
|
|
294
|
+
errorMessage: `Process timed out after ${timeout} ms`
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
catch (error) {
|
|
298
|
+
return {
|
|
299
|
+
isError: true,
|
|
300
|
+
output: undefined as any,
|
|
301
|
+
processInstanceId: instanceData.instanceId,
|
|
302
|
+
errorMessage: `Failed to get process result: ${error}`
|
|
303
|
+
};
|
|
268
304
|
}
|
|
269
305
|
|
|
270
|
-
console.warn(`Codenotch startProcess: process ${processId} timed out after ${timeout} ms`);
|
|
271
|
-
return {
|
|
272
|
-
isError: true,
|
|
273
|
-
output: undefined as any,
|
|
274
|
-
processInstanceId: instanceData.instanceId,
|
|
275
|
-
errorMessage: `Process timed out after ${timeout} ms`
|
|
276
|
-
};
|
|
277
306
|
|
|
278
|
-
|
|
279
307
|
},
|
|
280
308
|
getUrlParams: () => {
|
|
281
309
|
let result: { [key: string]: string } = {};
|
package/src/models/Codenotch.ts
CHANGED
|
@@ -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,11 +85,10 @@ export interface IProcessResult<T = any> {
|
|
|
63
85
|
*/
|
|
64
86
|
export interface ICodenotchApi {
|
|
65
87
|
|
|
66
|
-
/**
|
|
67
|
-
* Start a server-side BPMN process and wait for its completion.
|
|
68
|
-
*/
|
|
69
88
|
startProcess<TInput, TOutput>(processId: string, inputs: TInput): Promise<IProcessResult<TOutput>>;
|
|
70
89
|
|
|
90
|
+
startProcessObj<In, Out>(p: Process<In, Out, any>, inputs: In): Promise<IProcessResult<Out>>;
|
|
91
|
+
|
|
71
92
|
/** Runtime environment (cluster, service, language, theme dictionaries, manifests…). Populated by `init()`. */
|
|
72
93
|
readonly env: ICodenotchEnv;
|
|
73
94
|
|