1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
import { SortMeta } from 'primeng/api'
import { HttpParams } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { ComponentPaginationLight } from './component-pagination.model'
import { RestPagination } from './rest-pagination'
interface QueryStringFilterPrefixes {
[key: string]: {
prefix: string
handler?: (v: string) => string | number
multiple?: boolean
isBoolean?: boolean
}
}
type ParseQueryStringFilters <K extends keyof any> = Partial<Record<K, string | number | boolean | (string | number | boolean)[]>>
type ParseQueryStringFiltersResult <K extends keyof any> = ParseQueryStringFilters<K> & { search?: string }
@Injectable()
export class RestService {
addRestGetParams (params: HttpParams, pagination?: RestPagination, sort?: SortMeta | string) {
let newParams = params
if (pagination !== undefined) {
newParams = newParams.set('start', pagination.start.toString())
.set('count', pagination.count.toString())
}
if (sort !== undefined) {
let sortString = ''
if (typeof sort === 'string') {
sortString = sort
} else {
const sortPrefix = sort.order === 1 ? '' : '-'
sortString = sortPrefix + sort.field
}
newParams = newParams.set('sort', sortString)
}
return newParams
}
addArrayParams (params: HttpParams, name: string, values: (string | number)[]) {
for (const v of values) {
params = params.append(name, v)
}
return params
}
addObjectParams (params: HttpParams, object: { [ name: string ]: any }) {
for (const name of Object.keys(object)) {
const value = object[name]
if (value === undefined || value === null) continue
if (Array.isArray(value)) {
params = this.addArrayParams(params, name, value)
} else {
params = params.append(name, value)
}
}
return params
}
componentToRestPagination (componentPagination: ComponentPaginationLight): RestPagination {
const start: number = (componentPagination.currentPage - 1) * componentPagination.itemsPerPage
const count: number = componentPagination.itemsPerPage
return { start, count }
}
/*
* Returns an object containing the filters and the remaining search
*/
parseQueryStringFilter <T extends QueryStringFilterPrefixes> (q: string, prefixes: T): ParseQueryStringFiltersResult<keyof T> {
if (!q) return {}
// Tokenize the strings using spaces that are not in quotes
const tokens = q.match(/(?:[^\s"]+|"[^"]*")+/g)
.filter(token => !!token)
// Build prefix array
const prefixeStrings = Object.values(prefixes)
.map(p => p.prefix)
// Search is the querystring minus defined filters
const searchTokens = tokens.filter(t => {
return prefixeStrings.every(prefixString => t.startsWith(prefixString) === false)
})
const additionalFilters: ParseQueryStringFilters<keyof T> = {}
for (const prefixKey of Object.keys(prefixes) as (keyof T)[]) {
const prefixObj = prefixes[prefixKey]
const prefix = prefixObj.prefix
const matchedTokens = tokens.filter(t => t.startsWith(prefix))
.map(t => t.slice(prefix.length)) // Keep the value filter
.map(t => t.replace(/^"|"$/g, '')) // Remove ""
.map(t => {
if (prefixObj.handler) return prefixObj.handler(t)
if (prefixObj.isBoolean) {
if (t === 'true') return true
if (t === 'false') return false
return undefined
}
return t
})
.filter(t => t !== null && t !== undefined)
if (matchedTokens.length === 0) continue
additionalFilters[prefixKey] = prefixObj.multiple === true
? matchedTokens
: matchedTokens[0]
}
return {
search: searchTokens.join(' ') || undefined,
...additionalFilters
}
}
}
|