Skip to content

Commit e47612a

Browse files
niizomesezen
andauthored
[CDX-390]: update VarioationsMap type (#440)
* [CDX-390]: update VarioationsMap type * [CDX-390]: add discriminated union for VariationValue types * [CDX-390]: update FilterNode to union type * Address comment * Lint * Update types * Update types * Address comments --------- Co-authored-by: Enes Kutay SEZEN <eneskutaysezen@gmail.com>
1 parent 0426731 commit e47612a

5 files changed

Lines changed: 88 additions & 6 deletions

File tree

src/types/browse.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
ResultSources,
1414
SortOption,
1515
VariationsMap,
16+
VariationsMapResponse,
1617
} from '.';
1718
import EventDispatcher from './event-dispatcher';
1819

@@ -136,7 +137,7 @@ export interface BrowseResultData extends Record<string, any> {
136137
is_slotted: false;
137138
labels: Record<string, any>;
138139
variations?: Record<string, any>[];
139-
variations_map?: Record<string, any> | Record<string, any>[];
140+
variations_map?: VariationsMapResponse;
140141
}
141142

142143
export interface BrowseRequestType extends Record<string, any> {

src/types/index.d.ts

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export * from './browse';
1212
export * from './tracker';
1313
export * from './event-dispatcher';
1414

15+
type RequireAtLeastOne<T, Keys extends keyof T = keyof T> =
16+
Pick<T, Exclude<keyof T, Keys>> &
17+
{ [K in Keys]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<Keys, K>>> }[Keys];
18+
1519
export interface NetworkParameters extends Record<string, any> {
1620
timeout?: number;
1721
}
@@ -215,6 +219,24 @@ export type FilterExpressionRange = {
215219

216220
export type FilterExpressionRangeValue = ['-inf' | number, 'inf' | number];
217221

222+
export interface VariationsMapSingleFilter {
223+
field: string;
224+
value: string | number | boolean;
225+
}
226+
227+
export interface VariationsMapRange {
228+
field: string;
229+
range: FilterExpressionRangeValue;
230+
}
231+
232+
export type FilterNode = VariationsMapSingleFilter | VariationsMapRange;
233+
234+
export type FilterBy = RequireAtLeastOne<{
235+
and?: Array<FilterNode | FilterBy>;
236+
or?: Array<FilterNode | FilterBy>;
237+
not?: FilterNode | FilterBy;
238+
}>;
239+
218240
export interface Item extends Record<string, any> {
219241
value: string;
220242
is_slotted: boolean;
@@ -223,6 +245,7 @@ export interface Item extends Record<string, any> {
223245
data?: ItemData;
224246
strategy?: { id: string };
225247
variations?: { data?: ItemData, value: string }[]
248+
variations_map?: VariationsMapResponse;
226249
}
227250

228251
export interface ItemData extends Record<string, any> {
@@ -244,16 +267,32 @@ export interface SearchSuggestion extends Item {
244267
} & ItemData;
245268
}
246269

270+
export type VariationsMapResponse = Array<Record<string, unknown>> | Record<string, unknown>;
271+
272+
export type Aggregation = 'first' | 'min' | 'max' | 'all' | 'count' | 'field_count' | 'value_count';
273+
274+
export interface VariationsMapBaseValue {
275+
aggregation: Aggregation;
276+
field: string;
277+
}
278+
279+
export interface VariationsMapValueCount extends VariationsMapBaseValue {
280+
aggregation: 'value_count';
281+
value: boolean | number | string;
282+
}
283+
284+
export interface VariationsMapStandardValue extends VariationsMapBaseValue {
285+
aggregation: Exclude<Aggregation, 'value_count'>;
286+
}
287+
247288
export interface VariationsMap {
248-
group_by: Array<{
289+
group_by?: Array<{
249290
name: string,
250291
field: string
251292
}>;
293+
filter_by?: FilterBy;
252294
values: {
253-
[key: string]: {
254-
aggregation: 'first' | 'min' | 'max' | 'all',
255-
field: string
256-
},
295+
[key: string]: VariationsMapValueCount | VariationsMapStandardValue,
257296
},
258297
dtype: 'array' | 'object'
259298
}

src/types/tests/autocomplete.test-d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,20 @@ expectAssignable<AutocompleteResponse>({
6565
field: 'data.VariationId',
6666
},
6767
],
68+
filter_by: {
69+
and: [
70+
{ field: 'data.availability', value: 'in stock' },
71+
{
72+
not: { field: 'data.discontinued', value: true },
73+
},
74+
{
75+
or: [
76+
{ field: 'data.price', range: [10, 100] },
77+
{ field: 'data.backorderable', value: 'true' },
78+
],
79+
},
80+
],
81+
},
6882
values: {
6983
availability: {
7084
aggregation: 'all',

src/types/tests/browse.test-d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,20 @@ expectAssignable<GetBrowseResultsResponse>({
123123
field: 'data.VariationId',
124124
},
125125
],
126+
filter_by: {
127+
and: [
128+
{ field: 'data.availability', value: 'in stock' },
129+
{
130+
not: { field: 'data.discontinued', value: true },
131+
},
132+
{
133+
or: [
134+
{ field: 'data.price', range: [10, 100] },
135+
{ field: 'data.backorderable', value: 'true' },
136+
],
137+
},
138+
],
139+
},
126140
values: {
127141
availability: {
128142
aggregation: 'all',

src/types/tests/search.test-d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,20 @@ expectAssignable<SearchResponse>({
132132
field: 'data.VariationId',
133133
},
134134
],
135+
filter_by: {
136+
and: [
137+
{ field: 'data.availability', value: 'in stock' },
138+
{
139+
not: { field: 'data.discontinued', value: true },
140+
},
141+
{
142+
or: [
143+
{ field: 'data.price', range: [10, 100] },
144+
{ field: 'data.backorderable', value: 'true' },
145+
],
146+
},
147+
],
148+
},
135149
values: {
136150
availability: {
137151
aggregation: 'all',

0 commit comments

Comments
 (0)