front/debug-full-flow.js
2026-03-23 10:00:44 +08:00

234 lines
8.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Debug the full flow: interface data -> component processing
const VERBOSE_SCRIPT_LOGS = process.env.DEBUG_SCRIPT_LOGS === 'true';
if (!VERBOSE_SCRIPT_LOGS) {
console.log = () => {};
console.warn = () => {};
}
console.log('=== Debugging Full Flow ===\n');
// 模拟后端返回的接口数据
const mockInterfaceData = {
id: 123,
requestMethod: 'POST',
requestAddress: 'https://api.example.com/test',
contentType: 'application/json', // 确保包含json
inParams: `{
"clientNo": "string",
"trackNo": "string",
"serviceNo": "string",
"productCode": "string",
"weight": 3,
"length": 3,
"width": 3,
"height": 3,
"vatNo": "string",
"iossNo": "string",
"eoriNo": "string",
"warehouseId": "string",
"isCod": "false",
"codCurrency": "string",
"codSum": "string",
"receiver": {
"name": "string",
"company": "string",
"phone": "string",
"email": "string",
"country": "string",
"state": "string",
"city": "string",
"street": "string",
"houseNo": "string",
"address": "string",
"address2": "string",
"postalCode": "string"
},
"sender": {
"name": "string",
"company": "string",
"phone": "string",
"email": "string",
"country": "string",
"state": "string",
"city": "string",
"street": "string",
"address": "string",
"address2": "string",
"postalCode": "string"
},
"items": [
{
"nameEn": "string",
"nameCn": "string",
"value": "fake_simple_value",
"currency": "string",
"weight": 3,
"quantity": 3,
"hsCode": "string",
"sku": "string",
"url": "string",
"originCountry": "string",
"note": "string"
}
]
}`
};
console.log('Mock interface data:');
console.log('- contentType:', mockInterfaceData.contentType);
console.log('- inParams type:', typeof mockInterfaceData.inParams);
console.log('- inParams length:', mockInterfaceData.inParams.length);
console.log('- inParams first 100 chars:', mockInterfaceData.inParams.substring(0, 100));
// 模拟组件中的处理逻辑
function simulateComponentProcessing(data) {
console.log('\n=== Simulating Component Processing ===');
// 模拟 bodyConfig
const bodyConfig = {
bodyType: 'none',
body: '',
rawBody: ''
};
// 1. 设置请求方法
console.log('\n1. Setting request method...');
if (data?.requestMethod !== undefined && data?.requestMethod !== null) {
console.log('requestMethod found:', data.requestMethod);
}
// 2. 设置请求地址
console.log('\n2. Setting request address...');
if (data?.requestAddress) {
console.log('requestAddress found:', data.requestAddress);
}
// 3. 设置请求类型(contentType)
console.log('\n3. Setting bodyType from contentType...');
if (data?.contentType) {
const contentTypeStr = String(data.contentType).toLowerCase();
console.log('contentType detected:', contentTypeStr);
if (contentTypeStr.includes('json')) {
bodyConfig.bodyType = 'json';
console.log('bodyType set to: json');
} else if (contentTypeStr.includes('xml')) {
bodyConfig.bodyType = 'xml';
console.log('bodyType set to: xml');
} else if (contentTypeStr.includes('x-www-form-urlencoded')) {
bodyConfig.bodyType = 'x-www-form-urlencoded';
console.log('bodyType set to: x-www-form-urlencoded');
} else if (contentTypeStr.includes('form-data') || contentTypeStr.includes('multipart')) {
bodyConfig.bodyType = 'form-data';
console.log('bodyType set to: form-data');
} else {
bodyConfig.bodyType = 'raw';
console.log('bodyType set to: raw');
}
} else {
console.log('no contentType found in data');
bodyConfig.bodyType = 'json'; // 默认设置为json
console.log('bodyType defaulted to: json');
}
// 4. 处理 inParams 数据
console.log('\n4. Processing inParams data...');
if (data?.inParams) {
console.log('inParams found in data:', typeof data.inParams, data.inParams?.substring?.(0, 100));
// 使用处理函数
const processedInParams = processInParamsData(data.inParams);
console.log('current bodyType:', bodyConfig.bodyType);
// 根据 bodyType 设置相应的数据
if (bodyConfig.bodyType === 'json' || bodyConfig.bodyType === 'xml') {
bodyConfig.body = processedInParams;
console.log('assigned to bodyConfig.body, length:', processedInParams.length);
console.log('bodyConfig.body first 200 chars:', processedInParams.substring(0, 200));
} else if (bodyConfig.bodyType === 'raw') {
bodyConfig.rawBody = processedInParams;
console.log('assigned to bodyConfig.rawBody, length:', processedInParams.length);
} else {
console.log('bodyType is neither json/xml nor raw:', bodyConfig.bodyType);
}
console.log('inParams processing completed, final data length:', processedInParams.length);
} else {
console.log('no inParams found in data');
}
console.log('\n=== Final bodyConfig state ===');
console.log('bodyType:', bodyConfig.bodyType);
console.log('body length:', bodyConfig.body.length);
console.log('rawBody length:', bodyConfig.rawBody.length);
return bodyConfig;
}
// 处理 inParams 数据的函数
function processInParamsData(inParams) {
console.log('processInParamsData called with:', typeof inParams, inParams?.substring?.(0, 100));
if (!inParams) {
console.log('processInParamsData: input is empty');
return '';
}
// 如果是对象,直接格式化
if (typeof inParams === 'object') {
console.log('processInParamsData: input is object, formatting directly');
const result = JSON.stringify(inParams, null, 2);
console.log('processInParamsData: object result length:', result.length);
return result;
}
// 如果不是字符串,返回原值
if (typeof inParams !== 'string') {
console.log('processInParamsData: input is not string, converting to string');
return String(inParams);
}
console.log('processInParamsData: processing string input, length:', inParams.length);
try {
// 步骤1: 处理转义字符 - 将字面量的转义序列转换为实际字符
let processed = inParams;
console.log('processInParamsData: before escape processing, first 100 chars:', processed.substring(0, 100));
// 按顺序处理转义字符(重要:顺序不能错)
processed = processed.replace(/\\r\\n/g, '\n'); // Windows 换行符转义 -> 实际换行
processed = processed.replace(/\\n/g, '\n'); // Unix 换行符转义 -> 实际换行
processed = processed.replace(/\\r/g, '\r'); // 回车符转义 -> 实际回车
processed = processed.replace(/\\t/g, '\t'); // 制表符转义 -> 实际制表符
processed = processed.replace(/\\\\/g, '\\'); // 反斜杠转义 -> 实际反斜杠
processed = processed.replace(/\\"/g, '"'); // 双引号转义 -> 实际双引号
console.log('processInParamsData: after escape processing, first 100 chars:', processed.substring(0, 100));
// 步骤2: 解析处理后的字符串为 JSON 对象
console.log('processInParamsData: attempting to parse JSON...');
const parsedObject = JSON.parse(processed);
console.log('processInParamsData: JSON parsed successfully, keys:', Object.keys(parsedObject));
// 步骤3: 格式化为美观的 JSON 字符串(2 空格缩进)
const result = JSON.stringify(parsedObject, null, 2);
console.log('processInParamsData: formatted result length:', result.length);
console.log('processInParamsData: formatted result first 200 chars:', result.substring(0, 200));
return result;
} catch (error) {
console.warn('processInParamsData 处理失败:', error);
console.warn('processInParamsData: original input:', inParams);
// 如果处理失败,返回原始数据
return inParams;
}
}
// 运行模拟
const result = simulateComponentProcessing(mockInterfaceData);
console.log('\n=== Simulation Complete ===');
console.log('Final result body length:', result.body.length);
console.log('Should display in MonacoEditor:', result.bodyType === 'json' && result.body.length > 0);