corrad-bp/server/api/process/test-api-node.post.js
Md Afiq Iskandar dce3e7f0f2 Add API Call Node Configuration and Integration
- Introduced a new component, ApiNodeConfiguration, for configuring API call nodes within the process builder.
- Enhanced ProcessBuilderComponents to include the new API Call node type with default properties.
- Implemented ApiCallNode in ProcessFlowNodes for rendering API call nodes with relevant details.
- Added a backend endpoint for testing API node configurations, allowing users to validate API calls without executing the entire process.
- Updated VariableManager to default to global scope for new variables, ensuring consistency in variable management.
- Improved the overall process builder experience by integrating API call functionality and enhancing variable handling.
2025-05-19 13:43:04 +08:00

73 lines
2.3 KiB
JavaScript

import { executeApiCall } from '../../../services/apiNodeService';
/**
* Test API Node Endpoint
*
* This endpoint allows testing API node configurations without executing
* the entire process. It takes the node configuration and process variables
* as input and returns the API call result.
*/
export default defineEventHandler(async (event) => {
try {
// Get request body
const body = await readBody(event);
// Extract node configuration and process variables
const { nodeConfig, processVariables } = body;
// Validate input
if (!nodeConfig || !nodeConfig.apiUrl) {
return {
success: false,
error: {
message: 'Invalid API node configuration. Missing apiUrl.'
}
};
}
// Structure the variables for the API call
const structuredVariables = {
global: {},
process: {},
...processVariables
};
// Categorize variables by scope if they're not already structured
if (!processVariables.global && !processVariables.process) {
Object.entries(processVariables || {}).forEach(([name, value]) => {
// Determine which variables are global based on the presence of an actual variable in the store
// This would usually be handled by the process execution engine
if (name === nodeConfig.outputVariable || name === nodeConfig.errorVariable) {
structuredVariables.global[name] = value;
} else {
structuredVariables.process[name] = value;
}
});
}
// Execute the API call
const result = await executeApiCall(nodeConfig, structuredVariables);
// Update global variables with the result
if (result.success && nodeConfig.outputVariable) {
structuredVariables.global[nodeConfig.outputVariable] = result.data;
} else if (!result.success && nodeConfig.errorVariable) {
structuredVariables.global[nodeConfig.errorVariable] = result.error;
}
// Add the updated variables to the result
result.variables = structuredVariables;
// Return the result
return result;
} catch (error) {
// Handle errors
return {
success: false,
error: {
message: error.message || 'An error occurred while testing the API node',
stack: process.env.NODE_ENV === 'development' ? error.stack : undefined
}
};
}
});