diff --git a/src/types/browse.d.ts b/src/types/browse.d.ts index f5063c28..fd92b478 100644 --- a/src/types/browse.d.ts +++ b/src/types/browse.d.ts @@ -13,6 +13,7 @@ import { ResultSources, SortOption, VariationsMap, + VariationsMapResponse, } from '.'; import EventDispatcher from './event-dispatcher'; @@ -136,7 +137,7 @@ export interface BrowseResultData extends Record { is_slotted: false; labels: Record; variations?: Record[]; - variations_map?: Record | Record[]; + variations_map?: VariationsMapResponse; } export interface BrowseRequestType extends Record { diff --git a/src/types/index.d.ts b/src/types/index.d.ts index 998e7784..6ba5fc57 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -12,6 +12,10 @@ export * from './browse'; export * from './tracker'; export * from './event-dispatcher'; +type RequireAtLeastOne = + Pick> & + { [K in Keys]-?: Required> & Partial>> }[Keys]; + export interface NetworkParameters extends Record { timeout?: number; } @@ -215,6 +219,24 @@ export type FilterExpressionRange = { export type FilterExpressionRangeValue = ['-inf' | number, 'inf' | number]; +export interface VariationsMapSingleFilter { + field: string; + value: string | number | boolean; +} + +export interface VariationsMapRange { + field: string; + range: FilterExpressionRangeValue; +} + +export type FilterNode = VariationsMapSingleFilter | VariationsMapRange; + +export type FilterBy = RequireAtLeastOne<{ + and?: Array; + or?: Array; + not?: FilterNode | FilterBy; +}>; + export interface Item extends Record { value: string; is_slotted: boolean; @@ -223,6 +245,7 @@ export interface Item extends Record { data?: ItemData; strategy?: { id: string }; variations?: { data?: ItemData, value: string }[] + variations_map?: VariationsMapResponse; } export interface ItemData extends Record { @@ -244,16 +267,32 @@ export interface SearchSuggestion extends Item { } & ItemData; } +export type VariationsMapResponse = Array> | Record; + +export type Aggregation = 'first' | 'min' | 'max' | 'all' | 'count' | 'field_count' | 'value_count'; + +export interface VariationsMapBaseValue { + aggregation: Aggregation; + field: string; +} + +export interface VariationsMapValueCount extends VariationsMapBaseValue { + aggregation: 'value_count'; + value: boolean | number | string; +} + +export interface VariationsMapStandardValue extends VariationsMapBaseValue { + aggregation: Exclude; +} + export interface VariationsMap { - group_by: Array<{ + group_by?: Array<{ name: string, field: string }>; + filter_by?: FilterBy; values: { - [key: string]: { - aggregation: 'first' | 'min' | 'max' | 'all', - field: string - }, + [key: string]: VariationsMapValueCount | VariationsMapStandardValue, }, dtype: 'array' | 'object' } diff --git a/src/types/tests/autocomplete.test-d.ts b/src/types/tests/autocomplete.test-d.ts index a3021394..52180a4d 100644 --- a/src/types/tests/autocomplete.test-d.ts +++ b/src/types/tests/autocomplete.test-d.ts @@ -65,6 +65,20 @@ expectAssignable({ field: 'data.VariationId', }, ], + filter_by: { + and: [ + { field: 'data.availability', value: 'in stock' }, + { + not: { field: 'data.discontinued', value: true }, + }, + { + or: [ + { field: 'data.price', range: [10, 100] }, + { field: 'data.backorderable', value: 'true' }, + ], + }, + ], + }, values: { availability: { aggregation: 'all', diff --git a/src/types/tests/browse.test-d.ts b/src/types/tests/browse.test-d.ts index 06abafc9..c3e6176f 100644 --- a/src/types/tests/browse.test-d.ts +++ b/src/types/tests/browse.test-d.ts @@ -123,6 +123,20 @@ expectAssignable({ field: 'data.VariationId', }, ], + filter_by: { + and: [ + { field: 'data.availability', value: 'in stock' }, + { + not: { field: 'data.discontinued', value: true }, + }, + { + or: [ + { field: 'data.price', range: [10, 100] }, + { field: 'data.backorderable', value: 'true' }, + ], + }, + ], + }, values: { availability: { aggregation: 'all', diff --git a/src/types/tests/search.test-d.ts b/src/types/tests/search.test-d.ts index 1bd91f49..1dfe49c6 100644 --- a/src/types/tests/search.test-d.ts +++ b/src/types/tests/search.test-d.ts @@ -132,6 +132,20 @@ expectAssignable({ field: 'data.VariationId', }, ], + filter_by: { + and: [ + { field: 'data.availability', value: 'in stock' }, + { + not: { field: 'data.discontinued', value: true }, + }, + { + or: [ + { field: 'data.price', range: [10, 100] }, + { field: 'data.backorderable', value: 'true' }, + ], + }, + ], + }, values: { availability: { aggregation: 'all',