diff --git a/package.json b/package.json index e7bdae9..d7ab0d1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "axp-ts", - "version": "1.11.1", + "version": "1.11.2", "description": "TypeScript helper library", "author": "AntoXa PRO ", "homepage": "https://antoxahub.ru/antoxa/axp-ts", diff --git a/src/requests/find-filter.ts b/src/requests/find-filter.ts index d156f03..f83477c 100644 --- a/src/requests/find-filter.ts +++ b/src/requests/find-filter.ts @@ -35,6 +35,17 @@ export class FindFilter implements TFindFilter { pagination?: TPaginationQuery sort?: string + static getQuery(filter: TFindFilter): T { + return Object.assign( + { ...filter.obj, ...filter.pagination }, + { sort: filter.sort } + ) as T + } + + static parse(filter: TFindFilter): FindFilter { + return new FindFilter(this.getQuery(filter)) + } + constructor(query?: T) { // Copy fiends. let queryCopy: T = Object.assign({}, query) @@ -59,6 +70,9 @@ export class FindFilter implements TFindFilter { this.obj = queryCopy } + /** + * @deprecated. + */ setPagination(pagination?: TPaginationQuery) { this.pagination = new Pagination(pagination).getQuery() } @@ -71,40 +85,38 @@ export class FindFilter implements TFindFilter { } } - isEqual(filters: TFindFilter[]) { - return isEqual([this, ...filters]) - } - - toString(filter?: TFindFilter, opt?: { base?: string }): string { + toString(opt?: { base?: string }): string { let url = opt?.base?.replace(/[?]$/, '') || '' - if (filter) { - const query: string[] = [] + 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) - } + // Params and Pagination. + for (const [key, val] of Object.entries({ + ...this.obj, + ...this.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) {} - } + // Sort. + if (this.sort) { + try { + query.push('sort=' + encodeURIComponent(this.sort)) + } catch (e) {} + } - if (query.length) { - url += '?' + query.join('&') - } + if (query.length) { + url += '?' + query.join('&') } return url } + + isEqual(filters: TFindFilter[]) { + return isEqual([this, ...filters]) + } }