Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/types/browse.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
ResultSources,
SortOption,
VariationsMap,
VariationsMapResponse,
} from '.';
import EventDispatcher from './event-dispatcher';

Expand Down Expand Up @@ -136,7 +137,7 @@ export interface BrowseResultData extends Record<string, any> {
is_slotted: false;
labels: Record<string, any>;
variations?: Record<string, any>[];
variations_map?: Record<string, any> | Record<string, any>[];
variations_map?: VariationsMapResponse;
}

export interface BrowseRequestType extends Record<string, any> {
Expand Down
49 changes: 44 additions & 5 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export * from './browse';
export * from './tracker';
export * from './event-dispatcher';

type RequireAtLeastOne<T, Keys extends keyof T = keyof T> =
Pick<T, Exclude<keyof T, Keys>> &
{ [K in Keys]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<Keys, K>>> }[Keys];

export interface NetworkParameters extends Record<string, any> {
timeout?: number;
}
Expand Down Expand Up @@ -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<FilterNode | FilterBy>;
or?: Array<FilterNode | FilterBy>;
not?: FilterNode | FilterBy;
}>;

export interface Item extends Record<string, any> {
value: string;
is_slotted: boolean;
Expand All @@ -223,6 +245,7 @@ export interface Item extends Record<string, any> {
data?: ItemData;
strategy?: { id: string };
variations?: { data?: ItemData, value: string }[]
variations_map?: VariationsMapResponse;
}

export interface ItemData extends Record<string, any> {
Expand All @@ -244,16 +267,32 @@ export interface SearchSuggestion extends Item {
} & ItemData;
}

export type VariationsMapResponse = Array<Record<string, unknown>> | Record<string, unknown>;

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<Aggregation, 'value_count'>;
}

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'
}
Expand Down
14 changes: 14 additions & 0 deletions src/types/tests/autocomplete.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ expectAssignable<AutocompleteResponse>({
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',
Expand Down
14 changes: 14 additions & 0 deletions src/types/tests/browse.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ expectAssignable<GetBrowseResultsResponse>({
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',
Expand Down
14 changes: 14 additions & 0 deletions src/types/tests/search.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,20 @@ expectAssignable<SearchResponse>({
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',
Expand Down
Loading