Fix endpoint condition source handling

This commit is contained in:
fawney19
2026-05-07 15:56:22 +08:00
parent 6317587a6b
commit 2b439094c5
5 changed files with 49 additions and 11 deletions

View File

@@ -140,7 +140,7 @@ export interface BodyRuleConditionLeaf {
path: string
op: BodyRuleConditionOp
value?: unknown // exists / not_exists 不需要 value
source?: 'request_headers' // 不填表示请求体;填 request_headers 表示请求头
source?: 'body' | 'current' | 'original' | 'request_headers' | 'headers'
}
export interface BodyRuleConditionAll {

View File

@@ -130,6 +130,9 @@
<SelectItem value="body">
请求体
</SelectItem>
<SelectItem value="original">
原始请求体
</SelectItem>
<SelectItem value="request_headers">
请求头
</SelectItem>

View File

@@ -1459,6 +1459,7 @@ const RESERVED_BODY_FIELDS = new Set([
const BODY_RULE_JSON_ACTIONS = new Set(['set', 'drop', 'rename', 'append', 'insert', 'regex_replace'])
const CONDITION_JSON_OPS = new Set(['eq', 'neq', 'gt', 'lt', 'gte', 'lte', 'starts_with', 'ends_with', 'contains', 'matches', 'exists', 'not_exists', 'in', 'type_is'])
const CONDITION_JSON_SOURCES = new Set(['body', 'current', 'original', 'request_headers', 'headers'])
function isJsonObject(value: unknown): value is Record<string, unknown> {
return value !== null && typeof value === 'object' && !Array.isArray(value)
@@ -1508,8 +1509,8 @@ function validateJsonConditionShape(condition: Record<string, unknown>, label: s
if (typeof condition.op !== 'string' || !CONDITION_JSON_OPS.has(condition.op)) {
return `${label}.op 无效`
}
if (condition.source !== undefined && condition.source !== 'request_headers') {
return `${label}.source 只能是 request_headers请求体条件不要填写 source`
if (condition.source !== undefined && (typeof condition.source !== 'string' || !CONDITION_JSON_SOURCES.has(condition.source))) {
return `${label}.source 无效`
}
return null
}

View File

@@ -1,6 +1,6 @@
import type { BodyRuleCondition, BodyRuleConditionOp } from '@/api/endpoints'
export type ConditionSource = 'body' | 'request_headers'
export type ConditionSource = 'body' | 'original' | 'request_headers'
export type ConditionGroupMode = 'all' | 'any'
export interface EditableConditionLeaf {
@@ -96,6 +96,8 @@ export function conditionToEditable(condition?: BodyRuleCondition | null): Edita
: '',
source: source === 'request_headers' || source === 'headers'
? 'request_headers'
: source === 'original'
? 'original'
: 'body',
}
}
@@ -117,7 +119,11 @@ export function editableConditionToApi(node: EditableConditionNode | null): Body
const base = {
path,
op: node.op,
...(node.source === 'request_headers' ? { source: 'request_headers' as const } : {}),
...(node.source === 'request_headers'
? { source: 'request_headers' as const }
: node.source === 'original'
? { source: 'original' as const }
: {}),
}
if (node.op === 'exists' || node.op === 'not_exists') {