1
1
//! Endpoint for searching and discovery functionality
2
2
3
3
use crate :: auth:: AuthCheck ;
4
+ use axum:: Json ;
4
5
use axum:: extract:: FromRequestParts ;
5
6
use axum_extra:: extract:: Query ;
6
- use axum_extra:: json;
7
- use axum_extra:: response:: ErasedJson ;
8
7
use derive_more:: Deref ;
9
8
use diesel:: dsl:: { InnerJoinQuerySource , LeftJoinQuerySource , exists} ;
10
9
use diesel:: prelude:: * ;
@@ -28,6 +27,29 @@ use crate::util::RequestUtils;
28
27
use crate :: util:: string_excl_null:: StringExclNull ;
29
28
use crates_io_diesel_helpers:: { array_agg, canon_crate_name, lower} ;
30
29
30
+ #[ derive( Debug , Serialize , utoipa:: ToSchema ) ]
31
+ pub struct ListResponse {
32
+ crates : Vec < EncodableCrate > ,
33
+
34
+ #[ schema( inline) ]
35
+ meta : ListMeta ,
36
+ }
37
+
38
+ #[ derive( Debug , Serialize , utoipa:: ToSchema ) ]
39
+ pub struct ListMeta {
40
+ /// The total number of crates that match the query.
41
+ #[ schema( example = 123 ) ]
42
+ total : i64 ,
43
+
44
+ /// Query string to the next page of results, if any.
45
+ #[ schema( example = "?page=3" ) ]
46
+ next_page : Option < String > ,
47
+
48
+ /// Query string to the previous page of results, if any.
49
+ #[ schema( example = "?page=1" ) ]
50
+ prev_page : Option < String > ,
51
+ }
52
+
31
53
/// Returns a list of crates.
32
54
///
33
55
/// Called in a variety of scenarios in the front end, including:
@@ -44,13 +66,13 @@ use crates_io_diesel_helpers::{array_agg, canon_crate_name, lower};
44
66
( "cookie" = [ ] ) ,
45
67
) ,
46
68
tag = "crates" ,
47
- responses( ( status = 200 , description = "Successful Response" ) ) ,
69
+ responses( ( status = 200 , description = "Successful Response" , body = inline ( ListResponse ) ) ) ,
48
70
) ]
49
71
pub async fn list_crates (
50
72
app : AppState ,
51
73
params : ListQueryParams ,
52
74
req : Parts ,
53
- ) -> AppResult < ErasedJson > {
75
+ ) -> AppResult < Json < ListResponse > > {
54
76
// Notes:
55
77
// The different use cases this function covers is handled through passing
56
78
// in parameters in the GET request.
@@ -240,12 +262,12 @@ pub async fn list_crates(
240
262
} )
241
263
. collect :: < Vec < _ > > ( ) ;
242
264
243
- Ok ( json ! ( {
244
- "crates" : crates,
245
- " meta" : {
246
- "total" : total,
247
- "next_page" : next_page,
248
- "prev_page" : prev_page,
265
+ Ok ( Json ( ListResponse {
266
+ crates,
267
+ meta : ListMeta {
268
+ total,
269
+ next_page,
270
+ prev_page,
249
271
} ,
250
272
} ) )
251
273
}
0 commit comments