@contractspec/lib.workflow-composer 1.57.0 → 1.59.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.
- package/dist/browser/index.js +183 -0
- package/dist/composer.d.ts +7 -11
- package/dist/composer.d.ts.map +1 -1
- package/dist/index.d.ts +7 -7
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +183 -6
- package/dist/injector.d.ts +3 -7
- package/dist/injector.d.ts.map +1 -1
- package/dist/merger.d.ts +2 -6
- package/dist/merger.d.ts.map +1 -1
- package/dist/node/index.js +183 -0
- package/dist/templates.d.ts +9 -13
- package/dist/templates.d.ts.map +1 -1
- package/dist/types.d.ts +23 -27
- package/dist/types.d.ts.map +1 -1
- package/dist/validator.d.ts +6 -10
- package/dist/validator.d.ts.map +1 -1
- package/package.json +20 -15
- package/dist/composer.js +0 -30
- package/dist/composer.js.map +0 -1
- package/dist/injector.js +0 -73
- package/dist/injector.js.map +0 -1
- package/dist/merger.js +0 -8
- package/dist/merger.js.map +0 -1
- package/dist/templates.js +0 -18
- package/dist/templates.js.map +0 -1
- package/dist/validator.js +0 -37
- package/dist/validator.js.map +0 -1
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
// src/templates.ts
|
|
2
|
+
function approvalStepTemplate(options) {
|
|
3
|
+
return {
|
|
4
|
+
id: options.id,
|
|
5
|
+
label: options.label,
|
|
6
|
+
type: options.type ?? "human",
|
|
7
|
+
description: options.description ?? "Tenant-specific approval",
|
|
8
|
+
action: options.action,
|
|
9
|
+
guard: options.guardExpression ? {
|
|
10
|
+
type: "expression",
|
|
11
|
+
value: options.guardExpression
|
|
12
|
+
} : undefined
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
// src/validator.ts
|
|
16
|
+
function validateExtension(extension, base) {
|
|
17
|
+
const issues = [];
|
|
18
|
+
const stepIds = new Set(base.definition.steps.map((step) => step.id));
|
|
19
|
+
extension.customSteps?.forEach((injection, idx) => {
|
|
20
|
+
if (!injection.inject.id) {
|
|
21
|
+
issues.push({
|
|
22
|
+
code: "workflow.extension.step.id",
|
|
23
|
+
message: `customSteps[${idx}] is missing an id`
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
if (!injection.after && !injection.before) {
|
|
27
|
+
issues.push({
|
|
28
|
+
code: "workflow.extension.step.anchor",
|
|
29
|
+
message: `customSteps[${idx}] must set after or before`
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
if (injection.after && !stepIds.has(injection.after)) {
|
|
33
|
+
issues.push({
|
|
34
|
+
code: "workflow.extension.step.after",
|
|
35
|
+
message: `customSteps[${idx}] references unknown step "${injection.after}"`
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
if (injection.before && !stepIds.has(injection.before)) {
|
|
39
|
+
issues.push({
|
|
40
|
+
code: "workflow.extension.step.before",
|
|
41
|
+
message: `customSteps[${idx}] references unknown step "${injection.before}"`
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
extension.hiddenSteps?.forEach((stepId) => {
|
|
46
|
+
if (!stepIds.has(stepId)) {
|
|
47
|
+
issues.push({
|
|
48
|
+
code: "workflow.extension.hidden-step",
|
|
49
|
+
message: `hidden step "${stepId}" does not exist`
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
if (issues.length) {
|
|
54
|
+
const reason = issues.map((issue) => `${issue.code}: ${issue.message}`).join("; ");
|
|
55
|
+
throw new Error(`Invalid workflow extension for ${extension.workflow}: ${reason}`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// src/injector.ts
|
|
60
|
+
function applyWorkflowExtension(base, extension) {
|
|
61
|
+
validateExtension(extension, base);
|
|
62
|
+
const spec = cloneWorkflowSpec(base);
|
|
63
|
+
const steps = [...spec.definition.steps];
|
|
64
|
+
let transitions = [...spec.definition.transitions];
|
|
65
|
+
const hiddenSet = new Set(extension.hiddenSteps ?? []);
|
|
66
|
+
hiddenSet.forEach((stepId) => {
|
|
67
|
+
const idx = steps.findIndex((step) => step.id === stepId);
|
|
68
|
+
if (idx !== -1) {
|
|
69
|
+
steps.splice(idx, 1);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
if (hiddenSet.size) {
|
|
73
|
+
transitions = transitions.filter((transition) => !hiddenSet.has(transition.from) && !hiddenSet.has(transition.to));
|
|
74
|
+
}
|
|
75
|
+
extension.customSteps?.forEach((injection) => {
|
|
76
|
+
insertStep(steps, injection);
|
|
77
|
+
wireTransitions(transitions, injection);
|
|
78
|
+
});
|
|
79
|
+
spec.definition.steps = steps;
|
|
80
|
+
spec.definition.transitions = dedupeTransitions(transitions);
|
|
81
|
+
spec.meta = {
|
|
82
|
+
...spec.meta,
|
|
83
|
+
version: spec.meta.version
|
|
84
|
+
};
|
|
85
|
+
return spec;
|
|
86
|
+
}
|
|
87
|
+
function insertStep(steps, injection) {
|
|
88
|
+
const anchorIndex = resolveAnchorIndex(steps, injection);
|
|
89
|
+
if (anchorIndex === -1) {
|
|
90
|
+
throw new Error(`Unable to place injected step "${injection.inject.id}"`);
|
|
91
|
+
}
|
|
92
|
+
steps.splice(anchorIndex, 0, { ...injection.inject });
|
|
93
|
+
}
|
|
94
|
+
function resolveAnchorIndex(steps, injection) {
|
|
95
|
+
if (injection.after) {
|
|
96
|
+
const idx = steps.findIndex((step) => step.id === injection.after);
|
|
97
|
+
return idx === -1 ? -1 : idx + 1;
|
|
98
|
+
}
|
|
99
|
+
if (injection.before) {
|
|
100
|
+
const idx = steps.findIndex((step) => step.id === injection.before);
|
|
101
|
+
return idx === -1 ? -1 : idx;
|
|
102
|
+
}
|
|
103
|
+
return steps.length;
|
|
104
|
+
}
|
|
105
|
+
function wireTransitions(transitions, injection) {
|
|
106
|
+
if (!injection.inject.id)
|
|
107
|
+
return;
|
|
108
|
+
if (injection.transitionFrom) {
|
|
109
|
+
transitions.push({
|
|
110
|
+
from: injection.transitionFrom,
|
|
111
|
+
to: injection.inject.id,
|
|
112
|
+
condition: injection.when
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
if (injection.transitionTo) {
|
|
116
|
+
transitions.push({
|
|
117
|
+
from: injection.inject.id,
|
|
118
|
+
to: injection.transitionTo,
|
|
119
|
+
condition: injection.when
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
function dedupeTransitions(transitions) {
|
|
124
|
+
const seen = new Set;
|
|
125
|
+
const result = [];
|
|
126
|
+
transitions.forEach((transition) => {
|
|
127
|
+
const key = `${transition.from}->${transition.to}:${transition.condition ?? ""}`;
|
|
128
|
+
if (seen.has(key))
|
|
129
|
+
return;
|
|
130
|
+
seen.add(key);
|
|
131
|
+
result.push(transition);
|
|
132
|
+
});
|
|
133
|
+
return result;
|
|
134
|
+
}
|
|
135
|
+
function cloneWorkflowSpec(spec) {
|
|
136
|
+
return JSON.parse(JSON.stringify(spec));
|
|
137
|
+
}
|
|
138
|
+
// src/composer.ts
|
|
139
|
+
import { satisfies } from "compare-versions";
|
|
140
|
+
|
|
141
|
+
class WorkflowComposer {
|
|
142
|
+
extensions = [];
|
|
143
|
+
register(extension) {
|
|
144
|
+
this.extensions.push(extension);
|
|
145
|
+
return this;
|
|
146
|
+
}
|
|
147
|
+
registerMany(extensions) {
|
|
148
|
+
extensions.forEach((extension) => this.register(extension));
|
|
149
|
+
return this;
|
|
150
|
+
}
|
|
151
|
+
compose(params) {
|
|
152
|
+
const applicable = this.extensions.filter((extension) => matches(params, extension)).sort((a, b) => (a.priority ?? 0) - (b.priority ?? 0));
|
|
153
|
+
return applicable.reduce((acc, extension) => applyWorkflowExtension(acc, extension), params.base);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
function matches(params, extension) {
|
|
157
|
+
if (extension.workflow !== params.base.meta.key)
|
|
158
|
+
return false;
|
|
159
|
+
if (extension.baseVersion && !satisfies(params.base.meta.version, extension.baseVersion)) {
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
if (extension.tenantId && extension.tenantId !== params.tenantId) {
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
if (extension.role && extension.role !== params.role) {
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
if (extension.device && extension.device !== params.device) {
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
173
|
+
// src/merger.ts
|
|
174
|
+
function mergeExtensions(extensions) {
|
|
175
|
+
return extensions.sort((a, b) => (a.priority ?? 0) - (b.priority ?? 0));
|
|
176
|
+
}
|
|
177
|
+
export {
|
|
178
|
+
validateExtension,
|
|
179
|
+
mergeExtensions,
|
|
180
|
+
approvalStepTemplate,
|
|
181
|
+
applyWorkflowExtension,
|
|
182
|
+
WorkflowComposer
|
|
183
|
+
};
|
package/dist/composer.d.ts
CHANGED
|
@@ -1,13 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
registerMany(extensions: WorkflowExtension[]): this;
|
|
9
|
-
compose(params: ComposeParams): WorkflowSpec;
|
|
1
|
+
import type { WorkflowSpec } from '@contractspec/lib.contracts';
|
|
2
|
+
import type { ComposeParams, WorkflowExtension } from './types';
|
|
3
|
+
export declare class WorkflowComposer {
|
|
4
|
+
private readonly extensions;
|
|
5
|
+
register(extension: WorkflowExtension): this;
|
|
6
|
+
registerMany(extensions: WorkflowExtension[]): this;
|
|
7
|
+
compose(params: ComposeParams): WorkflowSpec;
|
|
10
8
|
}
|
|
11
|
-
//#endregion
|
|
12
|
-
export { WorkflowComposer };
|
|
13
9
|
//# sourceMappingURL=composer.d.ts.map
|
package/dist/composer.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"composer.d.ts","
|
|
1
|
+
{"version":3,"file":"composer.d.ts","sourceRoot":"","sources":["../src/composer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAEhE,OAAO,KAAK,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAGhE,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,QAAQ,CAAC,UAAU,CAA2B;IAEtD,QAAQ,CAAC,SAAS,EAAE,iBAAiB,GAAG,IAAI;IAK5C,YAAY,CAAC,UAAU,EAAE,iBAAiB,EAAE,GAAG,IAAI;IAKnD,OAAO,CAAC,MAAM,EAAE,aAAa,GAAG,YAAY;CAU7C"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
export * from './types';
|
|
2
|
+
export * from './templates';
|
|
3
|
+
export * from './injector';
|
|
4
|
+
export * from './composer';
|
|
5
|
+
export * from './validator';
|
|
6
|
+
export * from './merger';
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,UAAU,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,184 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
// @bun
|
|
2
|
+
// src/templates.ts
|
|
3
|
+
function approvalStepTemplate(options) {
|
|
4
|
+
return {
|
|
5
|
+
id: options.id,
|
|
6
|
+
label: options.label,
|
|
7
|
+
type: options.type ?? "human",
|
|
8
|
+
description: options.description ?? "Tenant-specific approval",
|
|
9
|
+
action: options.action,
|
|
10
|
+
guard: options.guardExpression ? {
|
|
11
|
+
type: "expression",
|
|
12
|
+
value: options.guardExpression
|
|
13
|
+
} : undefined
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
// src/validator.ts
|
|
17
|
+
function validateExtension(extension, base) {
|
|
18
|
+
const issues = [];
|
|
19
|
+
const stepIds = new Set(base.definition.steps.map((step) => step.id));
|
|
20
|
+
extension.customSteps?.forEach((injection, idx) => {
|
|
21
|
+
if (!injection.inject.id) {
|
|
22
|
+
issues.push({
|
|
23
|
+
code: "workflow.extension.step.id",
|
|
24
|
+
message: `customSteps[${idx}] is missing an id`
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
if (!injection.after && !injection.before) {
|
|
28
|
+
issues.push({
|
|
29
|
+
code: "workflow.extension.step.anchor",
|
|
30
|
+
message: `customSteps[${idx}] must set after or before`
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
if (injection.after && !stepIds.has(injection.after)) {
|
|
34
|
+
issues.push({
|
|
35
|
+
code: "workflow.extension.step.after",
|
|
36
|
+
message: `customSteps[${idx}] references unknown step "${injection.after}"`
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
if (injection.before && !stepIds.has(injection.before)) {
|
|
40
|
+
issues.push({
|
|
41
|
+
code: "workflow.extension.step.before",
|
|
42
|
+
message: `customSteps[${idx}] references unknown step "${injection.before}"`
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
extension.hiddenSteps?.forEach((stepId) => {
|
|
47
|
+
if (!stepIds.has(stepId)) {
|
|
48
|
+
issues.push({
|
|
49
|
+
code: "workflow.extension.hidden-step",
|
|
50
|
+
message: `hidden step "${stepId}" does not exist`
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
if (issues.length) {
|
|
55
|
+
const reason = issues.map((issue) => `${issue.code}: ${issue.message}`).join("; ");
|
|
56
|
+
throw new Error(`Invalid workflow extension for ${extension.workflow}: ${reason}`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
6
59
|
|
|
7
|
-
|
|
60
|
+
// src/injector.ts
|
|
61
|
+
function applyWorkflowExtension(base, extension) {
|
|
62
|
+
validateExtension(extension, base);
|
|
63
|
+
const spec = cloneWorkflowSpec(base);
|
|
64
|
+
const steps = [...spec.definition.steps];
|
|
65
|
+
let transitions = [...spec.definition.transitions];
|
|
66
|
+
const hiddenSet = new Set(extension.hiddenSteps ?? []);
|
|
67
|
+
hiddenSet.forEach((stepId) => {
|
|
68
|
+
const idx = steps.findIndex((step) => step.id === stepId);
|
|
69
|
+
if (idx !== -1) {
|
|
70
|
+
steps.splice(idx, 1);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
if (hiddenSet.size) {
|
|
74
|
+
transitions = transitions.filter((transition) => !hiddenSet.has(transition.from) && !hiddenSet.has(transition.to));
|
|
75
|
+
}
|
|
76
|
+
extension.customSteps?.forEach((injection) => {
|
|
77
|
+
insertStep(steps, injection);
|
|
78
|
+
wireTransitions(transitions, injection);
|
|
79
|
+
});
|
|
80
|
+
spec.definition.steps = steps;
|
|
81
|
+
spec.definition.transitions = dedupeTransitions(transitions);
|
|
82
|
+
spec.meta = {
|
|
83
|
+
...spec.meta,
|
|
84
|
+
version: spec.meta.version
|
|
85
|
+
};
|
|
86
|
+
return spec;
|
|
87
|
+
}
|
|
88
|
+
function insertStep(steps, injection) {
|
|
89
|
+
const anchorIndex = resolveAnchorIndex(steps, injection);
|
|
90
|
+
if (anchorIndex === -1) {
|
|
91
|
+
throw new Error(`Unable to place injected step "${injection.inject.id}"`);
|
|
92
|
+
}
|
|
93
|
+
steps.splice(anchorIndex, 0, { ...injection.inject });
|
|
94
|
+
}
|
|
95
|
+
function resolveAnchorIndex(steps, injection) {
|
|
96
|
+
if (injection.after) {
|
|
97
|
+
const idx = steps.findIndex((step) => step.id === injection.after);
|
|
98
|
+
return idx === -1 ? -1 : idx + 1;
|
|
99
|
+
}
|
|
100
|
+
if (injection.before) {
|
|
101
|
+
const idx = steps.findIndex((step) => step.id === injection.before);
|
|
102
|
+
return idx === -1 ? -1 : idx;
|
|
103
|
+
}
|
|
104
|
+
return steps.length;
|
|
105
|
+
}
|
|
106
|
+
function wireTransitions(transitions, injection) {
|
|
107
|
+
if (!injection.inject.id)
|
|
108
|
+
return;
|
|
109
|
+
if (injection.transitionFrom) {
|
|
110
|
+
transitions.push({
|
|
111
|
+
from: injection.transitionFrom,
|
|
112
|
+
to: injection.inject.id,
|
|
113
|
+
condition: injection.when
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
if (injection.transitionTo) {
|
|
117
|
+
transitions.push({
|
|
118
|
+
from: injection.inject.id,
|
|
119
|
+
to: injection.transitionTo,
|
|
120
|
+
condition: injection.when
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
function dedupeTransitions(transitions) {
|
|
125
|
+
const seen = new Set;
|
|
126
|
+
const result = [];
|
|
127
|
+
transitions.forEach((transition) => {
|
|
128
|
+
const key = `${transition.from}->${transition.to}:${transition.condition ?? ""}`;
|
|
129
|
+
if (seen.has(key))
|
|
130
|
+
return;
|
|
131
|
+
seen.add(key);
|
|
132
|
+
result.push(transition);
|
|
133
|
+
});
|
|
134
|
+
return result;
|
|
135
|
+
}
|
|
136
|
+
function cloneWorkflowSpec(spec) {
|
|
137
|
+
return JSON.parse(JSON.stringify(spec));
|
|
138
|
+
}
|
|
139
|
+
// src/composer.ts
|
|
140
|
+
import { satisfies } from "compare-versions";
|
|
141
|
+
|
|
142
|
+
class WorkflowComposer {
|
|
143
|
+
extensions = [];
|
|
144
|
+
register(extension) {
|
|
145
|
+
this.extensions.push(extension);
|
|
146
|
+
return this;
|
|
147
|
+
}
|
|
148
|
+
registerMany(extensions) {
|
|
149
|
+
extensions.forEach((extension) => this.register(extension));
|
|
150
|
+
return this;
|
|
151
|
+
}
|
|
152
|
+
compose(params) {
|
|
153
|
+
const applicable = this.extensions.filter((extension) => matches(params, extension)).sort((a, b) => (a.priority ?? 0) - (b.priority ?? 0));
|
|
154
|
+
return applicable.reduce((acc, extension) => applyWorkflowExtension(acc, extension), params.base);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
function matches(params, extension) {
|
|
158
|
+
if (extension.workflow !== params.base.meta.key)
|
|
159
|
+
return false;
|
|
160
|
+
if (extension.baseVersion && !satisfies(params.base.meta.version, extension.baseVersion)) {
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
if (extension.tenantId && extension.tenantId !== params.tenantId) {
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
if (extension.role && extension.role !== params.role) {
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
if (extension.device && extension.device !== params.device) {
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
174
|
+
// src/merger.ts
|
|
175
|
+
function mergeExtensions(extensions) {
|
|
176
|
+
return extensions.sort((a, b) => (a.priority ?? 0) - (b.priority ?? 0));
|
|
177
|
+
}
|
|
178
|
+
export {
|
|
179
|
+
validateExtension,
|
|
180
|
+
mergeExtensions,
|
|
181
|
+
approvalStepTemplate,
|
|
182
|
+
applyWorkflowExtension,
|
|
183
|
+
WorkflowComposer
|
|
184
|
+
};
|
package/dist/injector.d.ts
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
//#region src/injector.d.ts
|
|
5
|
-
declare function applyWorkflowExtension(base: WorkflowSpec, extension: WorkflowExtension): WorkflowSpec;
|
|
6
|
-
//#endregion
|
|
7
|
-
export { applyWorkflowExtension };
|
|
1
|
+
import type { WorkflowSpec } from '@contractspec/lib.contracts';
|
|
2
|
+
import type { WorkflowExtension } from './types';
|
|
3
|
+
export declare function applyWorkflowExtension(base: WorkflowSpec, extension: WorkflowExtension): WorkflowSpec;
|
|
8
4
|
//# sourceMappingURL=injector.d.ts.map
|
package/dist/injector.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"injector.d.ts","
|
|
1
|
+
{"version":3,"file":"injector.d.ts","sourceRoot":"","sources":["../src/injector.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAChE,OAAO,KAAK,EAAE,iBAAiB,EAAiB,MAAM,SAAS,CAAC;AAGhE,wBAAgB,sBAAsB,CACpC,IAAI,EAAE,YAAY,EAClB,SAAS,EAAE,iBAAiB,GAC3B,YAAY,CAmCd"}
|
package/dist/merger.d.ts
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
import { WorkflowExtension } from
|
|
2
|
-
|
|
3
|
-
//#region src/merger.d.ts
|
|
4
|
-
declare function mergeExtensions(extensions: WorkflowExtension[]): WorkflowExtension[];
|
|
5
|
-
//#endregion
|
|
6
|
-
export { mergeExtensions };
|
|
1
|
+
import type { WorkflowExtension } from './types';
|
|
2
|
+
export declare function mergeExtensions(extensions: WorkflowExtension[]): WorkflowExtension[];
|
|
7
3
|
//# sourceMappingURL=merger.d.ts.map
|
package/dist/merger.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"merger.d.ts","
|
|
1
|
+
{"version":3,"file":"merger.d.ts","sourceRoot":"","sources":["../src/merger.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAEjD,wBAAgB,eAAe,CAC7B,UAAU,EAAE,iBAAiB,EAAE,GAC9B,iBAAiB,EAAE,CAErB"}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
// src/templates.ts
|
|
2
|
+
function approvalStepTemplate(options) {
|
|
3
|
+
return {
|
|
4
|
+
id: options.id,
|
|
5
|
+
label: options.label,
|
|
6
|
+
type: options.type ?? "human",
|
|
7
|
+
description: options.description ?? "Tenant-specific approval",
|
|
8
|
+
action: options.action,
|
|
9
|
+
guard: options.guardExpression ? {
|
|
10
|
+
type: "expression",
|
|
11
|
+
value: options.guardExpression
|
|
12
|
+
} : undefined
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
// src/validator.ts
|
|
16
|
+
function validateExtension(extension, base) {
|
|
17
|
+
const issues = [];
|
|
18
|
+
const stepIds = new Set(base.definition.steps.map((step) => step.id));
|
|
19
|
+
extension.customSteps?.forEach((injection, idx) => {
|
|
20
|
+
if (!injection.inject.id) {
|
|
21
|
+
issues.push({
|
|
22
|
+
code: "workflow.extension.step.id",
|
|
23
|
+
message: `customSteps[${idx}] is missing an id`
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
if (!injection.after && !injection.before) {
|
|
27
|
+
issues.push({
|
|
28
|
+
code: "workflow.extension.step.anchor",
|
|
29
|
+
message: `customSteps[${idx}] must set after or before`
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
if (injection.after && !stepIds.has(injection.after)) {
|
|
33
|
+
issues.push({
|
|
34
|
+
code: "workflow.extension.step.after",
|
|
35
|
+
message: `customSteps[${idx}] references unknown step "${injection.after}"`
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
if (injection.before && !stepIds.has(injection.before)) {
|
|
39
|
+
issues.push({
|
|
40
|
+
code: "workflow.extension.step.before",
|
|
41
|
+
message: `customSteps[${idx}] references unknown step "${injection.before}"`
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
extension.hiddenSteps?.forEach((stepId) => {
|
|
46
|
+
if (!stepIds.has(stepId)) {
|
|
47
|
+
issues.push({
|
|
48
|
+
code: "workflow.extension.hidden-step",
|
|
49
|
+
message: `hidden step "${stepId}" does not exist`
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
if (issues.length) {
|
|
54
|
+
const reason = issues.map((issue) => `${issue.code}: ${issue.message}`).join("; ");
|
|
55
|
+
throw new Error(`Invalid workflow extension for ${extension.workflow}: ${reason}`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// src/injector.ts
|
|
60
|
+
function applyWorkflowExtension(base, extension) {
|
|
61
|
+
validateExtension(extension, base);
|
|
62
|
+
const spec = cloneWorkflowSpec(base);
|
|
63
|
+
const steps = [...spec.definition.steps];
|
|
64
|
+
let transitions = [...spec.definition.transitions];
|
|
65
|
+
const hiddenSet = new Set(extension.hiddenSteps ?? []);
|
|
66
|
+
hiddenSet.forEach((stepId) => {
|
|
67
|
+
const idx = steps.findIndex((step) => step.id === stepId);
|
|
68
|
+
if (idx !== -1) {
|
|
69
|
+
steps.splice(idx, 1);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
if (hiddenSet.size) {
|
|
73
|
+
transitions = transitions.filter((transition) => !hiddenSet.has(transition.from) && !hiddenSet.has(transition.to));
|
|
74
|
+
}
|
|
75
|
+
extension.customSteps?.forEach((injection) => {
|
|
76
|
+
insertStep(steps, injection);
|
|
77
|
+
wireTransitions(transitions, injection);
|
|
78
|
+
});
|
|
79
|
+
spec.definition.steps = steps;
|
|
80
|
+
spec.definition.transitions = dedupeTransitions(transitions);
|
|
81
|
+
spec.meta = {
|
|
82
|
+
...spec.meta,
|
|
83
|
+
version: spec.meta.version
|
|
84
|
+
};
|
|
85
|
+
return spec;
|
|
86
|
+
}
|
|
87
|
+
function insertStep(steps, injection) {
|
|
88
|
+
const anchorIndex = resolveAnchorIndex(steps, injection);
|
|
89
|
+
if (anchorIndex === -1) {
|
|
90
|
+
throw new Error(`Unable to place injected step "${injection.inject.id}"`);
|
|
91
|
+
}
|
|
92
|
+
steps.splice(anchorIndex, 0, { ...injection.inject });
|
|
93
|
+
}
|
|
94
|
+
function resolveAnchorIndex(steps, injection) {
|
|
95
|
+
if (injection.after) {
|
|
96
|
+
const idx = steps.findIndex((step) => step.id === injection.after);
|
|
97
|
+
return idx === -1 ? -1 : idx + 1;
|
|
98
|
+
}
|
|
99
|
+
if (injection.before) {
|
|
100
|
+
const idx = steps.findIndex((step) => step.id === injection.before);
|
|
101
|
+
return idx === -1 ? -1 : idx;
|
|
102
|
+
}
|
|
103
|
+
return steps.length;
|
|
104
|
+
}
|
|
105
|
+
function wireTransitions(transitions, injection) {
|
|
106
|
+
if (!injection.inject.id)
|
|
107
|
+
return;
|
|
108
|
+
if (injection.transitionFrom) {
|
|
109
|
+
transitions.push({
|
|
110
|
+
from: injection.transitionFrom,
|
|
111
|
+
to: injection.inject.id,
|
|
112
|
+
condition: injection.when
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
if (injection.transitionTo) {
|
|
116
|
+
transitions.push({
|
|
117
|
+
from: injection.inject.id,
|
|
118
|
+
to: injection.transitionTo,
|
|
119
|
+
condition: injection.when
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
function dedupeTransitions(transitions) {
|
|
124
|
+
const seen = new Set;
|
|
125
|
+
const result = [];
|
|
126
|
+
transitions.forEach((transition) => {
|
|
127
|
+
const key = `${transition.from}->${transition.to}:${transition.condition ?? ""}`;
|
|
128
|
+
if (seen.has(key))
|
|
129
|
+
return;
|
|
130
|
+
seen.add(key);
|
|
131
|
+
result.push(transition);
|
|
132
|
+
});
|
|
133
|
+
return result;
|
|
134
|
+
}
|
|
135
|
+
function cloneWorkflowSpec(spec) {
|
|
136
|
+
return JSON.parse(JSON.stringify(spec));
|
|
137
|
+
}
|
|
138
|
+
// src/composer.ts
|
|
139
|
+
import { satisfies } from "compare-versions";
|
|
140
|
+
|
|
141
|
+
class WorkflowComposer {
|
|
142
|
+
extensions = [];
|
|
143
|
+
register(extension) {
|
|
144
|
+
this.extensions.push(extension);
|
|
145
|
+
return this;
|
|
146
|
+
}
|
|
147
|
+
registerMany(extensions) {
|
|
148
|
+
extensions.forEach((extension) => this.register(extension));
|
|
149
|
+
return this;
|
|
150
|
+
}
|
|
151
|
+
compose(params) {
|
|
152
|
+
const applicable = this.extensions.filter((extension) => matches(params, extension)).sort((a, b) => (a.priority ?? 0) - (b.priority ?? 0));
|
|
153
|
+
return applicable.reduce((acc, extension) => applyWorkflowExtension(acc, extension), params.base);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
function matches(params, extension) {
|
|
157
|
+
if (extension.workflow !== params.base.meta.key)
|
|
158
|
+
return false;
|
|
159
|
+
if (extension.baseVersion && !satisfies(params.base.meta.version, extension.baseVersion)) {
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
if (extension.tenantId && extension.tenantId !== params.tenantId) {
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
if (extension.role && extension.role !== params.role) {
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
if (extension.device && extension.device !== params.device) {
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
173
|
+
// src/merger.ts
|
|
174
|
+
function mergeExtensions(extensions) {
|
|
175
|
+
return extensions.sort((a, b) => (a.priority ?? 0) - (b.priority ?? 0));
|
|
176
|
+
}
|
|
177
|
+
export {
|
|
178
|
+
validateExtension,
|
|
179
|
+
mergeExtensions,
|
|
180
|
+
approvalStepTemplate,
|
|
181
|
+
applyWorkflowExtension,
|
|
182
|
+
WorkflowComposer
|
|
183
|
+
};
|
package/dist/templates.d.ts
CHANGED
|
@@ -1,15 +1,11 @@
|
|
|
1
|
-
import { Step, StepAction, StepType } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
action?: StepAction;
|
|
10
|
-
guardExpression?: string;
|
|
1
|
+
import type { Step, StepAction, StepType } from '@contractspec/lib.contracts';
|
|
2
|
+
export interface StepTemplateOptions {
|
|
3
|
+
id: string;
|
|
4
|
+
label: string;
|
|
5
|
+
type?: StepType;
|
|
6
|
+
description?: string;
|
|
7
|
+
action?: StepAction;
|
|
8
|
+
guardExpression?: string;
|
|
11
9
|
}
|
|
12
|
-
declare function approvalStepTemplate(options: StepTemplateOptions): Step;
|
|
13
|
-
//#endregion
|
|
14
|
-
export { StepTemplateOptions, approvalStepTemplate };
|
|
10
|
+
export declare function approvalStepTemplate(options: StepTemplateOptions): Step;
|
|
15
11
|
//# sourceMappingURL=templates.d.ts.map
|
package/dist/templates.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"templates.d.ts","
|
|
1
|
+
{"version":3,"file":"templates.d.ts","sourceRoot":"","sources":["../src/templates.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,6BAA6B,CAAC;AAE9E,MAAM,WAAW,mBAAmB;IAClC,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,QAAQ,CAAC;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,MAAM,CAAC,EAAE,UAAU,CAAC;IACpB,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B;AAED,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,mBAAmB,GAAG,IAAI,CAcvE"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,32 +1,28 @@
|
|
|
1
|
-
import { Step, WorkflowSpec } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
role?: string;
|
|
7
|
-
device?: string;
|
|
1
|
+
import type { Step, WorkflowSpec } from '@contractspec/lib.contracts';
|
|
2
|
+
export interface WorkflowExtensionScope {
|
|
3
|
+
tenantId?: string;
|
|
4
|
+
role?: string;
|
|
5
|
+
device?: string;
|
|
8
6
|
}
|
|
9
|
-
interface StepInjection {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
7
|
+
export interface StepInjection {
|
|
8
|
+
id?: string;
|
|
9
|
+
after?: string;
|
|
10
|
+
before?: string;
|
|
11
|
+
inject: Step;
|
|
12
|
+
when?: string;
|
|
13
|
+
transitionTo?: string;
|
|
14
|
+
transitionFrom?: string;
|
|
17
15
|
}
|
|
18
|
-
interface WorkflowExtension extends WorkflowExtensionScope {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
16
|
+
export interface WorkflowExtension extends WorkflowExtensionScope {
|
|
17
|
+
workflow: string;
|
|
18
|
+
baseVersion?: string;
|
|
19
|
+
priority?: number;
|
|
20
|
+
customSteps?: StepInjection[];
|
|
21
|
+
hiddenSteps?: string[];
|
|
22
|
+
metadata?: Record<string, unknown>;
|
|
23
|
+
annotations?: Record<string, unknown>;
|
|
26
24
|
}
|
|
27
|
-
interface ComposeParams extends WorkflowExtensionScope {
|
|
28
|
-
|
|
25
|
+
export interface ComposeParams extends WorkflowExtensionScope {
|
|
26
|
+
base: WorkflowSpec;
|
|
29
27
|
}
|
|
30
|
-
//#endregion
|
|
31
|
-
export { ComposeParams, StepInjection, WorkflowExtension, WorkflowExtensionScope };
|
|
32
28
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAEtE,MAAM,WAAW,sBAAsB;IACrC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,aAAa;IAC5B,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,IAAI,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAED,MAAM,WAAW,iBAAkB,SAAQ,sBAAsB;IAC/D,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,WAAW,CAAC,EAAE,aAAa,EAAE,CAAC;IAC9B,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;IACvB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACnC,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACvC;AAED,MAAM,WAAW,aAAc,SAAQ,sBAAsB;IAC3D,IAAI,EAAE,YAAY,CAAC;CACpB"}
|
package/dist/validator.d.ts
CHANGED
|
@@ -1,12 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
code: string;
|
|
7
|
-
message: string;
|
|
1
|
+
import type { WorkflowSpec } from '@contractspec/lib.contracts';
|
|
2
|
+
import type { WorkflowExtension } from './types';
|
|
3
|
+
export interface WorkflowExtensionValidationIssue {
|
|
4
|
+
code: string;
|
|
5
|
+
message: string;
|
|
8
6
|
}
|
|
9
|
-
declare function validateExtension(extension: WorkflowExtension, base: WorkflowSpec): void;
|
|
10
|
-
//#endregion
|
|
11
|
-
export { WorkflowExtensionValidationIssue, validateExtension };
|
|
7
|
+
export declare function validateExtension(extension: WorkflowExtension, base: WorkflowSpec): void;
|
|
12
8
|
//# sourceMappingURL=validator.d.ts.map
|
package/dist/validator.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validator.d.ts","
|
|
1
|
+
{"version":3,"file":"validator.d.ts","sourceRoot":"","sources":["../src/validator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAChE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAEjD,MAAM,WAAW,gCAAgC;IAC/C,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,wBAAgB,iBAAiB,CAC/B,SAAS,EAAE,iBAAiB,EAC5B,IAAI,EAAE,YAAY,QAoDnB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contractspec/lib.workflow-composer",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.59.0",
|
|
4
4
|
"description": "Tenant-aware workflow composition helpers for ContractSpec.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"contractspec",
|
|
@@ -18,35 +18,40 @@
|
|
|
18
18
|
"scripts": {
|
|
19
19
|
"publish:pkg": "bun publish --tolerate-republish --ignore-scripts --verbose",
|
|
20
20
|
"publish:pkg:canary": "bun publish:pkg --tag canary",
|
|
21
|
-
"build": "bun build:
|
|
22
|
-
"build:bundle": "
|
|
23
|
-
"build:types": "
|
|
24
|
-
"dev": "bun
|
|
21
|
+
"build": "bun run prebuild && bun run build:bundle && bun run build:types",
|
|
22
|
+
"build:bundle": "contractspec-bun-build transpile",
|
|
23
|
+
"build:types": "contractspec-bun-build types",
|
|
24
|
+
"dev": "contractspec-bun-build dev",
|
|
25
25
|
"clean": "rimraf dist .turbo",
|
|
26
26
|
"lint": "bun lint:fix",
|
|
27
27
|
"lint:fix": "eslint src --fix",
|
|
28
28
|
"lint:check": "eslint src",
|
|
29
|
-
"test": "bun test"
|
|
29
|
+
"test": "bun test",
|
|
30
|
+
"prebuild": "contractspec-bun-build prebuild",
|
|
31
|
+
"typecheck": "tsc --noEmit"
|
|
30
32
|
},
|
|
31
33
|
"dependencies": {
|
|
32
|
-
"@contractspec/lib.contracts": "1.
|
|
34
|
+
"@contractspec/lib.contracts": "1.59.0",
|
|
33
35
|
"compare-versions": "^6.1.1"
|
|
34
36
|
},
|
|
35
37
|
"devDependencies": {
|
|
36
|
-
"@contractspec/tool.
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"typescript": "^5.9.3"
|
|
38
|
+
"@contractspec/tool.typescript": "1.59.0",
|
|
39
|
+
"typescript": "^5.9.3",
|
|
40
|
+
"@contractspec/tool.bun": "1.58.0"
|
|
40
41
|
},
|
|
41
42
|
"exports": {
|
|
42
|
-
".": "./
|
|
43
|
-
"./*": "./*"
|
|
43
|
+
".": "./src/index.ts"
|
|
44
44
|
},
|
|
45
45
|
"publishConfig": {
|
|
46
46
|
"access": "public",
|
|
47
47
|
"exports": {
|
|
48
|
-
".":
|
|
49
|
-
|
|
48
|
+
".": {
|
|
49
|
+
"types": "./dist/index.d.ts",
|
|
50
|
+
"bun": "./dist/index.js",
|
|
51
|
+
"node": "./dist/node/index.mjs",
|
|
52
|
+
"browser": "./dist/browser/index.js",
|
|
53
|
+
"default": "./dist/index.js"
|
|
54
|
+
}
|
|
50
55
|
},
|
|
51
56
|
"registry": "https://registry.npmjs.org/"
|
|
52
57
|
},
|
package/dist/composer.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { applyWorkflowExtension } from "./injector.js";
|
|
2
|
-
import { satisfies } from "compare-versions";
|
|
3
|
-
|
|
4
|
-
//#region src/composer.ts
|
|
5
|
-
var WorkflowComposer = class {
|
|
6
|
-
extensions = [];
|
|
7
|
-
register(extension) {
|
|
8
|
-
this.extensions.push(extension);
|
|
9
|
-
return this;
|
|
10
|
-
}
|
|
11
|
-
registerMany(extensions) {
|
|
12
|
-
extensions.forEach((extension) => this.register(extension));
|
|
13
|
-
return this;
|
|
14
|
-
}
|
|
15
|
-
compose(params) {
|
|
16
|
-
return this.extensions.filter((extension) => matches(params, extension)).sort((a, b) => (a.priority ?? 0) - (b.priority ?? 0)).reduce((acc, extension) => applyWorkflowExtension(acc, extension), params.base);
|
|
17
|
-
}
|
|
18
|
-
};
|
|
19
|
-
function matches(params, extension) {
|
|
20
|
-
if (extension.workflow !== params.base.meta.key) return false;
|
|
21
|
-
if (extension.baseVersion && !satisfies(params.base.meta.version, extension.baseVersion)) return false;
|
|
22
|
-
if (extension.tenantId && extension.tenantId !== params.tenantId) return false;
|
|
23
|
-
if (extension.role && extension.role !== params.role) return false;
|
|
24
|
-
if (extension.device && extension.device !== params.device) return false;
|
|
25
|
-
return true;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
//#endregion
|
|
29
|
-
export { WorkflowComposer };
|
|
30
|
-
//# sourceMappingURL=composer.js.map
|
package/dist/composer.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"composer.js","names":[],"sources":["../src/composer.ts"],"sourcesContent":["import type { WorkflowSpec } from '@contractspec/lib.contracts';\nimport { applyWorkflowExtension } from './injector';\nimport type { ComposeParams, WorkflowExtension } from './types';\nimport { satisfies } from 'compare-versions';\n\nexport class WorkflowComposer {\n private readonly extensions: WorkflowExtension[] = [];\n\n register(extension: WorkflowExtension): this {\n this.extensions.push(extension);\n return this;\n }\n\n registerMany(extensions: WorkflowExtension[]): this {\n extensions.forEach((extension) => this.register(extension));\n return this;\n }\n\n compose(params: ComposeParams): WorkflowSpec {\n const applicable = this.extensions\n .filter((extension) => matches(params, extension))\n .sort((a, b) => (a.priority ?? 0) - (b.priority ?? 0));\n\n return applicable.reduce(\n (acc, extension) => applyWorkflowExtension(acc, extension),\n params.base\n );\n }\n}\n\nfunction matches(params: ComposeParams, extension: WorkflowExtension) {\n if (extension.workflow !== params.base.meta.key) return false;\n if (\n extension.baseVersion &&\n !satisfies(params.base.meta.version, extension.baseVersion)\n ) {\n return false;\n }\n if (extension.tenantId && extension.tenantId !== params.tenantId) {\n return false;\n }\n if (extension.role && extension.role !== params.role) {\n return false;\n }\n if (extension.device && extension.device !== params.device) {\n return false;\n }\n return true;\n}\n"],"mappings":";;;;AAKA,IAAa,mBAAb,MAA8B;CAC5B,AAAiB,aAAkC,EAAE;CAErD,SAAS,WAAoC;AAC3C,OAAK,WAAW,KAAK,UAAU;AAC/B,SAAO;;CAGT,aAAa,YAAuC;AAClD,aAAW,SAAS,cAAc,KAAK,SAAS,UAAU,CAAC;AAC3D,SAAO;;CAGT,QAAQ,QAAqC;AAK3C,SAJmB,KAAK,WACrB,QAAQ,cAAc,QAAQ,QAAQ,UAAU,CAAC,CACjD,MAAM,GAAG,OAAO,EAAE,YAAY,MAAM,EAAE,YAAY,GAAG,CAEtC,QACf,KAAK,cAAc,uBAAuB,KAAK,UAAU,EAC1D,OAAO,KACR;;;AAIL,SAAS,QAAQ,QAAuB,WAA8B;AACpE,KAAI,UAAU,aAAa,OAAO,KAAK,KAAK,IAAK,QAAO;AACxD,KACE,UAAU,eACV,CAAC,UAAU,OAAO,KAAK,KAAK,SAAS,UAAU,YAAY,CAE3D,QAAO;AAET,KAAI,UAAU,YAAY,UAAU,aAAa,OAAO,SACtD,QAAO;AAET,KAAI,UAAU,QAAQ,UAAU,SAAS,OAAO,KAC9C,QAAO;AAET,KAAI,UAAU,UAAU,UAAU,WAAW,OAAO,OAClD,QAAO;AAET,QAAO"}
|
package/dist/injector.js
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { validateExtension } from "./validator.js";
|
|
2
|
-
|
|
3
|
-
//#region src/injector.ts
|
|
4
|
-
function applyWorkflowExtension(base, extension) {
|
|
5
|
-
validateExtension(extension, base);
|
|
6
|
-
const spec = cloneWorkflowSpec(base);
|
|
7
|
-
const steps = [...spec.definition.steps];
|
|
8
|
-
let transitions = [...spec.definition.transitions];
|
|
9
|
-
const hiddenSet = new Set(extension.hiddenSteps ?? []);
|
|
10
|
-
hiddenSet.forEach((stepId) => {
|
|
11
|
-
const idx = steps.findIndex((step) => step.id === stepId);
|
|
12
|
-
if (idx !== -1) steps.splice(idx, 1);
|
|
13
|
-
});
|
|
14
|
-
if (hiddenSet.size) transitions = transitions.filter((transition) => !hiddenSet.has(transition.from) && !hiddenSet.has(transition.to));
|
|
15
|
-
extension.customSteps?.forEach((injection) => {
|
|
16
|
-
insertStep(steps, injection);
|
|
17
|
-
wireTransitions(transitions, injection);
|
|
18
|
-
});
|
|
19
|
-
spec.definition.steps = steps;
|
|
20
|
-
spec.definition.transitions = dedupeTransitions(transitions);
|
|
21
|
-
spec.meta = {
|
|
22
|
-
...spec.meta,
|
|
23
|
-
version: spec.meta.version
|
|
24
|
-
};
|
|
25
|
-
return spec;
|
|
26
|
-
}
|
|
27
|
-
function insertStep(steps, injection) {
|
|
28
|
-
const anchorIndex = resolveAnchorIndex(steps, injection);
|
|
29
|
-
if (anchorIndex === -1) throw new Error(`Unable to place injected step "${injection.inject.id}"`);
|
|
30
|
-
steps.splice(anchorIndex, 0, { ...injection.inject });
|
|
31
|
-
}
|
|
32
|
-
function resolveAnchorIndex(steps, injection) {
|
|
33
|
-
if (injection.after) {
|
|
34
|
-
const idx = steps.findIndex((step) => step.id === injection.after);
|
|
35
|
-
return idx === -1 ? -1 : idx + 1;
|
|
36
|
-
}
|
|
37
|
-
if (injection.before) {
|
|
38
|
-
const idx = steps.findIndex((step) => step.id === injection.before);
|
|
39
|
-
return idx === -1 ? -1 : idx;
|
|
40
|
-
}
|
|
41
|
-
return steps.length;
|
|
42
|
-
}
|
|
43
|
-
function wireTransitions(transitions, injection) {
|
|
44
|
-
if (!injection.inject.id) return;
|
|
45
|
-
if (injection.transitionFrom) transitions.push({
|
|
46
|
-
from: injection.transitionFrom,
|
|
47
|
-
to: injection.inject.id,
|
|
48
|
-
condition: injection.when
|
|
49
|
-
});
|
|
50
|
-
if (injection.transitionTo) transitions.push({
|
|
51
|
-
from: injection.inject.id,
|
|
52
|
-
to: injection.transitionTo,
|
|
53
|
-
condition: injection.when
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
function dedupeTransitions(transitions) {
|
|
57
|
-
const seen = /* @__PURE__ */ new Set();
|
|
58
|
-
const result = [];
|
|
59
|
-
transitions.forEach((transition) => {
|
|
60
|
-
const key = `${transition.from}->${transition.to}:${transition.condition ?? ""}`;
|
|
61
|
-
if (seen.has(key)) return;
|
|
62
|
-
seen.add(key);
|
|
63
|
-
result.push(transition);
|
|
64
|
-
});
|
|
65
|
-
return result;
|
|
66
|
-
}
|
|
67
|
-
function cloneWorkflowSpec(spec) {
|
|
68
|
-
return JSON.parse(JSON.stringify(spec));
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
//#endregion
|
|
72
|
-
export { applyWorkflowExtension };
|
|
73
|
-
//# sourceMappingURL=injector.js.map
|
package/dist/injector.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"injector.js","names":[],"sources":["../src/injector.ts"],"sourcesContent":["import type { WorkflowSpec } from '@contractspec/lib.contracts';\nimport type { WorkflowExtension, StepInjection } from './types';\nimport { validateExtension } from './validator';\n\nexport function applyWorkflowExtension(\n base: WorkflowSpec,\n extension: WorkflowExtension\n): WorkflowSpec {\n validateExtension(extension, base);\n const spec = cloneWorkflowSpec(base);\n\n const steps = [...spec.definition.steps];\n let transitions = [...spec.definition.transitions];\n\n const hiddenSet = new Set(extension.hiddenSteps ?? []);\n\n hiddenSet.forEach((stepId) => {\n const idx = steps.findIndex((step) => step.id === stepId);\n if (idx !== -1) {\n steps.splice(idx, 1);\n }\n });\n\n if (hiddenSet.size) {\n transitions = transitions.filter(\n (transition) =>\n !hiddenSet.has(transition.from) && !hiddenSet.has(transition.to)\n );\n }\n\n extension.customSteps?.forEach((injection) => {\n insertStep(steps, injection);\n wireTransitions(transitions, injection);\n });\n\n spec.definition.steps = steps;\n spec.definition.transitions = dedupeTransitions(transitions);\n spec.meta = {\n ...spec.meta,\n version: spec.meta.version,\n };\n return spec;\n}\n\nfunction insertStep(\n steps: WorkflowSpec['definition']['steps'],\n injection: StepInjection\n) {\n const anchorIndex = resolveAnchorIndex(steps, injection);\n if (anchorIndex === -1) {\n throw new Error(`Unable to place injected step \"${injection.inject.id}\"`);\n }\n steps.splice(anchorIndex, 0, { ...injection.inject });\n}\n\nfunction resolveAnchorIndex(\n steps: WorkflowSpec['definition']['steps'],\n injection: StepInjection\n) {\n if (injection.after) {\n const idx = steps.findIndex((step) => step.id === injection.after);\n return idx === -1 ? -1 : idx + 1;\n }\n if (injection.before) {\n const idx = steps.findIndex((step) => step.id === injection.before);\n return idx === -1 ? -1 : idx;\n }\n return steps.length;\n}\n\nfunction wireTransitions(\n transitions: WorkflowSpec['definition']['transitions'],\n injection: StepInjection\n) {\n if (!injection.inject.id) return;\n\n if (injection.transitionFrom) {\n transitions.push({\n from: injection.transitionFrom,\n to: injection.inject.id,\n condition: injection.when,\n });\n }\n\n if (injection.transitionTo) {\n transitions.push({\n from: injection.inject.id,\n to: injection.transitionTo,\n condition: injection.when,\n });\n }\n}\n\nfunction dedupeTransitions(\n transitions: WorkflowSpec['definition']['transitions']\n): WorkflowSpec['definition']['transitions'] {\n const seen = new Set<string>();\n const result: typeof transitions = [];\n transitions.forEach((transition) => {\n const key = `${transition.from}->${transition.to}:${transition.condition ?? ''}`;\n if (seen.has(key)) return;\n seen.add(key);\n result.push(transition);\n });\n return result;\n}\n\nfunction cloneWorkflowSpec(spec: WorkflowSpec): WorkflowSpec {\n return JSON.parse(JSON.stringify(spec));\n}\n"],"mappings":";;;AAIA,SAAgB,uBACd,MACA,WACc;AACd,mBAAkB,WAAW,KAAK;CAClC,MAAM,OAAO,kBAAkB,KAAK;CAEpC,MAAM,QAAQ,CAAC,GAAG,KAAK,WAAW,MAAM;CACxC,IAAI,cAAc,CAAC,GAAG,KAAK,WAAW,YAAY;CAElD,MAAM,YAAY,IAAI,IAAI,UAAU,eAAe,EAAE,CAAC;AAEtD,WAAU,SAAS,WAAW;EAC5B,MAAM,MAAM,MAAM,WAAW,SAAS,KAAK,OAAO,OAAO;AACzD,MAAI,QAAQ,GACV,OAAM,OAAO,KAAK,EAAE;GAEtB;AAEF,KAAI,UAAU,KACZ,eAAc,YAAY,QACvB,eACC,CAAC,UAAU,IAAI,WAAW,KAAK,IAAI,CAAC,UAAU,IAAI,WAAW,GAAG,CACnE;AAGH,WAAU,aAAa,SAAS,cAAc;AAC5C,aAAW,OAAO,UAAU;AAC5B,kBAAgB,aAAa,UAAU;GACvC;AAEF,MAAK,WAAW,QAAQ;AACxB,MAAK,WAAW,cAAc,kBAAkB,YAAY;AAC5D,MAAK,OAAO;EACV,GAAG,KAAK;EACR,SAAS,KAAK,KAAK;EACpB;AACD,QAAO;;AAGT,SAAS,WACP,OACA,WACA;CACA,MAAM,cAAc,mBAAmB,OAAO,UAAU;AACxD,KAAI,gBAAgB,GAClB,OAAM,IAAI,MAAM,kCAAkC,UAAU,OAAO,GAAG,GAAG;AAE3E,OAAM,OAAO,aAAa,GAAG,EAAE,GAAG,UAAU,QAAQ,CAAC;;AAGvD,SAAS,mBACP,OACA,WACA;AACA,KAAI,UAAU,OAAO;EACnB,MAAM,MAAM,MAAM,WAAW,SAAS,KAAK,OAAO,UAAU,MAAM;AAClE,SAAO,QAAQ,KAAK,KAAK,MAAM;;AAEjC,KAAI,UAAU,QAAQ;EACpB,MAAM,MAAM,MAAM,WAAW,SAAS,KAAK,OAAO,UAAU,OAAO;AACnE,SAAO,QAAQ,KAAK,KAAK;;AAE3B,QAAO,MAAM;;AAGf,SAAS,gBACP,aACA,WACA;AACA,KAAI,CAAC,UAAU,OAAO,GAAI;AAE1B,KAAI,UAAU,eACZ,aAAY,KAAK;EACf,MAAM,UAAU;EAChB,IAAI,UAAU,OAAO;EACrB,WAAW,UAAU;EACtB,CAAC;AAGJ,KAAI,UAAU,aACZ,aAAY,KAAK;EACf,MAAM,UAAU,OAAO;EACvB,IAAI,UAAU;EACd,WAAW,UAAU;EACtB,CAAC;;AAIN,SAAS,kBACP,aAC2C;CAC3C,MAAM,uBAAO,IAAI,KAAa;CAC9B,MAAM,SAA6B,EAAE;AACrC,aAAY,SAAS,eAAe;EAClC,MAAM,MAAM,GAAG,WAAW,KAAK,IAAI,WAAW,GAAG,GAAG,WAAW,aAAa;AAC5E,MAAI,KAAK,IAAI,IAAI,CAAE;AACnB,OAAK,IAAI,IAAI;AACb,SAAO,KAAK,WAAW;GACvB;AACF,QAAO;;AAGT,SAAS,kBAAkB,MAAkC;AAC3D,QAAO,KAAK,MAAM,KAAK,UAAU,KAAK,CAAC"}
|
package/dist/merger.js
DELETED
package/dist/merger.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"merger.js","names":[],"sources":["../src/merger.ts"],"sourcesContent":["import type { WorkflowExtension } from './types';\n\nexport function mergeExtensions(\n extensions: WorkflowExtension[]\n): WorkflowExtension[] {\n return extensions.sort((a, b) => (a.priority ?? 0) - (b.priority ?? 0));\n}\n"],"mappings":";AAEA,SAAgB,gBACd,YACqB;AACrB,QAAO,WAAW,MAAM,GAAG,OAAO,EAAE,YAAY,MAAM,EAAE,YAAY,GAAG"}
|
package/dist/templates.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
//#region src/templates.ts
|
|
2
|
-
function approvalStepTemplate(options) {
|
|
3
|
-
return {
|
|
4
|
-
id: options.id,
|
|
5
|
-
label: options.label,
|
|
6
|
-
type: options.type ?? "human",
|
|
7
|
-
description: options.description ?? "Tenant-specific approval",
|
|
8
|
-
action: options.action,
|
|
9
|
-
guard: options.guardExpression ? {
|
|
10
|
-
type: "expression",
|
|
11
|
-
value: options.guardExpression
|
|
12
|
-
} : void 0
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
//#endregion
|
|
17
|
-
export { approvalStepTemplate };
|
|
18
|
-
//# sourceMappingURL=templates.js.map
|
package/dist/templates.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"templates.js","names":[],"sources":["../src/templates.ts"],"sourcesContent":["import type { Step, StepAction, StepType } from '@contractspec/lib.contracts';\n\nexport interface StepTemplateOptions {\n id: string;\n label: string;\n type?: StepType;\n description?: string;\n action?: StepAction;\n guardExpression?: string;\n}\n\nexport function approvalStepTemplate(options: StepTemplateOptions): Step {\n return {\n id: options.id,\n label: options.label,\n type: options.type ?? 'human',\n description: options.description ?? 'Tenant-specific approval',\n action: options.action,\n guard: options.guardExpression\n ? {\n type: 'expression',\n value: options.guardExpression,\n }\n : undefined,\n };\n}\n"],"mappings":";AAWA,SAAgB,qBAAqB,SAAoC;AACvE,QAAO;EACL,IAAI,QAAQ;EACZ,OAAO,QAAQ;EACf,MAAM,QAAQ,QAAQ;EACtB,aAAa,QAAQ,eAAe;EACpC,QAAQ,QAAQ;EAChB,OAAO,QAAQ,kBACX;GACE,MAAM;GACN,OAAO,QAAQ;GAChB,GACD;EACL"}
|
package/dist/validator.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
//#region src/validator.ts
|
|
2
|
-
function validateExtension(extension, base) {
|
|
3
|
-
const issues = [];
|
|
4
|
-
const stepIds = new Set(base.definition.steps.map((step) => step.id));
|
|
5
|
-
extension.customSteps?.forEach((injection, idx) => {
|
|
6
|
-
if (!injection.inject.id) issues.push({
|
|
7
|
-
code: "workflow.extension.step.id",
|
|
8
|
-
message: `customSteps[${idx}] is missing an id`
|
|
9
|
-
});
|
|
10
|
-
if (!injection.after && !injection.before) issues.push({
|
|
11
|
-
code: "workflow.extension.step.anchor",
|
|
12
|
-
message: `customSteps[${idx}] must set after or before`
|
|
13
|
-
});
|
|
14
|
-
if (injection.after && !stepIds.has(injection.after)) issues.push({
|
|
15
|
-
code: "workflow.extension.step.after",
|
|
16
|
-
message: `customSteps[${idx}] references unknown step "${injection.after}"`
|
|
17
|
-
});
|
|
18
|
-
if (injection.before && !stepIds.has(injection.before)) issues.push({
|
|
19
|
-
code: "workflow.extension.step.before",
|
|
20
|
-
message: `customSteps[${idx}] references unknown step "${injection.before}"`
|
|
21
|
-
});
|
|
22
|
-
});
|
|
23
|
-
extension.hiddenSteps?.forEach((stepId) => {
|
|
24
|
-
if (!stepIds.has(stepId)) issues.push({
|
|
25
|
-
code: "workflow.extension.hidden-step",
|
|
26
|
-
message: `hidden step "${stepId}" does not exist`
|
|
27
|
-
});
|
|
28
|
-
});
|
|
29
|
-
if (issues.length) {
|
|
30
|
-
const reason = issues.map((issue) => `${issue.code}: ${issue.message}`).join("; ");
|
|
31
|
-
throw new Error(`Invalid workflow extension for ${extension.workflow}: ${reason}`);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
//#endregion
|
|
36
|
-
export { validateExtension };
|
|
37
|
-
//# sourceMappingURL=validator.js.map
|
package/dist/validator.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"validator.js","names":[],"sources":["../src/validator.ts"],"sourcesContent":["import type { WorkflowSpec } from '@contractspec/lib.contracts';\nimport type { WorkflowExtension } from './types';\n\nexport interface WorkflowExtensionValidationIssue {\n code: string;\n message: string;\n}\n\nexport function validateExtension(\n extension: WorkflowExtension,\n base: WorkflowSpec\n) {\n const issues: WorkflowExtensionValidationIssue[] = [];\n const stepIds = new Set(base.definition.steps.map((step) => step.id));\n\n extension.customSteps?.forEach((injection, idx) => {\n if (!injection.inject.id) {\n issues.push({\n code: 'workflow.extension.step.id',\n message: `customSteps[${idx}] is missing an id`,\n });\n }\n\n if (!injection.after && !injection.before) {\n issues.push({\n code: 'workflow.extension.step.anchor',\n message: `customSteps[${idx}] must set after or before`,\n });\n }\n\n if (injection.after && !stepIds.has(injection.after)) {\n issues.push({\n code: 'workflow.extension.step.after',\n message: `customSteps[${idx}] references unknown step \"${injection.after}\"`,\n });\n }\n\n if (injection.before && !stepIds.has(injection.before)) {\n issues.push({\n code: 'workflow.extension.step.before',\n message: `customSteps[${idx}] references unknown step \"${injection.before}\"`,\n });\n }\n });\n\n extension.hiddenSteps?.forEach((stepId) => {\n if (!stepIds.has(stepId)) {\n issues.push({\n code: 'workflow.extension.hidden-step',\n message: `hidden step \"${stepId}\" does not exist`,\n });\n }\n });\n\n if (issues.length) {\n const reason = issues\n .map((issue) => `${issue.code}: ${issue.message}`)\n .join('; ');\n throw new Error(\n `Invalid workflow extension for ${extension.workflow}: ${reason}`\n );\n }\n}\n"],"mappings":";AAQA,SAAgB,kBACd,WACA,MACA;CACA,MAAM,SAA6C,EAAE;CACrD,MAAM,UAAU,IAAI,IAAI,KAAK,WAAW,MAAM,KAAK,SAAS,KAAK,GAAG,CAAC;AAErE,WAAU,aAAa,SAAS,WAAW,QAAQ;AACjD,MAAI,CAAC,UAAU,OAAO,GACpB,QAAO,KAAK;GACV,MAAM;GACN,SAAS,eAAe,IAAI;GAC7B,CAAC;AAGJ,MAAI,CAAC,UAAU,SAAS,CAAC,UAAU,OACjC,QAAO,KAAK;GACV,MAAM;GACN,SAAS,eAAe,IAAI;GAC7B,CAAC;AAGJ,MAAI,UAAU,SAAS,CAAC,QAAQ,IAAI,UAAU,MAAM,CAClD,QAAO,KAAK;GACV,MAAM;GACN,SAAS,eAAe,IAAI,6BAA6B,UAAU,MAAM;GAC1E,CAAC;AAGJ,MAAI,UAAU,UAAU,CAAC,QAAQ,IAAI,UAAU,OAAO,CACpD,QAAO,KAAK;GACV,MAAM;GACN,SAAS,eAAe,IAAI,6BAA6B,UAAU,OAAO;GAC3E,CAAC;GAEJ;AAEF,WAAU,aAAa,SAAS,WAAW;AACzC,MAAI,CAAC,QAAQ,IAAI,OAAO,CACtB,QAAO,KAAK;GACV,MAAM;GACN,SAAS,gBAAgB,OAAO;GACjC,CAAC;GAEJ;AAEF,KAAI,OAAO,QAAQ;EACjB,MAAM,SAAS,OACZ,KAAK,UAAU,GAAG,MAAM,KAAK,IAAI,MAAM,UAAU,CACjD,KAAK,KAAK;AACb,QAAM,IAAI,MACR,kCAAkC,UAAU,SAAS,IAAI,SAC1D"}
|