@frontic/nuxt 0.2.1 → 0.4.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/README.md +2 -1
- package/dist/runtime/composables/useFronticSearch.d.ts +17 -9
- package/dist/runtime/composables/useFronticSearch.js +149 -77
- 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 +61 -9
- package/dist/runtime/utils/state-helpers.d.ts +28 -2
- package/dist/runtime/utils/state-helpers.js +21 -1
- package/package.json +6 -3
package/README.md
CHANGED
|
@@ -6,9 +6,10 @@ Nuxt module for integrating with [Frontstack](https://frontstack.dev) headless c
|
|
|
6
6
|
|
|
7
7
|
- **Smart caching** via [Pinia Colada](https://pinia-colada.esm.dev/) - stale-while-revalidate, request deduplication, SSR hydration
|
|
8
8
|
- **Type-safe composables** - Full TypeScript support with generated types from your Frontstack schema
|
|
9
|
-
- **Search & filtering** - Ready-to-use search state management with filters, sorting, and
|
|
9
|
+
- **Search & filtering** - Ready-to-use search state management with filters, sorting, pagination, and infinite scroll
|
|
10
10
|
- **Page routing** - Dynamic page resolution with automatic redirects and 404 handling
|
|
11
11
|
- **Context management** - Locale and region switching with cookie persistence
|
|
12
|
+
- **Infinite scroll** - Bidirectional infinite loading with deep-link support via Pinia Colada's `useInfiniteQuery`
|
|
12
13
|
- **Built-in proxy** - CORS-free API requests through your Nuxt server
|
|
13
14
|
|
|
14
15
|
## Installation
|
|
@@ -57,10 +57,24 @@ export declare function useFronticSearch<TSearch extends keyof Listings, TFilter
|
|
|
57
57
|
*/
|
|
58
58
|
searchTerm: import("@vue/reactivity").Ref<string, string>;
|
|
59
59
|
/**
|
|
60
|
-
* Load the next page of results
|
|
61
|
-
*
|
|
60
|
+
* Load the next page of results.
|
|
61
|
+
* Standard mode: replaces current page with the next one.
|
|
62
|
+
* Infinite mode: appends items from the next page.
|
|
62
63
|
*/
|
|
63
|
-
loadNext: (
|
|
64
|
+
loadNext: () => Promise<unknown>;
|
|
65
|
+
/**
|
|
66
|
+
* Load the previous page of results.
|
|
67
|
+
* Standard mode: replaces current page with the previous one.
|
|
68
|
+
* Infinite mode: prepends items from the previous page.
|
|
69
|
+
*/
|
|
70
|
+
loadPrev: () => Promise<unknown>;
|
|
71
|
+
/**
|
|
72
|
+
* Jump to a specific page number.
|
|
73
|
+
* Standard mode: replaces current page with the requested one.
|
|
74
|
+
* Infinite mode: resets all loaded pages and restarts from the given page.
|
|
75
|
+
* @param page - The page number to load
|
|
76
|
+
*/
|
|
77
|
+
loadPage: (page: number) => Promise<unknown>;
|
|
64
78
|
/**
|
|
65
79
|
* Refresh the listing data (uses cache if valid).
|
|
66
80
|
* Call after external changes that should trigger a refresh.
|
|
@@ -71,12 +85,6 @@ export declare function useFronticSearch<TSearch extends keyof Listings, TFilter
|
|
|
71
85
|
* Use when you need guaranteed fresh data.
|
|
72
86
|
*/
|
|
73
87
|
refetch: () => Promise<void>;
|
|
74
|
-
/**
|
|
75
|
-
* Navigate to next or previous page.
|
|
76
|
-
* @param direction - 'next' or 'prev'
|
|
77
|
-
* @returns true if navigation was successful, false if no more pages
|
|
78
|
-
*/
|
|
79
|
-
paginate: (direction: "next" | "prev") => Promise<boolean>;
|
|
80
88
|
/**
|
|
81
89
|
* Sort results by a specific field. Call without arguments to reset to default.
|
|
82
90
|
* @param sortBy - Sort string in format 'field:asc' or 'field:desc', or omit to reset
|
|
@@ -1,10 +1,19 @@
|
|
|
1
1
|
import { useState } from "nuxt/app";
|
|
2
|
-
import { computed, watch, ref } from "vue";
|
|
3
|
-
import { useQuery,
|
|
2
|
+
import { computed, watch, ref, toValue } from "vue";
|
|
3
|
+
import { useQuery, useInfiniteQuery, useQueryCache } from "@pinia/colada";
|
|
4
4
|
import { useDebounceFn } from "@vueuse/core";
|
|
5
5
|
import { makeLogicalFilter, manageFilter, removeFilter } from "../utils/filter-helpers.js";
|
|
6
|
-
import {
|
|
7
|
-
|
|
6
|
+
import {
|
|
7
|
+
makeFilterState,
|
|
8
|
+
processAvailableFilters,
|
|
9
|
+
processAvailableSorts,
|
|
10
|
+
processAvailableAggregations,
|
|
11
|
+
applyFilterConfig,
|
|
12
|
+
countActiveFilters,
|
|
13
|
+
parseSortString,
|
|
14
|
+
isSortAllowed
|
|
15
|
+
} from "../utils/state-helpers.js";
|
|
16
|
+
import { fetchListingQuery, LISTING_QUERY_KEYS } from "./listings.js";
|
|
8
17
|
import { useFronticClient } from "./useFronticClient.js";
|
|
9
18
|
export function useFronticSearch(name, params, options) {
|
|
10
19
|
const queryCache = useQueryCache();
|
|
@@ -19,12 +28,17 @@ 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;
|
|
35
|
+
const limit = options?.limit;
|
|
25
36
|
const staleTime = options?.staleTime ?? 1e3 * 60 * 5;
|
|
37
|
+
const infinite = options?.infinite ?? false;
|
|
38
|
+
const initialPage = Math.max(1, Math.floor(options?.initialPage ?? 1));
|
|
26
39
|
const filterConfig = filter || {};
|
|
27
40
|
const sortConfig = sorting || {};
|
|
41
|
+
const aggregationConfig = aggregation || {};
|
|
28
42
|
const searchTerm = useState(`${cacheKey}-searchTerm`, () => "");
|
|
29
43
|
const _orFilterList = useState(`${cacheKey}-orFilterList`, () => []);
|
|
30
44
|
const _andFilterList = useState(`${cacheKey}-andFilterList`, () => []);
|
|
@@ -43,11 +57,13 @@ export function useFronticSearch(name, params, options) {
|
|
|
43
57
|
available: {
|
|
44
58
|
filter: [],
|
|
45
59
|
sorting: [],
|
|
60
|
+
aggregation: [],
|
|
46
61
|
nextPage: false,
|
|
47
62
|
prevPage: false,
|
|
48
63
|
count: {
|
|
49
64
|
filter: 0,
|
|
50
65
|
sorting: 0,
|
|
66
|
+
aggregation: 0,
|
|
51
67
|
result: 0,
|
|
52
68
|
page: 0
|
|
53
69
|
}
|
|
@@ -56,49 +72,114 @@ export function useFronticSearch(name, params, options) {
|
|
|
56
72
|
const apiQuery = computed(() => ({
|
|
57
73
|
sort: !disableSorting ? _sortQuery.value : void 0,
|
|
58
74
|
search: !disableSearch && searchTerm.value && searchTerm.value.trim().length > searchTermThreshold ? searchTerm.value : void 0,
|
|
59
|
-
filter: !disableFilter ? [...makeLogicalFilter("or", _orFilterList.value), ...makeLogicalFilter("and", _andFilterList.value)] : void 0
|
|
75
|
+
filter: !disableFilter ? [...makeLogicalFilter("or", _orFilterList.value), ...makeLogicalFilter("and", _andFilterList.value)] : void 0,
|
|
76
|
+
limit
|
|
60
77
|
}));
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
return await nextPageQuery.query();
|
|
89
|
-
},
|
|
90
|
-
onSuccess: (data2) => {
|
|
91
|
-
const newData = data2;
|
|
92
|
-
const newItems = newData?.items || [];
|
|
93
|
-
if (!newItems.length) return;
|
|
94
|
-
const cacheKeyValue = [cacheKey, JSON.stringify(apiQuery.value)];
|
|
95
|
-
const oldData = queryCache.getQueryData(cacheKeyValue);
|
|
96
|
-
queryCache.setQueryData(cacheKeyValue, {
|
|
97
|
-
...newData,
|
|
98
|
-
items: [...oldData?.items || [], ...newItems]
|
|
99
|
-
});
|
|
78
|
+
const queryConfig = {
|
|
79
|
+
name: listingName,
|
|
80
|
+
params,
|
|
81
|
+
query: apiQuery,
|
|
82
|
+
contextKey,
|
|
83
|
+
contextDomain,
|
|
84
|
+
requestUrl,
|
|
85
|
+
staleTime,
|
|
86
|
+
_client
|
|
87
|
+
};
|
|
88
|
+
let data;
|
|
89
|
+
let status;
|
|
90
|
+
let refresh;
|
|
91
|
+
let refetch;
|
|
92
|
+
let loadNextFn;
|
|
93
|
+
let loadPrevFn;
|
|
94
|
+
let loadPageFn;
|
|
95
|
+
async function fetchPage(pageNumber) {
|
|
96
|
+
const pageQuery = fetchListingQuery({
|
|
97
|
+
...queryConfig,
|
|
98
|
+
page: ref(pageNumber)
|
|
99
|
+
});
|
|
100
|
+
const response = await pageQuery.query();
|
|
101
|
+
if (response) {
|
|
102
|
+
const baseCacheKey = LISTING_QUERY_KEYS.detail(String(listingName), toValue(params), apiQuery.value);
|
|
103
|
+
queryCache.setQueryData(baseCacheKey, response);
|
|
104
|
+
updateState(response);
|
|
100
105
|
}
|
|
101
|
-
|
|
106
|
+
return response;
|
|
107
|
+
}
|
|
108
|
+
if (infinite) {
|
|
109
|
+
const infiniteResult = useInfiniteQuery({
|
|
110
|
+
key: () => LISTING_QUERY_KEYS.detail(String(listingName), toValue(params), apiQuery.value),
|
|
111
|
+
initialPageParam: initialPage,
|
|
112
|
+
query: async ({ pageParam }) => {
|
|
113
|
+
const pagedQueryFactory = fetchListingQuery({
|
|
114
|
+
...queryConfig,
|
|
115
|
+
page: ref(pageParam)
|
|
116
|
+
});
|
|
117
|
+
return await pagedQueryFactory.query();
|
|
118
|
+
},
|
|
119
|
+
getNextPageParam: (lastPage) => {
|
|
120
|
+
if (!lastPage || typeof lastPage !== "object") return void 0;
|
|
121
|
+
const page = lastPage.page;
|
|
122
|
+
if (!page || typeof page !== "object") return void 0;
|
|
123
|
+
return page.next ?? void 0;
|
|
124
|
+
},
|
|
125
|
+
getPreviousPageParam: (firstPage) => {
|
|
126
|
+
if (!firstPage || typeof firstPage !== "object") return void 0;
|
|
127
|
+
const page = firstPage.page;
|
|
128
|
+
if (!page || typeof page !== "object") return void 0;
|
|
129
|
+
return page.prev ?? void 0;
|
|
130
|
+
},
|
|
131
|
+
staleTime
|
|
132
|
+
});
|
|
133
|
+
const flattenedData = computed(() => {
|
|
134
|
+
const pages = infiniteResult.data.value?.pages;
|
|
135
|
+
if (!pages?.length) return void 0;
|
|
136
|
+
const lastPage = pages[pages.length - 1];
|
|
137
|
+
const allItems = pages.flatMap((page) => page?.items || []);
|
|
138
|
+
return {
|
|
139
|
+
...lastPage,
|
|
140
|
+
items: allItems
|
|
141
|
+
};
|
|
142
|
+
});
|
|
143
|
+
data = flattenedData;
|
|
144
|
+
status = infiniteResult.status;
|
|
145
|
+
refresh = infiniteResult.refresh;
|
|
146
|
+
refetch = infiniteResult.refetch;
|
|
147
|
+
loadNextFn = () => infiniteResult.loadNextPage();
|
|
148
|
+
loadPrevFn = () => infiniteResult.loadPreviousPage();
|
|
149
|
+
loadPageFn = async (page) => {
|
|
150
|
+
const cacheKey2 = LISTING_QUERY_KEYS.detail(String(listingName), toValue(params), apiQuery.value);
|
|
151
|
+
queryCache.setQueryData(cacheKey2, { pages: [], pageParams: [] });
|
|
152
|
+
const pagedQueryFactory = fetchListingQuery({
|
|
153
|
+
...queryConfig,
|
|
154
|
+
page: ref(page)
|
|
155
|
+
});
|
|
156
|
+
const response = await pagedQueryFactory.query();
|
|
157
|
+
if (response) {
|
|
158
|
+
queryCache.setQueryData(cacheKey2, { pages: [response], pageParams: [page] });
|
|
159
|
+
updateState(response);
|
|
160
|
+
}
|
|
161
|
+
return response;
|
|
162
|
+
};
|
|
163
|
+
} else {
|
|
164
|
+
const queryResult = useQuery(fetchListingQuery(queryConfig));
|
|
165
|
+
data = queryResult.data;
|
|
166
|
+
status = queryResult.status;
|
|
167
|
+
refresh = queryResult.refresh;
|
|
168
|
+
refetch = queryResult.refetch;
|
|
169
|
+
loadNextFn = async () => {
|
|
170
|
+
const pageData = data.value?.page;
|
|
171
|
+
if (!pageData?.next) return void 0;
|
|
172
|
+
return await fetchPage(pageData.next);
|
|
173
|
+
};
|
|
174
|
+
loadPrevFn = async () => {
|
|
175
|
+
const pageData = data.value?.page;
|
|
176
|
+
if (!pageData?.prev) return void 0;
|
|
177
|
+
return await fetchPage(pageData.prev);
|
|
178
|
+
};
|
|
179
|
+
loadPageFn = async (page) => {
|
|
180
|
+
return await fetchPage(page);
|
|
181
|
+
};
|
|
182
|
+
}
|
|
102
183
|
if (!disableSearch) {
|
|
103
184
|
watch(searchTerm, () => {
|
|
104
185
|
if (searchTerm.value.trim().length > searchTermThreshold || searchState.value.active.search) {
|
|
@@ -117,11 +198,14 @@ export function useFronticSearch(name, params, options) {
|
|
|
117
198
|
);
|
|
118
199
|
function updateState(response) {
|
|
119
200
|
const rawFilters = response.filter;
|
|
201
|
+
const rawAggregation = response.aggregation;
|
|
120
202
|
const pageData = response.page;
|
|
121
203
|
let activeFilters = makeFilterState(rawFilters);
|
|
122
204
|
activeFilters = applyFilterConfig(activeFilters, filterConfig);
|
|
123
205
|
const availableFilters = processAvailableFilters(rawFilters, filterConfig);
|
|
124
|
-
const
|
|
206
|
+
const responseHasSort = "sort" in response;
|
|
207
|
+
const availableSorts = processAvailableSorts(sortConfig, responseHasSort);
|
|
208
|
+
const availableAggregations = processAvailableAggregations(rawAggregation, aggregationConfig);
|
|
125
209
|
const activeFilterCount = countActiveFilters(activeFilters);
|
|
126
210
|
searchState.value = {
|
|
127
211
|
active: {
|
|
@@ -137,11 +221,13 @@ export function useFronticSearch(name, params, options) {
|
|
|
137
221
|
available: {
|
|
138
222
|
filter: availableFilters,
|
|
139
223
|
sorting: availableSorts,
|
|
224
|
+
aggregation: availableAggregations,
|
|
140
225
|
nextPage: pageData?.next !== void 0,
|
|
141
226
|
prevPage: pageData?.prev !== void 0,
|
|
142
227
|
count: {
|
|
143
228
|
filter: availableFilters.length,
|
|
144
229
|
sorting: availableSorts.length,
|
|
230
|
+
aggregation: availableAggregations.length,
|
|
145
231
|
result: response.total ?? 0,
|
|
146
232
|
page: pageData?.total ?? 1
|
|
147
233
|
}
|
|
@@ -155,28 +241,6 @@ export function useFronticSearch(name, params, options) {
|
|
|
155
241
|
}
|
|
156
242
|
}
|
|
157
243
|
const triggerSearchFromInput = useDebounceFn(refreshListing, searchDebounce);
|
|
158
|
-
async function paginate(direction) {
|
|
159
|
-
const pageData = data.value?.page;
|
|
160
|
-
const pageNumber = direction === "next" ? pageData?.next : pageData?.prev;
|
|
161
|
-
if (!pageNumber) return false;
|
|
162
|
-
searchState.value.active.page = pageNumber;
|
|
163
|
-
const pageQuery = fetchListingQuery({
|
|
164
|
-
name: listingName,
|
|
165
|
-
params,
|
|
166
|
-
query: apiQuery,
|
|
167
|
-
contextKey,
|
|
168
|
-
contextDomain,
|
|
169
|
-
requestUrl,
|
|
170
|
-
page: ref(pageNumber),
|
|
171
|
-
staleTime,
|
|
172
|
-
_client
|
|
173
|
-
});
|
|
174
|
-
const response = await pageQuery.query();
|
|
175
|
-
if (response) {
|
|
176
|
-
updateState(response);
|
|
177
|
-
}
|
|
178
|
-
return !!response;
|
|
179
|
-
}
|
|
180
244
|
async function sortResult(sortBy) {
|
|
181
245
|
if (disableSorting) return;
|
|
182
246
|
if (!sortBy) {
|
|
@@ -265,10 +329,24 @@ export function useFronticSearch(name, params, options) {
|
|
|
265
329
|
searchTerm,
|
|
266
330
|
// ========== Actions ==========
|
|
267
331
|
/**
|
|
268
|
-
* Load the next page of results
|
|
269
|
-
*
|
|
332
|
+
* Load the next page of results.
|
|
333
|
+
* Standard mode: replaces current page with the next one.
|
|
334
|
+
* Infinite mode: appends items from the next page.
|
|
335
|
+
*/
|
|
336
|
+
loadNext: loadNextFn,
|
|
337
|
+
/**
|
|
338
|
+
* Load the previous page of results.
|
|
339
|
+
* Standard mode: replaces current page with the previous one.
|
|
340
|
+
* Infinite mode: prepends items from the previous page.
|
|
270
341
|
*/
|
|
271
|
-
|
|
342
|
+
loadPrev: loadPrevFn,
|
|
343
|
+
/**
|
|
344
|
+
* Jump to a specific page number.
|
|
345
|
+
* Standard mode: replaces current page with the requested one.
|
|
346
|
+
* Infinite mode: resets all loaded pages and restarts from the given page.
|
|
347
|
+
* @param page - The page number to load
|
|
348
|
+
*/
|
|
349
|
+
loadPage: loadPageFn,
|
|
272
350
|
/**
|
|
273
351
|
* Refresh the listing data (uses cache if valid).
|
|
274
352
|
* Call after external changes that should trigger a refresh.
|
|
@@ -284,12 +362,6 @@ export function useFronticSearch(name, params, options) {
|
|
|
284
362
|
updateState(data.value);
|
|
285
363
|
}
|
|
286
364
|
},
|
|
287
|
-
/**
|
|
288
|
-
* Navigate to next or previous page.
|
|
289
|
-
* @param direction - 'next' or 'prev'
|
|
290
|
-
* @returns true if navigation was successful, false if no more pages
|
|
291
|
-
*/
|
|
292
|
-
paginate,
|
|
293
365
|
/**
|
|
294
366
|
* Sort results by a specific field. Call without arguments to reset to default.
|
|
295
367
|
* @param sortBy - Sort string in format 'field:asc' or 'field:desc', or omit to reset
|
|
@@ -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
|
*/
|
|
@@ -151,10 +171,30 @@ export interface UseFronticSearchOptions<TSearch extends keyof Listings = keyof
|
|
|
151
171
|
* Disable sorting functionality (default: false)
|
|
152
172
|
*/
|
|
153
173
|
disableSorting?: boolean;
|
|
174
|
+
/**
|
|
175
|
+
* Number of items per page (sent as `limit` to the Frontstack API).
|
|
176
|
+
* When omitted, the backend default is used.
|
|
177
|
+
*/
|
|
178
|
+
limit?: number;
|
|
154
179
|
/**
|
|
155
180
|
* Stale time in milliseconds (default: 5 minutes)
|
|
156
181
|
*/
|
|
157
182
|
staleTime?: number;
|
|
183
|
+
/**
|
|
184
|
+
* Enable infinite scroll mode.
|
|
185
|
+
* When true, loadNext()/loadPrev() append/prepend items from adjacent pages
|
|
186
|
+
* using Pinia Colada's useInfiniteQuery under the hood.
|
|
187
|
+
* When false (default), pagination replaces the current page.
|
|
188
|
+
*/
|
|
189
|
+
infinite?: boolean;
|
|
190
|
+
/**
|
|
191
|
+
* Starting page number for infinite scroll mode.
|
|
192
|
+
* Useful when deep-linking to a specific page (e.g., page 4 from URL).
|
|
193
|
+
* Users can then scroll both forward (loadNext) and backward (loadPrev).
|
|
194
|
+
* Only used when `infinite: true`. Values below 1 are clamped to 1.
|
|
195
|
+
* @default 1
|
|
196
|
+
*/
|
|
197
|
+
initialPage?: number;
|
|
158
198
|
}
|
|
159
199
|
/**
|
|
160
200
|
* Return type for the search composable
|
|
@@ -173,9 +213,27 @@ export interface UseSearchReturn<TSearch extends keyof Listings = keyof Listings
|
|
|
173
213
|
/** Current search term */
|
|
174
214
|
searchTerm: Ref<string>;
|
|
175
215
|
/**
|
|
176
|
-
*
|
|
216
|
+
* Load the next page of results.
|
|
217
|
+
* Standard mode: replaces current page with the next one.
|
|
218
|
+
* Infinite mode: appends items from the next page.
|
|
219
|
+
* @returns The page response, or undefined if no next page
|
|
177
220
|
*/
|
|
178
221
|
loadNext: () => Promise<Responses[TSearch] | undefined>;
|
|
222
|
+
/**
|
|
223
|
+
* Load the previous page of results.
|
|
224
|
+
* Standard mode: replaces current page with the previous one.
|
|
225
|
+
* Infinite mode: prepends items from the previous page.
|
|
226
|
+
* @returns The page response, or undefined if no previous page
|
|
227
|
+
*/
|
|
228
|
+
loadPrev: () => Promise<Responses[TSearch] | undefined>;
|
|
229
|
+
/**
|
|
230
|
+
* Jump to a specific page number.
|
|
231
|
+
* Standard mode: replaces current page with the requested one.
|
|
232
|
+
* Infinite mode: resets all loaded pages and restarts from the given page.
|
|
233
|
+
* @param page - The page number to load
|
|
234
|
+
* @returns The page response, or undefined if the page doesn't exist
|
|
235
|
+
*/
|
|
236
|
+
loadPage: (page: number) => Promise<Responses[TSearch] | undefined>;
|
|
179
237
|
/**
|
|
180
238
|
* Fetches the search with current query parameters (uses cache if valid)
|
|
181
239
|
*/
|
|
@@ -184,12 +242,6 @@ export interface UseSearchReturn<TSearch extends keyof Listings = keyof Listings
|
|
|
184
242
|
* Forces a fresh fetch of the search, bypassing cache
|
|
185
243
|
*/
|
|
186
244
|
refetch: () => Promise<void>;
|
|
187
|
-
/**
|
|
188
|
-
* Paginates the results in the specified direction
|
|
189
|
-
* @param direction - Direction to paginate ('next' or 'prev')
|
|
190
|
-
* @returns Promise<boolean> - Returns true if pagination was successful
|
|
191
|
-
*/
|
|
192
|
-
paginate: (direction: 'next' | 'prev') => Promise<boolean>;
|
|
193
245
|
/**
|
|
194
246
|
* Sorts the results by the specified field. Call without arguments to reset to default.
|
|
195
247
|
* Type-safe: only accepts valid sort strings for this search
|
|
@@ -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
|
*
|
|
@@ -73,7 +82,24 @@ export declare function processAvailableFilters<TFilters = Record<string, unknow
|
|
|
73
82
|
* // ]
|
|
74
83
|
* ```
|
|
75
84
|
*/
|
|
76
|
-
export declare function processAvailableSorts<TSorts = Record<string, unknown>>(config?: ListingFieldConfig): UiSort<TSorts>[];
|
|
85
|
+
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[];
|
|
77
103
|
/**
|
|
78
104
|
* Applies filter configuration to transform the filter state
|
|
79
105
|
* Handles include/exclude mode and label renaming
|
|
@@ -47,7 +47,8 @@ export function processAvailableFilters(rawFilters, config = {}) {
|
|
|
47
47
|
}
|
|
48
48
|
return sortByOrder(availableFilters, sortOrder, (f) => f.key);
|
|
49
49
|
}
|
|
50
|
-
export function processAvailableSorts(config = {}) {
|
|
50
|
+
export function processAvailableSorts(config = {}, responseHasSort = true) {
|
|
51
|
+
if (!responseHasSort) return [];
|
|
51
52
|
const { select = [], mode = "include", label: labels = {}, sort: sortOrder } = config;
|
|
52
53
|
const availableSorts = Object.entries(labels).filter(([value]) => {
|
|
53
54
|
if (select.length === 0) return true;
|
|
@@ -62,6 +63,25 @@ export function processAvailableSorts(config = {}) {
|
|
|
62
63
|
);
|
|
63
64
|
return sortByOrder(availableSorts, sortOrder, (s) => s.key);
|
|
64
65
|
}
|
|
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
|
+
}
|
|
65
85
|
export function applyFilterConfig(filters, config = {}) {
|
|
66
86
|
const { select = [], mode = "include", label: labels = {} } = config;
|
|
67
87
|
let result = filters;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frontic/nuxt",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.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",
|
|
@@ -44,8 +44,8 @@
|
|
|
44
44
|
],
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"@nuxt/kit": "^3.17.0",
|
|
47
|
-
"@pinia/colada": "^0.
|
|
48
|
-
"@pinia/colada-nuxt": "^0.3.
|
|
47
|
+
"@pinia/colada": "^1.0.0",
|
|
48
|
+
"@pinia/colada-nuxt": "^0.3.2",
|
|
49
49
|
"@pinia/nuxt": "^0.11.0",
|
|
50
50
|
"@vueuse/core": "^12.0.0",
|
|
51
51
|
"pinia": "^3.0.0"
|
|
@@ -53,6 +53,8 @@
|
|
|
53
53
|
"devDependencies": {
|
|
54
54
|
"@nuxt/module-builder": "^1.0.2",
|
|
55
55
|
"@nuxt/schema": "^3.17.0",
|
|
56
|
+
"@nuxt/test-utils": "^4.0.0",
|
|
57
|
+
"@vitest/coverage-v8": "^2",
|
|
56
58
|
"h3": "^1.15.0",
|
|
57
59
|
"nitropack": "^2.13.1",
|
|
58
60
|
"nuxt": "^3.17.0",
|
|
@@ -72,6 +74,7 @@
|
|
|
72
74
|
"typecheck": "vue-tsc --noEmit",
|
|
73
75
|
"test": "vitest run",
|
|
74
76
|
"test:watch": "vitest watch",
|
|
77
|
+
"test:coverage": "vitest run --coverage",
|
|
75
78
|
"clean": "rm -rf dist .nuxt"
|
|
76
79
|
}
|
|
77
80
|
}
|