@frontic/nuxt 0.8.3 → 1.0.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.
Files changed (52) hide show
  1. package/README.md +30 -2
  2. package/dist/module.d.mts +2 -132
  3. package/dist/module.json +2 -2
  4. package/dist/module.mjs +63 -7
  5. package/dist/runtime/composables/analytics.d.ts +82 -0
  6. package/dist/runtime/composables/analytics.js +38 -0
  7. package/dist/runtime/composables/blocks.d.ts +24 -22
  8. package/dist/runtime/composables/blocks.js +16 -16
  9. package/dist/runtime/composables/index.d.ts +1 -1
  10. package/dist/runtime/composables/listings.d.ts +22 -13
  11. package/dist/runtime/composables/listings.js +24 -25
  12. package/dist/runtime/composables/pages.d.ts +21 -18
  13. package/dist/runtime/composables/pages.js +15 -16
  14. package/dist/runtime/composables/trees.d.ts +24 -22
  15. package/dist/runtime/composables/trees.js +15 -25
  16. package/dist/runtime/composables/useFronticBlock.d.ts +22 -13
  17. package/dist/runtime/composables/useFronticBlock.js +19 -2
  18. package/dist/runtime/composables/useFronticClient.d.ts +6 -3
  19. package/dist/runtime/composables/useFronticContext.d.ts +12 -0
  20. package/dist/runtime/composables/useFronticContext.js +71 -9
  21. package/dist/runtime/composables/useFronticListing.d.ts +23 -19
  22. package/dist/runtime/composables/useFronticListing.js +22 -3
  23. package/dist/runtime/composables/useFronticPage.d.ts +35 -16
  24. package/dist/runtime/composables/useFronticPage.js +36 -7
  25. package/dist/runtime/composables/useFronticSearch.d.ts +4 -96
  26. package/dist/runtime/composables/useFronticSearch.js +179 -51
  27. package/dist/runtime/composables/useFronticTree.d.ts +23 -14
  28. package/dist/runtime/composables/useFronticTree.js +19 -2
  29. package/dist/runtime/server/plugins/fetch-secret.d.ts +2 -0
  30. package/dist/runtime/server/plugins/fetch-secret.js +20 -0
  31. package/dist/runtime/types/client.d.ts +34 -0
  32. package/dist/runtime/types/client.js +0 -0
  33. package/dist/runtime/types/index.d.ts +8 -3
  34. package/dist/runtime/types/listing.d.ts +113 -251
  35. package/dist/runtime/types/options.d.ts +139 -0
  36. package/dist/runtime/types/options.js +0 -0
  37. package/dist/runtime/types/query.d.ts +88 -0
  38. package/dist/runtime/types/query.js +0 -0
  39. package/dist/runtime/types/registry.d.ts +272 -0
  40. package/dist/runtime/types/registry.js +0 -0
  41. package/dist/runtime/types/search.d.ts +56 -36
  42. package/dist/runtime/utils/errors.d.ts +22 -0
  43. package/dist/runtime/utils/errors.js +3 -0
  44. package/dist/runtime/utils/index.d.ts +2 -0
  45. package/dist/runtime/utils/index.js +2 -0
  46. package/dist/runtime/utils/query-keys.d.ts +27 -0
  47. package/dist/runtime/utils/query-keys.js +18 -0
  48. package/dist/runtime/utils/state-helpers.d.ts +3 -6
  49. package/dist/runtime/utils/state-helpers.js +7 -8
  50. package/dist/types.d.mts +2 -2
  51. package/package.json +15 -15
  52. package/dist/runtime/frontic-stack.d.ts +0 -168
@@ -1,19 +1,63 @@
1
- import { useRuntimeConfig, useCookie } from "nuxt/app";
2
- import { ref, readonly, onMounted } from "vue";
1
+ import { useRuntimeConfig, useCookie, useState, useNuxtApp } from "nuxt/app";
2
+ import { readonly, onMounted, getCurrentInstance } from "vue";
3
+ import { useQueryCache } from "@pinia/colada";
3
4
  import { useFronticClient } from "./useFronticClient.js";
5
+ import { BLOCK_QUERY_KEYS } from "./blocks.js";
6
+ import { PAGE_QUERY_KEYS } from "./pages.js";
7
+ import { LISTING_QUERY_KEYS } from "./listings.js";
8
+ import { TREE_QUERY_KEYS } from "./trees.js";
9
+ function useContextRuntime() {
10
+ const nuxtApp = useNuxtApp();
11
+ return nuxtApp._fronticContext ??= {};
12
+ }
13
+ function callContextHook(nuxtApp, name, context) {
14
+ void nuxtApp.callHook(name, context);
15
+ }
4
16
  export function useFronticContext(options) {
5
17
  const client = useFronticClient();
6
18
  const config = useRuntimeConfig();
19
+ const nuxtApp = useNuxtApp();
20
+ const queryCache = useQueryCache();
7
21
  const fronticConfig = config.public.frontic;
8
22
  const cookieName = options?.cookieName ?? fronticConfig?.contextCookieName ?? "fs-context";
9
23
  const cookieMaxAge = options?.cookieMaxAge ?? fronticConfig?.contextCookieMaxAge ?? 60 * 60 * 24 * 365;
10
24
  const disableContext = options?.disableContext ?? fronticConfig?.disableContext ?? false;
11
25
  const tokenCookie = disableContext ? null : useCookie(cookieName, { maxAge: cookieMaxAge });
12
- const token = ref(tokenCookie?.value ?? null);
13
- const contexts = ref([]);
14
- const current = ref(null);
15
- const isLoading = ref(false);
16
- async function fetchContexts() {
26
+ const stateKey = (name) => `frontic:context:${cookieName}:${name}`;
27
+ const token = useState(stateKey("token"), () => tokenCookie?.value ?? null);
28
+ const contexts = useState(stateKey("list"), () => []);
29
+ const current = useState(stateKey("current"), () => null);
30
+ const isLoading = useState(stateKey("loading"), () => false);
31
+ if (tokenCookie?.value && !token.value) {
32
+ token.value = tokenCookie.value;
33
+ }
34
+ const runtime = useContextRuntime();
35
+ function broadcast(context) {
36
+ if (!context) return "same";
37
+ const signature = `${context.region}|${context.locale}|${context.scope}`;
38
+ if (runtime.lastBroadcast === signature) return "same";
39
+ const isFirst = runtime.lastBroadcast === void 0;
40
+ runtime.lastBroadcast = signature;
41
+ if (isFirst) callContextHook(nuxtApp, "frontic:context:ready", context);
42
+ callContextHook(nuxtApp, "frontic:context:updated", context);
43
+ return isFirst ? "first" : "changed";
44
+ }
45
+ async function invalidateFronticData() {
46
+ await Promise.all([
47
+ queryCache.invalidateQueries({ key: BLOCK_QUERY_KEYS.root }),
48
+ queryCache.invalidateQueries({ key: PAGE_QUERY_KEYS.root }),
49
+ queryCache.invalidateQueries({ key: LISTING_QUERY_KEYS.root }),
50
+ queryCache.invalidateQueries({ key: TREE_QUERY_KEYS.root })
51
+ ]);
52
+ }
53
+ function persistToken(newToken) {
54
+ if (!newToken || newToken === token.value) return;
55
+ token.value = newToken;
56
+ if (tokenCookie) {
57
+ tokenCookie.value = newToken;
58
+ }
59
+ }
60
+ async function loadContexts() {
17
61
  isLoading.value = true;
18
62
  try {
19
63
  const [contextList, newToken] = await client.contextList(token.value ?? void 0);
@@ -24,6 +68,7 @@ export function useFronticContext(options) {
24
68
  }
25
69
  if (newToken) {
26
70
  current.value = await client.context(newToken);
71
+ if (broadcast(current.value) === "changed") await invalidateFronticData();
27
72
  }
28
73
  } catch (error) {
29
74
  console.error("[@frontic/nuxt] Failed to fetch contexts:", error);
@@ -31,6 +76,13 @@ export function useFronticContext(options) {
31
76
  isLoading.value = false;
32
77
  }
33
78
  }
79
+ function fetchContexts() {
80
+ if (runtime.inflight) return runtime.inflight;
81
+ runtime.inflight = loadContexts().finally(() => {
82
+ runtime.inflight = void 0;
83
+ });
84
+ return runtime.inflight;
85
+ }
34
86
  async function update(context) {
35
87
  if (!token.value) {
36
88
  await fetchContexts();
@@ -42,14 +94,24 @@ export function useFronticContext(options) {
42
94
  isLoading.value = true;
43
95
  try {
44
96
  current.value = await client.contextUpdate(context, token.value);
97
+ persistToken(current.value?.token);
98
+ if (broadcast(current.value) === "changed") await invalidateFronticData();
45
99
  } catch (error) {
46
100
  console.error("[@frontic/nuxt] Failed to update context:", error);
47
101
  } finally {
48
102
  isLoading.value = false;
49
103
  }
50
104
  }
51
- if (import.meta.client && !disableContext) {
52
- onMounted(fetchContexts);
105
+ if (current.value) {
106
+ broadcast(current.value);
107
+ }
108
+ if (import.meta.client && !disableContext && !runtime.autoFetched) {
109
+ runtime.autoFetched = true;
110
+ if (getCurrentInstance()) {
111
+ onMounted(fetchContexts);
112
+ } else {
113
+ void fetchContexts();
114
+ }
53
115
  }
54
116
  return {
55
117
  /**
@@ -1,13 +1,8 @@
1
1
  import type { Ref, MaybeRef } from 'vue';
2
- import type { Listings, ListingParameters, ListingQueryFilters, ListingQuerySorts, Responses } from '@frontic/stack/generated-types';
3
- import type { Query } from '@frontic/stack/query-types';
4
- import type { FronticRequestOptions } from '../types/listing.js';
5
- /**
6
- * Resolves to the typed `Query<filter, sort>` shape for a given listing name.
7
- * Falls back to `Query<unknown, unknown>` when the listing isn't in the generated maps.
8
- */
9
- export type ListingQuery<TListing extends keyof Listings = keyof Listings> = Query<TListing extends keyof ListingQueryFilters ? ListingQueryFilters[TListing] : unknown, TListing extends keyof ListingQuerySorts ? ListingQuerySorts[TListing] : unknown>;
10
- export interface UseFronticListingOptions<TListing extends keyof Listings = keyof Listings> extends FronticRequestOptions {
2
+ import type { ListingName, ResponseOf } from '../types/registry.js';
3
+ import type { FronticRequestOptions, FronticQueryOptions, ListingArgs, ListingQuery } from '../types/listing.js';
4
+ export type { ListingQuery } from '../types/listing.js';
5
+ export interface UseFronticListingOptions<TListing extends ListingName = ListingName> extends FronticRequestOptions, FronticQueryOptions {
11
6
  /**
12
7
  * Query options forwarded to the Fetch API — `filter`, `sort`, `search`, `limit`, `page`.
13
8
  * Same shape as `client.listing(name, params, { query })`.
@@ -16,11 +11,6 @@ export interface UseFronticListingOptions<TListing extends keyof Listings = keyo
16
11
  * use useFronticSearch instead.
17
12
  */
18
13
  query?: MaybeRef<ListingQuery<TListing> | undefined>;
19
- /**
20
- * Cache stale time in milliseconds
21
- * @default 300000 (5 minutes)
22
- */
23
- staleTime?: number;
24
14
  }
25
15
  /**
26
16
  * Fetch a Frontic listing by name and parameters
@@ -55,21 +45,35 @@ export interface UseFronticListingOptions<TListing extends keyof Listings = keyo
55
45
  * })
56
46
  * ```
57
47
  */
58
- export declare function useFronticListing<T extends keyof Listings>(name: T, params: MaybeRef<T extends keyof ListingParameters ? ListingParameters[T] : Record<string, unknown>>, options?: UseFronticListingOptions<T>): {
48
+ export declare function useFronticListing<T extends ListingName>(name: T, ...args: ListingArgs<T, UseFronticListingOptions<T>>): {
59
49
  /**
60
50
  * The listing data, typed based on the listing name
61
51
  */
62
- listing: Ref<Responses[T] | undefined>;
52
+ listing: Ref<ResponseOf<T> | undefined>;
63
53
  /**
64
- * Query status: 'pending' | 'error' | 'success'
54
+ * Data status: 'pending' | 'error' | 'success'
65
55
  */
66
56
  status: import("vue").ShallowRef<import("@pinia/colada").DataStateStatus>;
57
+ /**
58
+ * Network status: 'idle' | 'loading'. Together with `status` this
59
+ * distinguishes a first load (`pending`) from a background refresh
60
+ * (`success` + 'loading').
61
+ */
62
+ asyncStatus: import("vue").ComputedRef<import("@pinia/colada").AsyncStatus>;
63
+ /**
64
+ * Whether a request is in flight (shortcut for `asyncStatus === 'loading'`)
65
+ */
66
+ isLoading: import("vue").ShallowRef<boolean>;
67
+ /**
68
+ * Error of the last fetch, null while pending or after success
69
+ */
70
+ error: import("vue").ShallowRef<Error | null>;
67
71
  /**
68
72
  * Manually refresh the listing data (uses cache if valid)
69
73
  */
70
- refresh: (throwOnError?: boolean) => Promise<import("@pinia/colada").DataState<unknown, Error, undefined>>;
74
+ refresh: (throwOnError?: boolean) => Promise<import("@pinia/colada").DataState<ResponseOf<T>, Error, undefined>>;
71
75
  /**
72
76
  * Force a fresh fetch of the listing data, bypassing cache
73
77
  */
74
- refetch: (throwOnError?: boolean) => Promise<import("@pinia/colada").DataState<unknown, Error, undefined>>;
78
+ refetch: (throwOnError?: boolean) => Promise<import("@pinia/colada").DataState<ResponseOf<T>, Error, undefined>>;
75
79
  };
@@ -1,7 +1,9 @@
1
1
  import { useQuery } from "@pinia/colada";
2
2
  import { fetchListingQuery } from "./listings.js";
3
3
  import { useFronticClient } from "./useFronticClient.js";
4
- export function useFronticListing(name, params, options) {
4
+ export function useFronticListing(name, ...args) {
5
+ const [rawParams, options] = args;
6
+ const params = rawParams ?? {};
5
7
  const _client = useFronticClient();
6
8
  const listingQuery = fetchListingQuery({
7
9
  name,
@@ -11,18 +13,35 @@ export function useFronticListing(name, params, options) {
11
13
  contextDomain: options?.contextDomain,
12
14
  requestUrl: options?.requestUrl,
13
15
  staleTime: options?.staleTime,
16
+ gcTime: options?.gcTime,
17
+ enabled: options?.enabled,
18
+ keepPreviousData: options?.keepPreviousData,
14
19
  _client
15
20
  });
16
- const { data, status, refresh, refetch } = useQuery(listingQuery);
21
+ const { data, status, asyncStatus, isLoading, error, refresh, refetch } = useQuery(listingQuery);
17
22
  return {
18
23
  /**
19
24
  * The listing data, typed based on the listing name
20
25
  */
21
26
  listing: data,
22
27
  /**
23
- * Query status: 'pending' | 'error' | 'success'
28
+ * Data status: 'pending' | 'error' | 'success'
24
29
  */
25
30
  status,
31
+ /**
32
+ * Network status: 'idle' | 'loading'. Together with `status` this
33
+ * distinguishes a first load (`pending`) from a background refresh
34
+ * (`success` + 'loading').
35
+ */
36
+ asyncStatus,
37
+ /**
38
+ * Whether a request is in flight (shortcut for `asyncStatus === 'loading'`)
39
+ */
40
+ isLoading,
41
+ /**
42
+ * Error of the last fetch, null while pending or after success
43
+ */
44
+ error,
26
45
  /**
27
46
  * Manually refresh the listing data (uses cache if valid)
28
47
  */
@@ -1,6 +1,7 @@
1
- import type { Ref, MaybeRef, ComputedRef } from 'vue';
2
- import type { FronticRequestOptions } from '../types/listing.js';
3
- export interface UseFronticPageOptions extends FronticRequestOptions {
1
+ import type { Ref, MaybeRef } from 'vue';
2
+ import type { Page } from '../types/registry.js';
3
+ import type { FronticRequestOptions, FronticQueryOptions } from '../types/listing.js';
4
+ export interface UseFronticPageOptions extends FronticRequestOptions, FronticQueryOptions {
4
5
  /**
5
6
  * Automatically redirect on 301 responses
6
7
  * @default true
@@ -12,10 +13,13 @@ export interface UseFronticPageOptions extends FronticRequestOptions {
12
13
  */
13
14
  throwOn404?: boolean;
14
15
  /**
15
- * Cache stale time in milliseconds
16
- * @default 300000 (5 minutes)
16
+ * Keep the previous page rendered while the next one resolves.
17
+ *
18
+ * Defaults to `false` here, unlike every other composable: the value held
19
+ * across a key change is a different page, not the same one reloading.
20
+ * @default false
17
21
  */
18
- staleTime?: number;
22
+ keepPreviousData?: boolean;
19
23
  }
20
24
  /**
21
25
  * Fetch a Frontic page by slug with automatic redirect handling
@@ -60,37 +64,52 @@ export declare function useFronticPage(slug?: MaybeRef<string>, options?: UseFro
60
64
  /**
61
65
  * Page data (the actual content block data)
62
66
  */
63
- data: ComputedRef<Page["data"] | undefined>;
67
+ data: import("vue").ComputedRef<Record<string, unknown> | undefined>;
64
68
  /**
65
- * Page type (e.g., 'ProductCategory', 'Product', 'Brand')
69
+ * Page type, as configured in Frontic (e.g. 'ProductPage', 'CategoryDetail').
70
+ * Resolves to the tenant's own literal union, so it narrows `data`.
66
71
  */
67
- type: ComputedRef<string | undefined>;
72
+ type: import("vue").ComputedRef<string | undefined>;
68
73
  /**
69
74
  * Block name to render (e.g., 'CategoryFull', 'ProductFull')
70
75
  */
71
- block: ComputedRef<string | undefined>;
76
+ block: import("vue").ComputedRef<string | undefined>;
72
77
  /**
73
78
  * Route metadata (code, redirect, alternates, context)
74
79
  */
75
- route: ComputedRef<RouteMeta | undefined>;
80
+ route: import("vue").ComputedRef<import("../types/registry.js").DefaultRouteMeta | undefined>;
76
81
  /**
77
82
  * Alternate URLs for hreflang tags
78
83
  */
79
- alternates: ComputedRef<AlternateRoute[] | undefined>;
84
+ alternates: import("vue").ComputedRef<import("../types/registry.js").DefaultAlternateRoute[] | undefined>;
80
85
  /**
81
86
  * Suggested route when context changes (for locale switching)
82
87
  */
83
- suggested: ComputedRef<AlternateRoute | undefined>;
88
+ suggested: import("vue").ComputedRef<import("../types/registry.js").DefaultAlternateRoute | undefined>;
84
89
  /**
85
- * Query status: 'pending' | 'error' | 'success'
90
+ * Data status: 'pending' | 'error' | 'success'
86
91
  */
87
92
  status: import("vue").ShallowRef<import("@pinia/colada").DataStateStatus>;
93
+ /**
94
+ * Network status: 'idle' | 'loading'. Together with `status` this
95
+ * distinguishes a first load (`pending`) from a background refresh
96
+ * (`success` + 'loading').
97
+ */
98
+ asyncStatus: import("vue").ComputedRef<import("@pinia/colada").AsyncStatus>;
99
+ /**
100
+ * Whether a request is in flight (shortcut for `asyncStatus === 'loading'`)
101
+ */
102
+ isLoading: import("vue").ShallowRef<boolean>;
103
+ /**
104
+ * Error of the last fetch, null while pending or after success
105
+ */
106
+ error: import("vue").ShallowRef<Error | null>;
88
107
  /**
89
108
  * Manually refresh the page data (uses cache if valid)
90
109
  */
91
- refresh: (throwOnError?: boolean) => Promise<import("@pinia/colada").DataState<Page, Error, undefined>>;
110
+ refresh: (throwOnError?: boolean) => Promise<import("@pinia/colada").DataState<import("../types/registry.js").DefaultPage, Error, undefined>>;
92
111
  /**
93
112
  * Force a fresh fetch of the page data, bypassing cache
94
113
  */
95
- refetch: (throwOnError?: boolean) => Promise<import("@pinia/colada").DataState<Page, Error, undefined>>;
114
+ refetch: (throwOnError?: boolean) => Promise<import("@pinia/colada").DataState<import("../types/registry.js").DefaultPage, Error, undefined>>;
96
115
  };
@@ -1,10 +1,13 @@
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 { emitOnce, bindRouteReset, FRONTIC_ANALYTICS_VERSION } from "./analytics.js";
6
7
  import { resolveI18nContextDomain } 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;
@@ -29,9 +32,15 @@ export function useFronticPage(slug, options) {
29
32
  contextDomain: options?.contextDomain,
30
33
  requestUrl: options?.requestUrl,
31
34
  staleTime: options?.staleTime,
35
+ gcTime: options?.gcTime,
36
+ enabled: options?.enabled,
37
+ // Page navigation swaps the entity, not the query over one entity, so a
38
+ // held value is a different page under the new URL. It would also put the
39
+ // previous page's route in front of the redirect/404 watcher below.
40
+ keepPreviousData: options?.keepPreviousData ?? false,
32
41
  _client
33
42
  });
34
- const { data, status, refresh, refetch } = useQuery(pageQuery);
43
+ const { data, status, asyncStatus, isLoading, error, refresh, refetch } = useQuery(pageQuery);
35
44
  const redirectOn301 = options?.redirectOn301 ?? fronticConfig?.redirectOn301 ?? true;
36
45
  const throwOn404 = options?.throwOn404 ?? fronticConfig?.throwOn404 ?? true;
37
46
  watch(
@@ -43,6 +52,11 @@ export function useFronticPage(slug, options) {
43
52
  } else if (throwOn404 && page.route.code === 404) {
44
53
  throw createError({ statusCode: 404, statusMessage: "Page not found" });
45
54
  }
55
+ if (page.route.code !== 200 || !("type" in page)) return;
56
+ emitOnce(nuxtApp, `page:resolved:${pageSlug.value}`, "frontic:page:resolved", {
57
+ page,
58
+ version: FRONTIC_ANALYTICS_VERSION
59
+ });
46
60
  },
47
61
  { immediate: true }
48
62
  );
@@ -54,15 +68,16 @@ export function useFronticPage(slug, options) {
54
68
  /**
55
69
  * Page data (the actual content block data)
56
70
  */
57
- data: computed(() => data.value?.data),
71
+ data: computed(() => data.value && "data" in data.value ? data.value.data : void 0),
58
72
  /**
59
- * Page type (e.g., 'ProductCategory', 'Product', 'Brand')
73
+ * Page type, as configured in Frontic (e.g. 'ProductPage', 'CategoryDetail').
74
+ * Resolves to the tenant's own literal union, so it narrows `data`.
60
75
  */
61
- type: computed(() => data.value?.type),
76
+ type: computed(() => data.value && "type" in data.value ? data.value.type : void 0),
62
77
  /**
63
78
  * Block name to render (e.g., 'CategoryFull', 'ProductFull')
64
79
  */
65
- block: computed(() => data.value?.block),
80
+ block: computed(() => data.value && "block" in data.value ? data.value.block : void 0),
66
81
  /**
67
82
  * Route metadata (code, redirect, alternates, context)
68
83
  */
@@ -76,9 +91,23 @@ export function useFronticPage(slug, options) {
76
91
  */
77
92
  suggested: computed(() => data.value?.route?.context?.suggested),
78
93
  /**
79
- * Query status: 'pending' | 'error' | 'success'
94
+ * Data status: 'pending' | 'error' | 'success'
80
95
  */
81
96
  status,
97
+ /**
98
+ * Network status: 'idle' | 'loading'. Together with `status` this
99
+ * distinguishes a first load (`pending`) from a background refresh
100
+ * (`success` + 'loading').
101
+ */
102
+ asyncStatus,
103
+ /**
104
+ * Whether a request is in flight (shortcut for `asyncStatus === 'loading'`)
105
+ */
106
+ isLoading,
107
+ /**
108
+ * Error of the last fetch, null while pending or after success
109
+ */
110
+ error,
82
111
  /**
83
112
  * Manually refresh the page data (uses cache if valid)
84
113
  */
@@ -6,10 +6,9 @@
6
6
  *
7
7
  * For simple listing access without search features, use useFronticListing instead.
8
8
  */
9
- import { type ShallowRef } from 'vue';
10
- import type { MaybeRef } from 'vue';
11
- import type { Listings, ListingParameters, ListingQueryFilters, ListingQuerySorts, Responses } from '@frontic/stack/generated-types';
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 keyof Listings, TFilters extends Record<string, unknown> = TSearch extends keyof ListingQueryFilters ? ListingQueryFilters[TSearch] : Record<string, unknown>, TSorts extends Record<string, unknown> = TSearch extends keyof ListingQuerySorts ? ListingQuerySorts[TSearch] : Record<string, unknown>>(name: TSearch, params: MaybeRef<TSearch extends keyof ListingParameters ? ListingParameters[TSearch] : Record<string, unknown>>, options?: UseFronticSearchOptions<TSearch, TFilters, TSorts>): {
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>;