@@ -395,16 +395,6 @@ export async function paginate<T extends ObjectLiteral>(
395395 items = await queryBuilder . getMany ( )
396396 }
397397
398- let path : string
399- const { queryOrigin, queryPath } = getQueryUrlComponents ( query . path )
400- if ( config . relativePath ) {
401- path = queryPath
402- } else if ( config . origin ) {
403- path = config . origin + queryPath
404- } else {
405- path = queryOrigin + queryPath
406- }
407-
408398 const sortByQuery = sortBy . map ( ( order ) => `&sortBy=${ order . join ( ':' ) } ` ) . join ( '' )
409399 const searchQuery = query . search ? `&search=${ query . search } ` : ''
410400
@@ -429,6 +419,18 @@ export async function paginate<T extends ObjectLiteral>(
429419
430420 const options = `&limit=${ limit } ${ sortByQuery } ${ searchQuery } ${ searchByQuery } ${ selectQuery } ${ filterQuery } `
431421
422+ let path : string = null
423+ if ( query . path !== null ) {
424+ // `query.path` does not exist in RPC/WS requests and is set to null then.
425+ const { queryOrigin, queryPath } = getQueryUrlComponents ( query . path )
426+ if ( config . relativePath ) {
427+ path = queryPath
428+ } else if ( config . origin ) {
429+ path = config . origin + queryPath
430+ } else {
431+ path = queryOrigin + queryPath
432+ }
433+ }
432434 const buildLink = ( p : number ) : string => path + '?page=' + p + options
433435
434436 const totalPages = isPaginated ? Math . ceil ( totalItems / limit ) : 1
@@ -446,13 +448,17 @@ export async function paginate<T extends ObjectLiteral>(
446448 select : isQuerySelected ? selectParams : undefined ,
447449 filter : query . filter ,
448450 } ,
449- links : {
450- first : page == 1 ? undefined : buildLink ( 1 ) ,
451- previous : page - 1 < 1 ? undefined : buildLink ( page - 1 ) ,
452- current : buildLink ( page ) ,
453- next : page + 1 > totalPages ? undefined : buildLink ( page + 1 ) ,
454- last : page == totalPages || ! totalItems ? undefined : buildLink ( totalPages ) ,
455- } ,
451+ // If there is no `path`, don't build links.
452+ links :
453+ path !== null
454+ ? {
455+ first : page == 1 ? undefined : buildLink ( 1 ) ,
456+ previous : page - 1 < 1 ? undefined : buildLink ( page - 1 ) ,
457+ current : buildLink ( page ) ,
458+ next : page + 1 > totalPages ? undefined : buildLink ( page + 1 ) ,
459+ last : page == totalPages || ! totalItems ? undefined : buildLink ( totalPages ) ,
460+ }
461+ : ( { } as Paginated < T > [ 'links' ] ) ,
456462 }
457463
458464 return Object . assign ( new Paginated < T > ( ) , results )
0 commit comments