@frontic/nuxt 0.1.11 → 0.1.13
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/dist/module.mjs +4 -0
- package/dist/runtime/composables/blocks.d.ts +68 -0
- package/dist/runtime/composables/index.d.ts +21 -0
- package/dist/runtime/composables/listings.d.ts +64 -0
- package/dist/runtime/composables/pages.d.ts +65 -0
- package/dist/runtime/composables/useFronticBlock.d.ts +54 -0
- package/dist/runtime/composables/useFronticClient.d.ts +56 -0
- package/dist/runtime/composables/useFronticClient.js +5 -4
- package/dist/runtime/composables/useFronticContext.d.ts +81 -0
- package/dist/runtime/composables/useFronticContext.js +6 -4
- package/dist/runtime/composables/useFronticListing.d.ts +58 -0
- package/dist/runtime/composables/useFronticPage.d.ts +96 -0
- package/dist/runtime/composables/useFronticPage.js +5 -3
- package/dist/runtime/composables/useFronticSearch.d.ts +124 -0
- package/dist/runtime/composables/useFronticSearch.js +4 -3
- package/dist/runtime/frontic-stack.d.ts +142 -0
- package/dist/runtime/server/api/frontic/[...path].d.ts +2 -0
- package/dist/runtime/server/api/frontic/[...path].js +1 -1
- package/dist/runtime/types/index.d.ts +13 -0
- package/dist/runtime/types/listing.d.ts +354 -0
- package/dist/runtime/types/search.d.ts +241 -0
- package/dist/runtime/utils/filter-helpers.d.ts +108 -0
- package/dist/runtime/utils/index.d.ts +7 -0
- package/dist/runtime/utils/state-helpers.d.ts +120 -0
- package/package.json +1 -1
package/dist/module.mjs
CHANGED
|
@@ -99,6 +99,10 @@ frontic: { composables: false }
|
|
|
99
99
|
tsConfig.compilerOptions ||= {};
|
|
100
100
|
tsConfig.compilerOptions.paths ||= {};
|
|
101
101
|
tsConfig.compilerOptions.paths["@frontic/stack/*"] = [`../${clientDirName}/*`];
|
|
102
|
+
if (hasFronticClient) {
|
|
103
|
+
tsConfig.include = tsConfig.include || [];
|
|
104
|
+
tsConfig.include.push(`../${clientDirName}/generated-types.d.ts`, `../${clientDirName}/fetch-api.d.ts`);
|
|
105
|
+
}
|
|
102
106
|
});
|
|
103
107
|
if (anyComposableEnabled) {
|
|
104
108
|
if (!hasNuxtModule("@pinia/nuxt", nuxt)) {
|
|
@@ -0,0 +1,68 @@
|
|
|
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
|
+
import type { MaybeRef } from 'vue';
|
|
10
|
+
import type { FronticRequestOptions } from '../types/listing.js';
|
|
11
|
+
export interface FetchBlockConfig<TBlock extends string> extends FronticRequestOptions {
|
|
12
|
+
name: TBlock;
|
|
13
|
+
key: MaybeRef<string>;
|
|
14
|
+
staleTime?: number;
|
|
15
|
+
_client?: FrontstackClient;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Query key factory for block queries
|
|
19
|
+
* Creates hierarchical, typed cache keys for blocks
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* BLOCK_QUERY_KEYS.root // ['blocks']
|
|
24
|
+
* BLOCK_QUERY_KEYS.block('ProductCard') // ['blocks', 'ProductCard']
|
|
25
|
+
* BLOCK_QUERY_KEYS.detail('ProductCard', 'abc123', 'context-token')
|
|
26
|
+
* // ['blocks', 'ProductCard', 'abc123', 'context-token']
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
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[];
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Generic query factory for fetching blocks
|
|
36
|
+
* Returns a query options object compatible with @pinia/colada
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* // With @pinia/colada
|
|
41
|
+
* const blockQuery = fetchBlockQuery({
|
|
42
|
+
* name: 'ProductCard',
|
|
43
|
+
* key: productId,
|
|
44
|
+
* contextKey: token.value
|
|
45
|
+
* })
|
|
46
|
+
*
|
|
47
|
+
* const { data, status } = useQuery(blockQuery)
|
|
48
|
+
*
|
|
49
|
+
* // With useAsyncData
|
|
50
|
+
* const { data, status } = useAsyncData(
|
|
51
|
+
* blockQuery.key().join('-'),
|
|
52
|
+
* blockQuery.query
|
|
53
|
+
* )
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export declare function fetchBlockQuery<TBlock extends keyof Blocks>(config: FetchBlockConfig<TBlock>): {
|
|
57
|
+
key: () => string[];
|
|
58
|
+
query: () => Promise<Responses[TBlock]>;
|
|
59
|
+
staleTime: number;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Creates a cache key string from query key array
|
|
63
|
+
* Useful for useAsyncData which requires string keys
|
|
64
|
+
*
|
|
65
|
+
* @param keyArray - Query key array from BLOCK_QUERY_KEYS
|
|
66
|
+
* @returns String cache key
|
|
67
|
+
*/
|
|
68
|
+
export declare function createBlockCacheKey(keyArray: readonly unknown[]): string;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Frontic Runtime Composables
|
|
3
|
+
*
|
|
4
|
+
* This file exports all composables that are auto-imported by the module.
|
|
5
|
+
*/
|
|
6
|
+
export { useFronticClient } from './useFronticClient.js';
|
|
7
|
+
export type { UseFronticClientOptions } from './useFronticClient.js';
|
|
8
|
+
export { useFronticSearch } from './useFronticSearch.js';
|
|
9
|
+
export { useFronticListing } from './useFronticListing.js';
|
|
10
|
+
export type { UseFronticListingOptions } from './useFronticListing.js';
|
|
11
|
+
export { fetchListingQuery, LISTING_QUERY_KEYS, createCacheKey } from './listings.js';
|
|
12
|
+
export { useFronticBlock } from './useFronticBlock.js';
|
|
13
|
+
export type { UseFronticBlockOptions } from './useFronticBlock.js';
|
|
14
|
+
export { fetchBlockQuery, BLOCK_QUERY_KEYS, createBlockCacheKey } from './blocks.js';
|
|
15
|
+
export type { FetchBlockConfig } from './blocks.js';
|
|
16
|
+
export { useFronticPage } from './useFronticPage.js';
|
|
17
|
+
export type { UseFronticPageOptions } from './useFronticPage.js';
|
|
18
|
+
export { fetchPageQuery, PAGE_QUERY_KEYS, createPageCacheKey } from './pages.js';
|
|
19
|
+
export type { FetchPageConfig } from './pages.js';
|
|
20
|
+
export { useFronticContext } from './useFronticContext.js';
|
|
21
|
+
export type { UseFronticContextOptions } from './useFronticContext.js';
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Listing Query Factory
|
|
3
|
+
*
|
|
4
|
+
* Provides query key management and query factory functions for Frontic listings.
|
|
5
|
+
* Compatible with both @pinia/colada and useAsyncData patterns.
|
|
6
|
+
*/
|
|
7
|
+
import type { FrontstackClient } from '@frontic/stack/generated-client';
|
|
8
|
+
import type { Listings, Responses } from '@frontic/stack/generated-types';
|
|
9
|
+
import type { FetchListingConfig } from '../types/listing.js';
|
|
10
|
+
/**
|
|
11
|
+
* Query key factory for listing queries
|
|
12
|
+
* Creates hierarchical, typed cache keys for listings
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* LISTING_QUERY_KEYS.root // ['listings']
|
|
17
|
+
* LISTING_QUERY_KEYS.listing('ProductSearch') // ['listings', 'ProductSearch']
|
|
18
|
+
* LISTING_QUERY_KEYS.detail('ProductSearch', { categoryKey: 'shoes' }, { filter: [] })
|
|
19
|
+
* // ['listings', 'ProductSearch', { categoryKey: 'shoes' }, { filter: [] }]
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
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[];
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Generic query factory for fetching listings
|
|
29
|
+
* Returns a query options object compatible with @pinia/colada
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* // With @pinia/colada
|
|
34
|
+
* const productQuery = fetchListingQuery({
|
|
35
|
+
* name: 'ProductSearch',
|
|
36
|
+
* params: {},
|
|
37
|
+
* query: { filter: [], sort: { field: 'name', order: 'asc' } },
|
|
38
|
+
* contextKey: token.value
|
|
39
|
+
* })
|
|
40
|
+
*
|
|
41
|
+
* const { data, status } = useQuery(productQuery)
|
|
42
|
+
*
|
|
43
|
+
* // With useAsyncData
|
|
44
|
+
* const { data, status } = useAsyncData(
|
|
45
|
+
* productQuery.key().join('-'),
|
|
46
|
+
* productQuery.query
|
|
47
|
+
* )
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export declare function fetchListingQuery<TListing extends keyof Listings>(config: FetchListingConfig<TListing> & {
|
|
51
|
+
_client?: FrontstackClient;
|
|
52
|
+
}): {
|
|
53
|
+
key: () => string[];
|
|
54
|
+
query: () => Promise<Responses[TListing]>;
|
|
55
|
+
staleTime: number;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Creates a cache key string from query key array
|
|
59
|
+
* Useful for useAsyncData which requires string keys
|
|
60
|
+
*
|
|
61
|
+
* @param keyArray - Query key array from LISTING_QUERY_KEYS
|
|
62
|
+
* @returns String cache key
|
|
63
|
+
*/
|
|
64
|
+
export declare function createCacheKey(keyArray: readonly unknown[]): string;
|
|
@@ -0,0 +1,65 @@
|
|
|
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
|
+
import type { MaybeRef } from 'vue';
|
|
9
|
+
import type { FronticRequestOptions } from '../types/listing.js';
|
|
10
|
+
export interface FetchPageConfig extends FronticRequestOptions {
|
|
11
|
+
slug: MaybeRef<string>;
|
|
12
|
+
staleTime?: number;
|
|
13
|
+
_client?: FrontstackClient;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Query key factory for page queries
|
|
17
|
+
* Creates hierarchical, typed cache keys for pages
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* PAGE_QUERY_KEYS.root // ['pages']
|
|
22
|
+
* PAGE_QUERY_KEYS.page('demo-shop.com/uk/products') // ['pages', 'demo-shop.com/uk/products']
|
|
23
|
+
* PAGE_QUERY_KEYS.detail('demo-shop.com/uk/products', 'context-token')
|
|
24
|
+
* // ['pages', 'demo-shop.com/uk/products', 'context-token']
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export declare const PAGE_QUERY_KEYS: {
|
|
28
|
+
root: string[];
|
|
29
|
+
page: (slug: string) => string[];
|
|
30
|
+
detail: (slug: string, contextKey?: string) => string[];
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Generic query factory for fetching pages
|
|
34
|
+
* Returns a query options object compatible with @pinia/colada
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* // With @pinia/colada
|
|
39
|
+
* const pageQuery = fetchPageQuery({
|
|
40
|
+
* slug: 'demo-shop.com/uk/products/shoes',
|
|
41
|
+
* contextKey: token.value
|
|
42
|
+
* })
|
|
43
|
+
*
|
|
44
|
+
* const { data, status } = useQuery(pageQuery)
|
|
45
|
+
*
|
|
46
|
+
* // With useAsyncData
|
|
47
|
+
* const { data, status } = useAsyncData(
|
|
48
|
+
* pageQuery.key().join('-'),
|
|
49
|
+
* pageQuery.query
|
|
50
|
+
* )
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export declare function fetchPageQuery(config: FetchPageConfig): {
|
|
54
|
+
key: () => string[];
|
|
55
|
+
query: () => Promise<Page>;
|
|
56
|
+
staleTime: number;
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Creates a cache key string from query key array
|
|
60
|
+
* Useful for useAsyncData which requires string keys
|
|
61
|
+
*
|
|
62
|
+
* @param keyArray - Query key array from PAGE_QUERY_KEYS
|
|
63
|
+
* @returns String cache key
|
|
64
|
+
*/
|
|
65
|
+
export declare function createPageCacheKey(keyArray: readonly unknown[]): string;
|
|
@@ -0,0 +1,54 @@
|
|
|
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;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Fetch a Frontic block by name and key
|
|
13
|
+
*
|
|
14
|
+
* Uses Pinia Colada for state management with automatic caching,
|
|
15
|
+
* stale-while-revalidate, and smart refetching.
|
|
16
|
+
*
|
|
17
|
+
* @param name - The block type name (e.g., 'ProductCard', 'CategoryFull')
|
|
18
|
+
* @param key - The unique identifier for the block
|
|
19
|
+
* @param options - Optional configuration
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* // Basic usage
|
|
24
|
+
* const { block, status, refresh, refetch } = useFronticBlock('ProductCard', productId)
|
|
25
|
+
*
|
|
26
|
+
* // With context and custom stale time
|
|
27
|
+
* const { block } = useFronticBlock('ProductFull', productId, {
|
|
28
|
+
* contextKey: contextToken,
|
|
29
|
+
* staleTime: 1000 * 60 * 10, // 10 minutes
|
|
30
|
+
* })
|
|
31
|
+
*
|
|
32
|
+
* // Reactive key
|
|
33
|
+
* const productKey = ref('product-123')
|
|
34
|
+
* const { block } = useFronticBlock('ProductCard', productKey)
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export declare function useFronticBlock<T extends keyof Blocks>(name: T, key: MaybeRef<string>, options?: UseFronticBlockOptions): {
|
|
38
|
+
/**
|
|
39
|
+
* The block data, typed based on the block name
|
|
40
|
+
*/
|
|
41
|
+
block: Ref<Responses[T] | undefined>;
|
|
42
|
+
/**
|
|
43
|
+
* Query status: 'pending' | 'error' | 'success'
|
|
44
|
+
*/
|
|
45
|
+
status: import("vue").ShallowRef<import("@pinia/colada").DataStateStatus>;
|
|
46
|
+
/**
|
|
47
|
+
* Manually refresh the block data (uses cache if valid)
|
|
48
|
+
*/
|
|
49
|
+
refresh: (throwOnError?: boolean) => Promise<import("@pinia/colada").DataState<unknown, Error, undefined>>;
|
|
50
|
+
/**
|
|
51
|
+
* Force a fresh fetch of the block data, bypassing cache
|
|
52
|
+
*/
|
|
53
|
+
refetch: (throwOnError?: boolean) => Promise<import("@pinia/colada").DataState<unknown, Error, undefined>>;
|
|
54
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { FrontstackClient } from '@frontic/stack/generated-client';
|
|
2
|
+
export interface UseFronticClientOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Override proxy behavior
|
|
5
|
+
* - `true`: Force proxy (even on server)
|
|
6
|
+
* - `false`: Force direct API (even on client)
|
|
7
|
+
* - `string`: Custom proxy path
|
|
8
|
+
* - `undefined`: Use default behavior (proxy on client, direct on server)
|
|
9
|
+
*/
|
|
10
|
+
proxy?: boolean | string;
|
|
11
|
+
/**
|
|
12
|
+
* Override contextDomain auto-detection
|
|
13
|
+
* - `false`: Disable auto-injection
|
|
14
|
+
* - `string`: Use specific domain
|
|
15
|
+
* - `undefined`: Auto-detect from request URL
|
|
16
|
+
*/
|
|
17
|
+
contextDomain?: false | string;
|
|
18
|
+
/**
|
|
19
|
+
* Override requestUrl tracking
|
|
20
|
+
* - `false`: Disable auto-injection
|
|
21
|
+
* - `string`: Use specific URL
|
|
22
|
+
* - `undefined`: Auto-detect from request URL
|
|
23
|
+
*/
|
|
24
|
+
requestUrl?: false | string;
|
|
25
|
+
/**
|
|
26
|
+
* API secret for authenticating Fetch API requests (server-only)
|
|
27
|
+
* - `string`: Use specific secret
|
|
28
|
+
* - `undefined`: Use module config fetchApiSecret (on server) or none (on client)
|
|
29
|
+
*
|
|
30
|
+
* Note: On client-side, secrets are injected by the proxy server instead
|
|
31
|
+
*/
|
|
32
|
+
secret?: string;
|
|
33
|
+
}
|
|
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 FrontstackClient 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;
|
|
@@ -1,18 +1,19 @@
|
|
|
1
|
-
import { useRuntimeConfig, useRequestURL } from "
|
|
1
|
+
import { useRuntimeConfig, useRequestURL } from "nuxt/app";
|
|
2
2
|
import { createClient } from "@frontic/stack/generated-client";
|
|
3
3
|
export function useFronticClient(options) {
|
|
4
4
|
const config = useRuntimeConfig();
|
|
5
|
+
const fronticConfig = config.public.frontic;
|
|
5
6
|
const requestURL = useRequestURL();
|
|
6
7
|
const resolveProxyUrl = () => {
|
|
7
8
|
if (options?.proxy === false) return void 0;
|
|
8
|
-
if (options?.proxy === true) return
|
|
9
|
+
if (options?.proxy === true) return fronticConfig?.proxyPath;
|
|
9
10
|
if (typeof options?.proxy === "string") return options.proxy;
|
|
10
|
-
return import.meta.server ? void 0 :
|
|
11
|
+
return import.meta.server ? void 0 : fronticConfig?.proxyPath;
|
|
11
12
|
};
|
|
12
13
|
const resolveContextDomain = () => {
|
|
13
14
|
if (options?.contextDomain === false) return void 0;
|
|
14
15
|
if (typeof options?.contextDomain === "string") return options.contextDomain;
|
|
15
|
-
return
|
|
16
|
+
return fronticConfig?.contextDomain;
|
|
16
17
|
};
|
|
17
18
|
const resolveRequestUrl = () => {
|
|
18
19
|
if (options?.requestUrl === false) return void 0;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { Ref } from 'vue';
|
|
2
|
+
export interface UseFronticContextOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Cookie name for persisting the context token
|
|
5
|
+
* @default 'fs-context'
|
|
6
|
+
*/
|
|
7
|
+
cookieName?: string;
|
|
8
|
+
/**
|
|
9
|
+
* Cookie max age in seconds
|
|
10
|
+
* @default 31536000 (1 year)
|
|
11
|
+
*/
|
|
12
|
+
cookieMaxAge?: number;
|
|
13
|
+
/**
|
|
14
|
+
* Disable automatic context fetching and cookie management
|
|
15
|
+
*
|
|
16
|
+
* When true:
|
|
17
|
+
* - The context endpoint will NOT be called automatically on mount
|
|
18
|
+
* - No context cookie will be set automatically
|
|
19
|
+
* - You must manually call `refresh()` to fetch contexts
|
|
20
|
+
* - You are responsible for storing and managing the context token
|
|
21
|
+
*
|
|
22
|
+
* @default false (uses module config)
|
|
23
|
+
*/
|
|
24
|
+
disableContext?: boolean;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Manage Frontic locale/region context with cookie persistence
|
|
28
|
+
*
|
|
29
|
+
* @param options - Optional configuration
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* const {
|
|
34
|
+
* contexts, // Available regions/locales
|
|
35
|
+
* current, // Current context
|
|
36
|
+
* token, // Context token
|
|
37
|
+
* update, // Switch locale
|
|
38
|
+
* isLoading,
|
|
39
|
+
* refresh,
|
|
40
|
+
* } = useFronticContext()
|
|
41
|
+
*
|
|
42
|
+
* // Switch locale
|
|
43
|
+
* await update({ region: 'de', locale: 'de-DE' })
|
|
44
|
+
*
|
|
45
|
+
* // Display locale switcher
|
|
46
|
+
* <select @change="update({ region: $event.target.value, locale: currentLocale })">
|
|
47
|
+
* <option v-for="ctx in contexts" :value="ctx.region">
|
|
48
|
+
* {{ ctx.region }} ({{ ctx.currency }})
|
|
49
|
+
* </option>
|
|
50
|
+
* </select>
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export declare function useFronticContext(options?: UseFronticContextOptions): {
|
|
54
|
+
/**
|
|
55
|
+
* Available context options (regions/locales)
|
|
56
|
+
*/
|
|
57
|
+
contexts: Readonly<Ref<ContextOption[]>>;
|
|
58
|
+
/**
|
|
59
|
+
* Current context (region, locale, scope, token)
|
|
60
|
+
*/
|
|
61
|
+
current: Readonly<Ref<Context | null>>;
|
|
62
|
+
/**
|
|
63
|
+
* Context token (used for API calls)
|
|
64
|
+
*/
|
|
65
|
+
token: Readonly<Ref<string | null>>;
|
|
66
|
+
/**
|
|
67
|
+
* Update the current context (switch locale/region)
|
|
68
|
+
*/
|
|
69
|
+
update: (context: {
|
|
70
|
+
region: string;
|
|
71
|
+
locale: string;
|
|
72
|
+
}) => Promise<void>;
|
|
73
|
+
/**
|
|
74
|
+
* Loading state
|
|
75
|
+
*/
|
|
76
|
+
isLoading: Readonly<Ref<boolean>>;
|
|
77
|
+
/**
|
|
78
|
+
* Manually refresh contexts and current context
|
|
79
|
+
*/
|
|
80
|
+
refresh: () => Promise<void>;
|
|
81
|
+
};
|
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import { useRuntimeConfig, useCookie
|
|
1
|
+
import { useRuntimeConfig, useCookie } from "nuxt/app";
|
|
2
|
+
import { ref, readonly, onMounted } from "vue";
|
|
2
3
|
import { useFronticClient } from "./useFronticClient.js";
|
|
3
4
|
export function useFronticContext(options) {
|
|
4
5
|
const client = useFronticClient();
|
|
5
6
|
const config = useRuntimeConfig();
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
7
|
+
const fronticConfig = config.public.frontic;
|
|
8
|
+
const cookieName = options?.cookieName ?? fronticConfig?.contextCookieName ?? "fs-context";
|
|
9
|
+
const cookieMaxAge = options?.cookieMaxAge ?? fronticConfig?.contextCookieMaxAge ?? 60 * 60 * 24 * 365;
|
|
10
|
+
const disableContext = options?.disableContext ?? fronticConfig?.disableContext ?? false;
|
|
9
11
|
const tokenCookie = disableContext ? null : useCookie(cookieName, { maxAge: cookieMaxAge });
|
|
10
12
|
const token = ref(tokenCookie?.value ?? null);
|
|
11
13
|
const contexts = ref([]);
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { Ref, MaybeRef } from 'vue';
|
|
2
|
+
import type { Listings, ListingParameters, Responses } from '@frontic/stack/generated-types';
|
|
3
|
+
import type { FronticRequestOptions } from '../types/listing.js';
|
|
4
|
+
export interface UseFronticListingOptions extends FronticRequestOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Cache stale time in milliseconds
|
|
7
|
+
* @default 300000 (5 minutes)
|
|
8
|
+
*/
|
|
9
|
+
staleTime?: number;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Fetch a Frontic listing by name and parameters
|
|
13
|
+
*
|
|
14
|
+
* Uses Pinia Colada for state management with automatic caching,
|
|
15
|
+
* stale-while-revalidate, and smart refetching.
|
|
16
|
+
*
|
|
17
|
+
* @param name - The listing name (e.g., 'ProductSearch', 'CategoryProducts')
|
|
18
|
+
* @param params - Parameters to pass to the listing endpoint
|
|
19
|
+
* @param options - Optional configuration
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* // Basic usage
|
|
24
|
+
* const { listing, status, refresh } = useFronticListing('CategoryProducts', {
|
|
25
|
+
* categoryKey: 'shoes'
|
|
26
|
+
* })
|
|
27
|
+
*
|
|
28
|
+
* // With context and custom stale time
|
|
29
|
+
* const { listing } = useFronticListing('ProductSearch', { query: searchQuery }, {
|
|
30
|
+
* contextKey: contextToken,
|
|
31
|
+
* staleTime: 1000 * 60 * 10, // 10 minutes
|
|
32
|
+
* })
|
|
33
|
+
*
|
|
34
|
+
* // Reactive params
|
|
35
|
+
* const categoryKey = ref('shoes')
|
|
36
|
+
* const { listing } = useFronticListing('CategoryProducts', computed(() => ({
|
|
37
|
+
* categoryKey: categoryKey.value
|
|
38
|
+
* })))
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export declare function useFronticListing<T extends keyof Listings>(name: T, params: MaybeRef<T extends keyof ListingParameters ? ListingParameters[T] : Record<string, unknown>>, options?: UseFronticListingOptions): {
|
|
42
|
+
/**
|
|
43
|
+
* The listing data, typed based on the listing name
|
|
44
|
+
*/
|
|
45
|
+
listing: Ref<Responses[T] | undefined>;
|
|
46
|
+
/**
|
|
47
|
+
* Query status: 'pending' | 'error' | 'success'
|
|
48
|
+
*/
|
|
49
|
+
status: import("vue").ShallowRef<import("@pinia/colada").DataStateStatus>;
|
|
50
|
+
/**
|
|
51
|
+
* Manually refresh the listing data (uses cache if valid)
|
|
52
|
+
*/
|
|
53
|
+
refresh: (throwOnError?: boolean) => Promise<import("@pinia/colada").DataState<unknown, Error, undefined>>;
|
|
54
|
+
/**
|
|
55
|
+
* Force a fresh fetch of the listing data, bypassing cache
|
|
56
|
+
*/
|
|
57
|
+
refetch: (throwOnError?: boolean) => Promise<import("@pinia/colada").DataState<unknown, Error, undefined>>;
|
|
58
|
+
};
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { Ref, MaybeRef, ComputedRef } from 'vue';
|
|
2
|
+
import type { FronticRequestOptions } from '../types/listing.js';
|
|
3
|
+
export interface UseFronticPageOptions extends FronticRequestOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Automatically redirect on 301 responses
|
|
6
|
+
* @default true
|
|
7
|
+
*/
|
|
8
|
+
redirectOn301?: boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Throw a 404 error when page is not found
|
|
11
|
+
* @default true
|
|
12
|
+
*/
|
|
13
|
+
throwOn404?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Cache stale time in milliseconds
|
|
16
|
+
* @default 300000 (5 minutes)
|
|
17
|
+
*/
|
|
18
|
+
staleTime?: number;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Fetch a Frontic page by slug with automatic redirect handling
|
|
22
|
+
*
|
|
23
|
+
* Uses Pinia Colada for state management with automatic caching,
|
|
24
|
+
* stale-while-revalidate, and smart refetching.
|
|
25
|
+
*
|
|
26
|
+
* @param slug - The page slug (e.g., 'demo-shop.com/uk/products/shoes'). If not provided, constructs from current route.
|
|
27
|
+
* @param options - Optional configuration
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* // Auto-detect from current route
|
|
32
|
+
* const { page, data, type, block, alternates, status, refresh, refetch } = useFronticPage()
|
|
33
|
+
*
|
|
34
|
+
* // Explicit slug
|
|
35
|
+
* const page = useFronticPage('demo-shop.com/uk/products/shoes')
|
|
36
|
+
*
|
|
37
|
+
* // With options
|
|
38
|
+
* const page = useFronticPage(slug, {
|
|
39
|
+
* redirectOn301: false, // Handle redirects manually
|
|
40
|
+
* throwOn404: false, // Handle 404 manually
|
|
41
|
+
* contextKey: contextToken,
|
|
42
|
+
* staleTime: 1000 * 60 * 10,
|
|
43
|
+
* })
|
|
44
|
+
*
|
|
45
|
+
* // For hreflang tags
|
|
46
|
+
* useHead({
|
|
47
|
+
* link: alternates.value?.map(alt => ({
|
|
48
|
+
* rel: 'alternate',
|
|
49
|
+
* hreflang: alt.lang ?? alt.locale,
|
|
50
|
+
* href: alt.href,
|
|
51
|
+
* })) ?? []
|
|
52
|
+
* })
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export declare function useFronticPage(slug?: MaybeRef<string>, options?: UseFronticPageOptions): {
|
|
56
|
+
/**
|
|
57
|
+
* Raw page response
|
|
58
|
+
*/
|
|
59
|
+
page: Ref<Page | undefined>;
|
|
60
|
+
/**
|
|
61
|
+
* Page data (the actual content block data)
|
|
62
|
+
*/
|
|
63
|
+
data: ComputedRef<Page["data"] | undefined>;
|
|
64
|
+
/**
|
|
65
|
+
* Page type (e.g., 'ProductCategory', 'Product', 'Brand')
|
|
66
|
+
*/
|
|
67
|
+
type: ComputedRef<string | undefined>;
|
|
68
|
+
/**
|
|
69
|
+
* Block name to render (e.g., 'CategoryFull', 'ProductFull')
|
|
70
|
+
*/
|
|
71
|
+
block: ComputedRef<string | undefined>;
|
|
72
|
+
/**
|
|
73
|
+
* Route metadata (code, redirect, alternates, context)
|
|
74
|
+
*/
|
|
75
|
+
route: ComputedRef<PageRoute | undefined>;
|
|
76
|
+
/**
|
|
77
|
+
* Alternate URLs for hreflang tags
|
|
78
|
+
*/
|
|
79
|
+
alternates: ComputedRef<AlternateRoute[] | undefined>;
|
|
80
|
+
/**
|
|
81
|
+
* Suggested route when context changes (for locale switching)
|
|
82
|
+
*/
|
|
83
|
+
suggested: ComputedRef<string | undefined>;
|
|
84
|
+
/**
|
|
85
|
+
* Query status: 'pending' | 'error' | 'success'
|
|
86
|
+
*/
|
|
87
|
+
status: import("vue").ShallowRef<import("@pinia/colada").DataStateStatus>;
|
|
88
|
+
/**
|
|
89
|
+
* Manually refresh the page data (uses cache if valid)
|
|
90
|
+
*/
|
|
91
|
+
refresh: (throwOnError?: boolean) => Promise<import("@pinia/colada").DataState<Page, Error, undefined>>;
|
|
92
|
+
/**
|
|
93
|
+
* Force a fresh fetch of the page data, bypassing cache
|
|
94
|
+
*/
|
|
95
|
+
refetch: (throwOnError?: boolean) => Promise<import("@pinia/colada").DataState<Page, Error, undefined>>;
|
|
96
|
+
};
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import { useRequestURL, useRuntimeConfig,
|
|
1
|
+
import { useRequestURL, useRuntimeConfig, navigateTo, createError } from "nuxt/app";
|
|
2
|
+
import { computed, toValue, watch } from "vue";
|
|
2
3
|
import { useQuery } from "@pinia/colada";
|
|
3
4
|
import { fetchPageQuery } from "./pages.js";
|
|
4
5
|
import { useFronticClient } from "./useFronticClient.js";
|
|
5
6
|
export function useFronticPage(slug, options) {
|
|
6
7
|
const requestURL = useRequestURL();
|
|
7
8
|
const config = useRuntimeConfig();
|
|
9
|
+
const fronticConfig = config.public.frontic;
|
|
8
10
|
const pageSlug = computed(() => {
|
|
9
11
|
if (slug) return toValue(slug);
|
|
10
12
|
return `${requestURL.host}${requestURL.pathname}`;
|
|
@@ -19,8 +21,8 @@ export function useFronticPage(slug, options) {
|
|
|
19
21
|
_client
|
|
20
22
|
});
|
|
21
23
|
const { data, status, refresh, refetch } = useQuery(pageQuery);
|
|
22
|
-
const redirectOn301 = options?.redirectOn301 ??
|
|
23
|
-
const throwOn404 = options?.throwOn404 ??
|
|
24
|
+
const redirectOn301 = options?.redirectOn301 ?? fronticConfig?.redirectOn301 ?? true;
|
|
25
|
+
const throwOn404 = options?.throwOn404 ?? fronticConfig?.throwOn404 ?? true;
|
|
24
26
|
watch(
|
|
25
27
|
data,
|
|
26
28
|
(page) => {
|