@frontic/nuxt 0.6.1 → 0.8.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 +9 -1
- package/dist/module.d.mts +3 -1
- package/dist/module.json +2 -2
- package/dist/module.mjs +13 -3
- package/dist/runtime/composables/index.d.ts +5 -1
- package/dist/runtime/composables/index.js +2 -0
- package/dist/runtime/composables/trees.d.ts +77 -0
- package/dist/runtime/composables/trees.js +49 -0
- package/dist/runtime/composables/useFronticClient.js +6 -0
- package/dist/runtime/composables/useFronticListing.d.ts +29 -12
- package/dist/runtime/composables/useFronticListing.js +1 -2
- package/dist/runtime/composables/useFronticSearch.d.ts +2 -2
- package/dist/runtime/composables/useFronticTree.d.ts +82 -0
- package/dist/runtime/composables/useFronticTree.js +45 -0
- package/dist/runtime/frontic-stack.d.ts +8 -1
- package/dist/runtime/types/listing.d.ts +4 -2
- package/package.json +19 -16
package/README.md
CHANGED
|
@@ -82,6 +82,14 @@ Fetch single blocks (products, categories):
|
|
|
82
82
|
const { block, status } = useFronticBlock('Product', 'product-handle')
|
|
83
83
|
```
|
|
84
84
|
|
|
85
|
+
### useFronticTree
|
|
86
|
+
|
|
87
|
+
Hierarchical menu trees:
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
const { items, status } = useFronticTree('CategoryNavigation', { depth: 2 })
|
|
91
|
+
```
|
|
92
|
+
|
|
85
93
|
### useFronticContext
|
|
86
94
|
|
|
87
95
|
Manage locale and region:
|
|
@@ -109,7 +117,7 @@ export default defineNuxtConfig({
|
|
|
109
117
|
contextCookieMaxAge: 31536000,
|
|
110
118
|
|
|
111
119
|
// Composables
|
|
112
|
-
composables: true, // or ['page', 'search', 'listing', 'block', 'context', 'client']
|
|
120
|
+
composables: true, // or ['page', 'search', 'listing', 'block', 'tree', 'context', 'client']
|
|
113
121
|
|
|
114
122
|
// UI components (optional)
|
|
115
123
|
componentsPrefix: '',
|
package/dist/module.d.mts
CHANGED
|
@@ -6,11 +6,12 @@ import { NuxtModule } from '@nuxt/schema';
|
|
|
6
6
|
* - 'block': useFronticBlock - Fetch single blocks (products, categories)
|
|
7
7
|
* - 'listing': useFronticListing - Fetch listings with caching
|
|
8
8
|
* - 'search': useFronticSearch - Full-featured search with filters/sorting
|
|
9
|
+
* - 'tree': useFronticTree - Fetch hierarchical menu trees
|
|
9
10
|
* - 'page': useFronticPage - Dynamic page routing
|
|
10
11
|
* - 'context': useFronticContext - Locale/region management
|
|
11
12
|
* - 'client': useFronticClient - Low-level API client
|
|
12
13
|
*/
|
|
13
|
-
type FronticComposable = 'block' | 'listing' | 'search' | 'page' | 'context' | 'client';
|
|
14
|
+
type FronticComposable = 'block' | 'listing' | 'search' | 'tree' | 'page' | 'context' | 'client';
|
|
14
15
|
/**
|
|
15
16
|
* Frontic composables configuration
|
|
16
17
|
* - true: Enable all composables (default)
|
|
@@ -44,6 +45,7 @@ interface FronticOptions {
|
|
|
44
45
|
* - 'block': useFronticBlock - Fetch single blocks
|
|
45
46
|
* - 'listing': useFronticListing - Fetch listings
|
|
46
47
|
* - 'search': useFronticSearch - Search with filters/sorting
|
|
48
|
+
* - 'tree': useFronticTree - Fetch hierarchical menu trees
|
|
47
49
|
* - 'page': useFronticPage - Dynamic page routing
|
|
48
50
|
* - 'context': useFronticContext - Locale/region management
|
|
49
51
|
* - 'client': useFronticClient - Low-level API client
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -5,7 +5,7 @@ import { fileURLToPath } from 'node:url';
|
|
|
5
5
|
import { defineNuxtModule, createResolver, addServerHandler, hasNuxtModule, installModule, addImports, addComponentsDir, addComponentExports } from '@nuxt/kit';
|
|
6
6
|
|
|
7
7
|
const _dirname = typeof __dirname !== "undefined" ? __dirname : dirname(fileURLToPath(import.meta.url));
|
|
8
|
-
const ALL_COMPOSABLES = ["block", "listing", "search", "page", "context", "client"];
|
|
8
|
+
const ALL_COMPOSABLES = ["block", "listing", "search", "tree", "page", "context", "client"];
|
|
9
9
|
function isComposableEnabled(config, name) {
|
|
10
10
|
if (config === true) return true;
|
|
11
11
|
if (config === false) return false;
|
|
@@ -28,9 +28,9 @@ const module$1 = defineNuxtModule({
|
|
|
28
28
|
meta: {
|
|
29
29
|
name: "frontic",
|
|
30
30
|
configKey: "frontic",
|
|
31
|
-
version: "0.0
|
|
31
|
+
version: "0.7.0",
|
|
32
32
|
compatibility: {
|
|
33
|
-
nuxt: ">=
|
|
33
|
+
nuxt: ">=4.0.0"
|
|
34
34
|
}
|
|
35
35
|
},
|
|
36
36
|
async setup(options, nuxt) {
|
|
@@ -66,6 +66,7 @@ const module$1 = defineNuxtModule({
|
|
|
66
66
|
const blockEnabled = isComposableEnabled(composablesConfig, "block");
|
|
67
67
|
const listingEnabled = isComposableEnabled(composablesConfig, "listing");
|
|
68
68
|
const searchEnabled = isComposableEnabled(composablesConfig, "search");
|
|
69
|
+
const treeEnabled = isComposableEnabled(composablesConfig, "tree");
|
|
69
70
|
const pageEnabled = isComposableEnabled(composablesConfig, "page");
|
|
70
71
|
const contextEnabled = isComposableEnabled(composablesConfig, "context");
|
|
71
72
|
const clientEnabled = isComposableEnabled(composablesConfig, "client");
|
|
@@ -171,6 +172,14 @@ frontic: { composables: false }
|
|
|
171
172
|
{ name: "isSortAllowed", from: join(utilsDir, "state-helpers") }
|
|
172
173
|
]);
|
|
173
174
|
}
|
|
175
|
+
if (treeEnabled) {
|
|
176
|
+
addImports([
|
|
177
|
+
{ name: "useFronticTree", from: join(composablesDir, "useFronticTree") },
|
|
178
|
+
{ name: "TREE_QUERY_KEYS", from: join(composablesDir, "trees") },
|
|
179
|
+
{ name: "fetchTreeQuery", from: join(composablesDir, "trees") },
|
|
180
|
+
{ name: "createTreeCacheKey", from: join(composablesDir, "trees") }
|
|
181
|
+
]);
|
|
182
|
+
}
|
|
174
183
|
if (pageEnabled) {
|
|
175
184
|
addImports([
|
|
176
185
|
{ name: "useFronticPage", from: join(composablesDir, "useFronticPage") },
|
|
@@ -196,6 +205,7 @@ frontic: { composables: false }
|
|
|
196
205
|
block: blockEnabled,
|
|
197
206
|
listing: listingEnabled,
|
|
198
207
|
search: searchEnabled,
|
|
208
|
+
tree: treeEnabled,
|
|
199
209
|
page: pageEnabled,
|
|
200
210
|
context: contextEnabled,
|
|
201
211
|
client: clientEnabled
|
|
@@ -7,7 +7,7 @@ export { useFronticClient } from './useFronticClient.js';
|
|
|
7
7
|
export type { UseFronticClientOptions } from './useFronticClient.js';
|
|
8
8
|
export { useFronticSearch } from './useFronticSearch.js';
|
|
9
9
|
export { useFronticListing } from './useFronticListing.js';
|
|
10
|
-
export type { UseFronticListingOptions } from './useFronticListing.js';
|
|
10
|
+
export type { UseFronticListingOptions, ListingQuery } from './useFronticListing.js';
|
|
11
11
|
export { fetchListingQuery, LISTING_QUERY_KEYS, createCacheKey } from './listings.js';
|
|
12
12
|
export { useFronticBlock } from './useFronticBlock.js';
|
|
13
13
|
export type { UseFronticBlockOptions } from './useFronticBlock.js';
|
|
@@ -17,5 +17,9 @@ export { useFronticPage } from './useFronticPage.js';
|
|
|
17
17
|
export type { UseFronticPageOptions } from './useFronticPage.js';
|
|
18
18
|
export { fetchPageQuery, PAGE_QUERY_KEYS, createPageCacheKey } from './pages.js';
|
|
19
19
|
export type { FetchPageConfig } from './pages.js';
|
|
20
|
+
export { useFronticTree } from './useFronticTree.js';
|
|
21
|
+
export type { UseFronticTreeOptions } from './useFronticTree.js';
|
|
22
|
+
export { fetchTreeQuery, TREE_QUERY_KEYS, createTreeCacheKey } from './trees.js';
|
|
23
|
+
export type { FetchTreeConfig } from './trees.js';
|
|
20
24
|
export { useFronticContext } from './useFronticContext.js';
|
|
21
25
|
export type { UseFronticContextOptions } from './useFronticContext.js';
|
|
@@ -6,4 +6,6 @@ export { useFronticBlock } from "./useFronticBlock.js";
|
|
|
6
6
|
export { fetchBlockQuery, BLOCK_QUERY_KEYS, createBlockCacheKey } from "./blocks.js";
|
|
7
7
|
export { useFronticPage } from "./useFronticPage.js";
|
|
8
8
|
export { fetchPageQuery, PAGE_QUERY_KEYS, createPageCacheKey } from "./pages.js";
|
|
9
|
+
export { useFronticTree } from "./useFronticTree.js";
|
|
10
|
+
export { fetchTreeQuery, TREE_QUERY_KEYS, createTreeCacheKey } from "./trees.js";
|
|
9
11
|
export { useFronticContext } from "./useFronticContext.js";
|
|
@@ -0,0 +1,77 @@
|
|
|
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
|
+
import type { MaybeRef } from 'vue';
|
|
10
|
+
import type { FronticRequestOptions } from '../types/listing.js';
|
|
11
|
+
export interface FetchTreeConfig<TTree extends string> extends FronticRequestOptions {
|
|
12
|
+
name: TTree;
|
|
13
|
+
key?: MaybeRef<string | undefined>;
|
|
14
|
+
depth?: MaybeRef<number | undefined>;
|
|
15
|
+
staleTime?: number;
|
|
16
|
+
_client?: FrontstackClient;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Query key factory for tree queries
|
|
20
|
+
* Creates hierarchical, typed cache keys for trees
|
|
21
|
+
*
|
|
22
|
+
* The `key` slot uses an empty-string sentinel (not `'root'`) when omitted, so
|
|
23
|
+
* a request for "all root nodes" can never collide with a request for the
|
|
24
|
+
* subtree of a node literally named `'root'`. The API rejects empty strings
|
|
25
|
+
* as node keys, so the sentinel is unambiguous.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* TREE_QUERY_KEYS.root // ['trees']
|
|
30
|
+
* TREE_QUERY_KEYS.tree('CategoryNavigation') // ['trees', 'CategoryNavigation']
|
|
31
|
+
* TREE_QUERY_KEYS.detail('CategoryNavigation', 'shop', 2, 'context-token')
|
|
32
|
+
* // ['trees', 'CategoryNavigation', 'shop', '2', 'context-token']
|
|
33
|
+
* TREE_QUERY_KEYS.detail('CategoryNavigation')
|
|
34
|
+
* // ['trees', 'CategoryNavigation', '', 'all', 'default']
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
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[];
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Generic query factory for fetching trees
|
|
44
|
+
* Returns a query options object compatible with @pinia/colada
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* // With @pinia/colada
|
|
49
|
+
* const treeQuery = fetchTreeQuery({
|
|
50
|
+
* name: 'CategoryNavigation',
|
|
51
|
+
* key: 'shop',
|
|
52
|
+
* depth: 2,
|
|
53
|
+
* contextKey: token.value
|
|
54
|
+
* })
|
|
55
|
+
*
|
|
56
|
+
* const { data, status } = useQuery(treeQuery)
|
|
57
|
+
*
|
|
58
|
+
* // With useAsyncData
|
|
59
|
+
* const { data, status } = useAsyncData(
|
|
60
|
+
* treeQuery.key().join('-'),
|
|
61
|
+
* treeQuery.query
|
|
62
|
+
* )
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export declare function fetchTreeQuery<TTree extends keyof Trees>(config: FetchTreeConfig<TTree>): {
|
|
66
|
+
key: () => string[];
|
|
67
|
+
query: () => Promise<Responses[TTree]>;
|
|
68
|
+
staleTime: number;
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* Creates a cache key string from query key array
|
|
72
|
+
* Useful for useAsyncData which requires string keys
|
|
73
|
+
*
|
|
74
|
+
* @param keyArray - Query key array from TREE_QUERY_KEYS
|
|
75
|
+
* @returns String cache key
|
|
76
|
+
*/
|
|
77
|
+
export declare function createTreeCacheKey(keyArray: readonly unknown[]): string;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
function unwrapRef(ref) {
|
|
2
|
+
return typeof ref === "object" && ref !== null && "value" in ref ? ref.value : ref;
|
|
3
|
+
}
|
|
4
|
+
export const TREE_QUERY_KEYS = {
|
|
5
|
+
root: ["trees"],
|
|
6
|
+
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
|
+
]
|
|
13
|
+
};
|
|
14
|
+
export function fetchTreeQuery(config) {
|
|
15
|
+
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
|
+
},
|
|
22
|
+
query: async () => {
|
|
23
|
+
let client = config._client;
|
|
24
|
+
if (!client) {
|
|
25
|
+
const { useFronticClient } = await import("./useFronticClient.js");
|
|
26
|
+
client = useFronticClient();
|
|
27
|
+
}
|
|
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);
|
|
33
|
+
const treeQuery = {};
|
|
34
|
+
if (key !== void 0) treeQuery.key = key;
|
|
35
|
+
if (depth !== void 0) treeQuery.depth = depth;
|
|
36
|
+
return await client.tree(config.name, {
|
|
37
|
+
...Object.keys(treeQuery).length > 0 && { query: treeQuery },
|
|
38
|
+
contextKey,
|
|
39
|
+
contextDomain,
|
|
40
|
+
requestUrl
|
|
41
|
+
});
|
|
42
|
+
},
|
|
43
|
+
staleTime: config.staleTime ?? 1e3 * 60 * 5
|
|
44
|
+
// 5 minutes default
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
export function createTreeCacheKey(keyArray) {
|
|
48
|
+
return keyArray.map((part) => typeof part === "object" ? JSON.stringify(part) : String(part)).join("-");
|
|
49
|
+
}
|
|
@@ -50,6 +50,12 @@ export function useFronticClient(options) {
|
|
|
50
50
|
...secret && { secret },
|
|
51
51
|
...reqOptions
|
|
52
52
|
}),
|
|
53
|
+
tree: (name, reqOptions) => baseClient.tree(name, {
|
|
54
|
+
...contextDomain && { contextDomain },
|
|
55
|
+
...requestUrlValue && { requestUrl: requestUrlValue },
|
|
56
|
+
...secret && { secret },
|
|
57
|
+
...reqOptions
|
|
58
|
+
}),
|
|
53
59
|
page: (slug, reqOptions) => baseClient.page(slug, {
|
|
54
60
|
...requestUrlValue && { requestUrl: requestUrlValue },
|
|
55
61
|
...secret && { secret },
|
|
@@ -1,7 +1,21 @@
|
|
|
1
1
|
import type { Ref, MaybeRef } from 'vue';
|
|
2
|
-
import type { Listings, ListingParameters, Responses } from '@frontic/stack/generated-types';
|
|
2
|
+
import type { Listings, ListingParameters, ListingQueryFilters, ListingQuerySorts, Responses } from '@frontic/stack/generated-types';
|
|
3
|
+
import type { Query } from '@frontic/stack/query-types';
|
|
3
4
|
import type { FronticRequestOptions } from '../types/listing.js';
|
|
4
|
-
|
|
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 {
|
|
11
|
+
/**
|
|
12
|
+
* Query options forwarded to the Fetch API — `filter`, `sort`, `search`, `limit`, `page`.
|
|
13
|
+
* Same shape as `client.listing(name, params, { query })`.
|
|
14
|
+
*
|
|
15
|
+
* For interactive UIs that need to mutate this state in response to user input,
|
|
16
|
+
* use useFronticSearch instead.
|
|
17
|
+
*/
|
|
18
|
+
query?: MaybeRef<ListingQuery<TListing> | undefined>;
|
|
5
19
|
/**
|
|
6
20
|
* Cache stale time in milliseconds
|
|
7
21
|
* @default 300000 (5 minutes)
|
|
@@ -25,20 +39,23 @@ export interface UseFronticListingOptions extends FronticRequestOptions {
|
|
|
25
39
|
* categoryKey: 'shoes'
|
|
26
40
|
* })
|
|
27
41
|
*
|
|
28
|
-
* // With
|
|
29
|
-
* const { listing } = useFronticListing('ProductSearch', {
|
|
30
|
-
*
|
|
31
|
-
*
|
|
42
|
+
* // With a static query (matches client.listing signature)
|
|
43
|
+
* const { listing } = useFronticListing('ProductSearch', {}, {
|
|
44
|
+
* query: {
|
|
45
|
+
* filter: [{ type: 'equals', field: 'properties.color', value: 'red' }],
|
|
46
|
+
* sort: { field: 'price.amount', order: 'asc' },
|
|
47
|
+
* limit: 12,
|
|
48
|
+
* },
|
|
32
49
|
* })
|
|
33
50
|
*
|
|
34
|
-
* // Reactive
|
|
35
|
-
* const
|
|
36
|
-
* const { listing } = useFronticListing('
|
|
37
|
-
*
|
|
38
|
-
* })
|
|
51
|
+
* // Reactive query — cache keys on each distinct query value
|
|
52
|
+
* const page = ref(1)
|
|
53
|
+
* const { listing } = useFronticListing('ProductSearch', {}, {
|
|
54
|
+
* query: computed(() => ({ limit: 20, page: page.value })),
|
|
55
|
+
* })
|
|
39
56
|
* ```
|
|
40
57
|
*/
|
|
41
|
-
export declare function useFronticListing<T extends keyof Listings>(name: T, params: MaybeRef<T extends keyof ListingParameters ? ListingParameters[T] : Record<string, unknown>>, options?: UseFronticListingOptions): {
|
|
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>): {
|
|
42
59
|
/**
|
|
43
60
|
* The listing data, typed based on the listing name
|
|
44
61
|
*/
|
|
@@ -6,8 +6,7 @@ export function useFronticListing(name, params, options) {
|
|
|
6
6
|
const listingQuery = fetchListingQuery({
|
|
7
7
|
name,
|
|
8
8
|
params,
|
|
9
|
-
query:
|
|
10
|
-
// No query for simple listing - use useFronticSearch for filtering/sorting
|
|
9
|
+
query: options?.query,
|
|
11
10
|
contextKey: options?.contextKey,
|
|
12
11
|
contextDomain: options?.contextDomain,
|
|
13
12
|
requestUrl: options?.requestUrl,
|
|
@@ -50,12 +50,12 @@ export declare function useFronticSearch<TSearch extends keyof Listings, TFilter
|
|
|
50
50
|
* Current search state including active filters, sorts, pagination, and available options.
|
|
51
51
|
* Use this to build filter/sort UI components.
|
|
52
52
|
*/
|
|
53
|
-
state: import("
|
|
53
|
+
state: import("vue").Ref<SearchState<TFilters, TSorts>, SearchState<TFilters, TSorts>>;
|
|
54
54
|
/**
|
|
55
55
|
* Reactive search term. Bind to an input field for live search.
|
|
56
56
|
* Search triggers automatically when term length exceeds searchTermThreshold.
|
|
57
57
|
*/
|
|
58
|
-
searchTerm: import("
|
|
58
|
+
searchTerm: import("vue").Ref<string, string>;
|
|
59
59
|
/**
|
|
60
60
|
* Load the next page of results.
|
|
61
61
|
* Standard mode: replaces current page with the next one.
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { Ref, MaybeRef, ComputedRef } from 'vue';
|
|
2
|
+
import type { Trees, Responses } from '@frontic/stack/generated-types';
|
|
3
|
+
import type { FronticRequestOptions } from '../types/listing.js';
|
|
4
|
+
/**
|
|
5
|
+
* Extracts the `items` array type from a tree response.
|
|
6
|
+
*
|
|
7
|
+
* When the user has a generated client, `Responses[T]` resolves to the concrete
|
|
8
|
+
* tree response shape (`{ items: TreeNode[] }`) and this gives back the typed
|
|
9
|
+
* node array — so consumers get full IDE autocomplete on `node.$items`,
|
|
10
|
+
* `node.key`, and the block fields. Falls back to `unknown[]` against the
|
|
11
|
+
* ambient `Responses { [key: string]: unknown }` declaration.
|
|
12
|
+
*/
|
|
13
|
+
type TreeItems<TResponse> = TResponse extends {
|
|
14
|
+
items: infer I;
|
|
15
|
+
} ? I : unknown[];
|
|
16
|
+
export interface UseFronticTreeOptions extends FronticRequestOptions {
|
|
17
|
+
/**
|
|
18
|
+
* Optional starting node key. When provided, the response contains the
|
|
19
|
+
* subtree rooted at this node. When omitted, all root-level nodes are returned.
|
|
20
|
+
*/
|
|
21
|
+
key?: MaybeRef<string | undefined>;
|
|
22
|
+
/**
|
|
23
|
+
* Optional level limit. Controls how many levels of `$items` are included.
|
|
24
|
+
* When omitted, all levels are returned (subject to the API's 250-node cap).
|
|
25
|
+
*/
|
|
26
|
+
depth?: MaybeRef<number | undefined>;
|
|
27
|
+
/**
|
|
28
|
+
* Cache stale time in milliseconds
|
|
29
|
+
* @default 300000 (5 minutes)
|
|
30
|
+
*/
|
|
31
|
+
staleTime?: number;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Fetch a Frontic tree by name
|
|
35
|
+
*
|
|
36
|
+
* Uses Pinia Colada for state management with automatic caching,
|
|
37
|
+
* stale-while-revalidate, and smart refetching.
|
|
38
|
+
*
|
|
39
|
+
* @param name - The tree name (e.g., 'CategoryNavigation')
|
|
40
|
+
* @param options - Optional configuration
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* // Fetch the entire tree
|
|
45
|
+
* const { tree, items, status, refresh, refetch } = useFronticTree('CategoryNavigation')
|
|
46
|
+
*
|
|
47
|
+
* // Fetch a subtree starting at a specific node, capped at 2 levels
|
|
48
|
+
* const { items } = useFronticTree('CategoryNavigation', { key: 'shop', depth: 2 })
|
|
49
|
+
*
|
|
50
|
+
* // Reactive starting key
|
|
51
|
+
* const startKey = ref<string | undefined>(undefined)
|
|
52
|
+
* const { items } = useFronticTree('CategoryNavigation', { key: startKey })
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export declare function useFronticTree<T extends keyof Trees>(name: T, options?: UseFronticTreeOptions): {
|
|
56
|
+
/**
|
|
57
|
+
* The full tree response, typed based on the tree name
|
|
58
|
+
*/
|
|
59
|
+
tree: Ref<Responses[T] | undefined>;
|
|
60
|
+
/**
|
|
61
|
+
* The tree's top-level nodes (shortcut for `tree.value?.items`).
|
|
62
|
+
* Each node carries its children on `$items`.
|
|
63
|
+
*
|
|
64
|
+
* Typed via `TreeItems<Responses[T]>` so consumers get the concrete node
|
|
65
|
+
* shape from their generated client — including the `$items` recursion
|
|
66
|
+
* and the fields produced by the tree's Detail Block.
|
|
67
|
+
*/
|
|
68
|
+
items: ComputedRef<TreeItems<Responses[T]> | undefined>;
|
|
69
|
+
/**
|
|
70
|
+
* Query status: 'pending' | 'error' | 'success'
|
|
71
|
+
*/
|
|
72
|
+
status: import("vue").ShallowRef<import("@pinia/colada").DataStateStatus>;
|
|
73
|
+
/**
|
|
74
|
+
* Manually refresh the tree data (uses cache if valid)
|
|
75
|
+
*/
|
|
76
|
+
refresh: (throwOnError?: boolean) => Promise<import("@pinia/colada").DataState<unknown, Error, undefined>>;
|
|
77
|
+
/**
|
|
78
|
+
* Force a fresh fetch of the tree data, bypassing cache
|
|
79
|
+
*/
|
|
80
|
+
refetch: (throwOnError?: boolean) => Promise<import("@pinia/colada").DataState<unknown, Error, undefined>>;
|
|
81
|
+
};
|
|
82
|
+
export {};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { computed } from "vue";
|
|
2
|
+
import { useQuery } from "@pinia/colada";
|
|
3
|
+
import { fetchTreeQuery } from "./trees.js";
|
|
4
|
+
import { useFronticClient } from "./useFronticClient.js";
|
|
5
|
+
export function useFronticTree(name, options) {
|
|
6
|
+
const _client = useFronticClient();
|
|
7
|
+
const treeQuery = fetchTreeQuery({
|
|
8
|
+
name,
|
|
9
|
+
key: options?.key,
|
|
10
|
+
depth: options?.depth,
|
|
11
|
+
contextKey: options?.contextKey,
|
|
12
|
+
contextDomain: options?.contextDomain,
|
|
13
|
+
requestUrl: options?.requestUrl,
|
|
14
|
+
staleTime: options?.staleTime,
|
|
15
|
+
_client
|
|
16
|
+
});
|
|
17
|
+
const { data, status, refresh, refetch } = useQuery(treeQuery);
|
|
18
|
+
return {
|
|
19
|
+
/**
|
|
20
|
+
* The full tree response, typed based on the tree name
|
|
21
|
+
*/
|
|
22
|
+
tree: data,
|
|
23
|
+
/**
|
|
24
|
+
* The tree's top-level nodes (shortcut for `tree.value?.items`).
|
|
25
|
+
* Each node carries its children on `$items`.
|
|
26
|
+
*
|
|
27
|
+
* Typed via `TreeItems<Responses[T]>` so consumers get the concrete node
|
|
28
|
+
* shape from their generated client — including the `$items` recursion
|
|
29
|
+
* and the fields produced by the tree's Detail Block.
|
|
30
|
+
*/
|
|
31
|
+
items: computed(() => data.value?.items),
|
|
32
|
+
/**
|
|
33
|
+
* Query status: 'pending' | 'error' | 'success'
|
|
34
|
+
*/
|
|
35
|
+
status,
|
|
36
|
+
/**
|
|
37
|
+
* Manually refresh the tree data (uses cache if valid)
|
|
38
|
+
*/
|
|
39
|
+
refresh,
|
|
40
|
+
/**
|
|
41
|
+
* Force a fresh fetch of the tree data, bypassing cache
|
|
42
|
+
*/
|
|
43
|
+
refetch
|
|
44
|
+
};
|
|
45
|
+
}
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
14
|
declare module '@frontic/stack/generated-client' {
|
|
15
|
-
import type { Blocks, Listings, ListingParameters, ListingQueryFilters, ListingQuerySorts, Responses } from '@frontic/stack/generated-types'
|
|
15
|
+
import type { Blocks, Listings, ListingParameters, ListingQueryFilters, ListingQuerySorts, Responses, Trees } from '@frontic/stack/generated-types'
|
|
16
16
|
import type { Query } from '@frontic/stack/query-types'
|
|
17
17
|
|
|
18
18
|
export interface RequestOptions {
|
|
@@ -22,6 +22,11 @@ declare module '@frontic/stack/generated-client' {
|
|
|
22
22
|
proxyUrl?: string
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
export interface TreeQuery {
|
|
26
|
+
key?: string
|
|
27
|
+
depth?: number
|
|
28
|
+
}
|
|
29
|
+
|
|
25
30
|
export interface ClientConfig {
|
|
26
31
|
proxyUrl?: string
|
|
27
32
|
}
|
|
@@ -35,6 +40,7 @@ declare module '@frontic/stack/generated-client' {
|
|
|
35
40
|
query?: Query<T extends keyof ListingQueryFilters ? ListingQueryFilters[T] : unknown, T extends keyof ListingQuerySorts ? ListingQuerySorts[T] : unknown>
|
|
36
41
|
} & RequestOptions
|
|
37
42
|
) => Promise<Responses[T]>
|
|
43
|
+
tree: <T extends keyof Trees>(name: T, config?: { query?: TreeQuery } & RequestOptions) => Promise<Responses[T]>
|
|
38
44
|
page: (slug: string, config?: RequestOptions) => Promise<Page>
|
|
39
45
|
context: (token: string, config?: RequestOptions) => Promise<Context>
|
|
40
46
|
contextList: (token?: string, config?: RequestOptions) => Promise<[ContextOption[], string]>
|
|
@@ -56,6 +62,7 @@ declare module '@frontic/stack/generated-types' {
|
|
|
56
62
|
export interface ListingParameters {}
|
|
57
63
|
export interface ListingQueryFilters {}
|
|
58
64
|
export interface ListingQuerySorts {}
|
|
65
|
+
export interface Trees {}
|
|
59
66
|
export interface Responses {
|
|
60
67
|
[key: string]: unknown
|
|
61
68
|
}
|
|
@@ -370,9 +370,11 @@ export interface FetchListingConfig<TListing extends keyof Listings = keyof List
|
|
|
370
370
|
*/
|
|
371
371
|
params: MaybeRef<TListing extends keyof ListingParameters ? ListingParameters[TListing] : Record<string, unknown>>;
|
|
372
372
|
/**
|
|
373
|
-
* Query configuration (filters, sort, search, pagination)
|
|
373
|
+
* Query configuration (filters, sort, search, pagination).
|
|
374
|
+
* The factory normalises `undefined` to `{}` internally, so a `MaybeRef`
|
|
375
|
+
* wrapping `undefined` is accepted — useful for reactive query options.
|
|
374
376
|
*/
|
|
375
|
-
query?: MaybeRef<Record<string, unknown
|
|
377
|
+
query?: MaybeRef<Record<string, unknown> | undefined>;
|
|
376
378
|
/**
|
|
377
379
|
* Optional page number for pagination
|
|
378
380
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frontic/nuxt",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Nuxt module for Frontic - data fetching composables with smart caching, page routing, and context management",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Frontic",
|
|
@@ -55,27 +55,30 @@
|
|
|
55
55
|
"clean": "rm -rf dist .nuxt"
|
|
56
56
|
},
|
|
57
57
|
"dependencies": {
|
|
58
|
-
"@nuxt/kit": "^
|
|
59
|
-
"@pinia/colada": "^1.
|
|
60
|
-
"@pinia/colada-nuxt": "^0.
|
|
58
|
+
"@nuxt/kit": "^4.4.5",
|
|
59
|
+
"@pinia/colada": "^1.3.0",
|
|
60
|
+
"@pinia/colada-nuxt": "^1.0.1",
|
|
61
61
|
"@pinia/nuxt": "^0.11.0",
|
|
62
|
-
"@vueuse/core": "^
|
|
62
|
+
"@vueuse/core": "^14.3.0",
|
|
63
63
|
"pinia": "^3.0.0"
|
|
64
64
|
},
|
|
65
65
|
"devDependencies": {
|
|
66
66
|
"@nuxt/module-builder": "^1.0.2",
|
|
67
|
-
"@nuxt/schema": "^
|
|
68
|
-
"@nuxt/test-utils": "^4.0.
|
|
69
|
-
"@vitest/coverage-v8": "^
|
|
70
|
-
"h3": "^1.15.
|
|
71
|
-
"nitropack": "^2.13.
|
|
72
|
-
"nuxt": "^
|
|
73
|
-
"typescript": "
|
|
74
|
-
"vitest": "^
|
|
75
|
-
"vue": "^3.
|
|
76
|
-
"vue-tsc": "2.
|
|
67
|
+
"@nuxt/schema": "^4.4.5",
|
|
68
|
+
"@nuxt/test-utils": "^4.0.3",
|
|
69
|
+
"@vitest/coverage-v8": "^4.1.6",
|
|
70
|
+
"h3": "^1.15.11",
|
|
71
|
+
"nitropack": "^2.13.4",
|
|
72
|
+
"nuxt": "^4.4.5",
|
|
73
|
+
"typescript": "^6.0.3",
|
|
74
|
+
"vitest": "^4.1.6",
|
|
75
|
+
"vue": "^3.5.34",
|
|
76
|
+
"vue-tsc": "^3.2.9"
|
|
77
77
|
},
|
|
78
78
|
"peerDependencies": {
|
|
79
|
-
"nuxt": ">=
|
|
79
|
+
"nuxt": ">=4.0.0"
|
|
80
|
+
},
|
|
81
|
+
"engines": {
|
|
82
|
+
"node": ">=20.19.0"
|
|
80
83
|
}
|
|
81
84
|
}
|