@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,18 +1,11 @@
1
- /**
2
- * Block Query Factory
3
- *
4
- * Provides query key management and query factory functions for Frontic blocks.
5
- * Compatible with both @pinia/colada and useAsyncData patterns.
6
- */
7
- import type { FrontstackClient } from '@frontic/stack/generated-client';
8
- import type { Blocks, Responses } from '@frontic/stack/generated-types';
9
1
  import type { MaybeRef } from 'vue';
10
- import type { FronticRequestOptions } from '../types/listing.js';
11
- export interface FetchBlockConfig<TBlock extends string> extends FronticRequestOptions {
2
+ import type { FronticClient } from '../types/client.js';
3
+ import type { BlockName, ResponseOf } from '../types/registry.js';
4
+ import type { FronticRequestOptions, FronticQueryOptions } from '../types/listing.js';
5
+ export interface FetchBlockConfig<TBlock extends string> extends FronticRequestOptions, FronticQueryOptions {
12
6
  name: TBlock;
13
7
  key: MaybeRef<string>;
14
- staleTime?: number;
15
- _client?: FrontstackClient;
8
+ _client?: FronticClient;
16
9
  }
17
10
  /**
18
11
  * Query key factory for block queries
@@ -22,14 +15,17 @@ export interface FetchBlockConfig<TBlock extends string> extends FronticRequestO
22
15
  * ```ts
23
16
  * BLOCK_QUERY_KEYS.root // ['blocks']
24
17
  * BLOCK_QUERY_KEYS.block('ProductCard') // ['blocks', 'ProductCard']
25
- * BLOCK_QUERY_KEYS.detail('ProductCard', 'abc123', 'context-token')
26
- * // ['blocks', 'ProductCard', 'abc123', 'context-token']
18
+ * BLOCK_QUERY_KEYS.detail('ProductCard', 'abc123', 'context-token', 'shop.com')
19
+ * // ['blocks', 'ProductCard', 'abc123', { context: 'context-token', domain: 'shop.com' }]
27
20
  * ```
28
21
  */
29
22
  export declare const BLOCK_QUERY_KEYS: {
30
- root: string[];
31
- block: <TBlock extends string>(blockName: TBlock) => string[];
32
- detail: <TBlock extends string>(blockName: TBlock, key: string, contextKey?: string) => string[];
23
+ root: readonly ["blocks"];
24
+ block: <TBlock extends string>(blockName: TBlock) => readonly ["blocks", TBlock];
25
+ detail: <TBlock extends string>(blockName: TBlock, key: string, contextKey?: string, contextDomain?: string) => readonly ["blocks", TBlock, string, {
26
+ readonly context: string | null;
27
+ readonly domain: string | null;
28
+ }];
33
29
  };
34
30
  /**
35
31
  * Generic query factory for fetching blocks
@@ -41,21 +37,27 @@ export declare const BLOCK_QUERY_KEYS: {
41
37
  * const blockQuery = fetchBlockQuery({
42
38
  * name: 'ProductCard',
43
39
  * key: productId,
44
- * contextKey: token.value
40
+ * contextKey: token
45
41
  * })
46
42
  *
47
43
  * const { data, status } = useQuery(blockQuery)
48
44
  *
49
45
  * // With useAsyncData
50
46
  * const { data, status } = useAsyncData(
51
- * blockQuery.key().join('-'),
47
+ * createBlockCacheKey(blockQuery.key()),
52
48
  * blockQuery.query
53
49
  * )
54
50
  * ```
55
51
  */
56
- export declare function fetchBlockQuery<TBlock extends keyof Blocks>(config: FetchBlockConfig<TBlock>): {
57
- key: () => string[];
58
- query: () => Promise<Responses[TBlock]>;
52
+ export declare function fetchBlockQuery<TBlock extends BlockName>(config: FetchBlockConfig<TBlock>): {
53
+ placeholderData?: ((previousData: ResponseOf<TBlock> | undefined) => ResponseOf<TBlock> | undefined) | undefined;
54
+ enabled: () => boolean;
55
+ gcTime?: number | undefined;
56
+ key: () => readonly ["blocks", TBlock, string, {
57
+ readonly context: string | null;
58
+ readonly domain: string | null;
59
+ }];
60
+ query: () => Promise<ResponseOf<TBlock>>;
59
61
  staleTime: number;
60
62
  };
61
63
  /**
@@ -1,32 +1,32 @@
1
- function unwrapRef(ref) {
2
- return typeof ref === "object" && ref !== null && "value" in ref ? ref.value : ref;
3
- }
1
+ import { toValue } from "vue";
2
+ import { DEFAULT_STALE_TIME, requestContextSlot } from "../utils/query-keys.js";
4
3
  export const BLOCK_QUERY_KEYS = {
5
4
  root: ["blocks"],
6
5
  block: (blockName) => [...BLOCK_QUERY_KEYS.root, blockName],
7
- detail: (blockName, key, contextKey) => [...BLOCK_QUERY_KEYS.block(blockName), key, contextKey ?? "default"]
6
+ detail: (blockName, key, contextKey, contextDomain) => [...BLOCK_QUERY_KEYS.block(blockName), key, requestContextSlot(contextKey, contextDomain)]
8
7
  };
9
8
  export function fetchBlockQuery(config) {
10
9
  return {
11
- key: () => {
12
- const key = unwrapRef(config.key);
13
- const contextKey = unwrapRef(config.contextKey);
14
- return BLOCK_QUERY_KEYS.detail(config.name, key, contextKey);
15
- },
10
+ key: () => BLOCK_QUERY_KEYS.detail(config.name, toValue(config.key), toValue(config.contextKey), toValue(config.contextDomain)),
16
11
  query: async () => {
17
12
  let client = config._client;
18
13
  if (!client) {
19
14
  const { useFronticClient } = await import("./useFronticClient.js");
20
15
  client = useFronticClient();
21
16
  }
22
- const key = unwrapRef(config.key);
23
- const contextKey = unwrapRef(config.contextKey);
24
- const contextDomain = unwrapRef(config.contextDomain);
25
- const requestUrl = unwrapRef(config.requestUrl);
26
- return await client.block(config.name, key, { contextKey, contextDomain, requestUrl });
17
+ return await client.block(config.name, toValue(config.key), {
18
+ contextKey: toValue(config.contextKey),
19
+ contextDomain: toValue(config.contextDomain),
20
+ requestUrl: toValue(config.requestUrl)
21
+ });
27
22
  },
28
- staleTime: config.staleTime ?? 1e3 * 60 * 5
29
- // 5 minutes default
23
+ staleTime: config.staleTime ?? DEFAULT_STALE_TIME,
24
+ ...config.gcTime !== void 0 && { gcTime: config.gcTime },
25
+ // A block cannot be fetched without a key; the caller's `enabled` gates on top
26
+ enabled: () => !!toValue(config.key) && (toValue(config.enabled) ?? true),
27
+ // keepPreviousData: show the previous entry's data while a changed key
28
+ // (new context, new block key) loads; never written to the cache
29
+ ...config.keepPreviousData !== false && { placeholderData: (previousData) => previousData }
30
30
  };
31
31
  }
32
32
  export function createBlockCacheKey(keyArray) {
@@ -22,4 +22,4 @@ export type { UseFronticTreeOptions } from './useFronticTree.js';
22
22
  export { fetchTreeQuery, TREE_QUERY_KEYS, createTreeCacheKey } from './trees.js';
23
23
  export type { FetchTreeConfig } from './trees.js';
24
24
  export { useFronticContext } from './useFronticContext.js';
25
- export type { UseFronticContextOptions } from './useFronticContext.js';
25
+ export type { UseFronticContextOptions, FronticContext } from './useFronticContext.js';
@@ -4,8 +4,8 @@
4
4
  * Provides query key management and query factory functions for Frontic listings.
5
5
  * Compatible with both @pinia/colada and useAsyncData patterns.
6
6
  */
7
- import type { FrontstackClient } from '@frontic/stack/generated-client';
8
- import type { Listings, Responses } from '@frontic/stack/generated-types';
7
+ import type { FronticClient } from '../types/client.js';
8
+ import type { ListingName, ResponseOf } from '../types/registry.js';
9
9
  import type { FetchListingConfig } from '../types/listing.js';
10
10
  /**
11
11
  * Query key factory for listing queries
@@ -15,14 +15,17 @@ import type { FetchListingConfig } from '../types/listing.js';
15
15
  * ```ts
16
16
  * LISTING_QUERY_KEYS.root // ['listings']
17
17
  * LISTING_QUERY_KEYS.listing('ProductSearch') // ['listings', 'ProductSearch']
18
- * LISTING_QUERY_KEYS.detail('ProductSearch', { categoryKey: 'shoes' }, { filter: [] })
19
- * // ['listings', 'ProductSearch', { categoryKey: 'shoes' }, { filter: [] }]
18
+ * LISTING_QUERY_KEYS.detail('ProductSearch', { categoryKey: 'shoes' }, { filter: [] }, 'context-token')
19
+ * // ['listings', 'ProductSearch', { categoryKey: 'shoes' }, { filter: [] }, { context: 'context-token', domain: null }]
20
20
  * ```
21
21
  */
22
22
  export declare const LISTING_QUERY_KEYS: {
23
- root: string[];
24
- listing: <TListing extends string>(listingName: TListing) => string[];
25
- detail: <TListing extends string>(listingName: TListing, parameters: Record<string, unknown>, query: Record<string, unknown>) => string[];
23
+ root: readonly ["listings"];
24
+ listing: <TListing extends string>(listingName: TListing) => readonly ["listings", TListing];
25
+ detail: <TListing extends string>(listingName: TListing, parameters: Record<string, unknown>, query: Record<string, unknown>, contextKey?: string, contextDomain?: string) => readonly ["listings", TListing, Record<string, unknown>, Record<string, unknown>, {
26
+ readonly context: string | null;
27
+ readonly domain: string | null;
28
+ }];
26
29
  };
27
30
  /**
28
31
  * Generic query factory for fetching listings
@@ -35,23 +38,29 @@ export declare const LISTING_QUERY_KEYS: {
35
38
  * name: 'ProductSearch',
36
39
  * params: {},
37
40
  * query: { filter: [], sort: { field: 'name', order: 'asc' } },
38
- * contextKey: token.value
41
+ * contextKey: token
39
42
  * })
40
43
  *
41
44
  * const { data, status } = useQuery(productQuery)
42
45
  *
43
46
  * // With useAsyncData
44
47
  * const { data, status } = useAsyncData(
45
- * productQuery.key().join('-'),
48
+ * createCacheKey(productQuery.key()),
46
49
  * productQuery.query
47
50
  * )
48
51
  * ```
49
52
  */
50
- export declare function fetchListingQuery<TListing extends keyof Listings>(config: FetchListingConfig<TListing> & {
51
- _client?: FrontstackClient;
53
+ export declare function fetchListingQuery<TListing extends ListingName>(config: FetchListingConfig<TListing> & {
54
+ _client?: FronticClient;
52
55
  }): {
53
- key: () => string[];
54
- query: () => Promise<Responses[TListing]>;
56
+ placeholderData?: ((previousData: ResponseOf<TListing> | undefined) => ResponseOf<TListing> | undefined) | undefined;
57
+ enabled?: import("vue").MaybeRefOrGetter<boolean> | undefined;
58
+ gcTime?: number | undefined;
59
+ key: () => readonly ["listings", string, Record<string, unknown>, Record<string, unknown>, {
60
+ readonly context: string | null;
61
+ readonly domain: string | null;
62
+ }];
63
+ query: () => Promise<ResponseOf<TListing>>;
55
64
  staleTime: number;
56
65
  };
57
66
  /**
@@ -1,31 +1,27 @@
1
- function unwrapRef(ref) {
2
- return typeof ref === "object" && ref !== null && "value" in ref ? ref.value : ref;
3
- }
1
+ import { toValue } from "vue";
2
+ import { DEFAULT_STALE_TIME, requestContextSlot, toKeyPart } from "../utils/query-keys.js";
4
3
  export const LISTING_QUERY_KEYS = {
5
4
  root: ["listings"],
6
5
  listing: (listingName) => [...LISTING_QUERY_KEYS.root, listingName],
7
- detail: (listingName, parameters, query) => [
8
- ...LISTING_QUERY_KEYS.listing(listingName),
9
- JSON.stringify(parameters),
10
- JSON.stringify(query)
11
- ]
6
+ detail: (listingName, parameters, query, contextKey, contextDomain) => [...LISTING_QUERY_KEYS.listing(listingName), toKeyPart(parameters), toKeyPart(query), requestContextSlot(contextKey, contextDomain)]
12
7
  };
13
8
  export function fetchListingQuery(config) {
14
9
  return {
15
10
  key: () => {
16
- const params = unwrapRef(config.params);
17
- const query = unwrapRef(config.query) ?? {};
18
- const page = unwrapRef(config.page);
11
+ const query = toValue(config.query) ?? {};
12
+ const page = toValue(config.page);
19
13
  const queryWithPage = page ? { ...query, page } : query;
20
- return LISTING_QUERY_KEYS.detail(config.name, params, queryWithPage);
14
+ return LISTING_QUERY_KEYS.detail(
15
+ config.name,
16
+ toValue(config.params),
17
+ queryWithPage,
18
+ toValue(config.contextKey),
19
+ toValue(config.contextDomain)
20
+ );
21
21
  },
22
22
  query: async () => {
23
- const params = unwrapRef(config.params);
24
- const query = unwrapRef(config.query) ?? {};
25
- const contextKey = unwrapRef(config.contextKey);
26
- const contextDomain = unwrapRef(config.contextDomain);
27
- const requestUrl = unwrapRef(config.requestUrl);
28
- const page = unwrapRef(config.page);
23
+ const query = toValue(config.query) ?? {};
24
+ const page = toValue(config.page);
29
25
  const queryWithPage = page ? { ...query, page } : query;
30
26
  try {
31
27
  let client = config._client;
@@ -33,20 +29,23 @@ export function fetchListingQuery(config) {
33
29
  const { useFronticClient } = await import("./useFronticClient.js");
34
30
  client = useFronticClient();
35
31
  }
36
- const result = await client.listing(config.name, params, {
32
+ return await client.listing(config.name, toValue(config.params), {
37
33
  query: queryWithPage,
38
- contextKey,
39
- contextDomain,
40
- requestUrl
34
+ contextKey: toValue(config.contextKey),
35
+ contextDomain: toValue(config.contextDomain),
36
+ requestUrl: toValue(config.requestUrl)
41
37
  });
42
- return result;
43
38
  } catch (error) {
44
39
  console.error(`[@frontic/nuxt] Failed to fetch listing "${String(config.name)}". Make sure the Frontic client is generated.`);
45
40
  throw error;
46
41
  }
47
42
  },
48
- staleTime: config.staleTime ?? 1e3 * 60 * 5
49
- // 5 minutes default
43
+ staleTime: config.staleTime ?? DEFAULT_STALE_TIME,
44
+ ...config.gcTime !== void 0 && { gcTime: config.gcTime },
45
+ ...config.enabled !== void 0 && { enabled: config.enabled },
46
+ // keepPreviousData: show the previous entry's data while a changed key
47
+ // (new page, filter, context) loads; never written to the cache
48
+ ...config.keepPreviousData !== false && { placeholderData: (previousData) => previousData }
50
49
  };
51
50
  }
52
51
  export function createCacheKey(keyArray) {
@@ -1,16 +1,10 @@
1
- /**
2
- * Page Query Factory
3
- *
4
- * Provides query key management and query factory functions for Frontic pages.
5
- * Compatible with both @pinia/colada and useAsyncData patterns.
6
- */
7
- import type { FrontstackClient } from '@frontic/stack/generated-client';
8
1
  import type { MaybeRef } from 'vue';
9
- import type { FronticRequestOptions } from '../types/listing.js';
10
- export interface FetchPageConfig extends FronticRequestOptions {
2
+ import type { FronticClient } from '../types/client.js';
3
+ import type { Page } from '../types/registry.js';
4
+ import type { FronticRequestOptions, FronticQueryOptions } from '../types/listing.js';
5
+ export interface FetchPageConfig extends FronticRequestOptions, FronticQueryOptions {
11
6
  slug: MaybeRef<string>;
12
- staleTime?: number;
13
- _client?: FrontstackClient;
7
+ _client?: FronticClient;
14
8
  }
15
9
  /**
16
10
  * Query key factory for page queries
@@ -21,13 +15,16 @@ export interface FetchPageConfig extends FronticRequestOptions {
21
15
  * PAGE_QUERY_KEYS.root // ['pages']
22
16
  * PAGE_QUERY_KEYS.page('demo-shop.com/uk/products') // ['pages', 'demo-shop.com/uk/products']
23
17
  * PAGE_QUERY_KEYS.detail('demo-shop.com/uk/products', 'context-token')
24
- * // ['pages', 'demo-shop.com/uk/products', 'context-token']
18
+ * // ['pages', 'demo-shop.com/uk/products', { context: 'context-token', domain: null }]
25
19
  * ```
26
20
  */
27
21
  export declare const PAGE_QUERY_KEYS: {
28
- root: string[];
29
- page: (slug: string) => string[];
30
- detail: (slug: string, contextKey?: string) => string[];
22
+ root: readonly ["pages"];
23
+ page: (slug: string) => readonly ["pages", string];
24
+ detail: (slug: string, contextKey?: string, contextDomain?: string) => readonly ["pages", string, {
25
+ readonly context: string | null;
26
+ readonly domain: string | null;
27
+ }];
31
28
  };
32
29
  /**
33
30
  * Generic query factory for fetching pages
@@ -38,20 +35,26 @@ export declare const PAGE_QUERY_KEYS: {
38
35
  * // With @pinia/colada
39
36
  * const pageQuery = fetchPageQuery({
40
37
  * slug: 'demo-shop.com/uk/products/shoes',
41
- * contextKey: token.value
38
+ * contextKey: token
42
39
  * })
43
40
  *
44
41
  * const { data, status } = useQuery(pageQuery)
45
42
  *
46
43
  * // With useAsyncData
47
44
  * const { data, status } = useAsyncData(
48
- * pageQuery.key().join('-'),
45
+ * createPageCacheKey(pageQuery.key()),
49
46
  * pageQuery.query
50
47
  * )
51
48
  * ```
52
49
  */
53
50
  export declare function fetchPageQuery(config: FetchPageConfig): {
54
- key: () => string[];
51
+ placeholderData?: ((previousData: Page | undefined) => import("../types/registry.js").DefaultPage | undefined) | undefined;
52
+ enabled?: import("vue").MaybeRefOrGetter<boolean> | undefined;
53
+ gcTime?: number | undefined;
54
+ key: () => readonly ["pages", string, {
55
+ readonly context: string | null;
56
+ readonly domain: string | null;
57
+ }];
55
58
  query: () => Promise<Page>;
56
59
  staleTime: number;
57
60
  };
@@ -1,32 +1,31 @@
1
- function unwrapRef(ref) {
2
- return typeof ref === "object" && ref !== null && "value" in ref ? ref.value : ref;
3
- }
1
+ import { toValue } from "vue";
2
+ import { DEFAULT_STALE_TIME, requestContextSlot } from "../utils/query-keys.js";
4
3
  export const PAGE_QUERY_KEYS = {
5
4
  root: ["pages"],
6
5
  page: (slug) => [...PAGE_QUERY_KEYS.root, slug],
7
- detail: (slug, contextKey) => [...PAGE_QUERY_KEYS.page(slug), contextKey ?? "default"]
6
+ detail: (slug, contextKey, contextDomain) => [...PAGE_QUERY_KEYS.page(slug), requestContextSlot(contextKey, contextDomain)]
8
7
  };
9
8
  export function fetchPageQuery(config) {
10
9
  return {
11
- key: () => {
12
- const slug = unwrapRef(config.slug);
13
- const contextKey = unwrapRef(config.contextKey);
14
- return PAGE_QUERY_KEYS.detail(slug, contextKey);
15
- },
10
+ key: () => PAGE_QUERY_KEYS.detail(toValue(config.slug), toValue(config.contextKey), toValue(config.contextDomain)),
16
11
  query: async () => {
17
12
  let client = config._client;
18
13
  if (!client) {
19
14
  const { useFronticClient } = await import("./useFronticClient.js");
20
15
  client = useFronticClient();
21
16
  }
22
- const slug = unwrapRef(config.slug);
23
- const contextKey = unwrapRef(config.contextKey);
24
- const contextDomain = unwrapRef(config.contextDomain);
25
- const requestUrl = unwrapRef(config.requestUrl);
26
- return await client.page(slug, { contextKey, contextDomain, requestUrl });
17
+ return await client.page(toValue(config.slug), {
18
+ contextKey: toValue(config.contextKey),
19
+ contextDomain: toValue(config.contextDomain),
20
+ requestUrl: toValue(config.requestUrl)
21
+ });
27
22
  },
28
- staleTime: config.staleTime ?? 1e3 * 60 * 5
29
- // 5 minutes default
23
+ staleTime: config.staleTime ?? DEFAULT_STALE_TIME,
24
+ ...config.gcTime !== void 0 && { gcTime: config.gcTime },
25
+ ...config.enabled !== void 0 && { enabled: config.enabled },
26
+ // keepPreviousData: show the previous entry's data while a changed key
27
+ // (new slug, new context) loads; never written to the cache
28
+ ...config.keepPreviousData !== false && { placeholderData: (previousData) => previousData }
30
29
  };
31
30
  }
32
31
  export function createPageCacheKey(keyArray) {
@@ -1,19 +1,12 @@
1
- /**
2
- * Tree Query Factory
3
- *
4
- * Provides query key management and query factory functions for Frontic trees.
5
- * Compatible with both @pinia/colada and useAsyncData patterns.
6
- */
7
- import type { FrontstackClient } from '@frontic/stack/generated-client';
8
- import type { Trees, Responses } from '@frontic/stack/generated-types';
9
1
  import type { MaybeRef } from 'vue';
10
- import type { FronticRequestOptions } from '../types/listing.js';
11
- export interface FetchTreeConfig<TTree extends string> extends FronticRequestOptions {
2
+ import type { FronticClient } from '../types/client.js';
3
+ import type { TreeName, ResponseOf } from '../types/registry.js';
4
+ import type { FronticRequestOptions, FronticQueryOptions } from '../types/listing.js';
5
+ export interface FetchTreeConfig<TTree extends string> extends FronticRequestOptions, FronticQueryOptions {
12
6
  name: TTree;
13
7
  key?: MaybeRef<string | undefined>;
14
8
  depth?: MaybeRef<number | undefined>;
15
- staleTime?: number;
16
- _client?: FrontstackClient;
9
+ _client?: FronticClient;
17
10
  }
18
11
  /**
19
12
  * Query key factory for tree queries
@@ -29,15 +22,18 @@ export interface FetchTreeConfig<TTree extends string> extends FronticRequestOpt
29
22
  * TREE_QUERY_KEYS.root // ['trees']
30
23
  * TREE_QUERY_KEYS.tree('CategoryNavigation') // ['trees', 'CategoryNavigation']
31
24
  * TREE_QUERY_KEYS.detail('CategoryNavigation', 'shop', 2, 'context-token')
32
- * // ['trees', 'CategoryNavigation', 'shop', '2', 'context-token']
25
+ * // ['trees', 'CategoryNavigation', 'shop', '2', { context: 'context-token', domain: null }]
33
26
  * TREE_QUERY_KEYS.detail('CategoryNavigation')
34
- * // ['trees', 'CategoryNavigation', '', 'all', 'default']
27
+ * // ['trees', 'CategoryNavigation', '', 'all', { context: null, domain: null }]
35
28
  * ```
36
29
  */
37
30
  export declare const TREE_QUERY_KEYS: {
38
- root: string[];
39
- tree: <TTree extends string>(treeName: TTree) => string[];
40
- detail: <TTree extends string>(treeName: TTree, key?: string, depth?: number, contextKey?: string) => string[];
31
+ root: readonly ["trees"];
32
+ tree: <TTree extends string>(treeName: TTree) => readonly ["trees", TTree];
33
+ detail: <TTree extends string>(treeName: TTree, key?: string, depth?: number, contextKey?: string, contextDomain?: string) => readonly ["trees", TTree, string, string, {
34
+ readonly context: string | null;
35
+ readonly domain: string | null;
36
+ }];
41
37
  };
42
38
  /**
43
39
  * Generic query factory for fetching trees
@@ -50,21 +46,27 @@ export declare const TREE_QUERY_KEYS: {
50
46
  * name: 'CategoryNavigation',
51
47
  * key: 'shop',
52
48
  * depth: 2,
53
- * contextKey: token.value
49
+ * contextKey: token
54
50
  * })
55
51
  *
56
52
  * const { data, status } = useQuery(treeQuery)
57
53
  *
58
54
  * // With useAsyncData
59
55
  * const { data, status } = useAsyncData(
60
- * treeQuery.key().join('-'),
56
+ * createTreeCacheKey(treeQuery.key()),
61
57
  * treeQuery.query
62
58
  * )
63
59
  * ```
64
60
  */
65
- export declare function fetchTreeQuery<TTree extends keyof Trees>(config: FetchTreeConfig<TTree>): {
66
- key: () => string[];
67
- query: () => Promise<Responses[TTree]>;
61
+ export declare function fetchTreeQuery<TTree extends TreeName>(config: FetchTreeConfig<TTree>): {
62
+ placeholderData?: ((previousData: ResponseOf<TTree> | undefined) => ResponseOf<TTree> | undefined) | undefined;
63
+ enabled?: import("vue").MaybeRefOrGetter<boolean> | undefined;
64
+ gcTime?: number | undefined;
65
+ key: () => readonly ["trees", TTree, string, string, {
66
+ readonly context: string | null;
67
+ readonly domain: string | null;
68
+ }];
69
+ query: () => Promise<ResponseOf<TTree>>;
68
70
  staleTime: number;
69
71
  };
70
72
  /**
@@ -1,47 +1,37 @@
1
- function unwrapRef(ref) {
2
- return typeof ref === "object" && ref !== null && "value" in ref ? ref.value : ref;
3
- }
1
+ import { toValue } from "vue";
2
+ import { DEFAULT_STALE_TIME, requestContextSlot } from "../utils/query-keys.js";
4
3
  export const TREE_QUERY_KEYS = {
5
4
  root: ["trees"],
6
5
  tree: (treeName) => [...TREE_QUERY_KEYS.root, treeName],
7
- detail: (treeName, key, depth, contextKey) => [
8
- ...TREE_QUERY_KEYS.tree(treeName),
9
- key ?? "",
10
- depth !== void 0 ? String(depth) : "all",
11
- contextKey ?? "default"
12
- ]
6
+ detail: (treeName, key, depth, contextKey, contextDomain) => [...TREE_QUERY_KEYS.tree(treeName), key ?? "", depth !== void 0 ? String(depth) : "all", requestContextSlot(contextKey, contextDomain)]
13
7
  };
14
8
  export function fetchTreeQuery(config) {
15
9
  return {
16
- key: () => {
17
- const key = unwrapRef(config.key);
18
- const depth = unwrapRef(config.depth);
19
- const contextKey = unwrapRef(config.contextKey);
20
- return TREE_QUERY_KEYS.detail(config.name, key, depth, contextKey);
21
- },
10
+ key: () => TREE_QUERY_KEYS.detail(config.name, toValue(config.key), toValue(config.depth), toValue(config.contextKey), toValue(config.contextDomain)),
22
11
  query: async () => {
23
12
  let client = config._client;
24
13
  if (!client) {
25
14
  const { useFronticClient } = await import("./useFronticClient.js");
26
15
  client = useFronticClient();
27
16
  }
28
- const key = unwrapRef(config.key);
29
- const depth = unwrapRef(config.depth);
30
- const contextKey = unwrapRef(config.contextKey);
31
- const contextDomain = unwrapRef(config.contextDomain);
32
- const requestUrl = unwrapRef(config.requestUrl);
17
+ const key = toValue(config.key);
18
+ const depth = toValue(config.depth);
33
19
  const treeQuery = {};
34
20
  if (key !== void 0) treeQuery.key = key;
35
21
  if (depth !== void 0) treeQuery.depth = depth;
36
22
  return await client.tree(config.name, {
37
23
  ...Object.keys(treeQuery).length > 0 && { query: treeQuery },
38
- contextKey,
39
- contextDomain,
40
- requestUrl
24
+ contextKey: toValue(config.contextKey),
25
+ contextDomain: toValue(config.contextDomain),
26
+ requestUrl: toValue(config.requestUrl)
41
27
  });
42
28
  },
43
- staleTime: config.staleTime ?? 1e3 * 60 * 5
44
- // 5 minutes default
29
+ staleTime: config.staleTime ?? DEFAULT_STALE_TIME,
30
+ ...config.gcTime !== void 0 && { gcTime: config.gcTime },
31
+ ...config.enabled !== void 0 && { enabled: config.enabled },
32
+ // keepPreviousData: show the previous entry's data while a changed key
33
+ // (new subtree, new context) loads; never written to the cache
34
+ ...config.keepPreviousData !== false && { placeholderData: (previousData) => previousData }
45
35
  };
46
36
  }
47
37
  export function createTreeCacheKey(keyArray) {
@@ -1,12 +1,7 @@
1
1
  import type { Ref, MaybeRef } from 'vue';
2
- import type { Blocks, Responses } from '@frontic/stack/generated-types';
3
- import type { FronticRequestOptions } from '../types/listing.js';
4
- export interface UseFronticBlockOptions extends FronticRequestOptions {
5
- /**
6
- * Cache stale time in milliseconds
7
- * @default 300000 (5 minutes)
8
- */
9
- staleTime?: number;
2
+ import type { BlockName, ResponseOf } from '../types/registry.js';
3
+ import type { FronticRequestOptions, FronticQueryOptions } from '../types/listing.js';
4
+ export interface UseFronticBlockOptions extends FronticRequestOptions, FronticQueryOptions {
10
5
  }
11
6
  /**
12
7
  * Fetch a Frontic block by name and key
@@ -34,21 +29,35 @@ export interface UseFronticBlockOptions extends FronticRequestOptions {
34
29
  * const { block } = useFronticBlock('ProductCard', productKey)
35
30
  * ```
36
31
  */
37
- export declare function useFronticBlock<T extends keyof Blocks>(name: T, key: MaybeRef<string>, options?: UseFronticBlockOptions): {
32
+ export declare function useFronticBlock<T extends BlockName>(name: T, key: MaybeRef<string>, options?: UseFronticBlockOptions): {
38
33
  /**
39
34
  * The block data, typed based on the block name
40
35
  */
41
- block: Ref<Responses[T] | undefined>;
36
+ block: Ref<ResponseOf<T> | undefined>;
42
37
  /**
43
- * Query status: 'pending' | 'error' | 'success'
38
+ * Data status: 'pending' | 'error' | 'success'
44
39
  */
45
40
  status: import("vue").ShallowRef<import("@pinia/colada").DataStateStatus>;
41
+ /**
42
+ * Network status: 'idle' | 'loading'. Together with `status` this
43
+ * distinguishes a first load (`pending`) from a background refresh
44
+ * (`success` + 'loading').
45
+ */
46
+ asyncStatus: import("vue").ComputedRef<import("@pinia/colada").AsyncStatus>;
47
+ /**
48
+ * Whether a request is in flight (shortcut for `asyncStatus === 'loading'`)
49
+ */
50
+ isLoading: import("vue").ShallowRef<boolean>;
51
+ /**
52
+ * Error of the last fetch, null while pending or after success
53
+ */
54
+ error: import("vue").ShallowRef<Error | null>;
46
55
  /**
47
56
  * Manually refresh the block data (uses cache if valid)
48
57
  */
49
- refresh: (throwOnError?: boolean) => Promise<import("@pinia/colada").DataState<unknown, Error, undefined>>;
58
+ refresh: (throwOnError?: boolean) => Promise<import("@pinia/colada").DataState<ResponseOf<T>, Error, undefined>>;
50
59
  /**
51
60
  * Force a fresh fetch of the block data, bypassing cache
52
61
  */
53
- refetch: (throwOnError?: boolean) => Promise<import("@pinia/colada").DataState<unknown, Error, undefined>>;
62
+ refetch: (throwOnError?: boolean) => Promise<import("@pinia/colada").DataState<ResponseOf<T>, Error, undefined>>;
54
63
  };