@@ -19,36 +19,54 @@ export const tokensToSearchParams = <RequestParamsKeys extends string>(
1919 return params ;
2020} ;
2121
22+ export type RequestParam = string | { min : number } | { max : number } ;
23+
24+ const convertTokenValueToRequestParam = ( token : PropertyFilterProps . Query [ 'tokens' ] [ number ] ) : RequestParam => {
25+ const { value, operator } = token ;
26+
27+ if ( operator === '>=' ) {
28+ return { min : Number ( value ) } ;
29+ }
30+
31+ if ( operator === '<=' ) {
32+ return { max : Number ( value ) } ;
33+ }
34+
35+ return value ;
36+ } ;
37+
2238export const tokensToRequestParams = < RequestParamsKeys extends string > ( {
2339 tokens,
2440 arrayFieldKeys,
2541} : {
2642 tokens : PropertyFilterProps . Query [ 'tokens' ] ;
2743 arrayFieldKeys ?: RequestParamsKeys [ ] ;
2844} ) => {
29- return tokens . reduce < Record < RequestParamsKeys , string | string [ ] > > (
45+ return tokens . reduce < Record < RequestParamsKeys , RequestParam | string [ ] > > (
3046 ( acc , token ) => {
3147 const propertyKey = token . propertyKey as RequestParamsKeys ;
3248
3349 if ( ! propertyKey ) {
3450 return acc ;
3551 }
3652
53+ const convertedValue = convertTokenValueToRequestParam ( token ) ;
54+
3755 if ( arrayFieldKeys ?. includes ( propertyKey ) ) {
3856 if ( Array . isArray ( acc [ propertyKey ] ) ) {
39- acc [ propertyKey ] . push ( token . value ) ;
57+ acc [ propertyKey ] . push ( convertedValue as string ) ;
4058 } else {
41- acc [ propertyKey ] = [ token . value ] ;
59+ acc [ propertyKey ] = [ convertedValue as string ] ;
4260 }
4361
4462 return acc ;
4563 }
4664
47- acc [ propertyKey ] = token . value ;
65+ acc [ propertyKey ] = convertedValue ;
4866
4967 return acc ;
5068 } ,
51- { } as Record < RequestParamsKeys , string > ,
69+ { } as Record < RequestParamsKeys , RequestParam > ,
5270 ) ;
5371} ;
5472
0 commit comments