1 import { SortMeta } from 'primeng/api'
2 import { HttpParams } from '@angular/common/http'
3 import { Injectable } from '@angular/core'
4 import { ComponentPaginationLight } from './component-pagination.model'
5 import { RestPagination } from './rest-pagination'
7 interface QueryStringFilterPrefixes {
10 handler?: (v: string) => string | number
16 type ParseQueryStringFilters <K extends keyof any> = Partial<Record<K, string | number | boolean | (string | number | boolean)[]>>
17 type ParseQueryStringFiltersResult <K extends keyof any> = ParseQueryStringFilters<K> & { search?: string }
20 export class RestService {
22 addRestGetParams (params: HttpParams, pagination?: RestPagination, sort?: SortMeta | string) {
23 let newParams = params
25 if (pagination !== undefined) {
26 newParams = newParams.set('start', pagination.start.toString())
27 .set('count', pagination.count.toString())
30 if (sort !== undefined) {
33 if (typeof sort === 'string') {
36 const sortPrefix = sort.order === 1 ? '' : '-'
37 sortString = sortPrefix + sort.field
40 newParams = newParams.set('sort', sortString)
46 addArrayParams (params: HttpParams, name: string, values: (string | number)[]) {
47 for (const v of values) {
48 params = params.append(name, v)
54 addObjectParams (params: HttpParams, object: { [ name: string ]: any }) {
55 for (const name of Object.keys(object)) {
56 const value = object[name]
57 if (value === undefined || value === null) continue
59 if (Array.isArray(value)) {
60 params = this.addArrayParams(params, name, value)
62 params = params.append(name, value)
69 componentToRestPagination (componentPagination: ComponentPaginationLight): RestPagination {
70 const start: number = (componentPagination.currentPage - 1) * componentPagination.itemsPerPage
71 const count: number = componentPagination.itemsPerPage
73 return { start, count }
77 * Returns an object containing the filters and the remaining search
79 parseQueryStringFilter <T extends QueryStringFilterPrefixes> (q: string, prefixes: T): ParseQueryStringFiltersResult<keyof T> {
82 // Tokenize the strings using spaces that are not in quotes
83 const tokens = q.match(/(?:[^\s"]+|"[^"]*")+/g)
84 .filter(token => !!token)
87 const prefixeStrings = Object.values(prefixes)
90 // Search is the querystring minus defined filters
91 const searchTokens = tokens.filter(t => {
92 return prefixeStrings.every(prefixString => t.startsWith(prefixString) === false)
95 const additionalFilters: ParseQueryStringFilters<keyof T> = {}
97 for (const prefixKey of Object.keys(prefixes) as (keyof T)[]) {
98 const prefixObj = prefixes[prefixKey]
99 const prefix = prefixObj.prefix
101 const matchedTokens = tokens.filter(t => t.startsWith(prefix))
102 .map(t => t.slice(prefix.length)) // Keep the value filter
103 .map(t => t.replace(/^"|"$/g, '')) // Remove ""
105 if (prefixObj.handler) return prefixObj.handler(t)
107 if (prefixObj.isBoolean) {
108 if (t === 'true') return true
109 if (t === 'false') return false
116 .filter(t => t !== null && t !== undefined)
118 if (matchedTokens.length === 0) continue
120 additionalFilters[prefixKey] = prefixObj.multiple === true
126 search: searchTokens.join(' ') || undefined,