@@ -4,8 +4,10 @@ const COLLECTION_OPERATORS = ['any', 'all'];
4
4
const BOOLEAN_FUNCTIONS = [ 'startswith' , 'endswith' , 'contains' ] ;
5
5
const SUPPORTED_EXPAND_PROPERTIES = [
6
6
'expand' ,
7
+ 'levels' ,
7
8
'select' ,
8
9
'top' ,
10
+ 'count' ,
9
11
'orderby' ,
10
12
'filter' ,
11
13
] ;
@@ -34,6 +36,8 @@ export type ExpandOptions<T> = {
34
36
filter : Filter ;
35
37
orderBy : OrderBy < T > ;
36
38
top : number ;
39
+ levels : number | 'max' ;
40
+ count : boolean | Filter ;
37
41
expand : Expand < T > ;
38
42
}
39
43
@@ -91,10 +95,10 @@ export const ITEM_ROOT = "";
91
95
export default function < T > ( {
92
96
select : $select ,
93
97
search : $search ,
94
- top : $top ,
95
- skip : $skip ,
96
98
skiptoken : $skiptoken ,
97
99
format : $format ,
100
+ top,
101
+ skip,
98
102
filter,
99
103
transform,
100
104
orderBy,
@@ -107,12 +111,7 @@ export default function <T>({
107
111
} : Partial < QueryOptions < T > > = { } ) {
108
112
let path = '' ;
109
113
110
- const params : any = {
111
- $filter : ( filter || count instanceof Object ) && buildFilter ( count instanceof Object ? count : filter ) ,
112
- $apply : transform && buildTransforms ( transform ) ,
113
- $expand : expand && buildExpand ( expand ) ,
114
- $orderby : orderBy && buildOrderBy ( orderBy ) ,
115
- } ;
114
+ const params : any = { } ;
116
115
117
116
if ( key ) {
118
117
if ( typeof key === 'object' ) {
@@ -125,6 +124,18 @@ export default function <T>({
125
124
}
126
125
}
127
126
127
+ if ( filter || typeof count === 'object' )
128
+ params . $filter = buildFilter ( typeof count === 'object' ? count : filter ) ;
129
+
130
+ if ( transform )
131
+ params . $apply = buildTransforms ( transform ) ;
132
+
133
+ if ( expand )
134
+ params . $expand = buildExpand ( expand ) ;
135
+
136
+ if ( orderBy )
137
+ params . $orderby = buildOrderBy ( orderBy ) ;
138
+
128
139
if ( count ) {
129
140
if ( typeof count === 'boolean' ) {
130
141
params . $count = true ;
@@ -133,6 +144,14 @@ export default function <T>({
133
144
}
134
145
}
135
146
147
+ if ( typeof top === 'number' ) {
148
+ params . $top = top ;
149
+ }
150
+
151
+ if ( typeof skip === 'number' ) {
152
+ params . $skip = skip ;
153
+ }
154
+
136
155
if ( action ) {
137
156
path += `/${ action } ` ;
138
157
}
@@ -159,7 +178,7 @@ export default function <T>({
159
178
. reduce ( ( acc , alias ) => Object . assign ( acc , { [ alias . handleName ( ) ] : alias . handleValue ( ) } ) , params ) ;
160
179
}
161
180
162
- return buildUrl ( path , { $select, $search, $top , $skip , $ skiptoken, $format, ...params } ) ;
181
+ return buildUrl ( path , { $select, $search, $skiptoken, $format, ...params } ) ;
163
182
}
164
183
165
184
function buildFilter ( filters : Filter = { } , propPrefix = '' ) : string {
@@ -402,12 +421,22 @@ function buildExpand<T>(expands: Expand<T>): string {
402
421
) {
403
422
return expandKeys
404
423
. map ( key => {
405
- const value =
406
- key === 'filter'
407
- ? buildFilter ( ( expands as NestedExpandOptions < any > ) [ key ] )
408
- : key . toLowerCase ( ) === 'orderby'
409
- ? buildOrderBy ( ( expands as NestedExpandOptions < any > ) [ key ] as OrderBy < T > )
410
- : buildExpand ( ( expands as NestedExpandOptions < any > ) [ key ] as Expand < T > ) ;
424
+ let value ;
425
+ switch ( key ) {
426
+ case 'filter' :
427
+ value = buildFilter ( ( expands as NestedExpandOptions < any > ) [ key ] ) ;
428
+ break ;
429
+ case 'orderBy' :
430
+ value = buildOrderBy ( ( expands as NestedExpandOptions < any > ) [ key ] as OrderBy < T > ) ;
431
+ break ;
432
+ case 'levels' :
433
+ case 'count' :
434
+ case 'top' :
435
+ value = `${ ( expands as NestedExpandOptions < any > ) [ key ] } ` ;
436
+ break ;
437
+ default :
438
+ value = buildExpand ( ( expands as NestedExpandOptions < any > ) [ key ] as Expand < T > ) ;
439
+ }
411
440
return `$${ key . toLowerCase ( ) } =${ value } ` ;
412
441
} )
413
442
. join ( ';' ) ;
@@ -508,13 +537,9 @@ function buildOrderBy<T>(orderBy: OrderBy<T>, prefix: string = ''): string {
508
537
509
538
function buildUrl ( path : string , params : PlainObject ) : string {
510
539
// This can be refactored using URL API. But IE does not support it.
511
- const queries : string [ ] = [ ] ;
512
- for ( const key of Object . getOwnPropertyNames ( params ) ) {
513
- const value = params [ key ] ;
514
- if ( value === 0 || ! ! value ) {
515
- queries . push ( `${ key } =${ value } ` ) ;
516
- }
517
- }
540
+ const queries : string [ ] = Object . getOwnPropertyNames ( params )
541
+ . filter ( key => params [ key ] !== undefined && params [ key ] !== '' )
542
+ . map ( key => `${ key } =${ params [ key ] } ` ) ;
518
543
return queries . length ? `${ path } ?${ queries . join ( '&' ) } ` : path ;
519
544
}
520
545
0 commit comments