@@ -326,20 +326,35 @@ pub fn readme(req: &mut dyn RequestExt) -> EndpointResult {
326
326
// FIXME: Not sure why this is necessary since /crates/:crate_id returns
327
327
// this information already, but ember is definitely requesting it
328
328
pub fn versions ( req : & mut dyn RequestExt ) -> EndpointResult {
329
- let pagination_options = PaginationOptions :: builder ( ) . gather ( req) ?;
330
329
let crate_name = & req. params ( ) [ "crate_id" ] ;
331
330
let conn = req. db_read ( ) ?;
332
331
let krate: Crate = Crate :: by_name ( crate_name) . first ( & * conn) ?;
333
- let data: Paginated < ( Version , Option < User > ) > = krate
334
- . all_versions ( )
335
- . left_outer_join ( users:: table)
336
- . select ( ( versions:: all_columns, users:: all_columns. nullable ( ) ) )
337
- . order ( versions:: created_at. desc ( ) )
338
- . pages_pagination ( pagination_options)
339
- . load ( & * conn) ?;
340
- let more = data. next_page_params ( ) . is_some ( ) ;
341
332
342
- let mut versions_and_publishers = data. into_iter ( ) . collect :: < Vec < _ > > ( ) ;
333
+ // to keep retrocompatibility, we paginate only if per_page parameter is present
334
+ let ( mut versions_and_publishers, meta) = if req. query ( ) . get ( "per_page" ) . is_some ( ) {
335
+ let pagination_options = PaginationOptions :: builder ( ) . gather ( req) ?;
336
+ let data: Paginated < ( Version , Option < User > ) > = krate
337
+ . all_versions ( )
338
+ . left_outer_join ( users:: table)
339
+ . select ( ( versions:: all_columns, users:: all_columns. nullable ( ) ) )
340
+ . order ( versions:: created_at. desc ( ) )
341
+ . pages_pagination ( pagination_options)
342
+ . load ( & * conn) ?;
343
+ let more = data. next_page_params ( ) . is_some ( ) ;
344
+
345
+ (
346
+ data. into_iter ( ) . collect :: < Vec < _ > > ( ) ,
347
+ Some ( json ! ( { "more" : more } ) ) ,
348
+ )
349
+ } else {
350
+ let data: Vec < ( Version , Option < User > ) > = krate
351
+ . all_versions ( )
352
+ . left_outer_join ( users:: table)
353
+ . select ( ( versions:: all_columns, users:: all_columns. nullable ( ) ) )
354
+ . load ( & * conn) ?;
355
+ ( data, None )
356
+ } ;
357
+
343
358
versions_and_publishers
344
359
. sort_by_cached_key ( |( version, _) | Reverse ( semver:: Version :: parse ( & version. num ) . ok ( ) ) ) ;
345
360
@@ -354,10 +369,18 @@ pub fn versions(req: &mut dyn RequestExt) -> EndpointResult {
354
369
. map ( |( ( v, pb) , aas) | EncodableVersion :: from ( v, crate_name, pb, aas) )
355
370
. collect :: < Vec < _ > > ( ) ;
356
371
357
- Ok ( req. json ( & json ! ( {
358
- "versions" : versions,
359
- "meta" : { "more" : more } ,
360
- } ) ) )
372
+ // probably here would be simplier with a concrete response type
373
+ let response = if let Some ( meta) = meta {
374
+ json ! ( {
375
+ "versions" : versions,
376
+ "meta" : meta,
377
+ } )
378
+ } else {
379
+ json ! ( {
380
+ "versions" : versions,
381
+ } )
382
+ } ;
383
+ Ok ( req. json ( & response) )
361
384
}
362
385
363
386
/// Handles the `GET /crates/:crate_id/reverse_dependencies` route.
0 commit comments