1- 'use strict' ;
2- require ( 'es6-promise' ) . polyfill ( ) ;
3- const apiInstance = require ( './api' ) . apiInstance ;
4- const RESPONSE_BAD_REQUEST = require ( './api' ) . RESPONSE_BAD_REQUEST ;
5- const RESPONSE_SERVER_ERROR = require ( './api' ) . RESPONSE_SERVER_ERROR ;
1+ import 'es6-promise/auto' ;
2+ import { apiInstance , RESPONSE_BAD_REQUEST , RESPONSE_SERVER_ERROR } from './api' ;
3+ import { Settings } from './settings' ;
4+ import { AxiosResponse } from 'axios' ;
5+
6+ interface RecommendOptions {
7+ type : 'RELATED_ITEMS' | 'FREQUENTLY_BOUGHT_TOGETHER' ;
8+ itemId ?: string ;
9+ blockId ?: string ;
10+ configurationKey ?: string ;
11+ }
12+
13+ /* eslint-disable @typescript-eslint/no-explicit-any */
14+ export interface Callback {
15+ ( response : any ) : void ;
16+ }
17+ /* eslint-enable @typescript-eslint/no-explicit-any */
18+
19+ interface SourceDocuments {
20+ page : number ;
21+ hits : Document [ ] ;
22+ total_hits : number ;
23+ }
24+
25+ interface GenericApiResponse {
26+ total_hits ?: number ;
27+ }
28+
29+ interface ConversationsApiResponse {
30+ response : {
31+ conversation_id : string ;
32+ answer : string ;
33+ ids : string [ ] ;
34+ source_documents : SourceDocuments ;
35+ } ;
36+ errors : string [ ] ;
37+ status : number ;
38+ }
39+
40+ /* eslint-disable @typescript-eslint/no-explicit-any */
41+ export type ExecuteApiFetch = (
42+ apiHostname : string ,
43+ sitekey : string ,
44+ type : string ,
45+ settings : Settings ,
46+ cb : Callback ,
47+ fuzzyRetry ?: boolean ,
48+ customFilterObject ?: Record < string , any > ,
49+ recommendOptions ?: RecommendOptions
50+ ) => void ;
51+ /* eslint-enable @typescript-eslint/no-explicit-any */
652
753/**
854 * Fetch search results of search suggestions from the Addsearch API
955 */
10- var executeApiFetch = function (
56+ const executeApiFetch : ExecuteApiFetch = function (
1157 apiHostname ,
1258 sitekey ,
1359 type ,
@@ -17,12 +63,14 @@ var executeApiFetch = function (
1763 customFilterObject ,
1864 recommendOptions
1965) {
20- var settingToQueryParam = function ( setting , key ) {
66+ /* eslint-disable @typescript-eslint/no-explicit-any */
67+ const settingToQueryParam = function ( setting : any , key : string ) {
2168 if ( setting || setting === false ) {
2269 return '&' + key + '=' + setting ;
2370 }
2471 return '' ;
2572 } ;
73+ /* eslint-enable @typescript-eslint/no-explicit-any */
2674
2775 // Validate query type
2876 if (
@@ -38,12 +86,12 @@ var executeApiFetch = function (
3886 return ;
3987 }
4088
41- var keyword = '' ;
42- var queryParamsString = '' ;
89+ let keyword = '' ;
90+ let queryParamsString = '' ;
4391
4492 // API Path (eq. /search, /suggest, /autocomplete/document-field)
45- var apiEndpoint = null ;
46- var apiPath = null ;
93+ let apiEndpoint : string | null = null ;
94+ let apiPath = null ;
4795
4896 // Search
4997 if ( type === 'search' ) {
@@ -62,7 +110,7 @@ var executeApiFetch = function (
62110 keyword = encodeURIComponent ( keyword ) ;
63111
64112 // Fuzzy
65- var fuzzy = settings . fuzzy ;
113+ let fuzzy = settings . fuzzy ;
66114 if ( fuzzy === 'retry' ) {
67115 // First call, non fuzzy
68116 if ( fuzzyRetry !== true ) {
@@ -142,16 +190,16 @@ var executeApiFetch = function (
142190
143191 // Stats fields
144192 if ( settings . statsFields ) {
145- for ( var i = 0 ; i < settings . statsFields . length ; i ++ ) {
193+ for ( let i = 0 ; i < settings . statsFields . length ; i ++ ) {
146194 queryParamsString = queryParamsString + '&fieldStat=' + settings . statsFields [ i ] ;
147195 }
148196 }
149197
150198 // Personalization events
151199 if ( settings . personalizationEvents && Array . isArray ( settings . personalizationEvents ) ) {
152200 for ( let i = 0 ; i < settings . personalizationEvents . length ; i ++ ) {
153- var obj = settings . personalizationEvents [ i ] ;
154- var key = Object . keys ( obj ) ;
201+ const obj = settings . personalizationEvents [ i ] ;
202+ const key = Object . keys ( obj ) [ 0 ] ;
155203 queryParamsString =
156204 queryParamsString + '&personalizationEvent=' + encodeURIComponent ( key + '=' + obj [ key ] ) ;
157205 }
@@ -185,7 +233,7 @@ var executeApiFetch = function (
185233 . post ( `https://api.addsearch.com/v2/indices/${ sitekey } /conversations` , {
186234 question : settings . keyword
187235 } )
188- . then ( function ( response ) {
236+ . then ( function ( response : AxiosResponse < ConversationsApiResponse > ) {
189237 if ( response . data . response ) {
190238 cb ( response . data . response ) ;
191239 } else {
@@ -269,8 +317,8 @@ var executeApiFetch = function (
269317 if ( type !== 'conversational-search' ) {
270318 apiInstance
271319 . get ( apiEndpoint )
272- . then ( function ( response ) {
273- var json = response . data ;
320+ . then ( function ( response : AxiosResponse < GenericApiResponse > ) {
321+ const json = response . data ;
274322
275323 // Search again with fuzzy=true if no hits
276324 if (
@@ -285,7 +333,7 @@ var executeApiFetch = function (
285333 else {
286334 // Cap fuzzy results to one page as quality decreases quickly
287335 if ( fuzzyRetry === true ) {
288- var pageSize = settings . paging . pageSize ;
336+ const pageSize = settings . paging . pageSize ;
289337 if ( json . total_hits >= pageSize ) {
290338 json . total_hits = pageSize ;
291339 }
@@ -307,4 +355,5 @@ var executeApiFetch = function (
307355 } ) ;
308356 }
309357} ;
310- module . exports = executeApiFetch ;
358+
359+ export default executeApiFetch ;
0 commit comments