@frontic/nuxt 0.8.4 → 1.1.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 +65 -4
- package/dist/module.d.mts +2 -132
- package/dist/module.json +2 -2
- package/dist/module.mjs +130 -15
- package/dist/runtime/composables/analytics.d.ts +82 -0
- package/dist/runtime/composables/analytics.js +38 -0
- package/dist/runtime/composables/blocks.d.ts +24 -22
- package/dist/runtime/composables/blocks.js +16 -16
- package/dist/runtime/composables/index.d.ts +1 -1
- package/dist/runtime/composables/listings.d.ts +22 -13
- package/dist/runtime/composables/listings.js +24 -25
- package/dist/runtime/composables/pages.d.ts +21 -18
- package/dist/runtime/composables/pages.js +15 -16
- package/dist/runtime/composables/trees.d.ts +24 -22
- package/dist/runtime/composables/trees.js +15 -25
- package/dist/runtime/composables/useFronticBlock.d.ts +22 -13
- package/dist/runtime/composables/useFronticBlock.js +25 -4
- package/dist/runtime/composables/useFronticClient.d.ts +9 -28
- package/dist/runtime/composables/useFronticClient.js +59 -48
- package/dist/runtime/composables/useFronticContext.d.ts +12 -0
- package/dist/runtime/composables/useFronticContext.js +71 -9
- package/dist/runtime/composables/useFronticListing.d.ts +23 -19
- package/dist/runtime/composables/useFronticListing.js +28 -5
- package/dist/runtime/composables/useFronticPage.d.ts +35 -16
- package/dist/runtime/composables/useFronticPage.js +42 -20
- package/dist/runtime/composables/useFronticSearch.d.ts +4 -96
- package/dist/runtime/composables/useFronticSearch.js +193 -60
- package/dist/runtime/composables/useFronticTree.d.ts +23 -14
- package/dist/runtime/composables/useFronticTree.js +25 -5
- package/dist/runtime/server/api/frontic/[...path].js +48 -10
- package/dist/runtime/server/middleware/sitemap.d.ts +2 -0
- package/dist/runtime/server/middleware/sitemap.js +55 -0
- package/dist/runtime/server/plugins/fetch-secret.d.ts +2 -0
- package/dist/runtime/server/plugins/fetch-secret.js +20 -0
- package/dist/runtime/types/client.d.ts +36 -0
- package/dist/runtime/types/client.js +0 -0
- package/dist/runtime/types/errors.d.ts +47 -0
- package/dist/runtime/types/errors.js +1 -0
- package/dist/runtime/types/index.d.ts +9 -3
- package/dist/runtime/types/listing.d.ts +113 -251
- package/dist/runtime/types/options.d.ts +169 -0
- package/dist/runtime/types/options.js +0 -0
- package/dist/runtime/types/query.d.ts +88 -0
- package/dist/runtime/types/query.js +0 -0
- package/dist/runtime/types/registry.d.ts +272 -0
- package/dist/runtime/types/registry.js +0 -0
- package/dist/runtime/types/search.d.ts +90 -38
- package/dist/runtime/utils/describe-error.d.ts +38 -0
- package/dist/runtime/utils/describe-error.js +38 -0
- package/dist/runtime/utils/errors.d.ts +22 -0
- package/dist/runtime/utils/errors.js +3 -0
- package/dist/runtime/utils/filter-helpers.d.ts +13 -0
- package/dist/runtime/utils/filter-helpers.js +14 -0
- package/dist/runtime/utils/index.d.ts +2 -0
- package/dist/runtime/utils/index.js +2 -0
- package/dist/runtime/utils/query-keys.d.ts +27 -0
- package/dist/runtime/utils/query-keys.js +18 -0
- package/dist/runtime/utils/resolve-context-domain.d.ts +14 -0
- package/dist/runtime/utils/resolve-context-domain.js +9 -1
- package/dist/runtime/utils/state-helpers.d.ts +3 -6
- package/dist/runtime/utils/state-helpers.js +7 -8
- package/dist/types.d.mts +2 -2
- package/package.json +15 -15
- package/dist/runtime/frontic-stack.d.ts +0 -168
|
@@ -1,37 +1,39 @@
|
|
|
1
|
-
import { useRequestURL, useRuntimeConfig, navigateTo, createError } from "nuxt/app";
|
|
1
|
+
import { useRequestURL, useRuntimeConfig, navigateTo, createError, useNuxtApp } from "nuxt/app";
|
|
2
2
|
import { computed, toValue, watch } from "vue";
|
|
3
3
|
import { useQuery } from "@pinia/colada";
|
|
4
4
|
import { fetchPageQuery } from "./pages.js";
|
|
5
5
|
import { useFronticClient } from "./useFronticClient.js";
|
|
6
|
-
import {
|
|
6
|
+
import { emitOnce, bindRouteReset, FRONTIC_ANALYTICS_VERSION } from "./analytics.js";
|
|
7
|
+
import { resolveContextDomain } from "../utils/resolve-context-domain.js";
|
|
7
8
|
export function useFronticPage(slug, options) {
|
|
9
|
+
const nuxtApp = useNuxtApp();
|
|
10
|
+
bindRouteReset(nuxtApp);
|
|
8
11
|
const requestURL = useRequestURL();
|
|
9
12
|
const config = useRuntimeConfig();
|
|
10
13
|
const fronticConfig = config.public.frontic;
|
|
14
|
+
const autoContextDomain = resolveContextDomain();
|
|
15
|
+
const resolvedContextDomain = computed(() => toValue(options?.contextDomain) ?? autoContextDomain);
|
|
11
16
|
const pageSlug = computed(() => {
|
|
12
17
|
if (slug) return toValue(slug);
|
|
13
18
|
const pathname = requestURL.pathname.replace(/\/+$/, "");
|
|
14
|
-
|
|
15
|
-
const optionDomain = options?.contextDomain ? toValue(options.contextDomain) : void 0;
|
|
16
|
-
if (typeof optionDomain === "string") {
|
|
17
|
-
domain = optionDomain;
|
|
18
|
-
} else if (typeof fronticConfig?.contextDomain === "string") {
|
|
19
|
-
domain = fronticConfig.contextDomain;
|
|
20
|
-
} else if (fronticConfig?.contextDomain === true) {
|
|
21
|
-
domain = resolveI18nContextDomain();
|
|
22
|
-
}
|
|
23
|
-
return `${domain ?? requestURL.host}${pathname}`;
|
|
19
|
+
return `${resolvedContextDomain.value ?? requestURL.host}${pathname}`;
|
|
24
20
|
});
|
|
25
|
-
const _client = useFronticClient();
|
|
21
|
+
const _client = useFronticClient({ contextDomain: false });
|
|
26
22
|
const pageQuery = fetchPageQuery({
|
|
27
23
|
slug: pageSlug,
|
|
28
24
|
contextKey: options?.contextKey,
|
|
29
|
-
contextDomain:
|
|
25
|
+
contextDomain: resolvedContextDomain,
|
|
30
26
|
requestUrl: options?.requestUrl,
|
|
31
27
|
staleTime: options?.staleTime,
|
|
28
|
+
gcTime: options?.gcTime,
|
|
29
|
+
enabled: options?.enabled,
|
|
30
|
+
// Page navigation swaps the entity, not the query over one entity, so a
|
|
31
|
+
// held value is a different page under the new URL. It would also put the
|
|
32
|
+
// previous page's route in front of the redirect/404 watcher below.
|
|
33
|
+
keepPreviousData: options?.keepPreviousData ?? false,
|
|
32
34
|
_client
|
|
33
35
|
});
|
|
34
|
-
const { data, status, refresh, refetch } = useQuery(pageQuery);
|
|
36
|
+
const { data, status, asyncStatus, isLoading, error, refresh, refetch } = useQuery(pageQuery);
|
|
35
37
|
const redirectOn301 = options?.redirectOn301 ?? fronticConfig?.redirectOn301 ?? true;
|
|
36
38
|
const throwOn404 = options?.throwOn404 ?? fronticConfig?.throwOn404 ?? true;
|
|
37
39
|
watch(
|
|
@@ -43,6 +45,11 @@ export function useFronticPage(slug, options) {
|
|
|
43
45
|
} else if (throwOn404 && page.route.code === 404) {
|
|
44
46
|
throw createError({ statusCode: 404, statusMessage: "Page not found" });
|
|
45
47
|
}
|
|
48
|
+
if (page.route.code !== 200 || !("type" in page)) return;
|
|
49
|
+
emitOnce(nuxtApp, `page:resolved:${pageSlug.value}`, "frontic:page:resolved", {
|
|
50
|
+
page,
|
|
51
|
+
version: FRONTIC_ANALYTICS_VERSION
|
|
52
|
+
});
|
|
46
53
|
},
|
|
47
54
|
{ immediate: true }
|
|
48
55
|
);
|
|
@@ -54,15 +61,16 @@ export function useFronticPage(slug, options) {
|
|
|
54
61
|
/**
|
|
55
62
|
* Page data (the actual content block data)
|
|
56
63
|
*/
|
|
57
|
-
data: computed(() => data.value
|
|
64
|
+
data: computed(() => data.value && "data" in data.value ? data.value.data : void 0),
|
|
58
65
|
/**
|
|
59
|
-
* Page type (e.g
|
|
66
|
+
* Page type, as configured in Frontic (e.g. 'ProductPage', 'CategoryDetail').
|
|
67
|
+
* Resolves to the tenant's own literal union, so it narrows `data`.
|
|
60
68
|
*/
|
|
61
|
-
type: computed(() => data.value
|
|
69
|
+
type: computed(() => data.value && "type" in data.value ? data.value.type : void 0),
|
|
62
70
|
/**
|
|
63
71
|
* Block name to render (e.g., 'CategoryFull', 'ProductFull')
|
|
64
72
|
*/
|
|
65
|
-
block: computed(() => data.value
|
|
73
|
+
block: computed(() => data.value && "block" in data.value ? data.value.block : void 0),
|
|
66
74
|
/**
|
|
67
75
|
* Route metadata (code, redirect, alternates, context)
|
|
68
76
|
*/
|
|
@@ -76,9 +84,23 @@ export function useFronticPage(slug, options) {
|
|
|
76
84
|
*/
|
|
77
85
|
suggested: computed(() => data.value?.route?.context?.suggested),
|
|
78
86
|
/**
|
|
79
|
-
*
|
|
87
|
+
* Data status: 'pending' | 'error' | 'success'
|
|
80
88
|
*/
|
|
81
89
|
status,
|
|
90
|
+
/**
|
|
91
|
+
* Network status: 'idle' | 'loading'. Together with `status` this
|
|
92
|
+
* distinguishes a first load (`pending`) from a background refresh
|
|
93
|
+
* (`success` + 'loading').
|
|
94
|
+
*/
|
|
95
|
+
asyncStatus,
|
|
96
|
+
/**
|
|
97
|
+
* Whether a request is in flight (shortcut for `asyncStatus === 'loading'`)
|
|
98
|
+
*/
|
|
99
|
+
isLoading,
|
|
100
|
+
/**
|
|
101
|
+
* Error of the last fetch, null while pending or after success
|
|
102
|
+
*/
|
|
103
|
+
error,
|
|
82
104
|
/**
|
|
83
105
|
* Manually refresh the page data (uses cache if valid)
|
|
84
106
|
*/
|
|
@@ -6,10 +6,9 @@
|
|
|
6
6
|
*
|
|
7
7
|
* For simple listing access without search features, use useFronticListing instead.
|
|
8
8
|
*/
|
|
9
|
-
import {
|
|
10
|
-
import type {
|
|
11
|
-
import type {
|
|
12
|
-
import type { UseFronticSearchOptions, SearchState } from '../types/search.js';
|
|
9
|
+
import type { ListingName, ListingFiltersOf, ListingSortsOf } from '../types/registry.js';
|
|
10
|
+
import type { ListingArgs } from '../types/listing.js';
|
|
11
|
+
import type { UseFronticSearchOptions, UseSearchReturn } from '../types/search.js';
|
|
13
12
|
/**
|
|
14
13
|
* Full-featured composable for managing Frontic search listings with filtering, sorting, and pagination
|
|
15
14
|
*
|
|
@@ -37,95 +36,4 @@ import type { UseFronticSearchOptions, SearchState } from '../types/search.js';
|
|
|
37
36
|
* await productSearch.loadNext()
|
|
38
37
|
* ```
|
|
39
38
|
*/
|
|
40
|
-
export declare function useFronticSearch<TSearch extends
|
|
41
|
-
/**
|
|
42
|
-
* The listing response containing items, pagination info, and filter metadata.
|
|
43
|
-
*/
|
|
44
|
-
result: ShallowRef<Responses[TSearch] | undefined>;
|
|
45
|
-
/**
|
|
46
|
-
* Query status: 'pending' | 'error' | 'success'
|
|
47
|
-
*/
|
|
48
|
-
status: ShallowRef<import("@pinia/colada").DataStateStatus>;
|
|
49
|
-
/**
|
|
50
|
-
* Current search state including active filters, sorts, pagination, and available options.
|
|
51
|
-
* Use this to build filter/sort UI components.
|
|
52
|
-
*/
|
|
53
|
-
state: import("vue").Ref<SearchState<TFilters, TSorts>, SearchState<TFilters, TSorts>>;
|
|
54
|
-
/**
|
|
55
|
-
* Reactive search term. Bind to an input field for live search.
|
|
56
|
-
* Search triggers automatically when term length exceeds searchTermThreshold.
|
|
57
|
-
*/
|
|
58
|
-
searchTerm: import("vue").Ref<string, string>;
|
|
59
|
-
/**
|
|
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.
|
|
63
|
-
*/
|
|
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>;
|
|
78
|
-
/**
|
|
79
|
-
* Refresh the listing data (uses cache if valid).
|
|
80
|
-
* Call after external changes that should trigger a refresh.
|
|
81
|
-
*/
|
|
82
|
-
refresh: () => Promise<void>;
|
|
83
|
-
/**
|
|
84
|
-
* Force a fresh fetch, bypassing cache.
|
|
85
|
-
* Use when you need guaranteed fresh data.
|
|
86
|
-
*/
|
|
87
|
-
refetch: () => Promise<void>;
|
|
88
|
-
/**
|
|
89
|
-
* Sort results by a specific field. Call without arguments to reset to default.
|
|
90
|
-
* @param sortBy - Sort string in format 'field:asc' or 'field:desc', or omit to reset
|
|
91
|
-
*/
|
|
92
|
-
sortResult: (sortBy?: string) => Promise<void>;
|
|
93
|
-
/**
|
|
94
|
-
* Reset sorting to default (backend default order).
|
|
95
|
-
*/
|
|
96
|
-
resetSorting: () => Promise<void>;
|
|
97
|
-
/**
|
|
98
|
-
* Set filter values for a specific field (replaces existing values).
|
|
99
|
-
* For range filters, pass an array with a single { from, to } object.
|
|
100
|
-
* @param filterField - The field to filter on (e.g., 'properties.color')
|
|
101
|
-
* @param filterOptions - Array of values to filter by, or [{ from, to }] for range filters
|
|
102
|
-
*/
|
|
103
|
-
filterResult: <K extends keyof TFilters & string>(filterField: K, filterOptions: Array<TFilters[K]>) => Promise<void>;
|
|
104
|
-
/**
|
|
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.
|
|
107
|
-
* @param filterField - The field to filter on
|
|
108
|
-
* @param filterOption - The value to add, or { from, to } for range filters
|
|
109
|
-
*/
|
|
110
|
-
addFilter: <K extends keyof TFilters & string>(filterField: K, filterOption: TFilters[K]) => Promise<void>;
|
|
111
|
-
/**
|
|
112
|
-
* Remove a filter option from a field, or remove all values for that field.
|
|
113
|
-
* @param filterField - The field to remove filter from
|
|
114
|
-
* @param filterOption - Optional specific value to remove (removes all if omitted)
|
|
115
|
-
*/
|
|
116
|
-
removeFilter: <K extends keyof TFilters & string>(filterField: K, filterOption?: TFilters[K]) => Promise<void>;
|
|
117
|
-
/**
|
|
118
|
-
* Reset filters for a specific field or all fields.
|
|
119
|
-
* @param filterField - Optional field to reset (resets all if omitted)
|
|
120
|
-
*/
|
|
121
|
-
resetFilter: (filterField?: keyof TFilters & string) => Promise<void>;
|
|
122
|
-
/**
|
|
123
|
-
* Clear the search term and refresh results.
|
|
124
|
-
*/
|
|
125
|
-
resetSearch: () => Promise<void>;
|
|
126
|
-
/**
|
|
127
|
-
* Reset all state (search term, filters, and sorting) to initial values.
|
|
128
|
-
* Equivalent to calling resetSearch(), resetFilter(), and resetSorting() together.
|
|
129
|
-
*/
|
|
130
|
-
reset: () => Promise<void>;
|
|
131
|
-
};
|
|
39
|
+
export declare function useFronticSearch<TSearch extends ListingName, TFilters = ListingFiltersOf<TSearch>, TSorts = ListingSortsOf<TSearch>>(name: TSearch, ...args: ListingArgs<TSearch, UseFronticSearchOptions<TSearch, TFilters, TSorts>>): UseSearchReturn<TSearch, TFilters, TSorts>;
|
|
@@ -1,38 +1,71 @@
|
|
|
1
|
-
import { useState } from "nuxt/app";
|
|
2
|
-
import { computed,
|
|
3
|
-
import { useQuery, useInfiniteQuery, useQueryCache } from "@pinia/colada";
|
|
1
|
+
import { useState, useNuxtApp } from "nuxt/app";
|
|
2
|
+
import { computed, ref, toValue, watch } from "vue";
|
|
3
|
+
import { useQuery, useInfiniteQuery, useQueryCache, setInfiniteQueryData } from "@pinia/colada";
|
|
4
|
+
import { resolveContextDomain } from "../utils/resolve-context-domain.js";
|
|
4
5
|
import { useDebounceFn } from "@vueuse/core";
|
|
5
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
makeLogicalFilter,
|
|
8
|
+
manageFilter,
|
|
9
|
+
removeFilter,
|
|
10
|
+
isRangeFilterValue,
|
|
11
|
+
manageRangeFilter,
|
|
12
|
+
removeRangeFilter,
|
|
13
|
+
seedFilterLists
|
|
14
|
+
} from "../utils/filter-helpers.js";
|
|
6
15
|
import { makeFilterState, processAvailableFilters, processAvailableSorts, applyFilterConfig, countActiveFilters, parseSortString, isSortAllowed } from "../utils/state-helpers.js";
|
|
7
16
|
import { fetchListingQuery, LISTING_QUERY_KEYS } from "./listings.js";
|
|
8
17
|
import { useFronticClient } from "./useFronticClient.js";
|
|
9
|
-
|
|
18
|
+
import { emit, emitOnce, bindRouteReset, toAnalyticsItems, FRONTIC_ANALYTICS_VERSION } from "./analytics.js";
|
|
19
|
+
export function useFronticSearch(name, ...args) {
|
|
20
|
+
const [rawParams, options] = args;
|
|
21
|
+
const params = rawParams ?? {};
|
|
10
22
|
const queryCache = useQueryCache();
|
|
11
|
-
const _client = useFronticClient();
|
|
23
|
+
const _client = useFronticClient({ contextDomain: false });
|
|
12
24
|
const listingName = name;
|
|
25
|
+
const nuxtApp = useNuxtApp();
|
|
26
|
+
bindRouteReset(nuxtApp);
|
|
13
27
|
const cacheKey = options?.cacheKey ?? String(name);
|
|
14
28
|
const contextKey = options?.contextKey;
|
|
15
|
-
const
|
|
29
|
+
const autoContextDomain = resolveContextDomain();
|
|
30
|
+
const contextDomain = computed(() => toValue(options?.contextDomain) ?? autoContextDomain);
|
|
16
31
|
const requestUrl = options?.requestUrl;
|
|
17
32
|
const searchTermThreshold = options?.searchTermThreshold ?? 2;
|
|
18
33
|
const searchDebounce = options?.searchDebounce ?? 200;
|
|
34
|
+
const searchCommitMs = options?.analytics?.searchCommitMs ?? 1e3;
|
|
35
|
+
const searchMinChars = options?.analytics?.searchMinChars ?? 3;
|
|
19
36
|
const orFilter = options?.orFilter ?? [];
|
|
20
37
|
const filter = options?.filter;
|
|
21
38
|
const sorting = options?.sorting;
|
|
22
39
|
const disableSearch = options?.disableSearch ?? false;
|
|
23
40
|
const disableFilter = options?.disableFilter ?? false;
|
|
24
41
|
const disableSorting = options?.disableSorting ?? false;
|
|
25
|
-
const limit = options?.limit;
|
|
26
42
|
const staleTime = options?.staleTime ?? 1e3 * 60 * 5;
|
|
27
43
|
const infinite = options?.infinite ?? false;
|
|
28
44
|
const initialPage = Math.max(1, Math.floor(options?.initialPage ?? 1));
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
45
|
+
if (import.meta.dev) {
|
|
46
|
+
const nuxtAppWithRegistry = nuxtApp;
|
|
47
|
+
const registry = nuxtAppWithRegistry._fronticSearchParams ??= /* @__PURE__ */ new Map();
|
|
48
|
+
const paramsSignature = JSON.stringify(toValue(params) ?? {});
|
|
49
|
+
const knownSignature = registry.get(cacheKey);
|
|
50
|
+
if (knownSignature !== void 0 && knownSignature !== paramsSignature) {
|
|
51
|
+
console.warn(
|
|
52
|
+
`[@frontic/nuxt] useFronticSearch('${String(name)}') is called with different params but the same cacheKey '${cacheKey}'. These instances share search/filter/page state. Pass a distinct \`cacheKey\` option to separate them.`
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
registry.set(cacheKey, paramsSignature);
|
|
56
|
+
}
|
|
57
|
+
const filterConfig = filter ?? {};
|
|
58
|
+
const sortConfig = sorting ?? {};
|
|
59
|
+
const seededFilters = seedFilterLists(options?.initialFilters, orFilter);
|
|
60
|
+
const seededSort = options?.initialSort && isSortAllowed(options.initialSort, sortConfig) ? parseSortString(options.initialSort) : void 0;
|
|
61
|
+
const seededSearch = options?.initialSearch?.trim() ? options.initialSearch : void 0;
|
|
62
|
+
const searchTerm = useState(`${cacheKey}-searchTerm`, () => seededSearch ?? "");
|
|
63
|
+
const _orFilterList = useState(`${cacheKey}-orFilterList`, () => seededFilters.or);
|
|
64
|
+
const _andFilterList = useState(`${cacheKey}-andFilterList`, () => seededFilters.and);
|
|
65
|
+
const _rangeFilterList = useState(`${cacheKey}-rangeFilterList`, () => seededFilters.range);
|
|
66
|
+
const _sortQuery = useState(`${cacheKey}-sortQuery`, () => seededSort ?? void 0);
|
|
67
|
+
const _page = useState(`${cacheKey}-page`, () => void 0);
|
|
68
|
+
const _search = useState(`${cacheKey}-search`, () => seededSearch);
|
|
36
69
|
const searchState = useState(`${cacheKey}-searchState`, () => ({
|
|
37
70
|
active: {
|
|
38
71
|
filter: {},
|
|
@@ -57,12 +90,14 @@ export function useFronticSearch(name, params, options) {
|
|
|
57
90
|
}
|
|
58
91
|
}
|
|
59
92
|
}));
|
|
60
|
-
const apiQuery = computed(
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
93
|
+
const apiQuery = computed(
|
|
94
|
+
() => ({
|
|
95
|
+
sort: !disableSorting ? _sortQuery.value : void 0,
|
|
96
|
+
search: !disableSearch ? _search.value : void 0,
|
|
97
|
+
filter: !disableFilter ? [...makeLogicalFilter("or", _orFilterList.value), ...makeLogicalFilter("and", _andFilterList.value), ..._rangeFilterList.value] : void 0,
|
|
98
|
+
limit: toValue(options?.limit)
|
|
99
|
+
})
|
|
100
|
+
);
|
|
66
101
|
const queryConfig = {
|
|
67
102
|
name: listingName,
|
|
68
103
|
params,
|
|
@@ -71,32 +106,27 @@ export function useFronticSearch(name, params, options) {
|
|
|
71
106
|
contextDomain,
|
|
72
107
|
requestUrl,
|
|
73
108
|
staleTime,
|
|
109
|
+
gcTime: options?.gcTime,
|
|
110
|
+
enabled: options?.enabled,
|
|
111
|
+
keepPreviousData: options?.keepPreviousData,
|
|
74
112
|
_client
|
|
75
113
|
};
|
|
76
114
|
let data;
|
|
77
115
|
let status;
|
|
116
|
+
let asyncStatus;
|
|
117
|
+
let isLoading;
|
|
118
|
+
let error;
|
|
78
119
|
let refresh;
|
|
79
120
|
let refetch;
|
|
80
121
|
let loadNextFn;
|
|
81
122
|
let loadPrevFn;
|
|
82
123
|
let loadPageFn;
|
|
83
|
-
async function fetchPage(pageNumber) {
|
|
84
|
-
const pageQuery = fetchListingQuery({
|
|
85
|
-
...queryConfig,
|
|
86
|
-
page: ref(pageNumber)
|
|
87
|
-
});
|
|
88
|
-
const response = await pageQuery.query();
|
|
89
|
-
if (response) {
|
|
90
|
-
const baseCacheKey = LISTING_QUERY_KEYS.detail(String(listingName), toValue(params), apiQuery.value);
|
|
91
|
-
queryCache.setQueryData(baseCacheKey, response);
|
|
92
|
-
updateState(response);
|
|
93
|
-
}
|
|
94
|
-
return response;
|
|
95
|
-
}
|
|
96
124
|
if (infinite) {
|
|
97
125
|
const infiniteResult = useInfiniteQuery({
|
|
98
|
-
key: () => LISTING_QUERY_KEYS.detail(String(listingName), toValue(params), apiQuery.value),
|
|
99
|
-
|
|
126
|
+
key: () => LISTING_QUERY_KEYS.detail(String(listingName), toValue(params), apiQuery.value, toValue(contextKey), toValue(contextDomain)),
|
|
127
|
+
// `_page` seeds the starting cursor (e.g. deep-linked page from the URL); it deliberately does NOT enter the
|
|
128
|
+
// key above — in infinite mode page is a cursor, not an identity, so keying on it would reset accumulation.
|
|
129
|
+
initialPageParam: _page.value ?? initialPage,
|
|
100
130
|
query: async ({ pageParam }) => {
|
|
101
131
|
const pagedQueryFactory = fetchListingQuery({
|
|
102
132
|
...queryConfig,
|
|
@@ -122,7 +152,7 @@ export function useFronticSearch(name, params, options) {
|
|
|
122
152
|
const pages = infiniteResult.data.value?.pages;
|
|
123
153
|
if (!pages?.length) return void 0;
|
|
124
154
|
const lastPage = pages[pages.length - 1];
|
|
125
|
-
const allItems = pages.flatMap((page) => page?.items
|
|
155
|
+
const allItems = pages.flatMap((page) => page?.items ?? []);
|
|
126
156
|
return {
|
|
127
157
|
...lastPage,
|
|
128
158
|
items: allItems
|
|
@@ -130,49 +160,75 @@ export function useFronticSearch(name, params, options) {
|
|
|
130
160
|
});
|
|
131
161
|
data = flattenedData;
|
|
132
162
|
status = infiniteResult.status;
|
|
163
|
+
asyncStatus = infiniteResult.asyncStatus;
|
|
164
|
+
isLoading = infiniteResult.isLoading;
|
|
165
|
+
error = infiniteResult.error;
|
|
133
166
|
refresh = infiniteResult.refresh;
|
|
134
167
|
refetch = infiniteResult.refetch;
|
|
135
|
-
loadNextFn = () =>
|
|
136
|
-
|
|
168
|
+
loadNextFn = async () => {
|
|
169
|
+
await infiniteResult.loadNextPage();
|
|
170
|
+
if (flattenedData.value) emitListingResolved(flattenedData.value, "loadMore");
|
|
171
|
+
return flattenedData.value;
|
|
172
|
+
};
|
|
173
|
+
loadPrevFn = async () => {
|
|
174
|
+
await infiniteResult.loadPreviousPage();
|
|
175
|
+
if (flattenedData.value) emitListingResolved(flattenedData.value, "loadMore");
|
|
176
|
+
return flattenedData.value;
|
|
177
|
+
};
|
|
137
178
|
loadPageFn = async (page) => {
|
|
138
|
-
const cacheKey2 = LISTING_QUERY_KEYS.detail(String(listingName), toValue(params), apiQuery.value);
|
|
139
|
-
queryCache.setQueryData(cacheKey2, { pages: [], pageParams: [] });
|
|
140
179
|
const pagedQueryFactory = fetchListingQuery({
|
|
141
180
|
...queryConfig,
|
|
142
181
|
page: ref(page)
|
|
143
182
|
});
|
|
144
183
|
const response = await pagedQueryFactory.query();
|
|
145
184
|
if (response) {
|
|
146
|
-
|
|
185
|
+
const listKey = LISTING_QUERY_KEYS.detail(
|
|
186
|
+
String(listingName),
|
|
187
|
+
toValue(params),
|
|
188
|
+
apiQuery.value,
|
|
189
|
+
toValue(contextKey),
|
|
190
|
+
toValue(contextDomain)
|
|
191
|
+
);
|
|
192
|
+
setInfiniteQueryData(queryCache, listKey, { pages: [response], pageParams: [page] });
|
|
147
193
|
updateState(response);
|
|
194
|
+
emitListingResolved(response, "page");
|
|
148
195
|
}
|
|
149
196
|
return response;
|
|
150
197
|
};
|
|
151
198
|
} else {
|
|
152
|
-
const queryResult = useQuery(fetchListingQuery(queryConfig));
|
|
199
|
+
const queryResult = useQuery(fetchListingQuery({ ...queryConfig, page: _page }));
|
|
153
200
|
data = queryResult.data;
|
|
154
201
|
status = queryResult.status;
|
|
202
|
+
asyncStatus = queryResult.asyncStatus;
|
|
203
|
+
isLoading = queryResult.isLoading;
|
|
204
|
+
error = queryResult.error;
|
|
155
205
|
refresh = queryResult.refresh;
|
|
156
206
|
refetch = queryResult.refetch;
|
|
207
|
+
async function goToPage(page) {
|
|
208
|
+
_page.value = page;
|
|
209
|
+
await withIntent("page");
|
|
210
|
+
return data.value;
|
|
211
|
+
}
|
|
157
212
|
loadNextFn = async () => {
|
|
158
213
|
const pageData = data.value?.page;
|
|
159
214
|
if (!pageData?.next) return void 0;
|
|
160
|
-
return await
|
|
215
|
+
return await goToPage(pageData.next);
|
|
161
216
|
};
|
|
162
217
|
loadPrevFn = async () => {
|
|
163
218
|
const pageData = data.value?.page;
|
|
164
219
|
if (!pageData?.prev) return void 0;
|
|
165
|
-
return await
|
|
220
|
+
return await goToPage(pageData.prev);
|
|
166
221
|
};
|
|
167
222
|
loadPageFn = async (page) => {
|
|
168
|
-
return await
|
|
223
|
+
return await goToPage(page);
|
|
169
224
|
};
|
|
170
225
|
}
|
|
171
226
|
if (!disableSearch) {
|
|
172
227
|
watch(searchTerm, () => {
|
|
173
|
-
if (searchTerm.value.trim().length > searchTermThreshold ||
|
|
228
|
+
if (searchTerm.value.trim().length > searchTermThreshold || _search.value !== void 0) {
|
|
174
229
|
triggerSearchFromInput();
|
|
175
230
|
}
|
|
231
|
+
commitSearchTerm();
|
|
176
232
|
});
|
|
177
233
|
}
|
|
178
234
|
watch(
|
|
@@ -180,10 +236,36 @@ export function useFronticSearch(name, params, options) {
|
|
|
180
236
|
(newData) => {
|
|
181
237
|
if (newData) {
|
|
182
238
|
updateState(newData);
|
|
239
|
+
emitListingResolved(newData, "initial");
|
|
183
240
|
}
|
|
184
241
|
},
|
|
185
242
|
{ immediate: true }
|
|
186
243
|
);
|
|
244
|
+
function emitListingResolved(response, trigger, changedFilter) {
|
|
245
|
+
const pageData = response.page;
|
|
246
|
+
const page = pageData?.current ?? 1;
|
|
247
|
+
const limit = pageData?.limit ?? 0;
|
|
248
|
+
const items = response.items ?? [];
|
|
249
|
+
const payload = {
|
|
250
|
+
listing: String(listingName),
|
|
251
|
+
cacheKey,
|
|
252
|
+
trigger,
|
|
253
|
+
...changedFilter && { changedFilter },
|
|
254
|
+
...searchState.value.active.search && { term: searchState.value.active.search },
|
|
255
|
+
filters: searchState.value.active.filter,
|
|
256
|
+
...searchState.value.active.sorting && { sort: searchState.value.active.sorting },
|
|
257
|
+
page,
|
|
258
|
+
resultCount: items.length,
|
|
259
|
+
totalCount: response.total ?? 0,
|
|
260
|
+
items: toAnalyticsItems(items, (page - 1) * limit),
|
|
261
|
+
version: FRONTIC_ANALYTICS_VERSION
|
|
262
|
+
};
|
|
263
|
+
if (trigger === "initial") {
|
|
264
|
+
emitOnce(nuxtApp, `${cacheKey}:initial`, "frontic:listing:resolved", payload);
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
emit(nuxtApp, "frontic:listing:resolved", payload);
|
|
268
|
+
}
|
|
187
269
|
function updateState(response) {
|
|
188
270
|
const rawFilters = response.filter;
|
|
189
271
|
const rawAggregation = response.aggregation;
|
|
@@ -228,18 +310,45 @@ export function useFronticSearch(name, params, options) {
|
|
|
228
310
|
filter: availableFilters.length,
|
|
229
311
|
sorting: availableSorts.length,
|
|
230
312
|
result: response.total ?? 0,
|
|
231
|
-
page: pageData?.
|
|
313
|
+
page: pageData?.last ?? 1
|
|
232
314
|
}
|
|
233
315
|
}
|
|
234
316
|
};
|
|
235
317
|
}
|
|
236
318
|
async function refreshListing() {
|
|
319
|
+
return runRefresh();
|
|
320
|
+
}
|
|
321
|
+
async function runRefresh(intent) {
|
|
237
322
|
await refresh();
|
|
238
323
|
if (data.value) {
|
|
239
324
|
updateState(data.value);
|
|
325
|
+
if (intent) emitListingResolved(data.value, intent.trigger, intent.changedFilter);
|
|
240
326
|
}
|
|
241
327
|
}
|
|
242
|
-
|
|
328
|
+
function resetPage() {
|
|
329
|
+
_page.value = void 0;
|
|
330
|
+
}
|
|
331
|
+
function withIntent(trigger, changedFilter) {
|
|
332
|
+
return runRefresh({ trigger, changedFilter });
|
|
333
|
+
}
|
|
334
|
+
const triggerSearchFromInput = useDebounceFn(() => {
|
|
335
|
+
const committed = searchTerm.value.trim().length > searchTermThreshold ? searchTerm.value : void 0;
|
|
336
|
+
if (committed === _search.value) return;
|
|
337
|
+
resetPage();
|
|
338
|
+
_search.value = committed;
|
|
339
|
+
return withIntent("term");
|
|
340
|
+
}, searchDebounce);
|
|
341
|
+
const commitSearchTerm = useDebounceFn(() => {
|
|
342
|
+
const term = searchTerm.value.trim();
|
|
343
|
+
if (term.length < searchMinChars) return;
|
|
344
|
+
emit(nuxtApp, "frontic:search:committed", {
|
|
345
|
+
term,
|
|
346
|
+
cacheKey,
|
|
347
|
+
resultCount: data.value?.items?.length ?? 0,
|
|
348
|
+
page: searchState.value.active.page,
|
|
349
|
+
version: FRONTIC_ANALYTICS_VERSION
|
|
350
|
+
});
|
|
351
|
+
}, searchCommitMs);
|
|
243
352
|
async function sortResult(sortBy) {
|
|
244
353
|
if (disableSorting) return;
|
|
245
354
|
if (!sortBy) {
|
|
@@ -253,30 +362,34 @@ export function useFronticSearch(name, params, options) {
|
|
|
253
362
|
_sortQuery.value = sortValue;
|
|
254
363
|
}
|
|
255
364
|
}
|
|
256
|
-
|
|
365
|
+
resetPage();
|
|
366
|
+
await withIntent("sort");
|
|
257
367
|
}
|
|
258
368
|
async function resetSorting() {
|
|
259
369
|
if (disableSorting) return;
|
|
260
370
|
_sortQuery.value = void 0;
|
|
261
|
-
|
|
371
|
+
resetPage();
|
|
372
|
+
await withIntent("sort");
|
|
262
373
|
}
|
|
263
374
|
async function filterResult(filterField, filterOptions) {
|
|
264
375
|
if (disableFilter) return;
|
|
376
|
+
resetPage();
|
|
265
377
|
if (filterOptions.length > 0 && isRangeFilterValue(filterOptions[0])) {
|
|
266
378
|
_rangeFilterList.value = manageRangeFilter(_rangeFilterList.value, filterField, filterOptions[0]);
|
|
267
|
-
await
|
|
379
|
+
await withIntent("filter", { field: filterField, value: filterOptions[0], action: "add" });
|
|
268
380
|
return;
|
|
269
381
|
}
|
|
270
382
|
const shouldUseOr = orFilter.includes(filterField);
|
|
271
383
|
const targetList = shouldUseOr ? _orFilterList : _andFilterList;
|
|
272
384
|
targetList.value = manageFilter(targetList.value, filterField, filterOptions);
|
|
273
|
-
await
|
|
385
|
+
await withIntent("filter", { field: filterField, value: filterOptions, action: "add" });
|
|
274
386
|
}
|
|
275
387
|
async function addFilterOption(filterField, filterOption) {
|
|
276
388
|
if (disableFilter) return;
|
|
389
|
+
resetPage();
|
|
277
390
|
if (isRangeFilterValue(filterOption)) {
|
|
278
391
|
_rangeFilterList.value = manageRangeFilter(_rangeFilterList.value, filterField, filterOption);
|
|
279
|
-
await
|
|
392
|
+
await withIntent("filter", { field: filterField, value: filterOption, action: "add" });
|
|
280
393
|
return;
|
|
281
394
|
}
|
|
282
395
|
const shouldUseOr = orFilter.includes(filterField);
|
|
@@ -284,18 +397,20 @@ export function useFronticSearch(name, params, options) {
|
|
|
284
397
|
const existingFilter = targetList.value.find((filter2) => filter2.field === filterField);
|
|
285
398
|
const currentValues = existingFilter ? Array.isArray(existingFilter.value) ? existingFilter.value : [existingFilter.value] : [];
|
|
286
399
|
targetList.value = manageFilter(targetList.value, filterField, [...currentValues, filterOption]);
|
|
287
|
-
await
|
|
400
|
+
await withIntent("filter", { field: filterField, value: filterOption, action: "add" });
|
|
288
401
|
}
|
|
289
402
|
async function removeFilterOption(filterField, filterOption) {
|
|
290
403
|
if (disableFilter) return;
|
|
404
|
+
resetPage();
|
|
291
405
|
_rangeFilterList.value = removeRangeFilter(_rangeFilterList.value, filterField);
|
|
292
406
|
const shouldUseOr = orFilter.includes(filterField);
|
|
293
407
|
const targetList = shouldUseOr ? _orFilterList : _andFilterList;
|
|
294
408
|
targetList.value = removeFilter(targetList.value, filterField, filterOption);
|
|
295
|
-
await
|
|
409
|
+
await withIntent("filter", { field: filterField, value: filterOption, action: "remove" });
|
|
296
410
|
}
|
|
297
411
|
async function resetFilter(filterField) {
|
|
298
412
|
if (disableFilter) return;
|
|
413
|
+
resetPage();
|
|
299
414
|
if (!filterField) {
|
|
300
415
|
_orFilterList.value = [];
|
|
301
416
|
_andFilterList.value = [];
|
|
@@ -306,19 +421,23 @@ export function useFronticSearch(name, params, options) {
|
|
|
306
421
|
const targetList = shouldUseOr ? _orFilterList : _andFilterList;
|
|
307
422
|
targetList.value = removeFilter(targetList.value, filterField);
|
|
308
423
|
}
|
|
309
|
-
await
|
|
424
|
+
await withIntent("filter", filterField ? { field: filterField, action: "remove" } : void 0);
|
|
310
425
|
}
|
|
311
426
|
async function resetSearch() {
|
|
312
427
|
searchTerm.value = "";
|
|
313
|
-
|
|
428
|
+
_search.value = void 0;
|
|
429
|
+
resetPage();
|
|
430
|
+
await withIntent("term");
|
|
314
431
|
}
|
|
315
432
|
async function reset() {
|
|
316
433
|
searchTerm.value = "";
|
|
434
|
+
_search.value = void 0;
|
|
317
435
|
_orFilterList.value = [];
|
|
318
436
|
_andFilterList.value = [];
|
|
319
437
|
_rangeFilterList.value = [];
|
|
320
438
|
_sortQuery.value = void 0;
|
|
321
|
-
|
|
439
|
+
_page.value = void 0;
|
|
440
|
+
await withIntent("reset");
|
|
322
441
|
}
|
|
323
442
|
return {
|
|
324
443
|
// ========== State ==========
|
|
@@ -327,9 +446,23 @@ export function useFronticSearch(name, params, options) {
|
|
|
327
446
|
*/
|
|
328
447
|
result: data,
|
|
329
448
|
/**
|
|
330
|
-
*
|
|
449
|
+
* Data status: 'pending' | 'error' | 'success'
|
|
331
450
|
*/
|
|
332
451
|
status,
|
|
452
|
+
/**
|
|
453
|
+
* Network status: 'idle' | 'loading'. Together with `status` this
|
|
454
|
+
* distinguishes a first load (`pending`) from a background refresh
|
|
455
|
+
* (`success` + 'loading').
|
|
456
|
+
*/
|
|
457
|
+
asyncStatus,
|
|
458
|
+
/**
|
|
459
|
+
* Whether a request is in flight (shortcut for `asyncStatus === 'loading'`)
|
|
460
|
+
*/
|
|
461
|
+
isLoading,
|
|
462
|
+
/**
|
|
463
|
+
* Error of the last fetch, null while pending or after success
|
|
464
|
+
*/
|
|
465
|
+
error,
|
|
333
466
|
/**
|
|
334
467
|
* Current search state including active filters, sorts, pagination, and available options.
|
|
335
468
|
* Use this to build filter/sort UI components.
|