Skip to content

Commit 0a285fe

Browse files
committed
Add Feathers Types for pagination util
1 parent 5eaeb0e commit 0a285fe

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

src/utils.ts

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,29 @@ import ObjectID from 'bson-objectid'
1616
import { globalModels as models } from './service-module/global-models'
1717
import stringify from 'fast-json-stable-stringify'
1818

19+
interface Query {
20+
[key: string]: any
21+
}
22+
interface PaginationOptions {
23+
default: number
24+
max: number
25+
}
26+
interface Params {
27+
query?: Query
28+
paginate?: false | Pick<PaginationOptions, 'max'>
29+
provider?: string
30+
route?: { [key: string]: string }
31+
headers?: { [key: string]: any }
32+
33+
[key: string]: any // (JL) not sure if we want this
34+
}
35+
interface Paginated<T> {
36+
total: number
37+
limit: number
38+
skip: number
39+
data: T[]
40+
}
41+
1942
export function stripSlashes(location: string) {
2043
return _trim(location, '/')
2144
}
@@ -283,28 +306,22 @@ export function updateOriginal(original, newData) {
283306
})
284307
}
285308

286-
export function getQueryInfo(params = {}, response = {}) {
287-
// @ts-ignore
309+
//@ts-ignore
310+
export function getQueryInfo(params: Params = {}, response: Paginated = {}) {
288311
const query = params.query || {}
289-
// @ts-ignore
290312
const qid = params.qid || 'default'
291-
// @ts-ignore
292-
const $limit = (response.limit !== null && response.limit !== undefined)
293-
// @ts-ignore
294-
? response.limit
295-
: query.$limit
296-
// @ts-ignore
297-
const $skip = (response.skip !== null && response.skip !== undefined)
298-
// @ts-ignore
299-
? response.skip
300-
: query.$skip
313+
const $limit =
314+
response.limit !== null && response.limit !== undefined
315+
? response.limit
316+
: query.$limit
317+
const $skip =
318+
response.skip !== null && response.skip !== undefined
319+
? response.skip
320+
: query.$skip
301321

302-
// @ts-ignore
303322
const queryParams = _omit(query, ['$limit', '$skip'])
304-
// @ts-ignore
305323
const queryId = stringify(queryParams)
306324
const pageParams = $limit !== undefined ? { $limit, $skip } : undefined
307-
// @ts-ignore
308325
const pageId = pageParams ? stringify(pageParams) : undefined
309326

310327
return {

0 commit comments

Comments
 (0)