@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.
Files changed (64) hide show
  1. package/README.md +65 -4
  2. package/dist/module.d.mts +2 -132
  3. package/dist/module.json +2 -2
  4. package/dist/module.mjs +130 -15
  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 +25 -4
  18. package/dist/runtime/composables/useFronticClient.d.ts +9 -28
  19. package/dist/runtime/composables/useFronticClient.js +59 -48
  20. package/dist/runtime/composables/useFronticContext.d.ts +12 -0
  21. package/dist/runtime/composables/useFronticContext.js +71 -9
  22. package/dist/runtime/composables/useFronticListing.d.ts +23 -19
  23. package/dist/runtime/composables/useFronticListing.js +28 -5
  24. package/dist/runtime/composables/useFronticPage.d.ts +35 -16
  25. package/dist/runtime/composables/useFronticPage.js +42 -20
  26. package/dist/runtime/composables/useFronticSearch.d.ts +4 -96
  27. package/dist/runtime/composables/useFronticSearch.js +193 -60
  28. package/dist/runtime/composables/useFronticTree.d.ts +23 -14
  29. package/dist/runtime/composables/useFronticTree.js +25 -5
  30. package/dist/runtime/server/api/frontic/[...path].js +48 -10
  31. package/dist/runtime/server/middleware/sitemap.d.ts +2 -0
  32. package/dist/runtime/server/middleware/sitemap.js +55 -0
  33. package/dist/runtime/server/plugins/fetch-secret.d.ts +2 -0
  34. package/dist/runtime/server/plugins/fetch-secret.js +20 -0
  35. package/dist/runtime/types/client.d.ts +36 -0
  36. package/dist/runtime/types/client.js +0 -0
  37. package/dist/runtime/types/errors.d.ts +47 -0
  38. package/dist/runtime/types/errors.js +1 -0
  39. package/dist/runtime/types/index.d.ts +9 -3
  40. package/dist/runtime/types/listing.d.ts +113 -251
  41. package/dist/runtime/types/options.d.ts +169 -0
  42. package/dist/runtime/types/options.js +0 -0
  43. package/dist/runtime/types/query.d.ts +88 -0
  44. package/dist/runtime/types/query.js +0 -0
  45. package/dist/runtime/types/registry.d.ts +272 -0
  46. package/dist/runtime/types/registry.js +0 -0
  47. package/dist/runtime/types/search.d.ts +90 -38
  48. package/dist/runtime/utils/describe-error.d.ts +38 -0
  49. package/dist/runtime/utils/describe-error.js +38 -0
  50. package/dist/runtime/utils/errors.d.ts +22 -0
  51. package/dist/runtime/utils/errors.js +3 -0
  52. package/dist/runtime/utils/filter-helpers.d.ts +13 -0
  53. package/dist/runtime/utils/filter-helpers.js +14 -0
  54. package/dist/runtime/utils/index.d.ts +2 -0
  55. package/dist/runtime/utils/index.js +2 -0
  56. package/dist/runtime/utils/query-keys.d.ts +27 -0
  57. package/dist/runtime/utils/query-keys.js +18 -0
  58. package/dist/runtime/utils/resolve-context-domain.d.ts +14 -0
  59. package/dist/runtime/utils/resolve-context-domain.js +9 -1
  60. package/dist/runtime/utils/state-helpers.d.ts +3 -6
  61. package/dist/runtime/utils/state-helpers.js +7 -8
  62. package/dist/types.d.mts +2 -2
  63. package/package.json +15 -15
  64. package/dist/runtime/frontic-stack.d.ts +0 -168
@@ -1,27 +1,48 @@
1
1
  import { useQuery } from "@pinia/colada";
2
+ import { computed, toValue } from "vue";
3
+ import { resolveContextDomain } from "../utils/resolve-context-domain.js";
2
4
  import { fetchBlockQuery } from "./blocks.js";
3
5
  import { useFronticClient } from "./useFronticClient.js";
4
6
  export function useFronticBlock(name, key, options) {
5
- const _client = useFronticClient();
7
+ const _client = useFronticClient({ contextDomain: false });
8
+ const autoContextDomain = resolveContextDomain();
9
+ const resolvedContextDomain = computed(() => toValue(options?.contextDomain) ?? autoContextDomain);
6
10
  const blockQuery = fetchBlockQuery({
7
11
  name,
8
12
  key,
9
13
  contextKey: options?.contextKey,
10
- contextDomain: options?.contextDomain,
14
+ contextDomain: resolvedContextDomain,
11
15
  requestUrl: options?.requestUrl,
12
16
  staleTime: options?.staleTime,
17
+ gcTime: options?.gcTime,
18
+ enabled: options?.enabled,
19
+ keepPreviousData: options?.keepPreviousData,
13
20
  _client
14
21
  });
15
- const { data, status, refresh, refetch } = useQuery(blockQuery);
22
+ const { data, status, asyncStatus, isLoading, error, refresh, refetch } = useQuery(blockQuery);
16
23
  return {
17
24
  /**
18
25
  * The block data, typed based on the block name
19
26
  */
20
27
  block: data,
21
28
  /**
22
- * Query status: 'pending' | 'error' | 'success'
29
+ * Data status: 'pending' | 'error' | 'success'
23
30
  */
24
31
  status,
32
+ /**
33
+ * Network status: 'idle' | 'loading'. Together with `status` this
34
+ * distinguishes a first load (`pending`) from a background refresh
35
+ * (`success` + 'loading').
36
+ */
37
+ asyncStatus,
38
+ /**
39
+ * Whether a request is in flight (shortcut for `asyncStatus === 'loading'`)
40
+ */
41
+ isLoading,
42
+ /**
43
+ * Error of the last fetch, null while pending or after success
44
+ */
45
+ error,
25
46
  /**
26
47
  * Manually refresh the block data (uses cache if valid)
27
48
  */
@@ -1,4 +1,5 @@
1
- import type { FrontstackClient } from '@frontic/stack/generated-client';
1
+ import type { FronticClient } from '../types/client.js';
2
+ export type { FronticClient } from '../types/client.js';
2
3
  export interface UseFronticClientOptions {
3
4
  /**
4
5
  * Override proxy behavior
@@ -9,10 +10,10 @@ export interface UseFronticClientOptions {
9
10
  */
10
11
  proxy?: boolean | string;
11
12
  /**
12
- * Override contextDomain auto-detection
13
- * - `false`: Disable auto-injection
13
+ * Override contextDomain resolution
14
+ * - `false`: Send no domain
14
15
  * - `string`: Use specific domain
15
- * - `undefined`: Auto-detect from request URL
16
+ * - `undefined`: Resolve from module config, or the active i18n locale when it is `true`
16
17
  */
17
18
  contextDomain?: false | string;
18
19
  /**
@@ -27,30 +28,10 @@ export interface UseFronticClientOptions {
27
28
  * - `string`: Use specific secret
28
29
  * - `undefined`: Use module config fetchApiSecret (on server) or none (on client)
29
30
  *
30
- * Note: On client-side, secrets are injected by the proxy server instead
31
+ * Requests are authenticated server-side without this: the proxy injects the
32
+ * secret for browser traffic, a Nitro plugin for SSR. Generated clients
33
+ * without `fs-secret` support drop this option.
31
34
  */
32
35
  secret?: string;
33
36
  }
34
- /**
35
- * Creates an SSR-aware Frontic client with automatic proxy and context injection
36
- *
37
- * @param options - Optional configuration to override defaults
38
- * @returns Type-safe Frontic client instance
39
- *
40
- * @example
41
- * ```ts
42
- * // Default behavior (proxy on client, auto contextDomain)
43
- * const client = useFronticClient()
44
- * const product = await client.block('ProductCard', productId)
45
- *
46
- * // Force direct API (bypass proxy)
47
- * const directClient = useFronticClient({ proxy: false })
48
- *
49
- * // Custom proxy path
50
- * const customClient = useFronticClient({ proxy: '/api/custom-frontic' })
51
- *
52
- * // Disable contextDomain auto-injection
53
- * const noContextClient = useFronticClient({ contextDomain: false })
54
- * ```
55
- */
56
- export declare function useFronticClient(options?: UseFronticClientOptions): FrontstackClient;
37
+ export declare function useFronticClient(options?: UseFronticClientOptions): FronticClient;
@@ -1,6 +1,13 @@
1
- import { useRuntimeConfig, useRequestURL } from "nuxt/app";
1
+ import { useRuntimeConfig, useRequestURL, useNuxtApp } from "nuxt/app";
2
2
  import { createClient } from "@frontic/stack/generated-client";
3
- import { resolveI18nContextDomain } from "../utils/resolve-context-domain.js";
3
+ import { resolveContextDomain } from "../utils/resolve-context-domain.js";
4
+ import { toFetchErrorDetail } from "../utils/describe-error.js";
5
+ import { FRONTIC_FETCH_ERROR_VERSION } from "../types/errors.js";
6
+ function withDefaults(defaults, reqOptions) {
7
+ if (!reqOptions) return defaults;
8
+ const provided = Object.fromEntries(Object.entries(reqOptions).filter(([, value]) => value !== void 0));
9
+ return { ...defaults, ...provided };
10
+ }
4
11
  export function useFronticClient(options) {
5
12
  const config = useRuntimeConfig();
6
13
  const fronticConfig = config.public.frontic;
@@ -11,14 +18,6 @@ export function useFronticClient(options) {
11
18
  if (typeof options?.proxy === "string") return options.proxy;
12
19
  return import.meta.server ? void 0 : fronticConfig?.proxyPath;
13
20
  };
14
- const resolveContextDomain = () => {
15
- if (options?.contextDomain === false) return void 0;
16
- if (typeof options?.contextDomain === "string") return options.contextDomain;
17
- const configDomain = fronticConfig?.contextDomain;
18
- if (typeof configDomain === "string") return configDomain;
19
- if (configDomain === true) return resolveI18nContextDomain();
20
- return void 0;
21
- };
22
21
  const resolveRequestUrl = () => {
23
22
  if (options?.requestUrl === false) return void 0;
24
23
  if (typeof options?.requestUrl === "string") return options.requestUrl;
@@ -31,49 +30,61 @@ export function useFronticClient(options) {
31
30
  }
32
31
  return void 0;
33
32
  };
33
+ const nuxtApp = useNuxtApp();
34
34
  const proxyUrl = resolveProxyUrl();
35
- const contextDomain = resolveContextDomain();
35
+ const contextDomain = resolveContextDomain(options?.contextDomain);
36
36
  const requestUrlValue = resolveRequestUrl();
37
37
  const secret = resolveSecret();
38
38
  const baseClient = createClient({ proxyUrl });
39
+ function reporting(operation, identity, reqOptions, run) {
40
+ return run().catch((error) => {
41
+ const payload = {
42
+ operation,
43
+ ...identity.name !== void 0 && { name: identity.name },
44
+ ...identity.slug !== void 0 && { slug: identity.slug },
45
+ ...identity.domain !== void 0 && { domain: identity.domain },
46
+ ...contextDomain && { contextDomain },
47
+ ...reqOptions?.contextKey && { contextKey: reqOptions.contextKey },
48
+ ...requestUrlValue && { requestUrl: requestUrlValue },
49
+ ...proxyUrl && { proxyUrl },
50
+ ...toFetchErrorDetail(error),
51
+ version: FRONTIC_FETCH_ERROR_VERSION
52
+ };
53
+ void Promise.resolve(nuxtApp.callHook("frontic:fetch:error", payload)).catch(() => {
54
+ });
55
+ throw error;
56
+ });
57
+ }
39
58
  const client = {
40
- block: (name, key, reqOptions) => baseClient.block(name, key, {
41
- ...contextDomain && { contextDomain },
42
- ...requestUrlValue && { requestUrl: requestUrlValue },
43
- ...secret && { secret },
44
- ...reqOptions
45
- // Per-request options override defaults
46
- }),
47
- listing: (name, params, reqOptions) => baseClient.listing(name, params, {
48
- ...contextDomain && { contextDomain },
49
- ...requestUrlValue && { requestUrl: requestUrlValue },
50
- ...secret && { secret },
51
- ...reqOptions
52
- }),
53
- tree: (name, reqOptions) => baseClient.tree(name, {
54
- ...contextDomain && { contextDomain },
55
- ...requestUrlValue && { requestUrl: requestUrlValue },
56
- ...secret && { secret },
57
- ...reqOptions
58
- }),
59
- page: (slug, reqOptions) => baseClient.page(slug, {
60
- ...requestUrlValue && { requestUrl: requestUrlValue },
61
- ...secret && { secret },
62
- ...reqOptions
63
- }),
64
- context: (token, reqOptions) => baseClient.context(token, {
65
- ...secret && { secret },
66
- ...reqOptions
67
- }),
68
- contextList: (token, reqOptions) => baseClient.contextList(token, {
69
- ...contextDomain && { contextDomain },
70
- ...secret && { secret },
71
- ...reqOptions
72
- }),
73
- contextUpdate: (context, token, reqOptions) => baseClient.contextUpdate(context, token, {
74
- ...secret && { secret },
75
- ...reqOptions
76
- })
59
+ block: (name, key, reqOptions) => reporting(
60
+ "block",
61
+ { name: String(name) },
62
+ reqOptions,
63
+ () => baseClient.block(name, key, withDefaults({ ...contextDomain && { contextDomain }, ...requestUrlValue && { requestUrl: requestUrlValue }, ...secret && { secret } }, reqOptions))
64
+ ),
65
+ listing: (name, params, reqOptions) => reporting(
66
+ "listing",
67
+ { name: String(name) },
68
+ reqOptions,
69
+ () => baseClient.listing(name, params, withDefaults({ ...contextDomain && { contextDomain }, ...requestUrlValue && { requestUrl: requestUrlValue }, ...secret && { secret } }, reqOptions))
70
+ ),
71
+ tree: (name, reqOptions) => reporting(
72
+ "tree",
73
+ { name: String(name) },
74
+ reqOptions,
75
+ () => baseClient.tree(name, withDefaults({ ...contextDomain && { contextDomain }, ...requestUrlValue && { requestUrl: requestUrlValue }, ...secret && { secret } }, reqOptions))
76
+ ),
77
+ page: (slug, reqOptions) => reporting("page", { slug }, reqOptions, () => baseClient.page(slug, withDefaults({ ...requestUrlValue && { requestUrl: requestUrlValue }, ...secret && { secret } }, reqOptions))),
78
+ context: (token, reqOptions) => reporting("context", {}, reqOptions, () => baseClient.context(token, withDefaults({ ...secret && { secret } }, reqOptions))),
79
+ contextList: (token, reqOptions) => reporting("context", {}, reqOptions, () => baseClient.contextList(token, withDefaults({ ...contextDomain && { contextDomain }, ...secret && { secret } }, reqOptions))),
80
+ contextUpdate: (context, token, reqOptions) => reporting("context", {}, reqOptions, () => baseClient.contextUpdate(context, token, withDefaults({ ...secret && { secret } }, reqOptions))),
81
+ // The Frontic domain is an explicit parameter, so no contextDomain injection
82
+ sitemap: (domain, index, reqOptions) => reporting(
83
+ "sitemap",
84
+ { domain },
85
+ reqOptions,
86
+ () => baseClient.sitemap(domain, index, withDefaults({ ...requestUrlValue && { requestUrl: requestUrlValue }, ...secret && { secret } }, reqOptions))
87
+ )
77
88
  };
78
89
  return client;
79
90
  }
@@ -1,4 +1,7 @@
1
1
  import type { Ref } from 'vue';
2
+ import type { Context, ContextOption } from '../types/registry.js';
3
+ /** Payload passed to the `frontic:context:*` hooks */
4
+ export type FronticContext = Context;
2
5
  export interface UseFronticContextOptions {
3
6
  /**
4
7
  * Cookie name for persisting the context token
@@ -49,6 +52,15 @@ export interface UseFronticContextOptions {
49
52
  * </option>
50
53
  * </select>
51
54
  * ```
55
+ *
56
+ * @example React to every context change from anywhere (plugin, middleware, component)
57
+ * ```ts
58
+ * export default defineNuxtPlugin((nuxtApp) => {
59
+ * nuxtApp.hook('frontic:context:updated', (context) => {
60
+ * analytics.setSessionProperties({ region: context.region, locale: context.locale })
61
+ * })
62
+ * })
63
+ * ```
52
64
  */
53
65
  export declare function useFronticContext(options?: UseFronticContextOptions): {
54
66
  /**
@@ -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,28 +1,51 @@
1
1
  import { useQuery } from "@pinia/colada";
2
+ import { computed, toValue } from "vue";
3
+ import { resolveContextDomain } from "../utils/resolve-context-domain.js";
2
4
  import { fetchListingQuery } from "./listings.js";
3
5
  import { useFronticClient } from "./useFronticClient.js";
4
- export function useFronticListing(name, params, options) {
5
- const _client = useFronticClient();
6
+ export function useFronticListing(name, ...args) {
7
+ const [rawParams, options] = args;
8
+ const params = rawParams ?? {};
9
+ const _client = useFronticClient({ contextDomain: false });
10
+ const autoContextDomain = resolveContextDomain();
11
+ const resolvedContextDomain = computed(() => toValue(options?.contextDomain) ?? autoContextDomain);
6
12
  const listingQuery = fetchListingQuery({
7
13
  name,
8
14
  params,
9
15
  query: options?.query,
10
16
  contextKey: options?.contextKey,
11
- contextDomain: options?.contextDomain,
17
+ contextDomain: resolvedContextDomain,
12
18
  requestUrl: options?.requestUrl,
13
19
  staleTime: options?.staleTime,
20
+ gcTime: options?.gcTime,
21
+ enabled: options?.enabled,
22
+ keepPreviousData: options?.keepPreviousData,
14
23
  _client
15
24
  });
16
- const { data, status, refresh, refetch } = useQuery(listingQuery);
25
+ const { data, status, asyncStatus, isLoading, error, refresh, refetch } = useQuery(listingQuery);
17
26
  return {
18
27
  /**
19
28
  * The listing data, typed based on the listing name
20
29
  */
21
30
  listing: data,
22
31
  /**
23
- * Query status: 'pending' | 'error' | 'success'
32
+ * Data status: 'pending' | 'error' | 'success'
24
33
  */
25
34
  status,
35
+ /**
36
+ * Network status: 'idle' | 'loading'. Together with `status` this
37
+ * distinguishes a first load (`pending`) from a background refresh
38
+ * (`success` + 'loading').
39
+ */
40
+ asyncStatus,
41
+ /**
42
+ * Whether a request is in flight (shortcut for `asyncStatus === 'loading'`)
43
+ */
44
+ isLoading,
45
+ /**
46
+ * Error of the last fetch, null while pending or after success
47
+ */
48
+ error,
26
49
  /**
27
50
  * Manually refresh the listing data (uses cache if valid)
28
51
  */
@@ -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
  };