@@ -68,49 +68,63 @@ export const fetchCollection = async <
68
68
} ;
69
69
readonly objects : RA < SerializedRecord < SCHEMA > > ;
70
70
} > (
71
- formatUrl (
71
+ buildUrl (
72
72
`/api/specify/${ tableName . toLowerCase ( ) } /` ,
73
- Object . fromEntries (
74
- filterArray (
75
- Array . from (
76
- Object . entries ( {
77
- ...filters ,
78
- ...advancedFilters ,
79
- } ) . map ( ( [ key , value ] ) => {
80
- const mapped =
81
- value === undefined
82
- ? undefined
83
- : mapValue ( key , value , tableName ) ;
84
- return mapped === undefined
85
- ? undefined
86
- : ( [ key . toLowerCase ( ) , mapped ] as const ) ;
87
- } )
88
- )
89
- )
90
- )
73
+ tableName ,
74
+ filters ,
75
+ advancedFilters
91
76
) ,
92
-
93
77
{ headers : { Accept : 'application/json' } }
94
78
) . then ( ( { data : { meta, objects } } ) => ( {
95
79
records : objects . map ( serializeResource ) ,
96
80
totalCount : meta . total_count ,
97
81
} ) ) ;
98
82
99
- function mapValue (
83
+ const buildUrl = <
84
+ TABLE_NAME extends keyof Tables ,
85
+ SCHEMA extends Tables [ TABLE_NAME ]
86
+ > (
87
+ baseUrl : string ,
88
+ tableName : TABLE_NAME ,
89
+ filters : CollectionFetchFilters < SCHEMA > ,
90
+ advancedFilters : IR < boolean | number | string > = { }
91
+ ) : string =>
92
+ formatUrl (
93
+ baseUrl ,
94
+ Object . fromEntries (
95
+ filterArray (
96
+ Object . entries ( {
97
+ ...filters ,
98
+ ...advancedFilters ,
99
+ } ) . map ( ( [ key , value ] ) => {
100
+ const mapped =
101
+ value === undefined
102
+ ? undefined
103
+ : mapQueryParameter ( key , value , tableName ) ;
104
+ return mapped === undefined
105
+ ? undefined
106
+ : ( [ key . toLowerCase ( ) , mapped ] as const ) ;
107
+ } )
108
+ )
109
+ )
110
+ ) ;
111
+
112
+ function mapQueryParameter (
100
113
key : string ,
101
114
value : unknown ,
102
115
tableName : keyof Tables
103
116
) : string | undefined {
104
- if ( key === 'orderBy' ) return ( value as string ) . toString ( ) . toLowerCase ( ) ;
117
+ if ( key === 'orderBy' ) return String ( value ) . toLowerCase ( ) ;
105
118
else if ( key === 'domainFilter' ) {
106
119
// GetScope() returns undefined for tables with only collectionmemberid.
107
120
const scopingField = tables [ tableName ] . getScope ( ) ;
108
121
return value === true &&
109
122
( tableName === 'Attachment' || typeof scopingField === 'object' )
110
123
? 'true'
111
124
: undefined ;
112
- } else if ( typeof value === 'boolean' ) return value ? 'True' : 'False' ;
113
- else return ( value as string ) . toString ( ) ;
125
+ } else if ( key === 'distinct' ) return value === true ? 'true' : undefined ;
126
+ else if ( typeof value === 'boolean' ) return value ? 'True' : 'False' ;
127
+ else return String ( value ) ;
114
128
}
115
129
116
130
/**
@@ -189,7 +203,6 @@ export const fetchRows = async <
189
203
} : Omit < CollectionFetchFilters < SCHEMA > , 'fields' > & {
190
204
readonly fields : FIELDS ;
191
205
readonly distinct ?: boolean ;
192
- readonly limit ?: number ;
193
206
} ,
194
207
/**
195
208
* Advanced filters, not type-safe.
@@ -201,31 +214,16 @@ export const fetchRows = async <
201
214
advancedFilters : IR < number | string > = { }
202
215
) : Promise < RA < FieldsToTypes < FIELDS > > > => {
203
216
const { data } = await ajax < RA < RA < boolean | number | string | null > > > (
204
- formatUrl (
217
+ buildUrl (
205
218
`/api/specify_rows/${ tableName . toLowerCase ( ) } /` ,
206
- Object . fromEntries (
207
- filterArray (
208
- Array . from (
209
- Object . entries ( {
210
- ...filters ,
211
- ...advancedFilters ,
212
- ...( distinct ? { distinct : 'true' } : { } ) ,
213
- fields : Object . keys ( fields ) . join ( ',' ) . toLowerCase ( ) ,
214
- } ) . map ( ( [ key , value ] ) =>
215
- value === undefined
216
- ? undefined
217
- : [
218
- key . toLowerCase ( ) ,
219
- key === 'orderBy'
220
- ? value . toString ( ) . toLowerCase ( )
221
- : value . toString ( ) ,
222
- ]
223
- )
224
- )
225
- )
226
- )
219
+ tableName ,
220
+ filters as CollectionFetchFilters < SCHEMA > ,
221
+ {
222
+ ...advancedFilters ,
223
+ distinct,
224
+ fields : Object . keys ( fields ) . join ( ',' ) . toLowerCase ( ) ,
225
+ }
227
226
) ,
228
-
229
227
{ headers : { Accept : 'application/json' } }
230
228
) ;
231
229
const keys = Object . keys ( fields ) ;
@@ -236,3 +234,8 @@ export const fetchRows = async <
236
234
) as FieldsToTypes < FIELDS >
237
235
) ;
238
236
} ;
237
+
238
+ export const exportsForTests = {
239
+ buildUrl,
240
+ mapQueryParameter,
241
+ } ;
0 commit comments