Skip to content

Commit fd91d67

Browse files
committed
feat(types): update types to orchestration-cluster-api types
* add type additional type checks in TaskExecution.js
1 parent 5415047 commit fd91d67

File tree

4 files changed

+198
-116
lines changed

4 files changed

+198
-116
lines changed

lib/TaskExecution.js

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ export default class TaskExecution extends EventEmitter {
182182
incident = getIncident(getProcessInstanceIncidentResult.response);
183183
}
184184

185-
const isCompleted = [ 'COMPLETED', 'TERMINATED', 'CANCELED' ].includes(state);
185+
const isCompleted = [ 'COMPLETED', 'TERMINATED', 'CANCELED' ].includes(state ?? '');
186186

187187
if (isCompleted || hasIncident) {
188188
const getProcessInstanceVariablesResult = await this._api.getProcessInstanceVariables(processInstanceKey);
@@ -215,7 +215,7 @@ export default class TaskExecution extends EventEmitter {
215215

216216
const variables = getVariables(
217217
getProcessInstanceVariablesResult.response.items,
218-
getProcessInstanceElementInstancesResult.response.items,
218+
getProcessInstanceElementInstancesResult.response.items ?? [],
219219
processInstance,
220220
element.id
221221
);
@@ -282,16 +282,21 @@ export default class TaskExecution extends EventEmitter {
282282
/**
283283
* Get the process instance key from the response.
284284
*
285-
* @param {CreateProcessInstanceResponse} response
285+
* @param {import('./types').CreateProcessInstanceResult} response
286286
*
287-
* @returns {string|null} The process instance key or null if not found.
287+
* @returns {string | null} The process instance key or null if not found.
288288
*/
289289
function getProcessInstanceKey(response) {
290290
const { processInstanceKey } = response;
291291

292292
return processInstanceKey || null;
293293
}
294294

295+
/**
296+
* @param {import('./types').ProcessInstanceSearchQueryResult} response
297+
* @param {string} processInstanceKey
298+
* @return {import('./types').ProcessInstanceResult | null}
299+
*/
295300
function getProcessInstance(response, processInstanceKey) {
296301
const { items = [] } = response;
297302

@@ -302,6 +307,11 @@ function getProcessInstance(response, processInstanceKey) {
302307
return items.find(item => item.processInstanceKey === processInstanceKey) || null;
303308
}
304309

310+
/**
311+
* @param {import('./types').ProcessInstanceSearchQueryResult} response
312+
* @param {string} processInstanceKey
313+
* @return {import('./types').ProcessInstanceStateEnum | null}
314+
*/
305315
function getProcessInstanceState(response, processInstanceKey) {
306316
const processInstance = getProcessInstance(response, processInstanceKey);
307317

@@ -312,6 +322,11 @@ function getProcessInstanceState(response, processInstanceKey) {
312322
return processInstance.state;
313323
}
314324

325+
/**
326+
* @param {import('./types').ProcessInstanceSearchQueryResult} response
327+
* @param {string} processInstanceKey
328+
* @returns {boolean}
329+
*/
315330
function hasProcessInstanceIncident(response, processInstanceKey) {
316331
const processInstance = getProcessInstance(response, processInstanceKey);
317332

@@ -322,6 +337,14 @@ function hasProcessInstanceIncident(response, processInstanceKey) {
322337
return processInstance.hasIncident || false;
323338
}
324339

340+
/**
341+
*
342+
* @param {import('./types').VariableSearchResult[]} getVariablesResponseItems
343+
* @param {import('./types').ElementInstanceResult[]} getElementInstancesResponseItems
344+
* @param {import('./types').ProcessInstanceResult} processInstance
345+
* @param elementId
346+
* @returns {object}
347+
*/
325348
export function getVariables(getVariablesResponseItems, getElementInstancesResponseItems, processInstance, elementId) {
326349
const variables = {};
327350

@@ -348,6 +371,11 @@ export function getVariables(getVariablesResponseItems, getElementInstancesRespo
348371
return variables;
349372
}
350373

374+
/**
375+
*
376+
* @param {import('./types').IncidentSearchQueryResult} response
377+
* @return {import('./types').IncidentResult | null}
378+
*/
351379
function getIncident(response) {
352380
const { items = [] } = response;
353381

@@ -358,11 +386,19 @@ function getIncident(response) {
358386
return items[0];
359387
}
360388

389+
/**
390+
*
391+
* @param {import('./types').VariableSearchResult} variable
392+
* @param {import('./types').ElementInstanceResult[]} elementInstances
393+
* @param {import('./types').ProcessInstanceResult} processInstance
394+
* @param {string} elementId
395+
* @return {null | string}
396+
*/
361397
function getScope(variable, elementInstances, processInstance, elementId) {
362398
const { scopeKey } = variable;
363399

364400
const elementInstance = elementInstances.find(elementInstance => {
365-
return elementInstance.elementInstanceKey === scopeKey && elementInstance.elementId === elementId;
401+
return String(elementInstance.elementInstanceKey) === String(scopeKey) && elementInstance.elementId === elementId;
366402
});
367403

368404
if (elementInstance) {
@@ -371,7 +407,7 @@ function getScope(variable, elementInstances, processInstance, elementId) {
371407

372408
const { processInstanceKey } = processInstance;
373409

374-
if (scopeKey === processInstanceKey) {
410+
if (String(scopeKey) === String(processInstanceKey)) {
375411
return SCOPES.PROCESS;
376412
}
377413

@@ -381,16 +417,16 @@ function getScope(variable, elementInstances, processInstance, elementId) {
381417
/**
382418
* Get the process definition key from the deployment response.
383419
*
384-
* @param {import('@camunda8/sdk/dist/c8/lib/C8Dto').DeployResourceResponse} deployResponse
420+
* @param {import('./types').ExtendedDeploymentResult} deployResponse
385421
* @param {string} processId
386422
*
387-
* @returns {string|null}
423+
* @returns {string | null}
388424
*/
389425
export function getProcessDefinitionKey(deployResponse, processId) {
390426
const { deployments = [] } = deployResponse;
391427

392428
for (const deployment of deployments) {
393-
if ('processDefinition' in deployment) {
429+
if (deployment.processDefinition) {
394430
const { processDefinition } = deployment;
395431

396432
if (processDefinition.processDefinitionId === processId) {
@@ -400,4 +436,4 @@ export function getProcessDefinitionKey(deployResponse, processId) {
400436
}
401437

402438
return null;
403-
}
439+
}

lib/types.ts

Lines changed: 89 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,135 @@
11
import type {
2-
CreateProcessInstanceResponse,
3-
DeployResourceResponse,
4-
SearchProcessInstanceResponse,
5-
SearchVariablesResponse,
6-
SearchElementInstancesResponse,
7-
SearchIncidentsResponse
8-
} from '@camunda8/sdk/dist/c8/lib/C8Dto';
2+
CamundaClient,
3+
CreateProcessInstanceResult,
4+
ElementInstanceResult,
5+
ElementInstanceSearchQueryResult,
6+
IncidentResult,
7+
IncidentSearchQueryResult,
8+
ProcessInstanceResult,
9+
ProcessInstanceSearchQueryResult,
10+
ProcessInstanceStateEnum,
11+
VariableSearchQueryResult,
12+
VariableSearchResult
13+
} from '@camunda8/orchestration-cluster-api';
14+
import type {ModdleElement} from "bpmn-js/lib/model/Types";
15+
16+
declare type ExtendedDeploymentResult = Awaited<ReturnType<CamundaClient['createDeployment']>>
17+
18+
export type {
19+
CreateProcessInstanceResult,
20+
ElementInstanceSearchQueryResult,
21+
IncidentSearchQueryResult,
22+
ProcessInstanceSearchQueryResult,
23+
ProcessInstanceResult,
24+
VariableSearchQueryResult,
25+
ExtendedDeploymentResult,
26+
ProcessInstanceStateEnum,
27+
VariableSearchResult,
28+
ElementInstanceResult,
29+
IncidentResult
30+
}
931

10-
import type {
11-
ModdleElement
12-
} from "bpmn-js/lib/model/Types";
1332

1433
export type Input = {
15-
[elementId: string]: string;
34+
[elementId: string]: string;
1635
};
1736

1837
export type Output = {
19-
[elementId: string]: ElementOutput
38+
[elementId: string]: ElementOutput
2039
};
2140

2241
export type VARIABLE_SCOPE = 'LOCAL' | 'PROCESS';
2342

2443
export type ElementOutputVariables = {
25-
[id: string]: {
26-
name: string;
27-
value: any;
28-
scope: VARIABLE_SCOPE;
29-
}
44+
[id: string]: {
45+
name: string;
46+
value: any;
47+
scope: VARIABLE_SCOPE;
48+
}
3049
};
3150

3251
export type ElementOutput = {
33-
success: boolean;
34-
variables?: ElementOutputVariables;
35-
error?: TaskExecutionError;
36-
incident?: any;
37-
operateUrl?: string;
38-
[key: string]: any;
52+
success: boolean;
53+
variables?: ElementOutputVariables;
54+
error?: TaskExecutionError;
55+
incident?: any;
56+
operateUrl?: string;
57+
[key: string]: any;
3958
} | undefined;
4059

4160
export type Config = {
42-
input: Input;
43-
output: Output;
61+
input: Input;
62+
output: Output;
4463
};
4564

4665
export type Variable = {
47-
name: string;
48-
type?: string;
49-
info?: string;
50-
isList?: boolean;
51-
entries?: Variable[];
52-
scope?: ModdleElement;
66+
name: string;
67+
type?: string;
68+
info?: string;
69+
isList?: boolean;
70+
entries?: Variable[];
71+
scope?: ModdleElement;
5372
};
5473

5574
export type Variables = Variable[];
5675

5776
export type ApiResponse<T> =
58-
| {
59-
success: true;
60-
response: T;
61-
}
62-
| {
63-
success: false;
64-
error: string;
65-
}
77+
| {
78+
success: true;
79+
response: T;
80+
}
81+
| {
82+
success: false;
83+
error: string;
84+
}
6685

6786
export type TaskExecutionApi = {
68-
deploy: () => Promise<ApiResponse<DeployResourceResponse>>;
69-
startInstance: (processDefinitionKey: string, elementId: string, variables: { [key: string]: any }) => Promise<ApiResponse<CreateProcessInstanceResponse>>;
70-
getProcessInstance: (processInstanceKey: string) => Promise<ApiResponse<SearchProcessInstanceResponse>>;
71-
getProcessInstanceVariables: (processInstanceKey: string) => Promise<ApiResponse<SearchVariablesResponse>>;
72-
getProcessInstanceElementInstances: (processInstanceKey: string) => Promise<ApiResponse<SearchElementInstancesResponse>>;
73-
getProcessInstanceIncident: (processInstanceKey: string) => Promise<ApiResponse<SearchIncidentsResponse>>;
87+
deploy: () => Promise<ApiResponse<ExtendedDeploymentResult>>;
88+
startInstance: (processDefinitionKey: string, elementId: string, variables: {
89+
[key: string]: any
90+
}) => Promise<ApiResponse<CreateProcessInstanceResult>>;
91+
getProcessInstance: (processInstanceKey: string) => Promise<ApiResponse<ProcessInstanceSearchQueryResult>>;
92+
getProcessInstanceVariables: (processInstanceKey: string) => Promise<ApiResponse<VariableSearchQueryResult>>;
93+
getProcessInstanceElementInstances: (processInstanceKey: string) => Promise<ApiResponse<ElementInstanceSearchQueryResult>>;
94+
getProcessInstanceIncident: (processInstanceKey: string) => Promise<ApiResponse<IncidentSearchQueryResult>>;
7495
};
7596

7697
export type TaskExecutionStatus =
77-
'idle' |
78-
'deploying' |
79-
'starting-instance' |
80-
'executing';
98+
'idle' |
99+
'deploying' |
100+
'starting-instance' |
101+
'executing';
81102

82103
export type TaskExecutionEvents =
83-
'taskExecution.status.changed' |
84-
'taskExecution.finished' |
85-
'taskExecution.error' |
86-
'taskExecution.interrupted';
104+
'taskExecution.status.changed' |
105+
'taskExecution.finished' |
106+
'taskExecution.error' |
107+
'taskExecution.interrupted';
87108

88109
export type TaskExecutionResult = {
89-
success: boolean;
90-
variables?: ElementOutputVariables;
91-
error?: TaskExecutionError;
92-
incident?: any;
110+
success: boolean;
111+
variables?: ElementOutputVariables;
112+
error?: TaskExecutionError;
113+
incident?: any;
93114
}
94115

95116
export type TaskExecutionError = {
96-
message: string;
97-
response?: any;
117+
message: string;
118+
response?: any;
98119
};
99120

100-
export type { Element, ModdleElement } from 'bpmn-js/lib/model/Types';
121+
export type {Element, ModdleElement} from 'bpmn-js/lib/model/Types';
101122

102123
export type Plugin = {
103-
priority?: number;
104-
render: Function;
105-
type: string;
106-
[key: string]: any;
124+
priority?: number;
125+
render: Function;
126+
type: string;
127+
[key: string]: any;
107128
};
108129

109130
export type PluginContextValue = {
110-
plugins: Plugin[];
111-
registerPlugin: (plugin: Plugin) => void;
112-
unregisterPlugin: (plugin: Plugin) => void;
113-
getPlugins: (pluginPoint: string) => Plugin[];
131+
plugins: Plugin[];
132+
registerPlugin: (plugin: Plugin) => void;
133+
unregisterPlugin: (plugin: Plugin) => void;
134+
getPlugins: (pluginPoint: string) => Plugin[];
114135
};

0 commit comments

Comments
 (0)