mrapids 0.1.7

Your OpenAPI, but executable
Documentation
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
#![allow(dead_code)]

use crate::cli::GenerateTarget;
use crate::core::swagger::SwaggerSpec;
use crate::core::validation::{SpecValidator, ValidationLevel};
use crate::utils::security::{validate_file_path, validate_output_path};
use anyhow::Result;
use colored::*;
use std::fs;
use std::path::{Path, PathBuf};

// Internal command structure for generate functionality
pub struct GenerateCommand {
    pub spec: PathBuf,
    pub target: GenerateTarget,
    pub output: PathBuf,
    pub client: bool,
    pub server: bool,
    pub both: bool,
    pub package_name: Option<String>,
    #[allow(dead_code)]
    pub skip_validation: bool,
}

pub fn generate_code(cmd: GenerateCommand) -> Result<()> {
    println!("🚀 {} Code Generator", "MicroRapid".bright_cyan());
    println!(
        "📄 Loading spec from: {}",
        cmd.spec.display().to_string().cyan()
    );

    // Validate input spec path
    validate_file_path(&cmd.spec)?;

    // Validate output directory path
    validate_output_path(&cmd.output)?;

    // Load and parse the spec
    let content = fs::read_to_string(&cmd.spec)
        .map_err(|e| anyhow::anyhow!("Cannot read spec file: {}", e))?;

    // Validate the specification before code generation
    println!("{} Validating specification...", "🔍".bright_cyan());
    let validator = SpecValidator::new()?;
    let validation_report = validator.validate_content(&content, ValidationLevel::Standard)?;

    if !validation_report.is_valid() {
        println!("\n{} Specification has validation errors:", "".red());
        validation_report.display();
        println!(
            "\n{} Code generation requires a valid specification",
            "💡".blue()
        );
        return Err(anyhow::anyhow!("Specification validation failed"));
    } else if validation_report.has_warnings() {
        println!("{} Specification has warnings:", "⚠️".yellow());
        validation_report.display();
        println!("{} Proceeding with code generation...", "➡️".blue());
    } else {
        println!("{} Specification is valid!", "".green());
    }

    let spec_value: serde_json::Value =
        if cmd.spec.extension().and_then(|s| s.to_str()) == Some("json") {
            serde_json::from_str(&content)?
        } else {
            serde_yaml::from_str(&content)?
        };

    // Detect spec format
    let is_swagger_2 = spec_value.get("swagger").is_some();
    let is_openapi_3 = spec_value.get("openapi").is_some();

    if !is_swagger_2 && !is_openapi_3 {
        return Err(anyhow::anyhow!("Unable to detect API specification format"));
    }

    // Parse spec
    let swagger_spec: SwaggerSpec = if cmd.spec.extension().and_then(|s| s.to_str()) == Some("json")
    {
        serde_json::from_str(&content)?
    } else {
        serde_yaml::from_str(&content)?
    };

    // Determine what to generate
    let generate_client = cmd.client || (!cmd.server && !cmd.both) || cmd.both;
    let generate_server = cmd.server || cmd.both;

    // Create output directory
    fs::create_dir_all(&cmd.output)?;

    println!("🎯 Target: {}", format!("{:?}", cmd.target).yellow());
    println!("📁 Output: {}", cmd.output.display().to_string().green());

    if generate_client {
        println!("\n📦 Generating {} code...", "client".bright_blue());
        generate_client_code(
            &swagger_spec,
            &cmd.target,
            &cmd.output,
            cmd.package_name.as_deref(),
        )?;
    }

    if generate_server {
        println!("\n📦 Generating {} code...", "server".bright_blue());
        generate_server_code(
            &swagger_spec,
            &cmd.target,
            &cmd.output,
            cmd.package_name.as_deref(),
        )?;
    }

    println!("\n✅ Code generation complete!");
    println!(
        "📁 Generated files in: {}",
        cmd.output.display().to_string().green()
    );

    Ok(())
}

fn generate_client_code(
    spec: &SwaggerSpec,
    target: &GenerateTarget,
    output_dir: &Path,
    package_name: Option<&str>,
) -> Result<()> {
    match target {
        GenerateTarget::Typescript => generate_typescript_client(spec, output_dir, package_name),
        GenerateTarget::Python => generate_python_client(spec, output_dir, package_name),
        GenerateTarget::Curl => generate_curl_commands(spec, output_dir),
        GenerateTarget::Postman => generate_postman_collection(spec, output_dir, package_name),
        _ => {
            println!(
                "⚠️  {} client generation not yet implemented",
                format!("{:?}", target).yellow()
            );
            Ok(())
        }
    }
}

fn generate_server_code(
    _spec: &SwaggerSpec,
    target: &GenerateTarget,
    _output_dir: &Path,
    _package_name: Option<&str>,
) -> Result<()> {
    match target {
        GenerateTarget::Typescript => {
            println!("  📝 Generating Express.js server stubs...");
            // TODO: Implement Express server generation
            Ok(())
        }
        GenerateTarget::Python => {
            println!("  📝 Generating FastAPI server stubs...");
            // TODO: Implement FastAPI server generation
            Ok(())
        }
        _ => {
            println!(
                "⚠️  {} server generation not yet implemented",
                format!("{:?}", target).yellow()
            );
            Ok(())
        }
    }
}

fn generate_typescript_client(
    spec: &SwaggerSpec,
    output_dir: &Path,
    package_name: Option<&str>,
) -> Result<()> {
    let client_name = package_name.unwrap_or("ApiClient");
    let file_path = output_dir.join("client.ts");

    let mut content = String::new();

    // Generate header
    content.push_str(&format!(
        r#"/**
 * {} API Client
 * Version: {}
 * Auto-generated by MicroRapid
 */

export interface RequestConfig {{
    baseURL?: string;
    headers?: Record<string, string>;
    timeout?: number;
}}

export class {} {{
    private baseURL: string;
    private headers: Record<string, string>;
    
    constructor(config: RequestConfig = {{}}) {{
        this.baseURL = config.baseURL || '{}';
        this.headers = config.headers || {{}};
    }}
    
    private async request<T>(
        method: string,
        path: string,
        params?: any,
        data?: any
    ): Promise<T> {{
        const url = new URL(path, this.baseURL);
        
        if (params) {{
            Object.keys(params).forEach(key => 
                url.searchParams.append(key, params[key])
            );
        }}
        
        const options: RequestInit = {{
            method,
            headers: {{
                'Content-Type': 'application/json',
                ...this.headers
            }}
        }};
        
        if (data) {{
            options.body = JSON.stringify(data);
        }}
        
        const response = await fetch(url.toString(), options);
        
        if (!response.ok) {{
            throw new Error(`HTTP ${{response.status}}: ${{response.statusText}}`);
        }}
        
        return response.json();
    }}
"#,
        spec.info.title,
        spec.info.version,
        client_name,
        spec.get_base_url()
    ));

    // Generate methods for each operation
    for (path, path_item) in &spec.paths {
        let operations = [
            ("get", &path_item.get),
            ("post", &path_item.post),
            ("put", &path_item.put),
            ("delete", &path_item.delete),
            ("patch", &path_item.patch),
        ];

        for (method, operation) in operations {
            if let Some(op) = operation {
                let method_name = op
                    .operation_id
                    .as_ref()
                    .map(|s| s.clone())
                    .unwrap_or_else(|| format!("{}_{}", method, sanitize_path(path)));

                let summary = op.summary.as_deref().unwrap_or("");

                content.push_str(&format!(
                    r#"
    /**
     * {}
     * {} {}
     */
    async {}(params?: any, data?: any): Promise<any> {{
        return this.request('{}', '{}', params, data);
    }}
"#,
                    summary,
                    method.to_uppercase(),
                    path,
                    method_name,
                    method.to_uppercase(),
                    path
                ));
            }
        }
    }

    content.push_str("}\n");

    fs::write(&file_path, content)?;
    println!("  ✅ Generated: {}", file_path.display());

    // Generate package.json
    let package_json = output_dir.join("package.json");
    let package_content = format!(
        r#"{{
  "name": "{}",
  "version": "1.0.0",
  "description": "{} API Client",
  "main": "client.js",
  "types": "client.ts",
  "scripts": {{
    "build": "tsc",
    "test": "echo \"No tests yet\""
  }},
  "keywords": ["api", "client", "openapi"],
  "author": "MicroRapid",
  "license": "MIT",
  "devDependencies": {{
    "typescript": "^5.0.0"
  }}
}}
"#,
        package_name.unwrap_or("api-client"),
        spec.info.title
    );

    fs::write(&package_json, package_content)?;
    println!("  ✅ Generated: {}", package_json.display());

    Ok(())
}

fn generate_python_client(
    spec: &SwaggerSpec,
    output_dir: &Path,
    package_name: Option<&str>,
) -> Result<()> {
    let client_name = package_name.unwrap_or("api_client");
    let file_path = output_dir.join(format!("{}.py", client_name));

    let mut content = String::new();

    // Generate header
    content.push_str(&format!(
        r#""""
{} API Client
Version: {}
Auto-generated by MicroRapid
"""

import requests
from typing import Dict, Any, Optional
from urllib.parse import urljoin


class {}:
    """API Client for {}"""
    
    def __init__(self, base_url: str = "{}", headers: Optional[Dict[str, str]] = None):
        self.base_url = base_url.rstrip('/')
        self.session = requests.Session()
        if headers:
            self.session.headers.update(headers)
    
    def _request(
        self,
        method: str,
        path: str,
        params: Optional[Dict[str, Any]] = None,
        json: Optional[Dict[str, Any]] = None,
        data: Optional[Any] = None
    ) -> Dict[str, Any]:
        """Make an HTTP request"""
        url = urljoin(self.base_url + '/', path.lstrip('/'))
        
        response = self.session.request(
            method=method,
            url=url,
            params=params,
            json=json,
            data=data
        )
        
        response.raise_for_status()
        return response.json() if response.content else {{}}
"#,
        spec.info.title,
        spec.info.version,
        to_pascal_case(client_name),
        spec.info.title,
        spec.get_base_url()
    ));

    // Generate methods for each operation
    for (path, path_item) in &spec.paths {
        let operations = [
            ("get", &path_item.get),
            ("post", &path_item.post),
            ("put", &path_item.put),
            ("delete", &path_item.delete),
            ("patch", &path_item.patch),
        ];

        for (method, operation) in operations {
            if let Some(op) = operation {
                let method_name = op
                    .operation_id
                    .as_ref()
                    .map(|s| to_snake_case(s))
                    .unwrap_or_else(|| format!("{}_{}", method, sanitize_path_snake(path)));

                let summary = op.summary.as_deref().unwrap_or("");

                content.push_str(&format!(
                    r#"
    def {}(self, params: Optional[Dict[str, Any]] = None, json: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
        """
        {}
        {} {}
        """
        return self._request('{}', '{}', params=params, json=json)
"#,
                    method_name,
                    summary,
                    method.to_uppercase(),
                    path,
                    method.to_uppercase(),
                    path
                ));
            }
        }
    }

    fs::write(&file_path, content)?;
    println!("  ✅ Generated: {}", file_path.display());

    // Generate requirements.txt
    let requirements = output_dir.join("requirements.txt");
    fs::write(&requirements, "requests>=2.28.0\n")?;
    println!("  ✅ Generated: {}", requirements.display());

    // Generate __init__.py
    let init_file = output_dir.join("__init__.py");
    fs::write(
        &init_file,
        format!(
            "from .{} import {}\n",
            client_name,
            to_pascal_case(client_name)
        ),
    )?;
    println!("  ✅ Generated: {}", init_file.display());

    Ok(())
}

fn generate_curl_commands(spec: &SwaggerSpec, output_dir: &Path) -> Result<()> {
    let file_path = output_dir.join("api_commands.sh");
    let mut content = String::new();

    content.push_str(&format!(
        r#"#!/bin/bash
# {} API - cURL Commands
# Version: {}
# Auto-generated by MicroRapid

BASE_URL="{}"

# Set your API key or auth token here if needed
# AUTH_HEADER="Authorization: Bearer YOUR_TOKEN"

"#,
        spec.info.title,
        spec.info.version,
        spec.get_base_url()
    ));

    for (path, path_item) in &spec.paths {
        let operations = [
            ("GET", &path_item.get),
            ("POST", &path_item.post),
            ("PUT", &path_item.put),
            ("DELETE", &path_item.delete),
            ("PATCH", &path_item.patch),
        ];

        for (method, operation) in operations {
            if let Some(op) = operation {
                let op_id = op.operation_id.as_deref().unwrap_or("operation");
                let summary = op.summary.as_deref().unwrap_or("");

                content.push_str(&format!(
                    r#"# {}
# {}
{}() {{
    curl -X {} \
        "${{BASE_URL}}{}" \
        -H "Content-Type: application/json" \
        -H "${{AUTH_HEADER:-}}" \
        "$@"
}}

"#,
                    op_id, summary, op_id, method, path
                ));
            }
        }
    }

    fs::write(&file_path, content)?;

    // Make the script executable
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mut perms = fs::metadata(&file_path)?.permissions();
        perms.set_mode(0o755);
        fs::set_permissions(&file_path, perms)?;
    }

    println!("  ✅ Generated: {}", file_path.display());
    Ok(())
}

fn generate_postman_collection(
    spec: &SwaggerSpec,
    output_dir: &Path,
    package_name: Option<&str>,
) -> Result<()> {
    let collection_name = package_name.unwrap_or(&spec.info.title);
    let file_path = output_dir.join("postman_collection.json");

    let mut items = Vec::new();

    for (path, path_item) in &spec.paths {
        let operations = [
            ("GET", &path_item.get),
            ("POST", &path_item.post),
            ("PUT", &path_item.put),
            ("DELETE", &path_item.delete),
            ("PATCH", &path_item.patch),
        ];

        for (method, operation) in operations {
            if let Some(op) = operation {
                let default_name = format!("{} {}", method, path);
                let name = op
                    .operation_id
                    .as_deref()
                    .or(op.summary.as_deref())
                    .unwrap_or(&default_name);

                items.push(serde_json::json!({
                    "name": name,
                    "request": {
                        "method": method,
                        "header": [
                            {
                                "key": "Content-Type",
                                "value": "application/json"
                            }
                        ],
                        "url": {
                            "raw": format!("{}{}", spec.get_base_url(), path),
                            "host": [spec.get_base_url()],
                            "path": path.split('/').filter(|s| !s.is_empty()).collect::<Vec<_>>()
                        }
                    }
                }));
            }
        }
    }

    let collection = serde_json::json!({
        "info": {
            "name": collection_name,
            "description": spec.info.description.as_deref().unwrap_or(""),
            "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
        },
        "item": items
    });

    fs::write(&file_path, serde_json::to_string_pretty(&collection)?)?;
    println!("  ✅ Generated: {}", file_path.display());

    Ok(())
}

// Helper functions
fn sanitize_path(path: &str) -> String {
    path.replace('/', "_")
        .replace('{', "")
        .replace('}', "")
        .trim_matches('_')
        .to_string()
}

fn sanitize_path_snake(path: &str) -> String {
    to_snake_case(&sanitize_path(path))
}

fn to_snake_case(s: &str) -> String {
    let mut result = String::new();
    for (i, ch) in s.chars().enumerate() {
        if ch.is_uppercase() && i > 0 {
            result.push('_');
        }
        result.push(ch.to_lowercase().next().unwrap());
    }
    result
}

fn to_pascal_case(s: &str) -> String {
    s.split('_')
        .map(|word| {
            let mut chars = word.chars();
            match chars.next() {
                None => String::new(),
                Some(first) => first.to_uppercase().chain(chars).collect(),
            }
        })
        .collect()
}