fix: handle query param correctly

This commit is contained in:
ppetzold 2020-06-27 00:14:35 +02:00
parent edce303747
commit 318052a1e1

View File

@ -14,22 +14,15 @@ export const Paginate = createParamDecorator(
const { query } = request const { query } = request
const path = request.protocol + '://' + request.get('host') + request.baseUrl + request.path const path = request.protocol + '://' + request.get('host') + request.baseUrl + request.path
function readParamAsArray(param: unknown): string[] { let sortBy: [string, string][] = []
const result = typeof param === 'string' ? [param] : param
if (Array.isArray(result) && result.every((value) => typeof value === 'string')) {
return result
}
return []
}
let sortBy: [string, string][] | undefined = undefined
if (query.sortBy) { if (query.sortBy) {
const params = readParamAsArray(query.sortBy) const params = !Array.isArray(query.sortBy) ? [query.sortBy] : query.sortBy
for (const param of params) { if (params.some((param) => typeof param === 'string')) {
for (const param of params as string[]) {
const items = param.split(':') const items = param.split(':')
if (items.length === 2) { if (items.length === 2) {
if (!sortBy) sortBy = [] sortBy.push([items[0], items[1]])
sortBy.push(items as [string, string]) }
} }
} }
} }
@ -37,7 +30,7 @@ export const Paginate = createParamDecorator(
return { return {
page: query.page ? parseInt(query.page.toString(), 10) : undefined, page: query.page ? parseInt(query.page.toString(), 10) : undefined,
limit: query.limit ? parseInt(query.limit.toString(), 10) : undefined, limit: query.limit ? parseInt(query.limit.toString(), 10) : undefined,
sortBy, sortBy: sortBy.length > 0 ? sortBy : undefined,
path, path,
} }
} }