11//! Endpoint for searching and discovery functionality
22
33use crate :: auth:: AuthCheck ;
4+ use axum:: Json ;
45use axum:: extract:: FromRequestParts ;
56use axum_extra:: extract:: Query ;
6- use axum_extra:: json;
7- use axum_extra:: response:: ErasedJson ;
87use derive_more:: Deref ;
98use diesel:: dsl:: { InnerJoinQuerySource , LeftJoinQuerySource , exists} ;
109use diesel:: prelude:: * ;
@@ -28,6 +27,29 @@ use crate::util::RequestUtils;
2827use crate :: util:: string_excl_null:: StringExclNull ;
2928use crates_io_diesel_helpers:: { array_agg, canon_crate_name, lower} ;
3029
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+
3153/// Returns a list of crates.
3254///
3355/// 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};
4466 ( "cookie" = [ ] ) ,
4567 ) ,
4668 tag = "crates" ,
47- responses( ( status = 200 , description = "Successful Response" ) ) ,
69+ responses( ( status = 200 , description = "Successful Response" , body = inline ( ListResponse ) ) ) ,
4870) ]
4971pub async fn list_crates (
5072 app : AppState ,
5173 params : ListQueryParams ,
5274 req : Parts ,
53- ) -> AppResult < ErasedJson > {
75+ ) -> AppResult < Json < ListResponse > > {
5476 // Notes:
5577 // The different use cases this function covers is handled through passing
5678 // in parameters in the GET request.
@@ -240,12 +262,12 @@ pub async fn list_crates(
240262 } )
241263 . collect :: < Vec < _ > > ( ) ;
242264
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,
249271 } ,
250272 } ) )
251273}
0 commit comments