@@ -165,30 +165,48 @@ export class JsonServer {
165
165
* @param collection - Data collection to paginate
166
166
* @param page - Current page number (default: 1)
167
167
* @param perPage - Items per page (default: 10)
168
- * @returns Object with pagination metadata and data
168
+ * @returns Paginated data with metadata
169
169
*/
170
170
private getPaginatedData (
171
171
collection : any [ ] ,
172
172
page : number = 1 ,
173
173
perPage : number = 10
174
174
) : Record < string , any > {
175
175
// Ensure valid page and perPage values
176
- page = Math . max ( 1 , page ) ;
177
- perPage = Math . max ( 1 , perPage ) ;
178
-
176
+ page = Math . max ( 1 , parseInt ( String ( page ) ) || 1 ) ;
177
+ perPage = Math . max ( 1 , parseInt ( String ( perPage ) ) || 10 ) ;
178
+
179
179
const total = collection . length ;
180
+ const totalPages = Math . ceil ( total / perPage ) ;
180
181
const start = ( page - 1 ) * perPage ;
181
182
const end = Math . min ( start + perPage , total ) ;
182
- const data = collection . slice ( start , end ) ;
183
183
184
- const paginationMeta = this . createPaginationMetadata ( total , page , perPage ) ;
184
+ // Get the data slice for the current page
185
+ const data = collection . slice ( start , end ) ;
185
186
186
187
return {
187
- ...paginationMeta ,
188
188
data,
189
+ first : 1 ,
190
+ prev : page > 1 ? page - 1 : null ,
191
+ next : page < totalPages ? page + 1 : null ,
192
+ last : totalPages || 1 ,
193
+ pages : totalPages || 1 ,
194
+ items : total ,
189
195
} ;
190
196
}
191
197
198
+ /**
199
+ * Checks if pagination should continue based on current page and total items
200
+ *
201
+ * @param currentPage - Current page number
202
+ * @param pageSize - Number of items per page
203
+ * @param totalItems - Total number of items in the collection
204
+ * @returns Boolean indicating if there are more pages
205
+ */
206
+ continueToIterate ( currentPage : number , pageSize : number , totalItems : number ) : boolean {
207
+ return currentPage * pageSize < totalItems ;
208
+ }
209
+
192
210
/**
193
211
* Get paginated resources from a collection
194
212
*
@@ -205,53 +223,34 @@ export class JsonServer {
205
223
if ( ! this . db [ resourceName ] ) {
206
224
return {
207
225
data : [ ] ,
208
- pagination : {
209
- currentPage : page ,
210
- pageSize,
211
- totalItems : 0 ,
212
- totalPages : 0 ,
213
- hasMore : false ,
214
- } ,
226
+ first : 1 ,
227
+ prev : null ,
228
+ next : null ,
229
+ last : 1 ,
230
+ pages : 1 ,
231
+ items : 0 ,
215
232
} ;
216
233
}
217
234
218
235
const collection = this . db [ resourceName ] ;
219
236
const totalItems = collection . length ;
220
- const totalPages = Math . ceil ( totalItems / pageSize ) ;
221
237
222
- const start = ( page - 1 ) * pageSize ;
223
- const end = start + pageSize ;
238
+ // Ensure page and pageSize are valid numbers
239
+ page = Math . max ( 1 , parseInt ( String ( page ) , 10 ) || 1 ) ;
240
+ pageSize = Math . max ( 1 , parseInt ( String ( pageSize ) , 10 ) || 10 ) ;
224
241
242
+ const start = ( page - 1 ) * pageSize ;
243
+ const end = Math . min ( start + pageSize , totalItems ) ;
225
244
const data = collection . slice ( start , end ) ;
226
245
246
+ const paginationMeta = this . createPaginationMetadata ( totalItems , page , pageSize ) ;
247
+
227
248
return {
228
- data,
229
- pagination : {
230
- currentPage : page ,
231
- pageSize,
232
- totalItems,
233
- totalPages,
234
- hasMore : this . continueToIterate ( page , pageSize , totalItems ) ,
235
- } ,
249
+ data : data ,
250
+ ...paginationMeta ,
236
251
} ;
237
252
}
238
253
239
- /**
240
- * Determines if pagination should continue to next page
241
- *
242
- * @param currentPage - Current page number
243
- * @param pageSize - Items per page
244
- * @param totalItems - Total number of items
245
- * @returns Boolean indicating if there are more pages to iterate
246
- */
247
- continueToIterate ( currentPage : number , pageSize : number , totalItems : number ) : boolean {
248
- // Calculate total number of pages
249
- const totalPages = Math . ceil ( totalItems / pageSize ) ;
250
-
251
- // Check if current page is less than total pages
252
- return currentPage < totalPages ;
253
- }
254
-
255
254
/**
256
255
* Load database from JSON file
257
256
*
@@ -421,9 +420,16 @@ export class JsonServer {
421
420
const page = pageParam ? Math . max ( 1 , parseInt ( pageParam as string ) || 1 ) : 1 ;
422
421
const perPage = perPageParam ? Math . max ( 1 , parseInt ( perPageParam as string ) || 10 ) : 10 ;
423
422
424
- // Apply pagination and return the paginated result
425
- const paginatedData = this . getPaginatedData ( filteredData , page , perPage ) ;
426
- return res . json ( paginatedData ) ;
423
+ // Get paginated data with proper metadata using the getPaginatedData method
424
+ const paginatedResult = this . getPaginatedData ( filteredData , page , perPage ) ;
425
+
426
+ // Set pagination headers
427
+ res . setHeader ( 'X-Total-Count' , paginatedResult . items . toString ( ) ) ;
428
+ res . setHeader ( 'X-Total-Pages' , paginatedResult . pages . toString ( ) ) ;
429
+ res . setHeader ( 'Access-Control-Expose-Headers' , 'X-Total-Count, X-Total-Pages' ) ;
430
+
431
+ // Return the paginated result
432
+ return res . json ( paginatedResult ) ;
427
433
}
428
434
429
435
res . json ( filteredData ) ;
0 commit comments