1- import { ExternalClient , InstanceOptions , IOContext } from "@vtex/api" ;
2- import { parseState } from "../utils/searchState" ;
1+ import type { InstanceOptions , IOContext } from '@vtex/api'
2+ import { ExternalClient } from '@vtex/api'
3+
4+ import { parseState } from '../utils/searchState'
5+ import type { IIntelligentSearchClient } from './intsch/types'
36
47const isPathTraversal = ( str : string ) => str . indexOf ( '..' ) >= 0
8+
59interface CorrectionParams {
610 query : string
711}
@@ -44,59 +48,89 @@ const decodeQuery = (query: string) => {
4448 }
4549}
4650
47- export class IntelligentSearchApi extends ExternalClient {
51+ export class IntelligentSearchApi
52+ extends ExternalClient
53+ implements IIntelligentSearchClient
54+ {
4855 private locale : string | undefined
4956
5057 public constructor ( context : IOContext , options ?: InstanceOptions ) {
51- super ( `http://${ context . workspace } --${ context . account } .myvtex.com/_v/api/intelligent-search` , context , {
52- ...options ,
53- headers : {
54- ...options ?. headers ,
58+ super (
59+ `http://${ context . workspace } --${ context . account } .myvtex.com/_v/api/intelligent-search` ,
60+ context ,
61+ {
62+ ...options ,
63+ headers : {
64+ ...options ?. headers ,
65+ } ,
5566 }
56- } )
67+ )
5768
5869 const { locale, tenant } = context
70+
5971 this . locale = locale ?? tenant ?. locale
6072 }
6173
6274 public async topSearches ( ) {
63- return this . http . get ( '/top_searches' , { params : {
64- locale : this . locale
65- } , metric : 'topSearches' } )
75+ return this . http . get ( '/top_searches' , {
76+ params : {
77+ locale : this . locale ,
78+ } ,
79+ metric : 'topSearches' ,
80+ } )
6681 }
6782
6883 public async correction ( params : CorrectionParams ) {
69- return this . http . get ( '/correction_search' , { params : { ...params , locale : this . locale } , metric : 'correction' } )
84+ return this . http . get ( '/correction_search' , {
85+ params : { ...params , locale : this . locale } ,
86+ metric : 'correction' ,
87+ } )
7088 }
7189
7290 public async searchSuggestions ( params : SearchSuggestionsParams ) {
73- return this . http . get ( '/search_suggestions' , { params : { ...params , locale : this . locale } , metric : 'searchSuggestions' } )
91+ return this . http . get ( '/search_suggestions' , {
92+ params : { ...params , locale : this . locale } ,
93+ metric : 'searchSuggestions' ,
94+ } )
7495 }
7596
76- public async autocompleteSearchSuggestions ( params : AutocompleteSearchSuggestionsParams ) {
77- return this . http . get ( '/autocomplete_suggestions' , { params : { ...params , locale : this . locale } , metric : 'autocompleteSearchSuggestions' } )
97+ public async fetchAutocompleteSuggestions (
98+ params : AutocompleteSearchSuggestionsParams
99+ ) {
100+ return this . http . get ( '/autocomplete_suggestions' , {
101+ params : { ...params , locale : this . locale } ,
102+ metric : 'autocompleteSearchSuggestions' ,
103+ } )
78104 }
79105
80106 public async banners ( params : BannersArgs , path : string ) {
81107 if ( isPathTraversal ( path ) ) {
82- throw new Error ( " Malformed URL" )
108+ throw new Error ( ' Malformed URL' )
83109 }
84110
85- return this . http . get ( `/banners/${ path } ` , { params : { ...params , query : params . query , locale : this . locale } , metric : 'banners' } )
111+ return this . http . get ( `/banners/${ path } ` , {
112+ params : { ...params , query : params . query , locale : this . locale } ,
113+ metric : 'banners' ,
114+ } )
86115 }
87116
88- public async facets ( params : FacetsArgs , path : string , shippingHeader ?: string [ ] ) {
117+ public async facets (
118+ params : FacetsArgs ,
119+ path : string ,
120+ shippingHeader ?: string [ ]
121+ ) {
89122 if ( isPathTraversal ( path ) ) {
90- throw new Error ( " Malformed URL" )
123+ throw new Error ( ' Malformed URL' )
91124 }
92125
93- const { query, leap, searchState} = params
126+ const { query, leap, searchState } = params
94127
95128 return this . http . get ( `/facets/${ path } ` , {
96129 params : {
97130 ...params ,
98131 query : query && decodeQuery ( query ) ,
99132 locale : this . locale ,
133+ // eslint-disable-next-line @typescript-eslint/camelcase
100134 bgy_leap : leap ? true : undefined ,
101135 ...parseState ( searchState ) ,
102136 } ,
@@ -107,16 +141,22 @@ export class IntelligentSearchApi extends ExternalClient {
107141 } )
108142 }
109143
110- public async productSearch ( params : SearchResultArgs , path : string , shippingHeader ?: string [ ] ) {
111- const { query, leap, searchState} = params
144+ public async productSearch (
145+ params : SearchResultArgs ,
146+ path : string ,
147+ shippingHeader ?: string [ ]
148+ ) {
149+ const { query, leap, searchState } = params
150+
112151 if ( isPathTraversal ( path ) ) {
113- throw new Error ( " Malformed URL" )
152+ throw new Error ( ' Malformed URL' )
114153 }
115154
116155 return this . http . get ( `/product_search/${ path } ` , {
117156 params : {
118157 query : query && decodeQuery ( query ) ,
119158 locale : this . locale ,
159+ // eslint-disable-next-line @typescript-eslint/camelcase
120160 bgy_leap : leap ? true : undefined ,
121161 ...parseState ( searchState ) ,
122162 ...params ,
@@ -128,16 +168,22 @@ export class IntelligentSearchApi extends ExternalClient {
128168 } )
129169 }
130170
131- public async sponsoredProducts ( params : SearchResultArgs , path : string , shippingHeader ?: string [ ] ) {
132- const { query, leap, searchState} = params
171+ public async sponsoredProducts (
172+ params : SearchResultArgs ,
173+ path : string ,
174+ shippingHeader ?: string [ ]
175+ ) {
176+ const { query, leap, searchState } = params
177+
133178 if ( isPathTraversal ( path ) ) {
134- throw new Error ( " Malformed URL" )
179+ throw new Error ( ' Malformed URL' )
135180 }
136181
137182 return this . http . get ( `/sponsored_products/${ path } ` , {
138183 params : {
139184 query : query && decodeQuery ( query ) ,
140185 locale : this . locale ,
186+ // eslint-disable-next-line @typescript-eslint/camelcase
141187 bgy_leap : leap ? true : undefined ,
142188 ...parseState ( searchState ) ,
143189 ...params ,
0 commit comments