@frontic/nuxt 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/runtime/composables/useFronticSearch.d.ts +5 -3
- package/dist/runtime/composables/useFronticSearch.js +24 -23
- package/dist/runtime/types/index.d.ts +2 -1
- package/dist/runtime/types/listing.d.ts +12 -26
- package/dist/runtime/types/search.d.ts +3 -23
- package/dist/runtime/utils/filter-helpers.d.ts +40 -0
- package/dist/runtime/utils/filter-helpers.js +26 -0
- package/dist/runtime/utils/state-helpers.d.ts +14 -26
- package/dist/runtime/utils/state-helpers.js +17 -29
- package/package.json +1 -1
|
@@ -96,14 +96,16 @@ export declare function useFronticSearch<TSearch extends keyof Listings, TFilter
|
|
|
96
96
|
resetSorting: () => Promise<void>;
|
|
97
97
|
/**
|
|
98
98
|
* Set filter values for a specific field (replaces existing values).
|
|
99
|
+
* For range filters, pass an array with a single { from, to } object.
|
|
99
100
|
* @param filterField - The field to filter on (e.g., 'properties.color')
|
|
100
|
-
* @param filterOptions - Array of values to filter by
|
|
101
|
+
* @param filterOptions - Array of values to filter by, or [{ from, to }] for range filters
|
|
101
102
|
*/
|
|
102
103
|
filterResult: <K extends keyof TFilters & string>(filterField: K, filterOptions: Array<TFilters[K]>) => Promise<void>;
|
|
103
104
|
/**
|
|
104
|
-
* Add a single filter option to a field (keeps existing values).
|
|
105
|
+
* Add a single filter option to a field (keeps existing values for equals filters).
|
|
106
|
+
* For range filters, pass a { from, to } object — replaces the existing range.
|
|
105
107
|
* @param filterField - The field to filter on
|
|
106
|
-
* @param filterOption - The value to add
|
|
108
|
+
* @param filterOption - The value to add, or { from, to } for range filters
|
|
107
109
|
*/
|
|
108
110
|
addFilter: <K extends keyof TFilters & string>(filterField: K, filterOption: TFilters[K]) => Promise<void>;
|
|
109
111
|
/**
|
|
@@ -2,17 +2,8 @@ import { useState } from "nuxt/app";
|
|
|
2
2
|
import { computed, watch, ref, toValue } from "vue";
|
|
3
3
|
import { useQuery, useInfiniteQuery, useQueryCache } from "@pinia/colada";
|
|
4
4
|
import { useDebounceFn } from "@vueuse/core";
|
|
5
|
-
import { makeLogicalFilter, manageFilter, removeFilter } from "../utils/filter-helpers.js";
|
|
6
|
-
import {
|
|
7
|
-
makeFilterState,
|
|
8
|
-
processAvailableFilters,
|
|
9
|
-
processAvailableSorts,
|
|
10
|
-
processAvailableAggregations,
|
|
11
|
-
applyFilterConfig,
|
|
12
|
-
countActiveFilters,
|
|
13
|
-
parseSortString,
|
|
14
|
-
isSortAllowed
|
|
15
|
-
} from "../utils/state-helpers.js";
|
|
5
|
+
import { makeLogicalFilter, manageFilter, removeFilter, isRangeFilterValue, manageRangeFilter, removeRangeFilter } from "../utils/filter-helpers.js";
|
|
6
|
+
import { makeFilterState, processAvailableFilters, processAvailableSorts, applyFilterConfig, countActiveFilters, parseSortString, isSortAllowed } from "../utils/state-helpers.js";
|
|
16
7
|
import { fetchListingQuery, LISTING_QUERY_KEYS } from "./listings.js";
|
|
17
8
|
import { useFronticClient } from "./useFronticClient.js";
|
|
18
9
|
export function useFronticSearch(name, params, options) {
|
|
@@ -28,7 +19,6 @@ export function useFronticSearch(name, params, options) {
|
|
|
28
19
|
const orFilter = options?.orFilter ?? [];
|
|
29
20
|
const filter = options?.filter;
|
|
30
21
|
const sorting = options?.sorting;
|
|
31
|
-
const aggregation = options?.aggregation;
|
|
32
22
|
const disableSearch = options?.disableSearch ?? false;
|
|
33
23
|
const disableFilter = options?.disableFilter ?? false;
|
|
34
24
|
const disableSorting = options?.disableSorting ?? false;
|
|
@@ -38,10 +28,10 @@ export function useFronticSearch(name, params, options) {
|
|
|
38
28
|
const initialPage = Math.max(1, Math.floor(options?.initialPage ?? 1));
|
|
39
29
|
const filterConfig = filter || {};
|
|
40
30
|
const sortConfig = sorting || {};
|
|
41
|
-
const aggregationConfig = aggregation || {};
|
|
42
31
|
const searchTerm = useState(`${cacheKey}-searchTerm`, () => "");
|
|
43
32
|
const _orFilterList = useState(`${cacheKey}-orFilterList`, () => []);
|
|
44
33
|
const _andFilterList = useState(`${cacheKey}-andFilterList`, () => []);
|
|
34
|
+
const _rangeFilterList = useState(`${cacheKey}-rangeFilterList`, () => []);
|
|
45
35
|
const _sortQuery = useState(`${cacheKey}-sortQuery`, () => void 0);
|
|
46
36
|
const searchState = useState(`${cacheKey}-searchState`, () => ({
|
|
47
37
|
active: {
|
|
@@ -57,13 +47,11 @@ export function useFronticSearch(name, params, options) {
|
|
|
57
47
|
available: {
|
|
58
48
|
filter: [],
|
|
59
49
|
sorting: [],
|
|
60
|
-
aggregation: [],
|
|
61
50
|
nextPage: false,
|
|
62
51
|
prevPage: false,
|
|
63
52
|
count: {
|
|
64
53
|
filter: 0,
|
|
65
54
|
sorting: 0,
|
|
66
|
-
aggregation: 0,
|
|
67
55
|
result: 0,
|
|
68
56
|
page: 0
|
|
69
57
|
}
|
|
@@ -72,7 +60,7 @@ export function useFronticSearch(name, params, options) {
|
|
|
72
60
|
const apiQuery = computed(() => ({
|
|
73
61
|
sort: !disableSorting ? _sortQuery.value : void 0,
|
|
74
62
|
search: !disableSearch && searchTerm.value && searchTerm.value.trim().length > searchTermThreshold ? searchTerm.value : void 0,
|
|
75
|
-
filter: !disableFilter ? [...makeLogicalFilter("or", _orFilterList.value), ...makeLogicalFilter("and", _andFilterList.value)] : void 0,
|
|
63
|
+
filter: !disableFilter ? [...makeLogicalFilter("or", _orFilterList.value), ...makeLogicalFilter("and", _andFilterList.value), ..._rangeFilterList.value] : void 0,
|
|
76
64
|
limit
|
|
77
65
|
}));
|
|
78
66
|
const queryConfig = {
|
|
@@ -202,10 +190,9 @@ export function useFronticSearch(name, params, options) {
|
|
|
202
190
|
const pageData = response.page;
|
|
203
191
|
let activeFilters = makeFilterState(rawFilters);
|
|
204
192
|
activeFilters = applyFilterConfig(activeFilters, filterConfig);
|
|
205
|
-
const availableFilters = processAvailableFilters(rawFilters, filterConfig);
|
|
193
|
+
const availableFilters = processAvailableFilters(rawFilters, rawAggregation, filterConfig);
|
|
206
194
|
const responseHasSort = "sort" in response;
|
|
207
195
|
const availableSorts = processAvailableSorts(sortConfig, responseHasSort);
|
|
208
|
-
const availableAggregations = processAvailableAggregations(rawAggregation, aggregationConfig);
|
|
209
196
|
const activeFilterCount = countActiveFilters(activeFilters);
|
|
210
197
|
searchState.value = {
|
|
211
198
|
active: {
|
|
@@ -221,13 +208,11 @@ export function useFronticSearch(name, params, options) {
|
|
|
221
208
|
available: {
|
|
222
209
|
filter: availableFilters,
|
|
223
210
|
sorting: availableSorts,
|
|
224
|
-
aggregation: availableAggregations,
|
|
225
211
|
nextPage: pageData?.next !== void 0,
|
|
226
212
|
prevPage: pageData?.prev !== void 0,
|
|
227
213
|
count: {
|
|
228
214
|
filter: availableFilters.length,
|
|
229
215
|
sorting: availableSorts.length,
|
|
230
|
-
aggregation: availableAggregations.length,
|
|
231
216
|
result: response.total ?? 0,
|
|
232
217
|
page: pageData?.total ?? 1
|
|
233
218
|
}
|
|
@@ -263,6 +248,11 @@ export function useFronticSearch(name, params, options) {
|
|
|
263
248
|
}
|
|
264
249
|
async function filterResult(filterField, filterOptions) {
|
|
265
250
|
if (disableFilter) return;
|
|
251
|
+
if (filterOptions.length > 0 && isRangeFilterValue(filterOptions[0])) {
|
|
252
|
+
_rangeFilterList.value = manageRangeFilter(_rangeFilterList.value, filterField, filterOptions[0]);
|
|
253
|
+
await refreshListing();
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
266
256
|
const shouldUseOr = orFilter.includes(filterField);
|
|
267
257
|
const targetList = shouldUseOr ? _orFilterList : _andFilterList;
|
|
268
258
|
targetList.value = manageFilter(targetList.value, filterField, filterOptions);
|
|
@@ -270,6 +260,11 @@ export function useFronticSearch(name, params, options) {
|
|
|
270
260
|
}
|
|
271
261
|
async function addFilterOption(filterField, filterOption) {
|
|
272
262
|
if (disableFilter) return;
|
|
263
|
+
if (isRangeFilterValue(filterOption)) {
|
|
264
|
+
_rangeFilterList.value = manageRangeFilter(_rangeFilterList.value, filterField, filterOption);
|
|
265
|
+
await refreshListing();
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
273
268
|
const shouldUseOr = orFilter.includes(filterField);
|
|
274
269
|
const targetList = shouldUseOr ? _orFilterList : _andFilterList;
|
|
275
270
|
const existingFilter = targetList.value.find((filter2) => filter2.field === filterField);
|
|
@@ -279,6 +274,7 @@ export function useFronticSearch(name, params, options) {
|
|
|
279
274
|
}
|
|
280
275
|
async function removeFilterOption(filterField, filterOption) {
|
|
281
276
|
if (disableFilter) return;
|
|
277
|
+
_rangeFilterList.value = removeRangeFilter(_rangeFilterList.value, filterField);
|
|
282
278
|
const shouldUseOr = orFilter.includes(filterField);
|
|
283
279
|
const targetList = shouldUseOr ? _orFilterList : _andFilterList;
|
|
284
280
|
targetList.value = removeFilter(targetList.value, filterField, filterOption);
|
|
@@ -289,7 +285,9 @@ export function useFronticSearch(name, params, options) {
|
|
|
289
285
|
if (!filterField) {
|
|
290
286
|
_orFilterList.value = [];
|
|
291
287
|
_andFilterList.value = [];
|
|
288
|
+
_rangeFilterList.value = [];
|
|
292
289
|
} else {
|
|
290
|
+
_rangeFilterList.value = removeRangeFilter(_rangeFilterList.value, filterField);
|
|
293
291
|
const shouldUseOr = orFilter.includes(filterField);
|
|
294
292
|
const targetList = shouldUseOr ? _orFilterList : _andFilterList;
|
|
295
293
|
targetList.value = removeFilter(targetList.value, filterField);
|
|
@@ -304,6 +302,7 @@ export function useFronticSearch(name, params, options) {
|
|
|
304
302
|
searchTerm.value = "";
|
|
305
303
|
_orFilterList.value = [];
|
|
306
304
|
_andFilterList.value = [];
|
|
305
|
+
_rangeFilterList.value = [];
|
|
307
306
|
_sortQuery.value = void 0;
|
|
308
307
|
await refreshListing();
|
|
309
308
|
}
|
|
@@ -373,14 +372,16 @@ export function useFronticSearch(name, params, options) {
|
|
|
373
372
|
resetSorting,
|
|
374
373
|
/**
|
|
375
374
|
* Set filter values for a specific field (replaces existing values).
|
|
375
|
+
* For range filters, pass an array with a single { from, to } object.
|
|
376
376
|
* @param filterField - The field to filter on (e.g., 'properties.color')
|
|
377
|
-
* @param filterOptions - Array of values to filter by
|
|
377
|
+
* @param filterOptions - Array of values to filter by, or [{ from, to }] for range filters
|
|
378
378
|
*/
|
|
379
379
|
filterResult,
|
|
380
380
|
/**
|
|
381
|
-
* Add a single filter option to a field (keeps existing values).
|
|
381
|
+
* Add a single filter option to a field (keeps existing values for equals filters).
|
|
382
|
+
* For range filters, pass a { from, to } object — replaces the existing range.
|
|
382
383
|
* @param filterField - The field to filter on
|
|
383
|
-
* @param filterOption - The value to add
|
|
384
|
+
* @param filterOption - The value to add, or { from, to } for range filters
|
|
384
385
|
*/
|
|
385
386
|
addFilter: addFilterOption,
|
|
386
387
|
/**
|
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
* Export all public types from this file.
|
|
5
5
|
*/
|
|
6
6
|
export type { FronticOptions, FronticComposable, FronticComposablesConfig } from '../../module.js';
|
|
7
|
-
export type { InclusionMode, FilterField, SortField, SortString, ListingFieldConfig, FilterOption, UiFilter, UiSort,
|
|
7
|
+
export type { InclusionMode, FilterField, SortField, SortString, ListingFieldConfig, FilterOption, UiFilter, UiSort, AggregationTotal, } from './listing.js';
|
|
8
|
+
export type { RangeFilterValue } from '../utils/filter-helpers.js';
|
|
8
9
|
export type { SearchState, UseFronticSearchOptions, UseSearchReturn } from './search.js';
|
|
9
10
|
export type { UseFronticClientOptions } from '../composables/useFronticClient.js';
|
|
10
11
|
export type { UseFronticBlockOptions } from '../composables/useFronticBlock.js';
|
|
@@ -99,6 +99,14 @@ export interface ListingFieldConfig<TFields = Record<string, unknown>> {
|
|
|
99
99
|
* sorting: { sort: ['default', 'price.amount:asc', 'price.amount:desc'] }
|
|
100
100
|
*/
|
|
101
101
|
sort?: Array<keyof TFields & string>;
|
|
102
|
+
/**
|
|
103
|
+
* Display type per field. Value is passed through as-is to UiFilter.type.
|
|
104
|
+
* Use any string that fits your UI rendering logic (e.g., 'range', 'boolean', 'color').
|
|
105
|
+
* Fields without a type config default to 'default'.
|
|
106
|
+
* @example
|
|
107
|
+
* type: { 'price.amount': 'range', 'properties.isNew': 'boolean' }
|
|
108
|
+
*/
|
|
109
|
+
type?: Partial<Record<keyof TFields & string, string>>;
|
|
102
110
|
}
|
|
103
111
|
/**
|
|
104
112
|
* Filter option from API response (matches Frontstack response structure)
|
|
@@ -122,6 +130,10 @@ export interface UiFilter<TFilters = Record<string, unknown>> {
|
|
|
122
130
|
label: string;
|
|
123
131
|
/** Available filter options with counts and selection state */
|
|
124
132
|
options: FilterOption[];
|
|
133
|
+
/** Display type for this filter. 'default' unless configured via type config. */
|
|
134
|
+
type: string;
|
|
135
|
+
/** Additional data for this field if available (e.g., min/max/avg for range filters from aggregation response) */
|
|
136
|
+
data?: AggregationTotal;
|
|
125
137
|
}
|
|
126
138
|
/**
|
|
127
139
|
* Statistical aggregation data for a single field from the Fetch API response
|
|
@@ -141,32 +153,6 @@ export interface AggregationTotal {
|
|
|
141
153
|
/** Total number of possible values for this field */
|
|
142
154
|
total: number;
|
|
143
155
|
}
|
|
144
|
-
/**
|
|
145
|
-
* Processed aggregation ready for UI consumption
|
|
146
|
-
* @example
|
|
147
|
-
* ```ts
|
|
148
|
-
* // Price range aggregation
|
|
149
|
-
* { key: 'price.amount', label: 'Price', min: 9.99, max: 299.99, avg: 89.50, total: 150 }
|
|
150
|
-
* ```
|
|
151
|
-
*/
|
|
152
|
-
export interface UiAggregation {
|
|
153
|
-
/** Aggregation field key (e.g., 'price.amount') */
|
|
154
|
-
key: string;
|
|
155
|
-
/** Human-readable label for the aggregation */
|
|
156
|
-
label: string;
|
|
157
|
-
/** Maximum value for this field */
|
|
158
|
-
max?: number;
|
|
159
|
-
/** Minimum value for this field */
|
|
160
|
-
min?: number;
|
|
161
|
-
/** Average value for this field */
|
|
162
|
-
avg?: number;
|
|
163
|
-
/** Sum of all values for this field */
|
|
164
|
-
sum?: number;
|
|
165
|
-
/** Number of items with this field */
|
|
166
|
-
count?: number;
|
|
167
|
-
/** Total number of possible values for this field */
|
|
168
|
-
total: number;
|
|
169
|
-
}
|
|
170
156
|
/**
|
|
171
157
|
* Processed sort option ready for UI consumption
|
|
172
158
|
* @template TSorts - Sort fields type for this listing (provides autocomplete for key/value)
|
|
@@ -7,8 +7,8 @@
|
|
|
7
7
|
import type { Ref, ShallowRef } from 'vue';
|
|
8
8
|
import type { UseQueryReturn } from '@pinia/colada';
|
|
9
9
|
import type { Listings, ListingQueryFilters, ListingQuerySorts, Responses } from '@frontic/stack/generated-types';
|
|
10
|
-
import type { FronticRequestOptions, ListingFieldConfig, UiFilter, UiSort
|
|
11
|
-
export type { FronticRequestOptions, ListingFieldConfig, FilterOption, UiFilter, UiSort,
|
|
10
|
+
import type { FronticRequestOptions, ListingFieldConfig, UiFilter, UiSort } from './listing.js';
|
|
11
|
+
export type { FronticRequestOptions, ListingFieldConfig, FilterOption, UiFilter, UiSort, AggregationTotal, InclusionMode } from './listing.js';
|
|
12
12
|
export type { Listings, ListingQueryFilters, ListingQuerySorts, Responses };
|
|
13
13
|
/**
|
|
14
14
|
* Search state exposed to consumers
|
|
@@ -41,12 +41,10 @@ export interface SearchState<TFilters = Record<string, unknown>, TSorts = Record
|
|
|
41
41
|
};
|
|
42
42
|
/** Available options for UI controls */
|
|
43
43
|
available: {
|
|
44
|
-
/** Filter options ready for rendering (keys typed based on listing schema) */
|
|
44
|
+
/** Filter options ready for rendering, including aggregation data when available (keys typed based on listing schema) */
|
|
45
45
|
filter: UiFilter<TFilters>[];
|
|
46
46
|
/** Sort options ready for rendering (keys typed based on listing schema) */
|
|
47
47
|
sorting: UiSort<TSorts>[];
|
|
48
|
-
/** Aggregation data for range/statistical UI components (e.g., price sliders) */
|
|
49
|
-
aggregation: UiAggregation[];
|
|
50
48
|
/** Whether next page is available */
|
|
51
49
|
nextPage: boolean;
|
|
52
50
|
/** Whether previous page is available */
|
|
@@ -57,8 +55,6 @@ export interface SearchState<TFilters = Record<string, unknown>, TSorts = Record
|
|
|
57
55
|
filter: number;
|
|
58
56
|
/** Number of sort options available */
|
|
59
57
|
sorting: number;
|
|
60
|
-
/** Number of aggregation fields available */
|
|
61
|
-
aggregation: number;
|
|
62
58
|
/** Total number of matching items */
|
|
63
59
|
result: number;
|
|
64
60
|
/** Total number of pages */
|
|
@@ -143,22 +139,6 @@ export interface UseFronticSearchOptions<TSearch extends keyof Listings = keyof
|
|
|
143
139
|
* }
|
|
144
140
|
*/
|
|
145
141
|
sorting?: ListingFieldConfig<TSorts>;
|
|
146
|
-
/**
|
|
147
|
-
* Aggregation configuration using unified structure
|
|
148
|
-
* Controls which aggregation fields to expose and how to label them
|
|
149
|
-
* @example
|
|
150
|
-
* // Show only price aggregation with a custom label
|
|
151
|
-
* aggregation: {
|
|
152
|
-
* select: ['price.amount'],
|
|
153
|
-
* label: { 'price.amount': 'Price' }
|
|
154
|
-
* }
|
|
155
|
-
*
|
|
156
|
-
* // Show all aggregations with custom labels
|
|
157
|
-
* aggregation: {
|
|
158
|
-
* label: { 'price.amount': 'Price', 'weight': 'Weight' }
|
|
159
|
-
* }
|
|
160
|
-
*/
|
|
161
|
-
aggregation?: ListingFieldConfig;
|
|
162
142
|
/**
|
|
163
143
|
* Disable search functionality (default: false)
|
|
164
144
|
*/
|
|
@@ -90,6 +90,46 @@ export declare function manageFilter<T = Record<string, unknown>>(filterList: Eq
|
|
|
90
90
|
* ```
|
|
91
91
|
*/
|
|
92
92
|
export declare function removeFilter<T = Record<string, unknown>>(filterList: EqualsFilter<T>[], field: keyof T & string, option?: T[keyof T]): EqualsFilter<T>[];
|
|
93
|
+
/**
|
|
94
|
+
* Value for a range filter (from/to bounds)
|
|
95
|
+
* Pass to addFilter/filterResult to create a range filter instead of an equals filter.
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```ts
|
|
99
|
+
* addFilter('price.amount', { from: 20, to: 100 })
|
|
100
|
+
* addFilter('price.amount', { to: 100 }) // no lower bound
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
export interface RangeFilterValue {
|
|
104
|
+
from?: number;
|
|
105
|
+
to?: number;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Range filter for the API query
|
|
109
|
+
*/
|
|
110
|
+
export interface RangeFilter {
|
|
111
|
+
type: 'range';
|
|
112
|
+
field: string;
|
|
113
|
+
from?: number;
|
|
114
|
+
to?: number;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Type guard to check if a value is a RangeFilterValue (object with from/to)
|
|
118
|
+
*/
|
|
119
|
+
export declare function isRangeFilterValue(value: unknown): value is RangeFilterValue;
|
|
120
|
+
/**
|
|
121
|
+
* Creates a range filter for the API query
|
|
122
|
+
*/
|
|
123
|
+
export declare function makeRangeFilter(field: string, value: RangeFilterValue): RangeFilter;
|
|
124
|
+
/**
|
|
125
|
+
* Manages range filter operations (add/update) on a range filter list.
|
|
126
|
+
* If a range filter for the field exists, it replaces it; otherwise, adds a new one.
|
|
127
|
+
*/
|
|
128
|
+
export declare function manageRangeFilter(filterList: RangeFilter[], field: string, value: RangeFilterValue): RangeFilter[];
|
|
129
|
+
/**
|
|
130
|
+
* Removes a range filter for the specified field
|
|
131
|
+
*/
|
|
132
|
+
export declare function removeRangeFilter(filterList: RangeFilter[], field: string): RangeFilter[];
|
|
93
133
|
/**
|
|
94
134
|
* Checks if a filter exists for a specific field
|
|
95
135
|
*
|
|
@@ -36,6 +36,32 @@ export function removeFilter(filterList, field, option) {
|
|
|
36
36
|
}
|
|
37
37
|
return manageFilter(filterList, field, newValues);
|
|
38
38
|
}
|
|
39
|
+
export function isRangeFilterValue(value) {
|
|
40
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
|
|
41
|
+
const obj = value;
|
|
42
|
+
return ("from" in obj || "to" in obj) && (!("from" in obj) || typeof obj.from === "number") && (!("to" in obj) || typeof obj.to === "number");
|
|
43
|
+
}
|
|
44
|
+
export function makeRangeFilter(field, value) {
|
|
45
|
+
return {
|
|
46
|
+
type: "range",
|
|
47
|
+
field,
|
|
48
|
+
...value.from !== void 0 && { from: value.from },
|
|
49
|
+
...value.to !== void 0 && { to: value.to }
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
export function manageRangeFilter(filterList, field, value) {
|
|
53
|
+
const newFilter = makeRangeFilter(field, value);
|
|
54
|
+
const existingIndex = filterList.findIndex((filter) => filter.field === field);
|
|
55
|
+
if (existingIndex !== -1) {
|
|
56
|
+
const newList = [...filterList];
|
|
57
|
+
newList[existingIndex] = newFilter;
|
|
58
|
+
return newList;
|
|
59
|
+
}
|
|
60
|
+
return [...filterList, newFilter];
|
|
61
|
+
}
|
|
62
|
+
export function removeRangeFilter(filterList, field) {
|
|
63
|
+
return filterList.filter((filter) => filter.field !== field);
|
|
64
|
+
}
|
|
39
65
|
export function hasFilter(filterList, field) {
|
|
40
66
|
return filterList.some((filter) => filter.field === field);
|
|
41
67
|
}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Functions for processing and transforming listing state for UI consumption.
|
|
5
5
|
*/
|
|
6
|
-
import type { ListingFieldConfig, UiFilter, UiSort,
|
|
6
|
+
import type { ListingFieldConfig, UiFilter, UiSort, AggregationTotal, FilterOption } from '../types/listing.js';
|
|
7
7
|
/**
|
|
8
8
|
* Frontstack filter response structure
|
|
9
9
|
*/
|
|
@@ -39,25 +39,30 @@ interface FrontstackAggregationResponse {
|
|
|
39
39
|
*/
|
|
40
40
|
export declare function makeFilterState(filters: FrontstackFilterResponse | undefined): Record<string, FilterOption[]>;
|
|
41
41
|
/**
|
|
42
|
-
* Processes raw filter
|
|
42
|
+
* Processes raw filter and aggregation responses into a unified array of UI-ready filter objects.
|
|
43
|
+
* Aggregation data is merged onto matching filters or added as new entries.
|
|
43
44
|
*
|
|
44
45
|
* @template TFilters - Filter fields type for this listing (provides typed keys)
|
|
45
46
|
* @param rawFilters - Raw filter response from Frontstack API
|
|
46
|
-
* @param
|
|
47
|
+
* @param rawAggregation - Raw aggregation response from Frontstack API
|
|
48
|
+
* @param config - Filter configuration (select, mode, label, sort, type)
|
|
47
49
|
* @returns Array of UiFilter objects ready for rendering (with typed keys)
|
|
48
50
|
*
|
|
49
51
|
* @example
|
|
50
52
|
* ```ts
|
|
51
|
-
* const filters = processAvailableFilters<ProductSearchFilters>(response.filter, {
|
|
52
|
-
* select: ['properties.color'],
|
|
53
|
+
* const filters = processAvailableFilters<ProductSearchFilters>(response.filter, response.aggregation, {
|
|
54
|
+
* select: ['properties.color', 'price.amount'],
|
|
53
55
|
* mode: 'include',
|
|
54
|
-
* label: { 'properties.color': 'Color' },
|
|
55
|
-
*
|
|
56
|
+
* label: { 'properties.color': 'Color', 'price.amount': 'Price' },
|
|
57
|
+
* type: { 'price.amount': 'range' },
|
|
56
58
|
* })
|
|
57
|
-
* // [
|
|
59
|
+
* // [
|
|
60
|
+
* // { key: 'properties.color', label: 'Color', type: 'default', options: [...] },
|
|
61
|
+
* // { key: 'price.amount', label: 'Price', type: 'range', options: [], data: { min: 9.99, max: 299.99, ... } },
|
|
62
|
+
* // ]
|
|
58
63
|
* ```
|
|
59
64
|
*/
|
|
60
|
-
export declare function processAvailableFilters<TFilters = Record<string, unknown>>(rawFilters: FrontstackFilterResponse | undefined, config?: ListingFieldConfig): UiFilter<TFilters>[];
|
|
65
|
+
export declare function processAvailableFilters<TFilters = Record<string, unknown>>(rawFilters: FrontstackFilterResponse | undefined, rawAggregation: FrontstackAggregationResponse | undefined, config?: ListingFieldConfig): UiFilter<TFilters>[];
|
|
61
66
|
/**
|
|
62
67
|
* Processes sort configuration into UI-ready sort options
|
|
63
68
|
*
|
|
@@ -83,23 +88,6 @@ export declare function processAvailableFilters<TFilters = Record<string, unknow
|
|
|
83
88
|
* ```
|
|
84
89
|
*/
|
|
85
90
|
export declare function processAvailableSorts<TSorts = Record<string, unknown>>(config?: ListingFieldConfig, responseHasSort?: boolean): UiSort<TSorts>[];
|
|
86
|
-
/**
|
|
87
|
-
* Processes raw aggregation response into UI-ready aggregation objects
|
|
88
|
-
*
|
|
89
|
-
* @param rawAggregation - Raw aggregation response from Frontstack API
|
|
90
|
-
* @param config - Aggregation configuration (select, mode, label, sort)
|
|
91
|
-
* @returns Array of UiAggregation objects ready for rendering
|
|
92
|
-
*
|
|
93
|
-
* @example
|
|
94
|
-
* ```ts
|
|
95
|
-
* const aggregations = processAvailableAggregations(response.aggregation, {
|
|
96
|
-
* select: ['price.amount'],
|
|
97
|
-
* label: { 'price.amount': 'Price' }
|
|
98
|
-
* })
|
|
99
|
-
* // [{ key: 'price.amount', label: 'Price', min: 9.99, max: 299.99, ... }]
|
|
100
|
-
* ```
|
|
101
|
-
*/
|
|
102
|
-
export declare function processAvailableAggregations(rawAggregation: FrontstackAggregationResponse | undefined, config?: ListingFieldConfig): UiAggregation[];
|
|
103
91
|
/**
|
|
104
92
|
* Applies filter configuration to transform the filter state
|
|
105
93
|
* Handles include/exclude mode and label renaming
|
|
@@ -27,23 +27,30 @@ function sortByOrder(items, order, getKey) {
|
|
|
27
27
|
return 0;
|
|
28
28
|
});
|
|
29
29
|
}
|
|
30
|
-
export function processAvailableFilters(rawFilters, config = {}) {
|
|
31
|
-
if (!rawFilters) return [];
|
|
32
|
-
const { select = [], mode = "include", label: labels = {}, sort: sortOrder } = config;
|
|
30
|
+
export function processAvailableFilters(rawFilters, rawAggregation, config = {}) {
|
|
31
|
+
if (!rawFilters && !rawAggregation) return [];
|
|
32
|
+
const { select = [], mode = "include", label: labels = {}, sort: sortOrder, type: typeConfig = {} } = config;
|
|
33
33
|
const availableFilters = [];
|
|
34
|
-
const
|
|
35
|
-
for (const key of
|
|
34
|
+
const allKeys = /* @__PURE__ */ new Set([...Object.keys(rawFilters || {}), ...Object.keys(rawAggregation || {})]);
|
|
35
|
+
for (const key of allKeys) {
|
|
36
36
|
const isInSelect = select.includes(key);
|
|
37
37
|
const shouldInclude = select.length === 0 || (mode === "include" ? isInSelect : !isInSelect);
|
|
38
38
|
if (!shouldInclude) continue;
|
|
39
|
-
const filterOptions = rawFilters[key] || [];
|
|
40
|
-
|
|
39
|
+
const filterOptions = rawFilters?.[key] || [];
|
|
40
|
+
const aggregationData = rawAggregation?.[key]?.total;
|
|
41
|
+
if (filterOptions.length === 0 && !aggregationData) continue;
|
|
41
42
|
const filterLabel = labels[key] || key;
|
|
42
|
-
|
|
43
|
+
const filterType = typeConfig[key] || "default";
|
|
44
|
+
const uiFilter = {
|
|
43
45
|
key,
|
|
44
46
|
label: filterLabel,
|
|
45
|
-
options: filterOptions
|
|
46
|
-
|
|
47
|
+
options: filterOptions,
|
|
48
|
+
type: filterType
|
|
49
|
+
};
|
|
50
|
+
if (aggregationData) {
|
|
51
|
+
uiFilter.data = aggregationData;
|
|
52
|
+
}
|
|
53
|
+
availableFilters.push(uiFilter);
|
|
47
54
|
}
|
|
48
55
|
return sortByOrder(availableFilters, sortOrder, (f) => f.key);
|
|
49
56
|
}
|
|
@@ -63,25 +70,6 @@ export function processAvailableSorts(config = {}, responseHasSort = true) {
|
|
|
63
70
|
);
|
|
64
71
|
return sortByOrder(availableSorts, sortOrder, (s) => s.key);
|
|
65
72
|
}
|
|
66
|
-
export function processAvailableAggregations(rawAggregation, config = {}) {
|
|
67
|
-
if (!rawAggregation) return [];
|
|
68
|
-
const { select = [], mode = "include", label: labels = {}, sort: sortOrder } = config;
|
|
69
|
-
const availableAggregations = [];
|
|
70
|
-
for (const key of Object.keys(rawAggregation)) {
|
|
71
|
-
const isInSelect = select.includes(key);
|
|
72
|
-
const shouldInclude = select.length === 0 || (mode === "include" ? isInSelect : !isInSelect);
|
|
73
|
-
if (!shouldInclude) continue;
|
|
74
|
-
const aggregationData = rawAggregation[key]?.total;
|
|
75
|
-
if (!aggregationData) continue;
|
|
76
|
-
const aggregationLabel = labels[key] || key;
|
|
77
|
-
availableAggregations.push({
|
|
78
|
-
key,
|
|
79
|
-
label: aggregationLabel,
|
|
80
|
-
...aggregationData
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
return sortByOrder(availableAggregations, sortOrder, (a) => a.key);
|
|
84
|
-
}
|
|
85
73
|
export function applyFilterConfig(filters, config = {}) {
|
|
86
74
|
const { select = [], mode = "include", label: labels = {} } = config;
|
|
87
75
|
let result = filters;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frontic/nuxt",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Nuxt module for Frontstack headless commerce - zero config data fetching composables with smart caching, page routing, and context management",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Frontic",
|