@frontic/nuxt 0.2.1 → 0.3.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.js +18 -1
- package/dist/runtime/types/index.d.ts +1 -1
- package/dist/runtime/types/listing.d.ts +44 -0
- package/dist/runtime/types/search.d.ts +22 -2
- package/dist/runtime/utils/state-helpers.d.ts +27 -1
- package/dist/runtime/utils/state-helpers.js +19 -0
- package/package.json +1 -1
|
@@ -3,7 +3,16 @@ import { computed, watch, ref } from "vue";
|
|
|
3
3
|
import { useQuery, useMutation, useQueryCache } from "@pinia/colada";
|
|
4
4
|
import { useDebounceFn } from "@vueuse/core";
|
|
5
5
|
import { makeLogicalFilter, manageFilter, removeFilter } from "../utils/filter-helpers.js";
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
makeFilterState,
|
|
8
|
+
processAvailableFilters,
|
|
9
|
+
processAvailableSorts,
|
|
10
|
+
processAvailableAggregations,
|
|
11
|
+
applyFilterConfig,
|
|
12
|
+
countActiveFilters,
|
|
13
|
+
parseSortString,
|
|
14
|
+
isSortAllowed
|
|
15
|
+
} from "../utils/state-helpers.js";
|
|
7
16
|
import { fetchListingQuery } from "./listings.js";
|
|
8
17
|
import { useFronticClient } from "./useFronticClient.js";
|
|
9
18
|
export function useFronticSearch(name, params, options) {
|
|
@@ -19,12 +28,14 @@ export function useFronticSearch(name, params, options) {
|
|
|
19
28
|
const orFilter = options?.orFilter ?? [];
|
|
20
29
|
const filter = options?.filter;
|
|
21
30
|
const sorting = options?.sorting;
|
|
31
|
+
const aggregation = options?.aggregation;
|
|
22
32
|
const disableSearch = options?.disableSearch ?? false;
|
|
23
33
|
const disableFilter = options?.disableFilter ?? false;
|
|
24
34
|
const disableSorting = options?.disableSorting ?? false;
|
|
25
35
|
const staleTime = options?.staleTime ?? 1e3 * 60 * 5;
|
|
26
36
|
const filterConfig = filter || {};
|
|
27
37
|
const sortConfig = sorting || {};
|
|
38
|
+
const aggregationConfig = aggregation || {};
|
|
28
39
|
const searchTerm = useState(`${cacheKey}-searchTerm`, () => "");
|
|
29
40
|
const _orFilterList = useState(`${cacheKey}-orFilterList`, () => []);
|
|
30
41
|
const _andFilterList = useState(`${cacheKey}-andFilterList`, () => []);
|
|
@@ -43,11 +54,13 @@ export function useFronticSearch(name, params, options) {
|
|
|
43
54
|
available: {
|
|
44
55
|
filter: [],
|
|
45
56
|
sorting: [],
|
|
57
|
+
aggregation: [],
|
|
46
58
|
nextPage: false,
|
|
47
59
|
prevPage: false,
|
|
48
60
|
count: {
|
|
49
61
|
filter: 0,
|
|
50
62
|
sorting: 0,
|
|
63
|
+
aggregation: 0,
|
|
51
64
|
result: 0,
|
|
52
65
|
page: 0
|
|
53
66
|
}
|
|
@@ -117,11 +130,13 @@ export function useFronticSearch(name, params, options) {
|
|
|
117
130
|
);
|
|
118
131
|
function updateState(response) {
|
|
119
132
|
const rawFilters = response.filter;
|
|
133
|
+
const rawAggregation = response.aggregation;
|
|
120
134
|
const pageData = response.page;
|
|
121
135
|
let activeFilters = makeFilterState(rawFilters);
|
|
122
136
|
activeFilters = applyFilterConfig(activeFilters, filterConfig);
|
|
123
137
|
const availableFilters = processAvailableFilters(rawFilters, filterConfig);
|
|
124
138
|
const availableSorts = processAvailableSorts(sortConfig);
|
|
139
|
+
const availableAggregations = processAvailableAggregations(rawAggregation, aggregationConfig);
|
|
125
140
|
const activeFilterCount = countActiveFilters(activeFilters);
|
|
126
141
|
searchState.value = {
|
|
127
142
|
active: {
|
|
@@ -137,11 +152,13 @@ export function useFronticSearch(name, params, options) {
|
|
|
137
152
|
available: {
|
|
138
153
|
filter: availableFilters,
|
|
139
154
|
sorting: availableSorts,
|
|
155
|
+
aggregation: availableAggregations,
|
|
140
156
|
nextPage: pageData?.next !== void 0,
|
|
141
157
|
prevPage: pageData?.prev !== void 0,
|
|
142
158
|
count: {
|
|
143
159
|
filter: availableFilters.length,
|
|
144
160
|
sorting: availableSorts.length,
|
|
161
|
+
aggregation: availableAggregations.length,
|
|
145
162
|
result: response.total ?? 0,
|
|
146
163
|
page: pageData?.total ?? 1
|
|
147
164
|
}
|
|
@@ -4,7 +4,7 @@
|
|
|
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, } from './listing.js';
|
|
7
|
+
export type { InclusionMode, FilterField, SortField, SortString, ListingFieldConfig, FilterOption, UiFilter, UiSort, UiAggregation, AggregationTotal, } from './listing.js';
|
|
8
8
|
export type { SearchState, UseFronticSearchOptions, UseSearchReturn } from './search.js';
|
|
9
9
|
export type { UseFronticClientOptions } from '../composables/useFronticClient.js';
|
|
10
10
|
export type { UseFronticBlockOptions } from '../composables/useFronticBlock.js';
|
|
@@ -123,6 +123,50 @@ export interface UiFilter<TFilters = Record<string, unknown>> {
|
|
|
123
123
|
/** Available filter options with counts and selection state */
|
|
124
124
|
options: FilterOption[];
|
|
125
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* Statistical aggregation data for a single field from the Fetch API response
|
|
128
|
+
* Contains min/max/avg/sum/count/total values useful for range UI components (e.g., price sliders)
|
|
129
|
+
*/
|
|
130
|
+
export interface AggregationTotal {
|
|
131
|
+
/** Maximum value for this field */
|
|
132
|
+
max?: number;
|
|
133
|
+
/** Minimum value for this field */
|
|
134
|
+
min?: number;
|
|
135
|
+
/** Average value for this field */
|
|
136
|
+
avg?: number;
|
|
137
|
+
/** Sum of all values for this field */
|
|
138
|
+
sum?: number;
|
|
139
|
+
/** Number of items with this field */
|
|
140
|
+
count?: number;
|
|
141
|
+
/** Total number of possible values for this field */
|
|
142
|
+
total: number;
|
|
143
|
+
}
|
|
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
|
+
}
|
|
126
170
|
/**
|
|
127
171
|
* Processed sort option ready for UI consumption
|
|
128
172
|
* @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 } from './listing.js';
|
|
11
|
-
export type { FronticRequestOptions, ListingFieldConfig, FilterOption, UiFilter, UiSort, InclusionMode } from './listing.js';
|
|
10
|
+
import type { FronticRequestOptions, ListingFieldConfig, UiFilter, UiSort, UiAggregation } from './listing.js';
|
|
11
|
+
export type { FronticRequestOptions, ListingFieldConfig, FilterOption, UiFilter, UiSort, UiAggregation, AggregationTotal, InclusionMode } from './listing.js';
|
|
12
12
|
export type { Listings, ListingQueryFilters, ListingQuerySorts, Responses };
|
|
13
13
|
/**
|
|
14
14
|
* Search state exposed to consumers
|
|
@@ -45,6 +45,8 @@ export interface SearchState<TFilters = Record<string, unknown>, TSorts = Record
|
|
|
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[];
|
|
48
50
|
/** Whether next page is available */
|
|
49
51
|
nextPage: boolean;
|
|
50
52
|
/** Whether previous page is available */
|
|
@@ -55,6 +57,8 @@ export interface SearchState<TFilters = Record<string, unknown>, TSorts = Record
|
|
|
55
57
|
filter: number;
|
|
56
58
|
/** Number of sort options available */
|
|
57
59
|
sorting: number;
|
|
60
|
+
/** Number of aggregation fields available */
|
|
61
|
+
aggregation: number;
|
|
58
62
|
/** Total number of matching items */
|
|
59
63
|
result: number;
|
|
60
64
|
/** Total number of pages */
|
|
@@ -139,6 +143,22 @@ export interface UseFronticSearchOptions<TSearch extends keyof Listings = keyof
|
|
|
139
143
|
* }
|
|
140
144
|
*/
|
|
141
145
|
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;
|
|
142
162
|
/**
|
|
143
163
|
* Disable search functionality (default: false)
|
|
144
164
|
*/
|
|
@@ -3,13 +3,22 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Functions for processing and transforming listing state for UI consumption.
|
|
5
5
|
*/
|
|
6
|
-
import type { ListingFieldConfig, UiFilter, UiSort, FilterOption } from '../types/listing.js';
|
|
6
|
+
import type { ListingFieldConfig, UiFilter, UiSort, UiAggregation, AggregationTotal, FilterOption } from '../types/listing.js';
|
|
7
7
|
/**
|
|
8
8
|
* Frontstack filter response structure
|
|
9
9
|
*/
|
|
10
10
|
interface FrontstackFilterResponse {
|
|
11
11
|
[key: string]: FilterOption[] | undefined;
|
|
12
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* Frontstack aggregation response structure
|
|
15
|
+
* Matches the _aggregation schema from the Fetch API
|
|
16
|
+
*/
|
|
17
|
+
interface FrontstackAggregationResponse {
|
|
18
|
+
[key: string]: {
|
|
19
|
+
total: AggregationTotal;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
13
22
|
/**
|
|
14
23
|
* Extracts active (selected) filters from the API response
|
|
15
24
|
*
|
|
@@ -74,6 +83,23 @@ export declare function processAvailableFilters<TFilters = Record<string, unknow
|
|
|
74
83
|
* ```
|
|
75
84
|
*/
|
|
76
85
|
export declare function processAvailableSorts<TSorts = Record<string, unknown>>(config?: ListingFieldConfig): 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[];
|
|
77
103
|
/**
|
|
78
104
|
* Applies filter configuration to transform the filter state
|
|
79
105
|
* Handles include/exclude mode and label renaming
|
|
@@ -62,6 +62,25 @@ export function processAvailableSorts(config = {}) {
|
|
|
62
62
|
);
|
|
63
63
|
return sortByOrder(availableSorts, sortOrder, (s) => s.key);
|
|
64
64
|
}
|
|
65
|
+
export function processAvailableAggregations(rawAggregation, config = {}) {
|
|
66
|
+
if (!rawAggregation) return [];
|
|
67
|
+
const { select = [], mode = "include", label: labels = {}, sort: sortOrder } = config;
|
|
68
|
+
const availableAggregations = [];
|
|
69
|
+
for (const key of Object.keys(rawAggregation)) {
|
|
70
|
+
const isInSelect = select.includes(key);
|
|
71
|
+
const shouldInclude = select.length === 0 || (mode === "include" ? isInSelect : !isInSelect);
|
|
72
|
+
if (!shouldInclude) continue;
|
|
73
|
+
const aggregationData = rawAggregation[key]?.total;
|
|
74
|
+
if (!aggregationData) continue;
|
|
75
|
+
const aggregationLabel = labels[key] || key;
|
|
76
|
+
availableAggregations.push({
|
|
77
|
+
key,
|
|
78
|
+
label: aggregationLabel,
|
|
79
|
+
...aggregationData
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
return sortByOrder(availableAggregations, sortOrder, (a) => a.key);
|
|
83
|
+
}
|
|
65
84
|
export function applyFilterConfig(filters, config = {}) {
|
|
66
85
|
const { select = [], mode = "include", label: labels = {} } = config;
|
|
67
86
|
let result = filters;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frontic/nuxt",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.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",
|