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 */
6
52
7
53
/**
8
54
* Fetch search results of search suggestions from the Addsearch API
9
55
*/
10
- var executeApiFetch = function (
56
+ const executeApiFetch : ExecuteApiFetch = function (
11
57
apiHostname ,
12
58
sitekey ,
13
59
type ,
@@ -17,12 +63,14 @@ var executeApiFetch = function (
17
63
customFilterObject ,
18
64
recommendOptions
19
65
) {
20
- var settingToQueryParam = function ( setting , key ) {
66
+ /* eslint-disable @typescript-eslint/no-explicit-any */
67
+ const settingToQueryParam = function ( setting : any , key : string ) {
21
68
if ( setting || setting === false ) {
22
69
return '&' + key + '=' + setting ;
23
70
}
24
71
return '' ;
25
72
} ;
73
+ /* eslint-enable @typescript-eslint/no-explicit-any */
26
74
27
75
// Validate query type
28
76
if (
@@ -38,12 +86,12 @@ var executeApiFetch = function (
38
86
return ;
39
87
}
40
88
41
- var keyword = '' ;
42
- var queryParamsString = '' ;
89
+ let keyword = '' ;
90
+ let queryParamsString = '' ;
43
91
44
92
// 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 ;
47
95
48
96
// Search
49
97
if ( type === 'search' ) {
@@ -62,7 +110,7 @@ var executeApiFetch = function (
62
110
keyword = encodeURIComponent ( keyword ) ;
63
111
64
112
// Fuzzy
65
- var fuzzy = settings . fuzzy ;
113
+ let fuzzy = settings . fuzzy ;
66
114
if ( fuzzy === 'retry' ) {
67
115
// First call, non fuzzy
68
116
if ( fuzzyRetry !== true ) {
@@ -142,16 +190,16 @@ var executeApiFetch = function (
142
190
143
191
// Stats fields
144
192
if ( settings . statsFields ) {
145
- for ( var i = 0 ; i < settings . statsFields . length ; i ++ ) {
193
+ for ( let i = 0 ; i < settings . statsFields . length ; i ++ ) {
146
194
queryParamsString = queryParamsString + '&fieldStat=' + settings . statsFields [ i ] ;
147
195
}
148
196
}
149
197
150
198
// Personalization events
151
199
if ( settings . personalizationEvents && Array . isArray ( settings . personalizationEvents ) ) {
152
200
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 ] ;
155
203
queryParamsString =
156
204
queryParamsString + '&personalizationEvent=' + encodeURIComponent ( key + '=' + obj [ key ] ) ;
157
205
}
@@ -185,7 +233,7 @@ var executeApiFetch = function (
185
233
. post ( `https://api.addsearch.com/v2/indices/${ sitekey } /conversations` , {
186
234
question : settings . keyword
187
235
} )
188
- . then ( function ( response ) {
236
+ . then ( function ( response : AxiosResponse < ConversationsApiResponse > ) {
189
237
if ( response . data . response ) {
190
238
cb ( response . data . response ) ;
191
239
} else {
@@ -269,8 +317,8 @@ var executeApiFetch = function (
269
317
if ( type !== 'conversational-search' ) {
270
318
apiInstance
271
319
. get ( apiEndpoint )
272
- . then ( function ( response ) {
273
- var json = response . data ;
320
+ . then ( function ( response : AxiosResponse < GenericApiResponse > ) {
321
+ const json = response . data ;
274
322
275
323
// Search again with fuzzy=true if no hits
276
324
if (
@@ -285,7 +333,7 @@ var executeApiFetch = function (
285
333
else {
286
334
// Cap fuzzy results to one page as quality decreases quickly
287
335
if ( fuzzyRetry === true ) {
288
- var pageSize = settings . paging . pageSize ;
336
+ const pageSize = settings . paging . pageSize ;
289
337
if ( json . total_hits >= pageSize ) {
290
338
json . total_hits = pageSize ;
291
339
}
@@ -307,4 +355,5 @@ var executeApiFetch = function (
307
355
} ) ;
308
356
}
309
357
} ;
310
- module . exports = executeApiFetch ;
358
+
359
+ export default executeApiFetch ;
0 commit comments