@lark-apaas/nestjs-capability 0.1.1 → 0.1.2-alpha.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/index.cjs +96 -11
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +33 -2
- package/dist/index.d.ts +33 -2
- package/dist/index.js +96 -11
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -216,15 +216,46 @@ type ErrorCode = (typeof ErrorCodes)[keyof typeof ErrorCodes];
|
|
|
216
216
|
* 求值规则:
|
|
217
217
|
* - 如果整个字符串是单个表达式,保留原始类型
|
|
218
218
|
* - 如果是字符串插值(多个表达式或混合内容),返回字符串
|
|
219
|
-
* -
|
|
219
|
+
* - 如果变量不存在:
|
|
220
|
+
* - 未传 schema:返回原始表达式
|
|
221
|
+
* - 传了 schema(整串表达式):按 schema 返回默认值
|
|
222
|
+
* - 传了 schema(字符串插值):保留原始表达式
|
|
220
223
|
*/
|
|
221
224
|
declare class TemplateEngineService {
|
|
222
225
|
private readonly EXPR_REGEX;
|
|
223
226
|
private readonly WHOLE_STRING_EXPR_REGEX;
|
|
224
|
-
|
|
227
|
+
/**
|
|
228
|
+
* 解析 formValue 模板
|
|
229
|
+
* @param template - formValue 模板对象
|
|
230
|
+
* @param input - 用户输入参数
|
|
231
|
+
* @param paramsSchema - 可选,输入参数的 JSON Schema,用于推断默认值
|
|
232
|
+
* @returns 解析后的参数对象
|
|
233
|
+
*/
|
|
234
|
+
resolve(template: unknown, input: Record<string, unknown>, paramsSchema?: JSONSchema): unknown;
|
|
225
235
|
private resolveString;
|
|
226
236
|
private resolveObject;
|
|
227
237
|
private getValueByPath;
|
|
238
|
+
/**
|
|
239
|
+
* 根据路径从 schema 获取默认值
|
|
240
|
+
* @param path - 变量路径,如 "a.b.c"
|
|
241
|
+
* @param schema - JSON Schema
|
|
242
|
+
* @returns 默认值,如果无法确定则返回 undefined
|
|
243
|
+
*/
|
|
244
|
+
private getDefaultValueForPath;
|
|
245
|
+
/**
|
|
246
|
+
* 根据路径查找对应的 schema 定义
|
|
247
|
+
* @param path - 变量路径,如 "a.b.c"
|
|
248
|
+
* @param schema - 根 JSON Schema
|
|
249
|
+
* @returns 路径对应的 schema,如果不存在则返回 undefined
|
|
250
|
+
*/
|
|
251
|
+
private getSchemaForPath;
|
|
252
|
+
/**
|
|
253
|
+
* 从 schema 获取默认值
|
|
254
|
+
* 优先级:default > type > undefined
|
|
255
|
+
* @param schema - 字段的 JSON Schema
|
|
256
|
+
* @returns 默认值
|
|
257
|
+
*/
|
|
258
|
+
private getDefaultValueFromSchema;
|
|
228
259
|
}
|
|
229
260
|
|
|
230
261
|
declare class PluginNotFoundError extends Error {
|
package/dist/index.d.ts
CHANGED
|
@@ -216,15 +216,46 @@ type ErrorCode = (typeof ErrorCodes)[keyof typeof ErrorCodes];
|
|
|
216
216
|
* 求值规则:
|
|
217
217
|
* - 如果整个字符串是单个表达式,保留原始类型
|
|
218
218
|
* - 如果是字符串插值(多个表达式或混合内容),返回字符串
|
|
219
|
-
* -
|
|
219
|
+
* - 如果变量不存在:
|
|
220
|
+
* - 未传 schema:返回原始表达式
|
|
221
|
+
* - 传了 schema(整串表达式):按 schema 返回默认值
|
|
222
|
+
* - 传了 schema(字符串插值):保留原始表达式
|
|
220
223
|
*/
|
|
221
224
|
declare class TemplateEngineService {
|
|
222
225
|
private readonly EXPR_REGEX;
|
|
223
226
|
private readonly WHOLE_STRING_EXPR_REGEX;
|
|
224
|
-
|
|
227
|
+
/**
|
|
228
|
+
* 解析 formValue 模板
|
|
229
|
+
* @param template - formValue 模板对象
|
|
230
|
+
* @param input - 用户输入参数
|
|
231
|
+
* @param paramsSchema - 可选,输入参数的 JSON Schema,用于推断默认值
|
|
232
|
+
* @returns 解析后的参数对象
|
|
233
|
+
*/
|
|
234
|
+
resolve(template: unknown, input: Record<string, unknown>, paramsSchema?: JSONSchema): unknown;
|
|
225
235
|
private resolveString;
|
|
226
236
|
private resolveObject;
|
|
227
237
|
private getValueByPath;
|
|
238
|
+
/**
|
|
239
|
+
* 根据路径从 schema 获取默认值
|
|
240
|
+
* @param path - 变量路径,如 "a.b.c"
|
|
241
|
+
* @param schema - JSON Schema
|
|
242
|
+
* @returns 默认值,如果无法确定则返回 undefined
|
|
243
|
+
*/
|
|
244
|
+
private getDefaultValueForPath;
|
|
245
|
+
/**
|
|
246
|
+
* 根据路径查找对应的 schema 定义
|
|
247
|
+
* @param path - 变量路径,如 "a.b.c"
|
|
248
|
+
* @param schema - 根 JSON Schema
|
|
249
|
+
* @returns 路径对应的 schema,如果不存在则返回 undefined
|
|
250
|
+
*/
|
|
251
|
+
private getSchemaForPath;
|
|
252
|
+
/**
|
|
253
|
+
* 从 schema 获取默认值
|
|
254
|
+
* 优先级:default > type > undefined
|
|
255
|
+
* @param schema - 字段的 JSON Schema
|
|
256
|
+
* @returns 默认值
|
|
257
|
+
*/
|
|
258
|
+
private getDefaultValueFromSchema;
|
|
228
259
|
}
|
|
229
260
|
|
|
230
261
|
declare class PluginNotFoundError extends Error {
|
package/dist/index.js
CHANGED
|
@@ -26,6 +26,15 @@ function _ts_decorate(decorators, target, key, desc) {
|
|
|
26
26
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
27
27
|
}
|
|
28
28
|
__name(_ts_decorate, "_ts_decorate");
|
|
29
|
+
var TYPE_DEFAULT_VALUES = {
|
|
30
|
+
array: [],
|
|
31
|
+
object: {},
|
|
32
|
+
string: "",
|
|
33
|
+
number: 0,
|
|
34
|
+
integer: 0,
|
|
35
|
+
boolean: false,
|
|
36
|
+
null: null
|
|
37
|
+
};
|
|
29
38
|
var TemplateEngineService = class {
|
|
30
39
|
static {
|
|
31
40
|
__name(this, "TemplateEngineService");
|
|
@@ -34,24 +43,40 @@ var TemplateEngineService = class {
|
|
|
34
43
|
EXPR_REGEX = /\{\{\s*input\.([a-zA-Z_][a-zA-Z_0-9]*(?:\.[a-zA-Z_][a-zA-Z_0-9]*)*\s*)\}\}/g;
|
|
35
44
|
// 匹配整串单个表达式
|
|
36
45
|
WHOLE_STRING_EXPR_REGEX = /^\{\{\s*input\.([a-zA-Z_][a-zA-Z_0-9]*(?:\.[a-zA-Z_][a-zA-Z_0-9]*)*)\s*\}\}$/;
|
|
37
|
-
|
|
46
|
+
/**
|
|
47
|
+
* 解析 formValue 模板
|
|
48
|
+
* @param template - formValue 模板对象
|
|
49
|
+
* @param input - 用户输入参数
|
|
50
|
+
* @param paramsSchema - 可选,输入参数的 JSON Schema,用于推断默认值
|
|
51
|
+
* @returns 解析后的参数对象
|
|
52
|
+
*/
|
|
53
|
+
resolve(template, input, paramsSchema) {
|
|
38
54
|
if (typeof template === "string") {
|
|
39
|
-
return this.resolveString(template, input);
|
|
55
|
+
return this.resolveString(template, input, paramsSchema);
|
|
40
56
|
}
|
|
41
57
|
if (Array.isArray(template)) {
|
|
42
|
-
return template.map((item) => this.resolve(item, input));
|
|
58
|
+
return template.map((item) => this.resolve(item, input, paramsSchema));
|
|
43
59
|
}
|
|
44
60
|
if (template !== null && typeof template === "object") {
|
|
45
|
-
return this.resolveObject(template, input);
|
|
61
|
+
return this.resolveObject(template, input, paramsSchema);
|
|
46
62
|
}
|
|
47
63
|
return template;
|
|
48
64
|
}
|
|
49
|
-
resolveString(template, input) {
|
|
65
|
+
resolveString(template, input, paramsSchema) {
|
|
50
66
|
const wholeMatch = template.match(this.WHOLE_STRING_EXPR_REGEX);
|
|
51
67
|
if (wholeMatch) {
|
|
52
68
|
const path2 = wholeMatch[1];
|
|
53
69
|
const value = this.getValueByPath(input, path2);
|
|
54
|
-
|
|
70
|
+
if (value !== void 0) {
|
|
71
|
+
return value;
|
|
72
|
+
}
|
|
73
|
+
if (paramsSchema) {
|
|
74
|
+
const defaultValue = this.getDefaultValueForPath(path2, paramsSchema);
|
|
75
|
+
if (defaultValue !== void 0) {
|
|
76
|
+
return defaultValue;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return template;
|
|
55
80
|
}
|
|
56
81
|
this.EXPR_REGEX.lastIndex = 0;
|
|
57
82
|
if (!this.EXPR_REGEX.test(template)) {
|
|
@@ -67,10 +92,10 @@ var TemplateEngineService = class {
|
|
|
67
92
|
});
|
|
68
93
|
return result;
|
|
69
94
|
}
|
|
70
|
-
resolveObject(template, input) {
|
|
95
|
+
resolveObject(template, input, paramsSchema) {
|
|
71
96
|
const result = {};
|
|
72
97
|
for (const [key, value] of Object.entries(template)) {
|
|
73
|
-
result[key] = this.resolve(value, input);
|
|
98
|
+
result[key] = this.resolve(value, input, paramsSchema);
|
|
74
99
|
}
|
|
75
100
|
return result;
|
|
76
101
|
}
|
|
@@ -85,6 +110,66 @@ var TemplateEngineService = class {
|
|
|
85
110
|
}
|
|
86
111
|
return current;
|
|
87
112
|
}
|
|
113
|
+
/**
|
|
114
|
+
* 根据路径从 schema 获取默认值
|
|
115
|
+
* @param path - 变量路径,如 "a.b.c"
|
|
116
|
+
* @param schema - JSON Schema
|
|
117
|
+
* @returns 默认值,如果无法确定则返回 undefined
|
|
118
|
+
*/
|
|
119
|
+
getDefaultValueForPath(path2, schema) {
|
|
120
|
+
const fieldSchema = this.getSchemaForPath(path2, schema);
|
|
121
|
+
if (!fieldSchema) {
|
|
122
|
+
return void 0;
|
|
123
|
+
}
|
|
124
|
+
return this.getDefaultValueFromSchema(fieldSchema);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* 根据路径查找对应的 schema 定义
|
|
128
|
+
* @param path - 变量路径,如 "a.b.c"
|
|
129
|
+
* @param schema - 根 JSON Schema
|
|
130
|
+
* @returns 路径对应的 schema,如果不存在则返回 undefined
|
|
131
|
+
*/
|
|
132
|
+
getSchemaForPath(path2, schema) {
|
|
133
|
+
const keys = path2.split(".");
|
|
134
|
+
let currentSchema = schema;
|
|
135
|
+
for (const key of keys) {
|
|
136
|
+
if (!currentSchema?.properties) {
|
|
137
|
+
return void 0;
|
|
138
|
+
}
|
|
139
|
+
currentSchema = currentSchema.properties[key];
|
|
140
|
+
if (!currentSchema) {
|
|
141
|
+
return void 0;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return currentSchema;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* 从 schema 获取默认值
|
|
148
|
+
* 优先级:default > type > undefined
|
|
149
|
+
* @param schema - 字段的 JSON Schema
|
|
150
|
+
* @returns 默认值
|
|
151
|
+
*/
|
|
152
|
+
getDefaultValueFromSchema(schema) {
|
|
153
|
+
if (schema.default !== void 0) {
|
|
154
|
+
return schema.default;
|
|
155
|
+
}
|
|
156
|
+
if (schema.oneOf || schema.anyOf) {
|
|
157
|
+
return void 0;
|
|
158
|
+
}
|
|
159
|
+
const type = schema.type;
|
|
160
|
+
if (!type) {
|
|
161
|
+
return void 0;
|
|
162
|
+
}
|
|
163
|
+
if (Array.isArray(type)) {
|
|
164
|
+
for (const t of type) {
|
|
165
|
+
if (t in TYPE_DEFAULT_VALUES) {
|
|
166
|
+
return TYPE_DEFAULT_VALUES[t];
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return void 0;
|
|
170
|
+
}
|
|
171
|
+
return TYPE_DEFAULT_VALUES[type];
|
|
172
|
+
}
|
|
88
173
|
};
|
|
89
174
|
TemplateEngineService = _ts_decorate([
|
|
90
175
|
Injectable()
|
|
@@ -570,7 +655,7 @@ var CapabilityService = class _CapabilityService {
|
|
|
570
655
|
if (!pluginInstance.hasAction(actionName)) {
|
|
571
656
|
throw new ActionNotFoundError(config.pluginKey, actionName);
|
|
572
657
|
}
|
|
573
|
-
const resolvedParams = config.formValue ? this.templateEngineService.resolve(config.formValue, input) : input;
|
|
658
|
+
const resolvedParams = config.formValue ? this.templateEngineService.resolve(config.formValue, input, config.paramsSchema) : input;
|
|
574
659
|
const context = this.buildActionContext(contextOverride);
|
|
575
660
|
const isStream = pluginInstance.isStreamAction?.(actionName) ?? false;
|
|
576
661
|
this.logger.log("Executing capability (call)", {
|
|
@@ -621,7 +706,7 @@ var CapabilityService = class _CapabilityService {
|
|
|
621
706
|
if (!pluginInstance.hasAction(actionName)) {
|
|
622
707
|
throw new ActionNotFoundError(config.pluginKey, actionName);
|
|
623
708
|
}
|
|
624
|
-
const resolvedParams = config.formValue ? this.templateEngineService.resolve(config.formValue, input) : input;
|
|
709
|
+
const resolvedParams = config.formValue ? this.templateEngineService.resolve(config.formValue, input, config.paramsSchema) : input;
|
|
625
710
|
const context = this.buildActionContext(contextOverride);
|
|
626
711
|
const isStream = pluginInstance.isStreamAction?.(actionName) ?? false;
|
|
627
712
|
this.logger.log("Executing capability (stream)", {
|
|
@@ -672,7 +757,7 @@ var CapabilityService = class _CapabilityService {
|
|
|
672
757
|
if (!pluginInstance.hasAction(actionName)) {
|
|
673
758
|
throw new ActionNotFoundError(config.pluginKey, actionName);
|
|
674
759
|
}
|
|
675
|
-
const resolvedParams = config.formValue ? this.templateEngineService.resolve(config.formValue, input) : input;
|
|
760
|
+
const resolvedParams = config.formValue ? this.templateEngineService.resolve(config.formValue, input, config.paramsSchema) : input;
|
|
676
761
|
const context = this.buildActionContext(contextOverride);
|
|
677
762
|
const isStream = pluginInstance.isStreamAction?.(actionName) ?? false;
|
|
678
763
|
this.logger.log("Executing capability (streamWithEvents)", {
|