-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
expressions.ts
132 lines (105 loc) · 4.25 KB
/
expressions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import type { ResolvableState } from '@/types/expressions';
import { ExpressionError, ExpressionParser, type Result } from 'n8n-workflow';
import { i18n } from '@/plugins/i18n';
import { useWorkflowsStore } from '@/stores/workflows.store';
export const isExpression = (expr: unknown) => {
if (typeof expr !== 'string') return false;
return expr.startsWith('=');
};
export const isEmptyExpression = (expr: string) => {
return /\{\{\s*\}\}/.test(expr);
};
export const removeExpressionPrefix = (expr: string) => {
return expr.startsWith('=') ? expr.slice(1) : expr;
};
export const isTestableExpression = (expr: string) => {
return ExpressionParser.splitExpression(expr).every((c) => {
if (c.type === 'text') {
return true;
}
return /\$secrets(\.[a-zA-Z0-9_]+)+$/.test(c.text.trim());
});
};
export const isNoExecDataExpressionError = (error: unknown): error is ExpressionError => {
return error instanceof ExpressionError && error.context.type === 'no_execution_data';
};
export const isNoNodeExecDataExpressionError = (error: unknown): error is ExpressionError => {
return error instanceof ExpressionError && error.context.type === 'no_node_execution_data';
};
export const isPairedItemIntermediateNodesError = (error: unknown): error is ExpressionError => {
return (
error instanceof ExpressionError && error.context.type === 'paired_item_intermediate_nodes'
);
};
export const isPairedItemNoConnectionError = (error: unknown): error is ExpressionError => {
return error instanceof ExpressionError && error.context.type === 'paired_item_no_connection';
};
export const isInvalidPairedItemError = (error: unknown): error is ExpressionError => {
return error instanceof ExpressionError && error.context.type === 'paired_item_invalid_info';
};
export const isNoPairedItemError = (error: unknown): error is ExpressionError => {
return error instanceof ExpressionError && error.context.type === 'paired_item_no_info';
};
export const isNoInputConnectionError = (error: unknown): error is ExpressionError => {
return error instanceof ExpressionError && error.context.type === 'no_input_connection';
};
export const isAnyPairedItemError = (error: unknown): error is ExpressionError => {
return error instanceof ExpressionError && error.context.functionality === 'pairedItem';
};
export const getResolvableState = (error: unknown, ignoreError = false): ResolvableState => {
if (!error) return 'valid';
if (
isNoExecDataExpressionError(error) ||
isNoNodeExecDataExpressionError(error) ||
isPairedItemIntermediateNodesError(error) ||
ignoreError
) {
return 'pending';
}
return 'invalid';
};
export const getExpressionErrorMessage = (error: Error): string => {
if (isNoExecDataExpressionError(error) || isPairedItemIntermediateNodesError(error)) {
return i18n.baseText('expressionModalInput.noExecutionData');
}
if (isNoNodeExecDataExpressionError(error)) {
const nodeCause = error.context.nodeCause as string;
return i18n.baseText('expressionModalInput.noNodeExecutionData', {
interpolate: { node: nodeCause },
});
}
if (isNoInputConnectionError(error)) {
return i18n.baseText('expressionModalInput.noInputConnection');
}
if (isPairedItemNoConnectionError(error)) {
return i18n.baseText('expressionModalInput.pairedItemConnectionError');
}
if (isInvalidPairedItemError(error) || isNoPairedItemError(error)) {
const nodeCause = error.context.nodeCause as string;
const isPinned = !!useWorkflowsStore().pinDataByNodeName(nodeCause);
if (isPinned) {
return i18n.baseText('expressionModalInput.pairedItemInvalidPinnedError', {
interpolate: { node: nodeCause },
});
}
}
if (isAnyPairedItemError(error)) {
return i18n.baseText('expressionModalInput.pairedItemError');
}
return error.message;
};
export const stringifyExpressionResult = (result: Result<unknown, Error>): string => {
if (!result.ok) {
if (getResolvableState(result.error) !== 'invalid') {
return '';
}
return `[${i18n.baseText('parameterInput.error')}: ${getExpressionErrorMessage(result.error)}]`;
}
if (result.result === null) {
return '';
}
if (typeof result.result === 'string' && result.result.length === 0) {
return i18n.baseText('parameterInput.emptyString');
}
return typeof result.result === 'string' ? result.result : JSON.stringify(result.result);
};