@@ -52,7 +52,7 @@ export interface ApiFetchCallback<T = any> {
5252
5353interface SourceDocuments {
5454 page : number ;
55- hits : Document [ ] ;
55+ hits : SearchResponseDocument [ ] ;
5656 total_hits : number ;
5757}
5858
@@ -84,6 +84,18 @@ export type ExecuteApiFetch = (
8484) => void ;
8585/* eslint-enable @typescript-eslint/no-explicit-any */
8686
87+ /**
88+ * Helper function to convert a setting to a query parameter string
89+ */
90+ /* eslint-disable @typescript-eslint/no-explicit-any */
91+ const settingToQueryParam = function ( setting : any , key : string ) : string {
92+ if ( setting !== null && setting !== undefined && setting !== '' ) {
93+ return '&' + key + '=' + setting ;
94+ }
95+ return '' ;
96+ } ;
97+ /* eslint-enable @typescript-eslint/no-explicit-any */
98+
8799/**
88100 * Fetch search results of search suggestions from the Addsearch API
89101 */
@@ -97,15 +109,6 @@ const executeApiFetch: ExecuteApiFetch = function (
97109 customFilterObject ,
98110 recommendOptions
99111) {
100- /* eslint-disable @typescript-eslint/no-explicit-any */
101- const settingToQueryParam = function ( setting : any , key : string ) {
102- if ( setting || setting === false ) {
103- return '&' + key + '=' + setting ;
104- }
105- return '' ;
106- } ;
107- /* eslint-enable @typescript-eslint/no-explicit-any */
108-
109112 // Validate query type
110113 if (
111114 type !== 'search' &&
@@ -138,23 +141,19 @@ const executeApiFetch: ExecuteApiFetch = function (
138141
139142 // Boolean operators (AND, OR, NOT) uppercase
140143 keyword = settings ?. enableLogicalOperators
141- ? keyword . replace ( / a n d / g, ' AND ' ) . replace ( / o r / g, ' OR ' ) . replace ( / n o t / g, ' NOT ' )
142- : keyword . replace ( / A N D / g, ' and ' ) . replace ( / O R / g, ' or ' ) . replace ( / N O T / g, ' not ' ) ;
144+ ? keyword . replaceAll ( ' and ' , ' AND ' ) . replaceAll ( ' or ' , ' OR ' ) . replaceAll ( ' not ' , ' NOT ' )
145+ : keyword
146+ . replaceAll ( ' AND ' , ' and ' )
147+ . replaceAll ( ' OR ' , ' or ' )
148+ . replaceAll ( ' NOT ' , ' not ' ) ;
143149
144150 // Escape
145151 keyword = encodeURIComponent ( keyword ) ;
146152
147153 // Fuzzy
148154 let fuzzy = settings ?. fuzzy ;
149155 if ( fuzzy === 'retry' ) {
150- // First call, non fuzzy
151- if ( fuzzyRetry !== true ) {
152- fuzzy = false ;
153- }
154- // Second call, fuzzy
155- else {
156- fuzzy = true ;
157- }
156+ fuzzy = fuzzyRetry === true ; // true on retry (second call), false on initial call
158157 }
159158
160159 // GET Parameters
@@ -188,8 +187,12 @@ const executeApiFetch: ExecuteApiFetch = function (
188187 collectAnalytics : settings ?. collectAnalytics ,
189188 postfixWildcard : settings ?. postfixWildcard ,
190189 categories : settings ?. categories ? settings ?. categories . split ( ',' ) : undefined ,
191- priceFromCents : settings ?. priceFromCents ? parseInt ( settings ?. priceFromCents , 10 ) : undefined ,
192- priceToCents : settings ?. priceToCents ? parseInt ( settings ?. priceToCents , 10 ) : undefined ,
190+ priceFromCents : settings ?. priceFromCents
191+ ? Number . parseInt ( settings ?. priceFromCents , 10 )
192+ : undefined ,
193+ priceToCents : settings ?. priceToCents
194+ ? Number . parseInt ( settings ?. priceToCents , 10 )
195+ : undefined ,
193196 dateFrom : settings ?. dateFrom ,
194197 dateTo : settings ?. dateTo ,
195198 paging : {
@@ -210,15 +213,13 @@ const executeApiFetch: ExecuteApiFetch = function (
210213
211214 // Add sortBy and sortOrder
212215 if ( Array . isArray ( settings ?. paging . sortBy ) && settings ?. paging . sortBy . length > 1 ) {
213- settings ?. paging . sortBy . forEach ( function ( value , index ) {
214- queryParamsString =
215- queryParamsString +
216+ for ( const [ index , value ] of settings . paging . sortBy . entries ( ) ) {
217+ queryParamsString +=
216218 settingToQueryParam ( value , 'sort' ) +
217- settingToQueryParam ( settings ? .paging . sortOrder [ index ] , 'order' ) ;
218- } ) ;
219+ settingToQueryParam ( settings . paging . sortOrder [ index ] , 'order' ) ;
220+ }
219221 } else {
220- queryParamsString =
221- queryParamsString +
222+ queryParamsString +=
222223 settingToQueryParam ( settings ?. paging . sortBy , 'sort' ) +
223224 settingToQueryParam ( settings ?. paging . sortOrder , 'order' ) ;
224225 }
@@ -339,10 +340,10 @@ const executeApiFetch: ExecuteApiFetch = function (
339340
340341 // Ai Answers
341342 else if ( type === 'ai-answers' ) {
342- // TODO use apiHostname instead of hardcoded URL
343343 apiInstance
344344 . post ( `https://${ apiHostname } /v2/indices/${ sitekey } /conversations` , {
345- question : settings ?. keyword
345+ question : settings ?. keyword ,
346+ filter : settings ?. aiAnswersFilterObject
346347 } )
347348 . then ( function ( response : AxiosResponse < ConversationsApiResponse > ) {
348349 if ( response . data . response ) {
0 commit comments