vika_cli/templates/context/
api_context.rs

1use serde::Serialize;
2
3/// Context for API client function generation.
4#[derive(Debug, Clone, Serialize)]
5pub struct ApiContext {
6    pub function_name: String,
7    pub operation_id: Option<String>,
8    pub http_method: String,
9    pub path: String,
10    pub path_params: Vec<Parameter>,
11    pub query_params: Vec<Parameter>,
12    pub request_body: Option<RequestBody>,
13    pub responses: Vec<Response>,
14    pub type_imports: String,
15    pub http_import: String,
16    pub return_type: String,
17    pub function_body: String,
18    pub response_types: String, // Response/Error type definitions
19    pub module_name: String,
20    pub params: String,
21    pub description: String,
22    /// Spec name (for multi-spec mode)
23    pub spec_name: Option<String>,
24}
25
26/// Parameter information for API functions.
27#[derive(Debug, Clone, Serialize)]
28pub struct Parameter {
29    pub name: String,
30    pub param_type: String,
31    pub optional: bool,
32    pub description: Option<String>,
33}
34
35/// Request body information.
36#[derive(Debug, Clone, Serialize)]
37pub struct RequestBody {
38    pub type_name: String,
39    pub description: Option<String>,
40}
41
42/// Response information.
43#[derive(Debug, Clone, Serialize)]
44pub struct Response {
45    pub status_code: u16,
46    pub body_type: String,
47}
48
49impl ApiContext {
50    /// Create a new ApiContext.
51    #[allow(clippy::too_many_arguments)]
52    pub fn new(
53        function_name: String,
54        operation_id: Option<String>,
55        http_method: String,
56        path: String,
57        path_params: Vec<Parameter>,
58        query_params: Vec<Parameter>,
59        request_body: Option<RequestBody>,
60        responses: Vec<Response>,
61        type_imports: String,
62        http_import: String,
63        return_type: String,
64        function_body: String,
65        response_types: String,
66        module_name: String,
67        params: String,
68        description: String,
69        spec_name: Option<String>,
70    ) -> Self {
71        Self {
72            function_name,
73            operation_id,
74            http_method,
75            path,
76            path_params,
77            query_params,
78            request_body,
79            responses,
80            type_imports,
81            http_import,
82            return_type,
83            function_body,
84            response_types,
85            module_name,
86            params,
87            description,
88            spec_name,
89        }
90    }
91}
92
93impl Parameter {
94    /// Create a new Parameter.
95    pub fn new(
96        name: String,
97        param_type: String,
98        optional: bool,
99        description: Option<String>,
100    ) -> Self {
101        Self {
102            name,
103            param_type,
104            optional,
105            description,
106        }
107    }
108}
109
110impl RequestBody {
111    /// Create a new RequestBody.
112    pub fn new(type_name: String, description: Option<String>) -> Self {
113        Self {
114            type_name,
115            description,
116        }
117    }
118}
119
120impl Response {
121    /// Create a new Response.
122    pub fn new(status_code: u16, body_type: String) -> Self {
123        Self {
124            status_code,
125            body_type,
126        }
127    }
128}