fix FindFilter
This commit is contained in:
parent
b1a4be4cf8
commit
17f05dd9e2
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "axp-ts",
|
||||
"version": "1.11.0",
|
||||
"version": "1.11.1",
|
||||
"description": "TypeScript helper library",
|
||||
"author": "AntoXa PRO <info@antoxa.pro>",
|
||||
"homepage": "https://antoxahub.ru/antoxa/axp-ts",
|
||||
|
@ -22,8 +22,8 @@ export type TQuery = z.infer<typeof querySchema>
|
||||
* Класс для работы с запросами (для удобства).
|
||||
*/
|
||||
export type TFindFilter<T extends TQuery> = {
|
||||
obj: Omit<T, 'page' | 'limit' | 'sort'>
|
||||
pagination: TPaginationQuery
|
||||
obj?: Omit<T, 'page' | 'limit' | 'sort'>
|
||||
pagination?: TPaginationQuery
|
||||
sort?: string
|
||||
}
|
||||
|
||||
@ -31,8 +31,8 @@ export type TFindFilter<T extends TQuery> = {
|
||||
* Класс для работы с запросами (для удобства).
|
||||
*/
|
||||
export class FindFilter<T extends TQuery> implements TFindFilter<T> {
|
||||
obj: Omit<T, 'page' | 'limit' | 'sort'>
|
||||
pagination: TPaginationQuery
|
||||
obj?: Omit<T, 'page' | 'limit' | 'sort'>
|
||||
pagination?: TPaginationQuery
|
||||
sort?: string
|
||||
|
||||
constructor(query?: T) {
|
||||
@ -74,4 +74,37 @@ export class FindFilter<T extends TQuery> implements TFindFilter<T> {
|
||||
isEqual(filters: TFindFilter<T>[]) {
|
||||
return isEqual([this, ...filters])
|
||||
}
|
||||
|
||||
toString(filter?: TFindFilter<TQuery>, opt?: { base?: string }): string {
|
||||
let url = opt?.base?.replace(/[?]$/, '') || ''
|
||||
|
||||
if (filter) {
|
||||
const query: string[] = []
|
||||
|
||||
// Params and Pagination.
|
||||
for (const [key, val] of Object.entries({
|
||||
...filter.obj,
|
||||
...filter.pagination
|
||||
})) {
|
||||
if (val) {
|
||||
const keyEncode = encodeURIComponent(key)
|
||||
const valEncode = encodeURIComponent(val)
|
||||
query.push(keyEncode + '=' + valEncode)
|
||||
}
|
||||
}
|
||||
|
||||
// Sort.
|
||||
if (filter.sort) {
|
||||
try {
|
||||
query.push('sort=' + encodeURIComponent(filter.sort))
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
if (query.length) {
|
||||
url += '?' + query.join('&')
|
||||
}
|
||||
}
|
||||
|
||||
return url
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user