Skip to content

Commit 61220d2

Browse files
committed
Add facets to the intsch client
1 parent 4d967f3 commit 61220d2

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

node/clients/intelligent-search-api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ interface AutocompleteSearchSuggestionsParams {
2929
query: string
3030
}
3131

32-
interface FacetsArgs {
32+
export type FacetsArgs = {
3333
query?: string
3434
page?: number
3535
count?: number

node/clients/intsch/index.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
CorrectionArgs,
99
CorrectionArgsV1,
1010
CorrectionResponse,
11+
FacetsResponse,
1112
FetchBannersArgs,
1213
FetchBannersArgsV1,
1314
FetchBannersResponse,
@@ -21,6 +22,7 @@ import type {
2122
TopSearchesResponse,
2223
} from './types'
2324
import type { SearchResultArgs } from '../../typings/Search'
25+
import type { FacetsArgs } from '../intelligent-search-api'
2426
import { decodeQuery, isPathTraversal } from '../intelligent-search-api'
2527
import { parseState } from '../../utils/searchState'
2628

@@ -163,4 +165,30 @@ export class Intsch extends JanusClient implements IIntelligentSearchClient {
163165
},
164166
})
165167
}
168+
169+
public facets(
170+
params: FacetsArgs,
171+
path: string,
172+
shippingHeader?: string[]
173+
): Promise<FacetsResponse> {
174+
if (isPathTraversal(path)) {
175+
throw new Error('Malformed URL')
176+
}
177+
178+
const { query, leap, searchState } = params
179+
180+
return this.http.get(`/api/intelligent-search/v0/facets/${path}`, {
181+
params: {
182+
...params,
183+
query: query && decodeQuery(query),
184+
locale: this.locale,
185+
bgy_leap: leap ? true : undefined,
186+
...parseState(searchState),
187+
},
188+
metric: 'facets-new',
189+
headers: {
190+
'x-vtex-shipping-options': shippingHeader ?? '',
191+
},
192+
})
193+
}
166194
}

node/clients/intsch/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { SearchResultArgs } from '../../typings/Search'
2+
import type { FacetsArgs } from '../intelligent-search-api'
23

34
export type AutocompleteSuggestionsArgs = {
45
query: string
@@ -97,6 +98,8 @@ export type ProductSearchResponse = {
9798
products: SearchProduct[]
9899
}
99100

101+
export type FacetsResponse = any
102+
100103
export interface IIntelligentSearchClient {
101104
fetchAutocompleteSuggestions(
102105
args: AutocompleteSuggestionsArgs
@@ -122,4 +125,9 @@ export interface IIntelligentSearchClient {
122125
path: string,
123126
shippingHeader?: string[]
124127
): Promise<ProductSearchResponse>
128+
facets(
129+
params: FacetsArgs,
130+
path: string,
131+
shippingHeader?: string[]
132+
): Promise<FacetsResponse>
125133
}

node/mocks/intsch.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import type {
66
CorrectionResponse,
77
FetchBannersResponse,
88
FetchProductResponse,
9+
ProductSearchResponse,
10+
FacetsResponse,
911
} from '../clients/intsch/types'
1012

1113
export class MockedIntschClient implements IIntelligentSearchClient {
@@ -91,6 +93,18 @@ export class MockedIntschClient implements IIntelligentSearchClient {
9193
} else {
9294
this.fetchBannersV1.mockResolvedValue(args?.fetchBannersV1 ?? null)
9395
}
96+
97+
if (args?.productSearch instanceof Error) {
98+
this.productSearch.mockRejectedValue(args.productSearch)
99+
} else {
100+
this.productSearch.mockResolvedValue(args?.productSearch ?? null)
101+
}
102+
103+
if (args?.facets instanceof Error) {
104+
this.facets.mockRejectedValue(args.facets)
105+
} else {
106+
this.facets.mockResolvedValue(args?.facets ?? null)
107+
}
94108
}
95109

96110
public fetchAutocompleteSuggestions = jest.fn()
@@ -104,6 +118,8 @@ export class MockedIntschClient implements IIntelligentSearchClient {
104118
public fetchSearchSuggestionsV1 = jest.fn()
105119
public fetchCorrectionV1 = jest.fn()
106120
public fetchBannersV1 = jest.fn()
121+
public productSearch = jest.fn()
122+
public facets = jest.fn()
107123
}
108124

109125
export type IntelligentSearchClientArgs = {
@@ -118,4 +134,6 @@ export type IntelligentSearchClientArgs = {
118134
fetchSearchSuggestionsV1?: SearchSuggestionsResponse | Error
119135
fetchCorrectionV1?: CorrectionResponse | Error
120136
fetchBannersV1?: FetchBannersResponse | Error
137+
productSearch?: ProductSearchResponse | Error
138+
facets?: FacetsResponse | Error
121139
}

0 commit comments

Comments
 (0)