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
use super::{util::{extract_handler_types, HandlerRequestType, TypeClass, space}, convert::{convert_primitive, TypescriptType}};
use walkdir::WalkDir;
use std::{
fs::{File, OpenOptions},
io::prelude::*, path::PathBuf
};
#[derive(Debug)]
pub enum Handler {
Query(TypedQueryHandler),
Mutation(TypedMutationHandler)
}
#[derive(Debug)]
pub struct TypedQueryHandler {
pub key: String,
pub request_type: HandlerRequestType,
pub path: Option<TypescriptType>,
pub query_params: Option<TypescriptType>
}
#[derive(Debug)]
pub struct TypedMutationHandler {
pub key: String,
pub request_type: HandlerRequestType,
pub query_params: Option<TypescriptType>,
pub path: Option<TypescriptType>,
pub body_type: Option<TypescriptType>
}
pub fn generate_handler_types(routes_path: PathBuf) -> Vec<Handler> {
let mut handlers: Vec<Handler> = Vec::new();
let routes_dir = routes_path.join("src/routes");
for route_file in WalkDir::new(routes_dir) {
let entry = match route_file {
Ok(val) => val,
Err(e) => panic!("An error occurred what attempting to parse directory: {}", e)
};
if entry.path().is_dir() {
continue;
}
let handler_key = entry.path().file_stem().unwrap().to_string_lossy().to_string();
let mut file = File::open(&entry.path()).unwrap();
let mut route_file_contents = String::new();
file.read_to_string(&mut route_file_contents).unwrap();
let handler_types = match extract_handler_types(&route_file_contents) {
Some(val) => {
if val.len() > 0 {
val
} else {
continue
}
},
None => continue
};
let mut query_params: Option<TypescriptType> = None;
let mut body_type: Option<TypescriptType> = None;
let mut path: Option<TypescriptType> = None;
let request_type = handler_types[0].as_ref().unwrap().handler_type.clone();
for typed in handler_types {
let rust_primitive = match typed {
Some(val) => val,
None => continue
};
let converted_type = convert_primitive(&rust_primitive.type_value);
match rust_primitive.class {
Some(TypeClass::InputBody) => body_type = Some(converted_type),
Some(TypeClass::QueryParam) => query_params = Some(converted_type),
Some(TypeClass::Path) => path = Some(converted_type),
_ => continue
}
}
match request_type {
HandlerRequestType::Get => {
handlers.push(Handler::Query(TypedQueryHandler {
key: handler_key,
request_type,
path,
query_params
}));
},
_ => {
handlers.push(Handler::Mutation(TypedMutationHandler {
key: handler_key,
request_type,
query_params,
path,
body_type
}));
}
}
}
handlers
}
pub fn create_typescript_types(out_dir: PathBuf, route_dir: PathBuf) {
let handlers = generate_handler_types(route_dir);
if handlers.len() < 1 {
return;
}
let mut file = OpenOptions::new().write(true).create(true).truncate(true).open(format!("{}/bindings.ts", out_dir.as_os_str().to_str().unwrap())).unwrap();
let mut queries_ts = String::from("{");
let mut mutations_ts = String::from("{");
for handler in handlers {
match handler {
Handler::Query(query) => {
let mut ts_type = format!("\n\t\t{}: {{\n", query.key);
let spacing = space();
let request_type = match query.request_type {
HandlerRequestType::Post => "post",
HandlerRequestType::Put => "put",
HandlerRequestType::Delete => "delete",
HandlerRequestType::Get => "get",
};
if let Some(query_params_type) = query.query_params {
let query_params = format!("\t\t\tquery_params: {}", query_params_type.typescript_type);
ts_type.push_str(&format!("{}{}\n", spacing, query_params));
}
if let Some(path_type) = query.path {
let path = format!("\t\t\tpath: {}", path_type.typescript_type);
ts_type.push_str(&format!("{}{}\n", spacing, path));
}
let request_type = format!("\t\t\ttype: '{}'", request_type);
ts_type.push_str(&format!("{}{}\n", spacing, request_type));
ts_type.push_str(&format!("\t\t}},\n"));
queries_ts.push_str(&ts_type);
},
Handler::Mutation(mutation) => {
let mut ts_type = format!("\n\t\t{}: {{\n", mutation.key);
let spacing = space();
let request_type = match mutation.request_type {
HandlerRequestType::Post => "post",
HandlerRequestType::Put => "put",
HandlerRequestType::Delete => "delete",
HandlerRequestType::Get => "get",
};
if let Some(query_params_type) = mutation.query_params {
let query_params = format!("\t\t\tquery_params: {}", query_params_type.typescript_type);
ts_type.push_str(&format!("{}{}\n", spacing, query_params));
}
if let Some(path_type) = mutation.path {
let path = format!("\t\t\tpath: {}", path_type.typescript_type);
ts_type.push_str(&format!("{}{}\n", spacing, path));
}
if let Some(body_type) = mutation.body_type {
let body = format!("\t\t\tbody: {}", body_type.typescript_type);
ts_type.push_str(&format!("{}{}\n", spacing, body));
}
let request_type = format!("\t\t\ttype: '{}'", request_type);
ts_type.push_str(&format!("{}{}\n", spacing, request_type));
ts_type.push_str(&format!("\t\t}}\n"));
mutations_ts.push_str(&ts_type);
}
}
}
queries_ts.push_str("\t},");
mutations_ts.push_str("\t},");
let mut handlers_interface = format!("\n\nexport interface Handlers {{\n");
handlers_interface.push_str(&format!("\tqueries: {}\n", queries_ts));
handlers_interface.push_str(&format!("\tmutations: {}\n", mutations_ts));
handlers_interface.push_str("}");
file.write_all(handlers_interface.as_bytes()).unwrap();
}