generateApi method

Future<bool> generateApi({
  1. required String apiName,
  2. required String featureName,
  3. required String pageName,
  4. required String method,
  5. required String pathUrl,
  6. required String returnData,
  7. String? header,
  8. String? cacheStrategy,
  9. int? ttl,
  10. bool? keepExpiredCache,
  11. String? appsName,
  12. String morphemeYamlPath = 'morpheme.yaml',
})

Generates API code directly using the API generation components

apiName - Name of the API to generate featureName - Name of the feature pageName - Name of the page method - HTTP method pathUrl - API path URL returnData - Return data type header - Header configuration file path cacheStrategy - Cache strategy ttl - Time to live for cache keepExpiredCache - Whether to keep expired cache appsName - Apps name for multi-app projects morphemeYamlPath - Path to morpheme.yaml file

Returns true if successful, false otherwise

Implementation

Future<bool> generateApi({
  required String apiName,
  required String featureName,
  required String pageName,
  required String method,
  required String pathUrl,
  required String returnData,
  String? header,
  String? cacheStrategy,
  int? ttl,
  bool? keepExpiredCache,
  String? appsName,
  String morphemeYamlPath = 'morpheme.yaml',
}) async {
  try {
    // Create argument results manually to simulate command line arguments
    final argResults = _createArgResults(
      apiName: apiName,
      featureName: featureName,
      pageName: pageName,
      method: method,
      pathUrl: pathUrl,
      returnData: returnData,
      header: header,
      cacheStrategy: cacheStrategy,
      ttl: ttl,
      keepExpiredCache: keepExpiredCache,
      appsName: appsName,
      morphemeYamlPath: morphemeYamlPath,
    );

    // Step 1: Load project configuration
    final yamlData = YamlHelper.loadFileYaml(morphemeYamlPath);
    final projectName =
        yamlData['project_name'] ?? yamlData['name'] ?? 'morpheme';

    // Step 2: Validate arguments and create configuration
    final config = _validator.validate(argResults, projectName);

    // Step 3: Process and validate configuration
    final processedConfig = _configManager.processConfiguration(config);
    _configManager.validateConfiguration(processedConfig);

    // Step 4: Validate generation sequence
    _orchestrator.validateGenerationSequence(processedConfig);

    // Step 5: Generate API components
    await _orchestrator.generateApi(processedConfig);

    return true;
  } catch (e) {
    StatusHelper.failed('API generation failed: $e');
    // Print stack trace for debugging in verbose mode
    // print('Stack trace: $stackTrace');
    return false;
  }
}