@frontic/nuxt 1.1.1 → 1.3.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 +37 -1
- package/dist/module.json +1 -1
- package/dist/runtime/composables/analytics.d.ts +7 -1
- package/dist/runtime/composables/blocks.js +3 -3
- package/dist/runtime/composables/listings.d.ts +22 -0
- package/dist/runtime/composables/listings.js +30 -15
- package/dist/runtime/composables/pages.js +3 -3
- package/dist/runtime/composables/trees.js +3 -3
- package/dist/runtime/composables/useFronticBlock.js +4 -2
- package/dist/runtime/composables/useFronticClient.d.ts +30 -0
- package/dist/runtime/composables/useFronticClient.js +45 -22
- package/dist/runtime/composables/useFronticContext.d.ts +4 -1
- package/dist/runtime/composables/useFronticContext.js +15 -20
- package/dist/runtime/composables/useFronticListing.d.ts +10 -4
- package/dist/runtime/composables/useFronticListing.js +6 -2
- package/dist/runtime/composables/useFronticPage.js +4 -2
- package/dist/runtime/composables/useFronticSearch.js +49 -13
- package/dist/runtime/composables/useFronticTree.js +4 -2
- package/dist/runtime/composables/useSearchUrlSync.d.ts +39 -0
- package/dist/runtime/composables/useSearchUrlSync.js +53 -0
- package/dist/runtime/server/client.d.ts +55 -0
- package/dist/runtime/server/client.js +53 -0
- package/dist/runtime/types/index.d.ts +1 -1
- package/dist/runtime/types/listing.d.ts +38 -2
- package/dist/runtime/types/options.d.ts +18 -4
- package/dist/runtime/types/registry.d.ts +21 -2
- package/dist/runtime/types/search.d.ts +63 -2
- package/dist/runtime/utils/describe-error.d.ts +19 -0
- package/dist/runtime/utils/describe-error.js +16 -0
- package/dist/runtime/utils/query-keys.d.ts +10 -5
- package/dist/runtime/utils/query-keys.js +5 -0
- package/dist/runtime/utils/request-defaults.d.ts +11 -0
- package/dist/runtime/utils/request-defaults.js +8 -0
- package/dist/runtime/utils/resolve-context-key.d.ts +30 -0
- package/dist/runtime/utils/resolve-context-key.js +37 -0
- package/dist/runtime/utils/search-url.d.ts +90 -0
- package/dist/runtime/utils/search-url.js +184 -0
- package/dist/runtime/utils/state-helpers.d.ts +13 -0
- package/dist/runtime/utils/state-helpers.js +6 -0
- package/package.json +5 -1
package/README.md
CHANGED
|
@@ -100,8 +100,44 @@ Manage locale and region:
|
|
|
100
100
|
|
|
101
101
|
```typescript
|
|
102
102
|
const { contexts, current, update } = useFronticContext()
|
|
103
|
+
await update({ region: 'de', locale: 'de-DE' })
|
|
103
104
|
```
|
|
104
105
|
|
|
106
|
+
The selection is stored against a context token, persisted in the `fs-context`
|
|
107
|
+
cookie. Every composable resolves that token and sends it — you do not pass it
|
|
108
|
+
per call — and it is part of every cache key, so a switch re-keys each active
|
|
109
|
+
query and the new context's content is fetched. `contextKey` overrides it for one
|
|
110
|
+
call, `contextKey: false` opts a call out so it resolves through the domain
|
|
111
|
+
alone (useful behind a shared cache), and `disableContext: true` hands token
|
|
112
|
+
management to you entirely.
|
|
113
|
+
|
|
114
|
+
### Server routes
|
|
115
|
+
|
|
116
|
+
`useFronticClient` is a Nuxt app composable and does not run in Nitro. A `server/`
|
|
117
|
+
route uses `createFronticServerClient(event)`, which resolves the context domain
|
|
118
|
+
from config, the visitor's token from the request's context cookie, and the
|
|
119
|
+
request URL from the event — and reports failures on the Nitro
|
|
120
|
+
`frontic:fetch:error` hook:
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
// server/api/stock.get.ts
|
|
124
|
+
import { createFronticServerClient } from '@frontic/nuxt/server'
|
|
125
|
+
|
|
126
|
+
export default defineEventHandler(async (event) => {
|
|
127
|
+
const client = createFronticServerClient(event)
|
|
128
|
+
return await client.listing('ProductSearch', {}, { query: { limit: 5 } })
|
|
129
|
+
})
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
The API secret needs no wiring — the module's Nitro plugin attaches it to every
|
|
133
|
+
Frontic request the server makes. `contextDomain: true` (read the active i18n
|
|
134
|
+
locale) cannot resolve here, since there is no i18n instance in Nitro; pass the
|
|
135
|
+
domain per call in that setup.
|
|
136
|
+
The module's `disableContext` is what decides whether the cookie is read here —
|
|
137
|
+
a per-instance `useFronticContext({ disableContext })` override lives inside the
|
|
138
|
+
Nuxt app, and a server route cannot see it. Pass `contextKey` explicitly if your
|
|
139
|
+
app overrides the module setting and also fetches server-side.
|
|
140
|
+
|
|
105
141
|
## Configuration
|
|
106
142
|
|
|
107
143
|
```typescript
|
|
@@ -114,7 +150,7 @@ export default defineNuxtConfig({
|
|
|
114
150
|
|
|
115
151
|
// API proxy
|
|
116
152
|
proxy: true, // or '/api/custom-path'
|
|
117
|
-
fetchApiSecret: process.env.FRONTIC_FETCH_SECRET,
|
|
153
|
+
fetchApiSecret: process.env.FRONTIC_FETCH_SECRET, // only if the project's Fetch API is protected
|
|
118
154
|
|
|
119
155
|
// Sitemap
|
|
120
156
|
sitemap: true,
|
package/dist/module.json
CHANGED
|
@@ -12,7 +12,13 @@ export interface FronticAnalyticsItem {
|
|
|
12
12
|
index: number;
|
|
13
13
|
data: unknown;
|
|
14
14
|
}
|
|
15
|
-
|
|
15
|
+
/**
|
|
16
|
+
* What caused a listing to resolve. `url` covers a change that arrived through the address bar —
|
|
17
|
+
* a back/forward navigation or an edited link under `urlQuerySync` — which no mutator reports
|
|
18
|
+
* because none was called. It carries no `changedFilter`: a restored URL can move several slices
|
|
19
|
+
* at once, and naming one of them would misreport the rest.
|
|
20
|
+
*/
|
|
21
|
+
export type FronticListingTrigger = 'initial' | 'term' | 'filter' | 'sort' | 'page' | 'loadMore' | 'reset' | 'url';
|
|
16
22
|
export interface FronticListingResolved {
|
|
17
23
|
listing: string;
|
|
18
24
|
cacheKey: string;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { toValue } from "vue";
|
|
2
|
-
import { DEFAULT_STALE_TIME, requestContextSlot } from "../utils/query-keys.js";
|
|
2
|
+
import { contextKeyOf, DEFAULT_STALE_TIME, requestContextSlot } from "../utils/query-keys.js";
|
|
3
3
|
export const BLOCK_QUERY_KEYS = {
|
|
4
4
|
root: ["blocks"],
|
|
5
5
|
block: (blockName) => [...BLOCK_QUERY_KEYS.root, blockName],
|
|
@@ -7,7 +7,7 @@ export const BLOCK_QUERY_KEYS = {
|
|
|
7
7
|
};
|
|
8
8
|
export function fetchBlockQuery(config) {
|
|
9
9
|
return {
|
|
10
|
-
key: () => BLOCK_QUERY_KEYS.detail(config.name, toValue(config.key),
|
|
10
|
+
key: () => BLOCK_QUERY_KEYS.detail(config.name, toValue(config.key), contextKeyOf(config), toValue(config.contextDomain)),
|
|
11
11
|
query: async () => {
|
|
12
12
|
let client = config._client;
|
|
13
13
|
if (!client) {
|
|
@@ -15,7 +15,7 @@ export function fetchBlockQuery(config) {
|
|
|
15
15
|
client = useFronticClient();
|
|
16
16
|
}
|
|
17
17
|
return await client.block(config.name, toValue(config.key), {
|
|
18
|
-
contextKey:
|
|
18
|
+
contextKey: contextKeyOf(config),
|
|
19
19
|
contextDomain: toValue(config.contextDomain),
|
|
20
20
|
requestUrl: toValue(config.requestUrl)
|
|
21
21
|
});
|
|
@@ -57,6 +57,8 @@ export declare function fetchListingQuery<TListing extends ListingName>(config:
|
|
|
57
57
|
staleTime: number;
|
|
58
58
|
gcTime?: number;
|
|
59
59
|
enabled?: MaybeRefOrGetter<boolean>;
|
|
60
|
+
initialData?: (() => ResponseOf<TListing> | undefined) | undefined;
|
|
61
|
+
initialDataUpdatedAt?: number | (() => number);
|
|
60
62
|
placeholderData?: ((previousData: ResponseOf<TListing> | undefined) => ResponseOf<TListing> | undefined) | undefined;
|
|
61
63
|
key: () => readonly ["listings", string, Record<string, unknown>, Record<string, unknown>, {
|
|
62
64
|
readonly context: string | null;
|
|
@@ -64,6 +66,22 @@ export declare function fetchListingQuery<TListing extends ListingName>(config:
|
|
|
64
66
|
}];
|
|
65
67
|
query: () => Promise<ResponseOf<TListing>>;
|
|
66
68
|
};
|
|
69
|
+
/**
|
|
70
|
+
* Restricts an `initialData` getter to the key the query started on.
|
|
71
|
+
*
|
|
72
|
+
* Pinia Colada seeds *every* cache entry it creates, not just the first one.
|
|
73
|
+
* Unguarded, the payload describing page 1 unfiltered would be handed to page
|
|
74
|
+
* 2, to each filter the user picks and to each sort — and reported `success`
|
|
75
|
+
* with a fresh timestamp, so none of them would ever fetch.
|
|
76
|
+
*
|
|
77
|
+
* The seed key is read when the query is built, in setup, which is the state
|
|
78
|
+
* the caller's payload describes. Compared through the cache's own hash rather
|
|
79
|
+
* than a plain stringify, so that a query object written in a different
|
|
80
|
+
* property order still counts as the same question — the cache treats key
|
|
81
|
+
* objects as order-insensitive, and a stricter guard here would drop the seed
|
|
82
|
+
* for a key the cache considers identical.
|
|
83
|
+
*/
|
|
84
|
+
export declare function seedInitialData<TData>(key: () => readonly unknown[], initialData: () => TData): () => TData | undefined;
|
|
67
85
|
/**
|
|
68
86
|
* Cache options shared by both query modes.
|
|
69
87
|
*
|
|
@@ -78,10 +96,14 @@ export declare function listingQueryOptions<TData>(config: {
|
|
|
78
96
|
gcTime?: number;
|
|
79
97
|
enabled?: MaybeRefOrGetter<boolean>;
|
|
80
98
|
keepPreviousData?: boolean;
|
|
99
|
+
initialData?: () => TData | undefined;
|
|
100
|
+
initialDataUpdatedAt?: number | (() => number);
|
|
81
101
|
}): {
|
|
82
102
|
staleTime: number;
|
|
83
103
|
gcTime?: number;
|
|
84
104
|
enabled?: MaybeRefOrGetter<boolean>;
|
|
105
|
+
initialData?: () => TData | undefined;
|
|
106
|
+
initialDataUpdatedAt?: number | (() => number);
|
|
85
107
|
placeholderData?: (previousData: TData | undefined) => TData | undefined;
|
|
86
108
|
};
|
|
87
109
|
/**
|
|
@@ -1,24 +1,26 @@
|
|
|
1
1
|
import { toValue } from "vue";
|
|
2
|
-
import {
|
|
2
|
+
import { toCacheKey } from "@pinia/colada";
|
|
3
|
+
import { contextKeyOf, DEFAULT_STALE_TIME, requestContextSlot, toKeyPart } from "../utils/query-keys.js";
|
|
3
4
|
export const LISTING_QUERY_KEYS = {
|
|
4
5
|
root: ["listings"],
|
|
5
6
|
listing: (listingName) => [...LISTING_QUERY_KEYS.root, listingName],
|
|
6
7
|
detail: (listingName, parameters, query, contextKey, contextDomain) => [...LISTING_QUERY_KEYS.listing(listingName), toKeyPart(parameters), toKeyPart(query), requestContextSlot(contextKey, contextDomain)]
|
|
7
8
|
};
|
|
8
9
|
export function fetchListingQuery(config) {
|
|
10
|
+
const key = () => {
|
|
11
|
+
const query = toValue(config.query) ?? {};
|
|
12
|
+
const page = toValue(config.page);
|
|
13
|
+
const queryWithPage = page ? { ...query, page } : query;
|
|
14
|
+
return LISTING_QUERY_KEYS.detail(
|
|
15
|
+
config.name,
|
|
16
|
+
toValue(config.params),
|
|
17
|
+
queryWithPage,
|
|
18
|
+
contextKeyOf(config),
|
|
19
|
+
toValue(config.contextDomain)
|
|
20
|
+
);
|
|
21
|
+
};
|
|
9
22
|
return {
|
|
10
|
-
key
|
|
11
|
-
const query = toValue(config.query) ?? {};
|
|
12
|
-
const page = toValue(config.page);
|
|
13
|
-
const queryWithPage = page ? { ...query, page } : query;
|
|
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
|
-
},
|
|
23
|
+
key,
|
|
22
24
|
query: async () => {
|
|
23
25
|
const query = toValue(config.query) ?? {};
|
|
24
26
|
const page = toValue(config.page);
|
|
@@ -31,7 +33,7 @@ export function fetchListingQuery(config) {
|
|
|
31
33
|
}
|
|
32
34
|
return await client.listing(config.name, toValue(config.params), {
|
|
33
35
|
query: queryWithPage,
|
|
34
|
-
contextKey:
|
|
36
|
+
contextKey: contextKeyOf(config),
|
|
35
37
|
contextDomain: toValue(config.contextDomain),
|
|
36
38
|
requestUrl: toValue(config.requestUrl)
|
|
37
39
|
});
|
|
@@ -40,14 +42,27 @@ export function fetchListingQuery(config) {
|
|
|
40
42
|
throw error;
|
|
41
43
|
}
|
|
42
44
|
},
|
|
43
|
-
...listingQueryOptions(
|
|
45
|
+
...listingQueryOptions({
|
|
46
|
+
...config,
|
|
47
|
+
...config.initialData && { initialData: seedInitialData(key, config.initialData) }
|
|
48
|
+
})
|
|
44
49
|
};
|
|
45
50
|
}
|
|
51
|
+
export function seedInitialData(key, initialData) {
|
|
52
|
+
const hash = () => toCacheKey(key());
|
|
53
|
+
const seedKey = hash();
|
|
54
|
+
return () => hash() === seedKey ? initialData() : void 0;
|
|
55
|
+
}
|
|
46
56
|
export function listingQueryOptions(config) {
|
|
47
57
|
return {
|
|
48
58
|
staleTime: config.staleTime ?? DEFAULT_STALE_TIME,
|
|
49
59
|
...config.gcTime !== void 0 && { gcTime: config.gcTime },
|
|
50
60
|
...config.enabled !== void 0 && { enabled: config.enabled },
|
|
61
|
+
// initialData: seeds the cache and reports `success`, so the query does not
|
|
62
|
+
// fetch while the seed is within staleTime. Expected pre-guarded by
|
|
63
|
+
// seedInitialData — see the note there.
|
|
64
|
+
...config.initialData !== void 0 && { initialData: config.initialData },
|
|
65
|
+
...config.initialDataUpdatedAt !== void 0 && { initialDataUpdatedAt: config.initialDataUpdatedAt },
|
|
51
66
|
// keepPreviousData: show the previous entry's data while a changed key
|
|
52
67
|
// (new page, filter, context) loads; never written to the cache
|
|
53
68
|
...config.keepPreviousData !== false && { placeholderData: (previousData) => previousData }
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { toValue } from "vue";
|
|
2
|
-
import { DEFAULT_STALE_TIME, requestContextSlot } from "../utils/query-keys.js";
|
|
2
|
+
import { contextKeyOf, DEFAULT_STALE_TIME, requestContextSlot } from "../utils/query-keys.js";
|
|
3
3
|
export const PAGE_QUERY_KEYS = {
|
|
4
4
|
root: ["pages"],
|
|
5
5
|
page: (slug) => [...PAGE_QUERY_KEYS.root, slug],
|
|
@@ -7,7 +7,7 @@ export const PAGE_QUERY_KEYS = {
|
|
|
7
7
|
};
|
|
8
8
|
export function fetchPageQuery(config) {
|
|
9
9
|
return {
|
|
10
|
-
key: () => PAGE_QUERY_KEYS.detail(toValue(config.slug),
|
|
10
|
+
key: () => PAGE_QUERY_KEYS.detail(toValue(config.slug), contextKeyOf(config), toValue(config.contextDomain)),
|
|
11
11
|
query: async () => {
|
|
12
12
|
let client = config._client;
|
|
13
13
|
if (!client) {
|
|
@@ -15,7 +15,7 @@ export function fetchPageQuery(config) {
|
|
|
15
15
|
client = useFronticClient();
|
|
16
16
|
}
|
|
17
17
|
return await client.page(toValue(config.slug), {
|
|
18
|
-
contextKey:
|
|
18
|
+
contextKey: contextKeyOf(config),
|
|
19
19
|
contextDomain: toValue(config.contextDomain),
|
|
20
20
|
requestUrl: toValue(config.requestUrl)
|
|
21
21
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { toValue } from "vue";
|
|
2
|
-
import { DEFAULT_STALE_TIME, requestContextSlot } from "../utils/query-keys.js";
|
|
2
|
+
import { contextKeyOf, DEFAULT_STALE_TIME, requestContextSlot } from "../utils/query-keys.js";
|
|
3
3
|
export const TREE_QUERY_KEYS = {
|
|
4
4
|
root: ["trees"],
|
|
5
5
|
tree: (treeName) => [...TREE_QUERY_KEYS.root, treeName],
|
|
@@ -7,7 +7,7 @@ export const TREE_QUERY_KEYS = {
|
|
|
7
7
|
};
|
|
8
8
|
export function fetchTreeQuery(config) {
|
|
9
9
|
return {
|
|
10
|
-
key: () => TREE_QUERY_KEYS.detail(config.name, toValue(config.key), toValue(config.depth),
|
|
10
|
+
key: () => TREE_QUERY_KEYS.detail(config.name, toValue(config.key), toValue(config.depth), contextKeyOf(config), toValue(config.contextDomain)),
|
|
11
11
|
query: async () => {
|
|
12
12
|
let client = config._client;
|
|
13
13
|
if (!client) {
|
|
@@ -21,7 +21,7 @@ export function fetchTreeQuery(config) {
|
|
|
21
21
|
if (depth !== void 0) treeQuery.depth = depth;
|
|
22
22
|
return await client.tree(config.name, {
|
|
23
23
|
...Object.keys(treeQuery).length > 0 && { query: treeQuery },
|
|
24
|
-
contextKey:
|
|
24
|
+
contextKey: contextKeyOf(config),
|
|
25
25
|
contextDomain: toValue(config.contextDomain),
|
|
26
26
|
requestUrl: toValue(config.requestUrl)
|
|
27
27
|
});
|
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
import { useQuery } from "@pinia/colada";
|
|
2
2
|
import { computed, toValue } from "vue";
|
|
3
3
|
import { resolveContextDomain } from "../utils/resolve-context-domain.js";
|
|
4
|
+
import { resolveContextKey, resolveCallContextKey } from "../utils/resolve-context-key.js";
|
|
4
5
|
import { fetchBlockQuery } from "./blocks.js";
|
|
5
6
|
import { useFronticClient } from "./useFronticClient.js";
|
|
6
7
|
export function useFronticBlock(name, key, options) {
|
|
7
|
-
const _client = useFronticClient({ contextDomain: false });
|
|
8
|
+
const _client = useFronticClient({ contextDomain: false, contextKey: false });
|
|
8
9
|
const autoContextDomain = resolveContextDomain();
|
|
10
|
+
const autoContextKey = resolveContextKey();
|
|
9
11
|
const resolvedContextDomain = computed(() => toValue(options?.contextDomain) ?? autoContextDomain);
|
|
10
12
|
const blockQuery = fetchBlockQuery({
|
|
11
13
|
name,
|
|
12
14
|
key,
|
|
13
|
-
contextKey: options?.contextKey,
|
|
15
|
+
contextKey: computed(() => resolveCallContextKey(toValue(options?.contextKey), autoContextKey)),
|
|
14
16
|
contextDomain: resolvedContextDomain,
|
|
15
17
|
requestUrl: options?.requestUrl,
|
|
16
18
|
staleTime: options?.staleTime,
|
|
@@ -16,6 +16,14 @@ export interface UseFronticClientOptions {
|
|
|
16
16
|
* - `undefined`: Resolve from module config, or the active i18n locale when it is `true`
|
|
17
17
|
*/
|
|
18
18
|
contextDomain?: false | string;
|
|
19
|
+
/**
|
|
20
|
+
* Override the context token sent as `fs-context`
|
|
21
|
+
* - `false`: Send no token, so the request resolves through the domain alone
|
|
22
|
+
* - `string`: Use a specific token
|
|
23
|
+
* - `undefined`: Read the token `useFronticContext` manages, unless the module
|
|
24
|
+
* is configured with `disableContext`
|
|
25
|
+
*/
|
|
26
|
+
contextKey?: false | string;
|
|
19
27
|
/**
|
|
20
28
|
* Override requestUrl tracking
|
|
21
29
|
* - `false`: Disable auto-injection
|
|
@@ -34,4 +42,26 @@ export interface UseFronticClientOptions {
|
|
|
34
42
|
*/
|
|
35
43
|
secret?: string;
|
|
36
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* Creates an SSR-aware Frontic client with automatic proxy and context injection
|
|
47
|
+
*
|
|
48
|
+
* @param options - Optional configuration to override defaults
|
|
49
|
+
* @returns Type-safe Frontic client instance
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* // Default behavior (proxy on client, auto contextDomain)
|
|
54
|
+
* const client = useFronticClient()
|
|
55
|
+
* const product = await client.block('ProductCard', productId)
|
|
56
|
+
*
|
|
57
|
+
* // Force direct API (bypass proxy)
|
|
58
|
+
* const directClient = useFronticClient({ proxy: false })
|
|
59
|
+
*
|
|
60
|
+
* // Custom proxy path
|
|
61
|
+
* const customClient = useFronticClient({ proxy: '/api/custom-frontic' })
|
|
62
|
+
*
|
|
63
|
+
* // Disable contextDomain auto-injection
|
|
64
|
+
* const noContextClient = useFronticClient({ contextDomain: false })
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
37
67
|
export declare function useFronticClient(options?: UseFronticClientOptions): FronticClient;
|
|
@@ -1,13 +1,9 @@
|
|
|
1
1
|
import { useRuntimeConfig, useRequestURL, useNuxtApp } from "nuxt/app";
|
|
2
2
|
import { createClient } from "@frontic/stack/generated-client";
|
|
3
3
|
import { resolveContextDomain } from "../utils/resolve-context-domain.js";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
if (!reqOptions) return defaults;
|
|
8
|
-
const provided = Object.fromEntries(Object.entries(reqOptions).filter(([, value]) => value !== void 0));
|
|
9
|
-
return { ...defaults, ...provided };
|
|
10
|
-
}
|
|
4
|
+
import { resolveContextKey } from "../utils/resolve-context-key.js";
|
|
5
|
+
import { withDefaults } from "../utils/request-defaults.js";
|
|
6
|
+
import { toFronticFetchError } from "../utils/describe-error.js";
|
|
11
7
|
export function useFronticClient(options) {
|
|
12
8
|
const config = useRuntimeConfig();
|
|
13
9
|
const fronticConfig = config.public.frontic;
|
|
@@ -33,23 +29,25 @@ export function useFronticClient(options) {
|
|
|
33
29
|
const nuxtApp = useNuxtApp();
|
|
34
30
|
const proxyUrl = resolveProxyUrl();
|
|
35
31
|
const contextDomain = resolveContextDomain(options?.contextDomain);
|
|
32
|
+
const contextKeyOption = options?.contextKey;
|
|
33
|
+
const contextKey = contextKeyOption === false ? () => void 0 : typeof contextKeyOption === "string" ? () => contextKeyOption : resolveContextKey();
|
|
34
|
+
const effectiveContextKey = (override) => override === false ? void 0 : override ?? contextKey();
|
|
36
35
|
const requestUrlValue = resolveRequestUrl();
|
|
37
36
|
const secret = resolveSecret();
|
|
38
37
|
const baseClient = createClient({ proxyUrl });
|
|
39
38
|
function reporting(operation, identity, reqOptions, run) {
|
|
40
39
|
return run().catch((error) => {
|
|
41
|
-
const payload =
|
|
40
|
+
const payload = toFronticFetchError(
|
|
42
41
|
operation,
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
};
|
|
42
|
+
identity,
|
|
43
|
+
{
|
|
44
|
+
...contextDomain && { contextDomain },
|
|
45
|
+
...effectiveContextKey(reqOptions?.contextKey) && { contextKey: effectiveContextKey(reqOptions?.contextKey) },
|
|
46
|
+
...requestUrlValue && { requestUrl: requestUrlValue },
|
|
47
|
+
...proxyUrl && { proxyUrl }
|
|
48
|
+
},
|
|
49
|
+
error
|
|
50
|
+
);
|
|
53
51
|
void Promise.resolve(nuxtApp.callHook("frontic:fetch:error", payload)).catch(() => {
|
|
54
52
|
});
|
|
55
53
|
throw error;
|
|
@@ -60,21 +58,46 @@ export function useFronticClient(options) {
|
|
|
60
58
|
"block",
|
|
61
59
|
{ name: String(name) },
|
|
62
60
|
reqOptions,
|
|
63
|
-
() => baseClient.block(
|
|
61
|
+
() => baseClient.block(
|
|
62
|
+
name,
|
|
63
|
+
key,
|
|
64
|
+
withDefaults(
|
|
65
|
+
{ ...contextDomain && { contextDomain }, ...contextKey() && { contextKey: contextKey() }, ...requestUrlValue && { requestUrl: requestUrlValue }, ...secret && { secret } },
|
|
66
|
+
reqOptions
|
|
67
|
+
)
|
|
68
|
+
)
|
|
64
69
|
),
|
|
65
70
|
listing: (name, params, reqOptions) => reporting(
|
|
66
71
|
"listing",
|
|
67
72
|
{ name: String(name) },
|
|
68
73
|
reqOptions,
|
|
69
|
-
() => baseClient.listing(
|
|
74
|
+
() => baseClient.listing(
|
|
75
|
+
name,
|
|
76
|
+
params,
|
|
77
|
+
withDefaults(
|
|
78
|
+
{ ...contextDomain && { contextDomain }, ...contextKey() && { contextKey: contextKey() }, ...requestUrlValue && { requestUrl: requestUrlValue }, ...secret && { secret } },
|
|
79
|
+
reqOptions
|
|
80
|
+
)
|
|
81
|
+
)
|
|
70
82
|
),
|
|
71
83
|
tree: (name, reqOptions) => reporting(
|
|
72
84
|
"tree",
|
|
73
85
|
{ name: String(name) },
|
|
74
86
|
reqOptions,
|
|
75
|
-
() => baseClient.tree(
|
|
87
|
+
() => baseClient.tree(
|
|
88
|
+
name,
|
|
89
|
+
withDefaults(
|
|
90
|
+
{ ...contextDomain && { contextDomain }, ...contextKey() && { contextKey: contextKey() }, ...requestUrlValue && { requestUrl: requestUrlValue }, ...secret && { secret } },
|
|
91
|
+
reqOptions
|
|
92
|
+
)
|
|
93
|
+
)
|
|
94
|
+
),
|
|
95
|
+
page: (slug, reqOptions) => reporting(
|
|
96
|
+
"page",
|
|
97
|
+
{ slug },
|
|
98
|
+
reqOptions,
|
|
99
|
+
() => baseClient.page(slug, withDefaults({ ...contextKey() && { contextKey: contextKey() }, ...requestUrlValue && { requestUrl: requestUrlValue }, ...secret && { secret } }, reqOptions))
|
|
76
100
|
),
|
|
77
|
-
page: (slug, reqOptions) => reporting("page", { slug }, reqOptions, () => baseClient.page(slug, withDefaults({ ...requestUrlValue && { requestUrl: requestUrlValue }, ...secret && { secret } }, reqOptions))),
|
|
78
101
|
context: (token, reqOptions) => reporting("context", {}, reqOptions, () => baseClient.context(token, withDefaults({ ...secret && { secret } }, reqOptions))),
|
|
79
102
|
contextList: (token, reqOptions) => reporting("context", {}, reqOptions, () => baseClient.contextList(token, withDefaults({ ...contextDomain && { contextDomain }, ...secret && { secret } }, reqOptions))),
|
|
80
103
|
contextUpdate: (context, token, reqOptions) => reporting("context", {}, reqOptions, () => baseClient.contextUpdate(context, token, withDefaults({ ...secret && { secret } }, reqOptions))),
|
|
@@ -83,7 +83,10 @@ export declare function useFronticContext(options?: UseFronticContextOptions): {
|
|
|
83
83
|
locale: string;
|
|
84
84
|
}) => Promise<void>;
|
|
85
85
|
/**
|
|
86
|
-
*
|
|
86
|
+
* Whether the context itself is resolving. Content that depends on it
|
|
87
|
+
* refetches on its own — the token is part of every query key, so a switch
|
|
88
|
+
* re-keys each query — and each composable reports that through its own
|
|
89
|
+
* `asyncStatus`.
|
|
87
90
|
*/
|
|
88
91
|
isLoading: Readonly<Ref<boolean>>;
|
|
89
92
|
/**
|
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
import { useRuntimeConfig, useCookie, useState, useNuxtApp } from "nuxt/app";
|
|
2
2
|
import { readonly, onMounted, getCurrentInstance } from "vue";
|
|
3
|
-
import { useQueryCache } from "@pinia/colada";
|
|
4
3
|
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
4
|
function useContextRuntime() {
|
|
10
5
|
const nuxtApp = useNuxtApp();
|
|
11
6
|
return nuxtApp._fronticContext ??= {};
|
|
@@ -17,7 +12,6 @@ export function useFronticContext(options) {
|
|
|
17
12
|
const client = useFronticClient();
|
|
18
13
|
const config = useRuntimeConfig();
|
|
19
14
|
const nuxtApp = useNuxtApp();
|
|
20
|
-
const queryCache = useQueryCache();
|
|
21
15
|
const fronticConfig = config.public.frontic;
|
|
22
16
|
const cookieName = options?.cookieName ?? fronticConfig?.contextCookieName ?? "fs-context";
|
|
23
17
|
const cookieMaxAge = options?.cookieMaxAge ?? fronticConfig?.contextCookieMaxAge ?? 60 * 60 * 24 * 365;
|
|
@@ -32,23 +26,21 @@ export function useFronticContext(options) {
|
|
|
32
26
|
token.value = tokenCookie.value;
|
|
33
27
|
}
|
|
34
28
|
const runtime = useContextRuntime();
|
|
29
|
+
runtime.scope ??= { cookieName, disableContext };
|
|
30
|
+
if (!disableContext) runtime.scope.disableContext = false;
|
|
31
|
+
if (import.meta.dev && runtime.scope.cookieName !== cookieName) {
|
|
32
|
+
console.warn(
|
|
33
|
+
`[@frontic/nuxt] useFronticContext is used with two cookie names ('${runtime.scope.cookieName}' and '${cookieName}'). Composables resolve their token from '${runtime.scope.cookieName}'; pass \`contextKey\` explicitly for the other one.`
|
|
34
|
+
);
|
|
35
|
+
}
|
|
35
36
|
function broadcast(context) {
|
|
36
|
-
if (!context) return
|
|
37
|
+
if (!context) return;
|
|
37
38
|
const signature = `${context.region}|${context.locale}|${context.scope}`;
|
|
38
|
-
if (runtime.lastBroadcast === signature) return
|
|
39
|
+
if (runtime.lastBroadcast === signature) return;
|
|
39
40
|
const isFirst = runtime.lastBroadcast === void 0;
|
|
40
41
|
runtime.lastBroadcast = signature;
|
|
41
42
|
if (isFirst) callContextHook(nuxtApp, "frontic:context:ready", context);
|
|
42
43
|
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
44
|
}
|
|
53
45
|
function persistToken(newToken) {
|
|
54
46
|
if (!newToken || newToken === token.value) return;
|
|
@@ -68,7 +60,7 @@ export function useFronticContext(options) {
|
|
|
68
60
|
}
|
|
69
61
|
if (newToken) {
|
|
70
62
|
current.value = await client.context(newToken);
|
|
71
|
-
|
|
63
|
+
broadcast(current.value);
|
|
72
64
|
}
|
|
73
65
|
} catch (error) {
|
|
74
66
|
console.error("[@frontic/nuxt] Failed to fetch contexts:", error);
|
|
@@ -95,7 +87,7 @@ export function useFronticContext(options) {
|
|
|
95
87
|
try {
|
|
96
88
|
current.value = await client.contextUpdate(context, token.value);
|
|
97
89
|
persistToken(current.value?.token);
|
|
98
|
-
|
|
90
|
+
broadcast(current.value);
|
|
99
91
|
} catch (error) {
|
|
100
92
|
console.error("[@frontic/nuxt] Failed to update context:", error);
|
|
101
93
|
} finally {
|
|
@@ -131,7 +123,10 @@ export function useFronticContext(options) {
|
|
|
131
123
|
*/
|
|
132
124
|
update,
|
|
133
125
|
/**
|
|
134
|
-
*
|
|
126
|
+
* Whether the context itself is resolving. Content that depends on it
|
|
127
|
+
* refetches on its own — the token is part of every query key, so a switch
|
|
128
|
+
* re-keys each query — and each composable reports that through its own
|
|
129
|
+
* `asyncStatus`.
|
|
135
130
|
*/
|
|
136
131
|
isLoading: readonly(isLoading),
|
|
137
132
|
/**
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { Ref, MaybeRef } from 'vue';
|
|
2
2
|
import type { ListingName, ResponseOf } from '../types/registry.js';
|
|
3
|
-
import type { FronticRequestOptions, FronticQueryOptions, ListingArgs, ListingQuery } from '../types/listing.js';
|
|
3
|
+
import type { FronticRequestOptions, FronticQueryOptions, FronticInitialDataOptions, ListingArgs, ListingQuery } from '../types/listing.js';
|
|
4
4
|
export type { ListingQuery } from '../types/listing.js';
|
|
5
|
-
export interface UseFronticListingOptions<TListing extends ListingName = ListingName> extends FronticRequestOptions, FronticQueryOptions {
|
|
5
|
+
export interface UseFronticListingOptions<TListing extends ListingName = ListingName> extends FronticRequestOptions, FronticQueryOptions, FronticInitialDataOptions<ResponseOf<TListing>> {
|
|
6
6
|
/**
|
|
7
7
|
* Query options forwarded to the Fetch API — `filter`, `sort`, `search`, `limit`, `page`.
|
|
8
8
|
* Same shape as `client.listing(name, params, { query })`.
|
|
@@ -43,6 +43,12 @@ export interface UseFronticListingOptions<TListing extends ListingName = Listing
|
|
|
43
43
|
* const { listing } = useFronticListing('ProductSearch', {}, {
|
|
44
44
|
* query: computed(() => ({ limit: 20, page: page.value })),
|
|
45
45
|
* })
|
|
46
|
+
*
|
|
47
|
+
* // Seeded from the page payload — renders on the first paint, no request
|
|
48
|
+
* const { data } = useFronticPage()
|
|
49
|
+
* const { listing } = useFronticListing('CategoryProducts', { key: 'shoes' }, {
|
|
50
|
+
* initialData: () => data.value?.products,
|
|
51
|
+
* })
|
|
46
52
|
* ```
|
|
47
53
|
*/
|
|
48
54
|
export declare function useFronticListing<T extends ListingName>(name: T, ...args: ListingArgs<T, UseFronticListingOptions<T>>): {
|
|
@@ -71,9 +77,9 @@ export declare function useFronticListing<T extends ListingName>(name: T, ...arg
|
|
|
71
77
|
/**
|
|
72
78
|
* Manually refresh the listing data (uses cache if valid)
|
|
73
79
|
*/
|
|
74
|
-
refresh: (throwOnError?: boolean) => Promise<import("@pinia/colada").DataState<ResponseOf<T>, Error, undefined>>;
|
|
80
|
+
refresh: (throwOnError?: boolean) => Promise<import("@pinia/colada").DataState<ResponseOf<T>, Error, ResponseOf<T> | undefined>>;
|
|
75
81
|
/**
|
|
76
82
|
* Force a fresh fetch of the listing data, bypassing cache
|
|
77
83
|
*/
|
|
78
|
-
refetch: (throwOnError?: boolean) => Promise<import("@pinia/colada").DataState<ResponseOf<T>, Error, undefined>>;
|
|
84
|
+
refetch: (throwOnError?: boolean) => Promise<import("@pinia/colada").DataState<ResponseOf<T>, Error, ResponseOf<T> | undefined>>;
|
|
79
85
|
};
|
|
@@ -1,25 +1,29 @@
|
|
|
1
1
|
import { useQuery } from "@pinia/colada";
|
|
2
2
|
import { computed, toValue } from "vue";
|
|
3
3
|
import { resolveContextDomain } from "../utils/resolve-context-domain.js";
|
|
4
|
+
import { resolveContextKey, resolveCallContextKey } from "../utils/resolve-context-key.js";
|
|
4
5
|
import { fetchListingQuery } from "./listings.js";
|
|
5
6
|
import { useFronticClient } from "./useFronticClient.js";
|
|
6
7
|
export function useFronticListing(name, ...args) {
|
|
7
8
|
const [rawParams, options] = args;
|
|
8
9
|
const params = rawParams ?? {};
|
|
9
|
-
const _client = useFronticClient({ contextDomain: false });
|
|
10
|
+
const _client = useFronticClient({ contextDomain: false, contextKey: false });
|
|
10
11
|
const autoContextDomain = resolveContextDomain();
|
|
12
|
+
const autoContextKey = resolveContextKey();
|
|
11
13
|
const resolvedContextDomain = computed(() => toValue(options?.contextDomain) ?? autoContextDomain);
|
|
12
14
|
const listingQuery = fetchListingQuery({
|
|
13
15
|
name,
|
|
14
16
|
params,
|
|
15
17
|
query: options?.query,
|
|
16
|
-
contextKey: options?.contextKey,
|
|
18
|
+
contextKey: computed(() => resolveCallContextKey(toValue(options?.contextKey), autoContextKey)),
|
|
17
19
|
contextDomain: resolvedContextDomain,
|
|
18
20
|
requestUrl: options?.requestUrl,
|
|
19
21
|
staleTime: options?.staleTime,
|
|
20
22
|
gcTime: options?.gcTime,
|
|
21
23
|
enabled: options?.enabled,
|
|
22
24
|
keepPreviousData: options?.keepPreviousData,
|
|
25
|
+
initialData: options?.initialData,
|
|
26
|
+
initialDataUpdatedAt: options?.initialDataUpdatedAt,
|
|
23
27
|
_client
|
|
24
28
|
});
|
|
25
29
|
const { data, status, asyncStatus, isLoading, error, refresh, refetch } = useQuery(listingQuery);
|
|
@@ -5,6 +5,7 @@ import { fetchPageQuery } from "./pages.js";
|
|
|
5
5
|
import { useFronticClient } from "./useFronticClient.js";
|
|
6
6
|
import { emitOnce, bindRouteReset, FRONTIC_ANALYTICS_VERSION } from "./analytics.js";
|
|
7
7
|
import { resolveContextDomain } from "../utils/resolve-context-domain.js";
|
|
8
|
+
import { resolveContextKey, resolveCallContextKey } from "../utils/resolve-context-key.js";
|
|
8
9
|
export function useFronticPage(slug, options) {
|
|
9
10
|
const nuxtApp = useNuxtApp();
|
|
10
11
|
bindRouteReset(nuxtApp);
|
|
@@ -12,16 +13,17 @@ export function useFronticPage(slug, options) {
|
|
|
12
13
|
const config = useRuntimeConfig();
|
|
13
14
|
const fronticConfig = config.public.frontic;
|
|
14
15
|
const autoContextDomain = resolveContextDomain();
|
|
16
|
+
const autoContextKey = resolveContextKey();
|
|
15
17
|
const resolvedContextDomain = computed(() => toValue(options?.contextDomain) ?? autoContextDomain);
|
|
16
18
|
const pageSlug = computed(() => {
|
|
17
19
|
if (slug) return toValue(slug);
|
|
18
20
|
const pathname = requestURL.pathname.replace(/\/+$/, "");
|
|
19
21
|
return `${resolvedContextDomain.value ?? requestURL.host}${pathname}`;
|
|
20
22
|
});
|
|
21
|
-
const _client = useFronticClient({ contextDomain: false });
|
|
23
|
+
const _client = useFronticClient({ contextDomain: false, contextKey: false });
|
|
22
24
|
const pageQuery = fetchPageQuery({
|
|
23
25
|
slug: pageSlug,
|
|
24
|
-
contextKey: options?.contextKey,
|
|
26
|
+
contextKey: computed(() => resolveCallContextKey(toValue(options?.contextKey), autoContextKey)),
|
|
25
27
|
contextDomain: resolvedContextDomain,
|
|
26
28
|
requestUrl: options?.requestUrl,
|
|
27
29
|
staleTime: options?.staleTime,
|