@frontic/nuxt 1.2.0 → 1.3.1
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/module.mjs +3 -3
- package/dist/runtime/composables/blocks.js +3 -3
- package/dist/runtime/composables/listings.js +3 -3
- 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 -24
- package/dist/runtime/composables/useFronticContext.d.ts +4 -1
- package/dist/runtime/composables/useFronticContext.js +15 -20
- package/dist/runtime/composables/useFronticListing.js +4 -2
- package/dist/runtime/composables/useFronticPage.js +4 -2
- package/dist/runtime/composables/useFronticSearch.js +18 -4
- package/dist/runtime/composables/useFronticTree.js +4 -2
- package/dist/runtime/server/client.d.ts +55 -0
- package/dist/runtime/server/client.js +53 -0
- package/dist/runtime/types/errors.d.ts +1 -1
- package/dist/runtime/types/index.d.ts +1 -1
- package/dist/runtime/types/options.d.ts +18 -4
- package/dist/runtime/types/registry.d.ts +11 -1
- 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/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
package/dist/module.mjs
CHANGED
|
@@ -296,17 +296,17 @@ export {}
|
|
|
296
296
|
const errorsModule = resolveModule("./runtime/types/errors");
|
|
297
297
|
addTypeTemplate({
|
|
298
298
|
filename: "types/frontic-fetch-error.d.ts",
|
|
299
|
-
getContents: () => `import type {
|
|
299
|
+
getContents: () => `import type { FronticFetchErrorPayload } from '${errorsModule}'
|
|
300
300
|
|
|
301
301
|
declare module '#app' {
|
|
302
302
|
interface RuntimeNuxtHooks {
|
|
303
|
-
'frontic:fetch:error': (payload:
|
|
303
|
+
'frontic:fetch:error': (payload: FronticFetchErrorPayload) => void | Promise<void>
|
|
304
304
|
}
|
|
305
305
|
}
|
|
306
306
|
|
|
307
307
|
declare module 'nitropack/types' {
|
|
308
308
|
interface NitroRuntimeHooks {
|
|
309
|
-
'frontic:fetch:error': (payload:
|
|
309
|
+
'frontic:fetch:error': (payload: FronticFetchErrorPayload) => void | Promise<void>
|
|
310
310
|
}
|
|
311
311
|
}
|
|
312
312
|
|
|
@@ -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
|
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { toValue } from "vue";
|
|
2
2
|
import { toCacheKey } from "@pinia/colada";
|
|
3
|
-
import { DEFAULT_STALE_TIME, requestContextSlot, toKeyPart } from "../utils/query-keys.js";
|
|
3
|
+
import { contextKeyOf, DEFAULT_STALE_TIME, requestContextSlot, toKeyPart } from "../utils/query-keys.js";
|
|
4
4
|
export const LISTING_QUERY_KEYS = {
|
|
5
5
|
root: ["listings"],
|
|
6
6
|
listing: (listingName) => [...LISTING_QUERY_KEYS.root, listingName],
|
|
@@ -15,7 +15,7 @@ export function fetchListingQuery(config) {
|
|
|
15
15
|
config.name,
|
|
16
16
|
toValue(config.params),
|
|
17
17
|
queryWithPage,
|
|
18
|
-
|
|
18
|
+
contextKeyOf(config),
|
|
19
19
|
toValue(config.contextDomain)
|
|
20
20
|
);
|
|
21
21
|
};
|
|
@@ -33,7 +33,7 @@ export function fetchListingQuery(config) {
|
|
|
33
33
|
}
|
|
34
34
|
return await client.listing(config.name, toValue(config.params), {
|
|
35
35
|
query: queryWithPage,
|
|
36
|
-
contextKey:
|
|
36
|
+
contextKey: contextKeyOf(config),
|
|
37
37
|
contextDomain: toValue(config.contextDomain),
|
|
38
38
|
requestUrl: toValue(config.requestUrl)
|
|
39
39
|
});
|
|
@@ -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,15 +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
|
-
const merged = { ...defaults, ...provided };
|
|
10
|
-
if (merged.requestUrl === false) delete merged.requestUrl;
|
|
11
|
-
return merged;
|
|
12
|
-
}
|
|
4
|
+
import { resolveContextKey } from "../utils/resolve-context-key.js";
|
|
5
|
+
import { withDefaults } from "../utils/request-defaults.js";
|
|
6
|
+
import { toFronticFetchErrorPayload } from "../utils/describe-error.js";
|
|
13
7
|
export function useFronticClient(options) {
|
|
14
8
|
const config = useRuntimeConfig();
|
|
15
9
|
const fronticConfig = config.public.frontic;
|
|
@@ -35,23 +29,25 @@ export function useFronticClient(options) {
|
|
|
35
29
|
const nuxtApp = useNuxtApp();
|
|
36
30
|
const proxyUrl = resolveProxyUrl();
|
|
37
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();
|
|
38
35
|
const requestUrlValue = resolveRequestUrl();
|
|
39
36
|
const secret = resolveSecret();
|
|
40
37
|
const baseClient = createClient({ proxyUrl });
|
|
41
38
|
function reporting(operation, identity, reqOptions, run) {
|
|
42
39
|
return run().catch((error) => {
|
|
43
|
-
const payload =
|
|
40
|
+
const payload = toFronticFetchErrorPayload(
|
|
44
41
|
operation,
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
};
|
|
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
|
+
);
|
|
55
51
|
void Promise.resolve(nuxtApp.callHook("frontic:fetch:error", payload)).catch(() => {
|
|
56
52
|
});
|
|
57
53
|
throw error;
|
|
@@ -62,21 +58,46 @@ export function useFronticClient(options) {
|
|
|
62
58
|
"block",
|
|
63
59
|
{ name: String(name) },
|
|
64
60
|
reqOptions,
|
|
65
|
-
() => 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
|
+
)
|
|
66
69
|
),
|
|
67
70
|
listing: (name, params, reqOptions) => reporting(
|
|
68
71
|
"listing",
|
|
69
72
|
{ name: String(name) },
|
|
70
73
|
reqOptions,
|
|
71
|
-
() => 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
|
+
)
|
|
72
82
|
),
|
|
73
83
|
tree: (name, reqOptions) => reporting(
|
|
74
84
|
"tree",
|
|
75
85
|
{ name: String(name) },
|
|
76
86
|
reqOptions,
|
|
77
|
-
() => 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))
|
|
78
100
|
),
|
|
79
|
-
page: (slug, reqOptions) => reporting("page", { slug }, reqOptions, () => baseClient.page(slug, withDefaults({ ...requestUrlValue && { requestUrl: requestUrlValue }, ...secret && { secret } }, reqOptions))),
|
|
80
101
|
context: (token, reqOptions) => reporting("context", {}, reqOptions, () => baseClient.context(token, withDefaults({ ...secret && { secret } }, reqOptions))),
|
|
81
102
|
contextList: (token, reqOptions) => reporting("context", {}, reqOptions, () => baseClient.contextList(token, withDefaults({ ...contextDomain && { contextDomain }, ...secret && { secret } }, reqOptions))),
|
|
82
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,19 +1,21 @@
|
|
|
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,
|
|
@@ -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,
|
|
@@ -2,6 +2,7 @@ import { useState, useNuxtApp } from "nuxt/app";
|
|
|
2
2
|
import { computed, ref, toValue, watch } from "vue";
|
|
3
3
|
import { useQuery, useInfiniteQuery, useQueryCache, setInfiniteQueryData } from "@pinia/colada";
|
|
4
4
|
import { resolveContextDomain } from "../utils/resolve-context-domain.js";
|
|
5
|
+
import { resolveContextKey, resolveCallContextKey } from "../utils/resolve-context-key.js";
|
|
5
6
|
import { useDebounceFn } from "@vueuse/core";
|
|
6
7
|
import {
|
|
7
8
|
makeLogicalFilter,
|
|
@@ -22,12 +23,13 @@ export function useFronticSearch(name, ...args) {
|
|
|
22
23
|
const [rawParams, options] = args;
|
|
23
24
|
const params = rawParams ?? {};
|
|
24
25
|
const queryCache = useQueryCache();
|
|
25
|
-
const _client = useFronticClient({ contextDomain: false });
|
|
26
|
+
const _client = useFronticClient({ contextDomain: false, contextKey: false });
|
|
26
27
|
const listingName = name;
|
|
27
28
|
const nuxtApp = useNuxtApp();
|
|
28
29
|
bindRouteReset(nuxtApp);
|
|
29
30
|
const cacheKey = options?.cacheKey ?? String(name);
|
|
30
|
-
const
|
|
31
|
+
const autoContextKey = resolveContextKey();
|
|
32
|
+
const contextKey = computed(() => resolveCallContextKey(toValue(options?.contextKey), autoContextKey));
|
|
31
33
|
const autoContextDomain = resolveContextDomain();
|
|
32
34
|
const contextDomain = computed(() => toValue(options?.contextDomain) ?? autoContextDomain);
|
|
33
35
|
const requestUrl = options?.requestUrl;
|
|
@@ -212,14 +214,26 @@ export function useFronticSearch(name, ...args) {
|
|
|
212
214
|
error = infiniteResult.error;
|
|
213
215
|
refresh = infiniteResult.refresh;
|
|
214
216
|
refetch = infiniteResult.refetch;
|
|
217
|
+
const edgePageParam = (edge) => {
|
|
218
|
+
const pageParams = infiniteResult.data.value?.pageParams;
|
|
219
|
+
return edge === "first" ? pageParams?.at(0) : pageParams?.at(-1);
|
|
220
|
+
};
|
|
221
|
+
const emitLoadedPage = (edge, pageParamBefore) => {
|
|
222
|
+
if (edgePageParam(edge) === pageParamBefore) return;
|
|
223
|
+
const pages = infiniteResult.data.value?.pages;
|
|
224
|
+
const loaded = edge === "first" ? pages?.at(0) : pages?.at(-1);
|
|
225
|
+
if (loaded) emitListingResolved(loaded, "loadMore");
|
|
226
|
+
};
|
|
215
227
|
loadNextFn = async () => {
|
|
228
|
+
const pageParamBefore = edgePageParam("last");
|
|
216
229
|
await infiniteResult.loadNextPage();
|
|
217
|
-
|
|
230
|
+
emitLoadedPage("last", pageParamBefore);
|
|
218
231
|
return flattenedData.value;
|
|
219
232
|
};
|
|
220
233
|
loadPrevFn = async () => {
|
|
234
|
+
const pageParamBefore = edgePageParam("first");
|
|
221
235
|
await infiniteResult.loadPreviousPage();
|
|
222
|
-
|
|
236
|
+
emitLoadedPage("first", pageParamBefore);
|
|
223
237
|
return flattenedData.value;
|
|
224
238
|
};
|
|
225
239
|
loadPageFn = async (page) => {
|
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
import { computed, toValue } from "vue";
|
|
2
2
|
import { useQuery } from "@pinia/colada";
|
|
3
3
|
import { resolveContextDomain } from "../utils/resolve-context-domain.js";
|
|
4
|
+
import { resolveContextKey, resolveCallContextKey } from "../utils/resolve-context-key.js";
|
|
4
5
|
import { fetchTreeQuery } from "./trees.js";
|
|
5
6
|
import { useFronticClient } from "./useFronticClient.js";
|
|
6
7
|
export function useFronticTree(name, 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 treeQuery = fetchTreeQuery({
|
|
11
13
|
name,
|
|
12
14
|
key: options?.key,
|
|
13
15
|
depth: options?.depth,
|
|
14
|
-
contextKey: options?.contextKey,
|
|
16
|
+
contextKey: computed(() => resolveCallContextKey(toValue(options?.contextKey), autoContextKey)),
|
|
15
17
|
contextDomain: resolvedContextDomain,
|
|
16
18
|
requestUrl: options?.requestUrl,
|
|
17
19
|
staleTime: options?.staleTime,
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Frontic client for Nitro.
|
|
3
|
+
*
|
|
4
|
+
* `useFronticClient` is a Nuxt app composable — it reaches for `useNuxtApp`,
|
|
5
|
+
* `useRequestURL` and `nuxt/app`'s `useRuntimeConfig`, none of which exist in a
|
|
6
|
+
* server route. Without this, a `server/` handler builds the generated client
|
|
7
|
+
* itself and silently loses everything the app client resolves: the context
|
|
8
|
+
* domain, the visitor's token, the originating URL, and any report of a
|
|
9
|
+
* failure.
|
|
10
|
+
*
|
|
11
|
+
* The token matters most. It sits in the request's own `fs-context` cookie, and
|
|
12
|
+
* a route that does not forward it answers with the domain's defaults — the
|
|
13
|
+
* same silent wrong-context bug the composables had, in the one place there is
|
|
14
|
+
* no rendered page to notice it on.
|
|
15
|
+
*
|
|
16
|
+
* The API secret is not resolved here: the module's Nitro plugin stamps
|
|
17
|
+
* `fs-secret` onto every request the generated client makes, this one included.
|
|
18
|
+
*/
|
|
19
|
+
import { type H3Event } from 'h3';
|
|
20
|
+
import type { FronticClient } from '../types/client.js';
|
|
21
|
+
export interface FronticServerClientOptions {
|
|
22
|
+
/**
|
|
23
|
+
* Override the context domain sent as `fs-domain`
|
|
24
|
+
* - `false`: send none
|
|
25
|
+
* - `string`: use this domain
|
|
26
|
+
* - `undefined`: use the module's configured `contextDomain`
|
|
27
|
+
*
|
|
28
|
+
* `contextDomain: true` (read the active i18n locale) cannot be honoured here
|
|
29
|
+
* — there is no i18n instance in Nitro — so a project configured that way
|
|
30
|
+
* resolves nothing and should pass the domain per call.
|
|
31
|
+
*/
|
|
32
|
+
contextDomain?: false | string;
|
|
33
|
+
/**
|
|
34
|
+
* Override the context token sent as `fs-context`
|
|
35
|
+
* - `false`: send none, so the request resolves through the domain alone
|
|
36
|
+
* - `string`: use this token
|
|
37
|
+
* - `undefined`: read the request's context cookie
|
|
38
|
+
*
|
|
39
|
+
* Whether that cookie is read at all follows the **module's** `disableContext`,
|
|
40
|
+
* not any per-instance override. `useFronticContext({ disableContext })` is a
|
|
41
|
+
* decision made inside the Nuxt app, and a Nitro route has no way to learn it —
|
|
42
|
+
* it has the request and the config, nothing else. An app that overrides the
|
|
43
|
+
* module setting in a component and also fetches from a server route passes
|
|
44
|
+
* `contextKey` here explicitly.
|
|
45
|
+
*/
|
|
46
|
+
contextKey?: false | string;
|
|
47
|
+
/**
|
|
48
|
+
* Override the URL recorded with the request
|
|
49
|
+
* - `false`: send none
|
|
50
|
+
* - `string`: use this URL
|
|
51
|
+
* - `undefined`: the URL of the incoming request
|
|
52
|
+
*/
|
|
53
|
+
requestUrl?: false | string;
|
|
54
|
+
}
|
|
55
|
+
export declare function createFronticServerClient(event: H3Event, options?: FronticServerClientOptions): FronticClient;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { getCookie, getRequestURL } from "h3";
|
|
2
|
+
import { useRuntimeConfig, useNitroApp } from "nitropack/runtime";
|
|
3
|
+
import { createClient } from "@frontic/stack/generated-client";
|
|
4
|
+
import { withDefaults } from "../utils/request-defaults.js";
|
|
5
|
+
import { toFronticFetchErrorPayload } from "../utils/describe-error.js";
|
|
6
|
+
function resolveOverride(override, fallback) {
|
|
7
|
+
return override === false ? void 0 : override ?? fallback();
|
|
8
|
+
}
|
|
9
|
+
export function createFronticServerClient(event, options) {
|
|
10
|
+
const config = useRuntimeConfig(event);
|
|
11
|
+
const fronticConfig = config.public.frontic;
|
|
12
|
+
const contextDomain = resolveOverride(
|
|
13
|
+
options?.contextDomain,
|
|
14
|
+
() => (
|
|
15
|
+
// `contextDomain: true` means "read the active i18n locale", and there is no
|
|
16
|
+
// i18n instance here. Resolving nothing beats guessing another region.
|
|
17
|
+
typeof fronticConfig?.contextDomain === "string" ? fronticConfig.contextDomain : void 0
|
|
18
|
+
)
|
|
19
|
+
);
|
|
20
|
+
const contextKey = resolveOverride(options?.contextKey, () => {
|
|
21
|
+
if (fronticConfig?.disableContext) return void 0;
|
|
22
|
+
return getCookie(event, fronticConfig?.contextCookieName ?? "fs-context");
|
|
23
|
+
});
|
|
24
|
+
const requestUrl = resolveOverride(options?.requestUrl, () => getRequestURL(event).href);
|
|
25
|
+
const baseClient = createClient({});
|
|
26
|
+
function reporting(operation, identity, reqOptions, run) {
|
|
27
|
+
return run().catch((error) => {
|
|
28
|
+
const reported = reqOptions?.contextKey === false ? void 0 : reqOptions?.contextKey ?? contextKey;
|
|
29
|
+
const payload = toFronticFetchErrorPayload(operation, identity, { contextDomain, contextKey: reported, requestUrl }, error);
|
|
30
|
+
const hooks = useNitroApp().hooks;
|
|
31
|
+
void Promise.resolve(hooks.callHook("frontic:fetch:error", payload)).catch(() => {
|
|
32
|
+
});
|
|
33
|
+
throw error;
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
const defaults = {
|
|
37
|
+
...contextDomain && { contextDomain },
|
|
38
|
+
...contextKey && { contextKey },
|
|
39
|
+
...requestUrl && { requestUrl }
|
|
40
|
+
};
|
|
41
|
+
return {
|
|
42
|
+
block: (name, key, reqOptions) => reporting("block", { name: String(name) }, reqOptions, () => baseClient.block(name, key, withDefaults(defaults, reqOptions))),
|
|
43
|
+
listing: (name, params, reqOptions) => reporting("listing", { name: String(name) }, reqOptions, () => baseClient.listing(name, params, withDefaults(defaults, reqOptions))),
|
|
44
|
+
tree: (name, reqOptions) => reporting("tree", { name: String(name) }, reqOptions, () => baseClient.tree(name, withDefaults(defaults, reqOptions))),
|
|
45
|
+
// A page is addressed by a slug that already carries its domain
|
|
46
|
+
page: (slug, reqOptions) => reporting("page", { slug }, reqOptions, () => baseClient.page(slug, withDefaults({ ...contextKey && { contextKey }, ...requestUrl && { requestUrl } }, reqOptions))),
|
|
47
|
+
context: (token, reqOptions) => reporting("context", {}, reqOptions, () => baseClient.context(token, withDefaults({}, reqOptions))),
|
|
48
|
+
contextList: (token, reqOptions) => reporting("context", {}, reqOptions, () => baseClient.contextList(token, withDefaults({ ...contextDomain && { contextDomain } }, reqOptions))),
|
|
49
|
+
contextUpdate: (context, token, reqOptions) => reporting("context", {}, reqOptions, () => baseClient.contextUpdate(context, token, withDefaults({}, reqOptions))),
|
|
50
|
+
// The Frontic domain is an explicit parameter, so no contextDomain injection
|
|
51
|
+
sitemap: (domain, index, reqOptions) => reporting("sitemap", { domain }, reqOptions, () => baseClient.sitemap(domain, index, withDefaults({ ...requestUrl && { requestUrl } }, reqOptions)))
|
|
52
|
+
};
|
|
53
|
+
}
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
*/
|
|
11
11
|
/** Which client call failed. `contextList` and `contextUpdate` both report as `context`. */
|
|
12
12
|
export type FronticFetchOperation = 'block' | 'listing' | 'tree' | 'page' | 'context' | 'proxy' | 'sitemap';
|
|
13
|
-
export interface
|
|
13
|
+
export interface FronticFetchErrorPayload {
|
|
14
14
|
operation: FronticFetchOperation;
|
|
15
15
|
/** Block, listing or tree name. Absent for `page`, `context`, `proxy` and `sitemap`. */
|
|
16
16
|
name?: string;
|
|
@@ -10,7 +10,7 @@ export type { FronticClient } from './client.js';
|
|
|
10
10
|
export type { FronticRequestOptions, FronticQueryOptions, InclusionMode, FilterField, SortField, SortString, SortStringOf, ListingQuery, ListingEnvelope, ListingFieldConfig, FilterOption, UiFilter, UiSort, AggregationStats, Pagination, FilterData, AggregationData, FilterGroup, AggregationGroup, } from './listing.js';
|
|
11
11
|
export type { RangeFilterValue } from '../utils/filter-helpers.js';
|
|
12
12
|
export type { FronticAnalyticsItem, FronticListingTrigger, FronticListingResolved, FronticSearchCommitted, FronticPageResolved } from '../composables/analytics.js';
|
|
13
|
-
export type {
|
|
13
|
+
export type { FronticFetchErrorPayload, FronticFetchOperation } from './errors.js';
|
|
14
14
|
export type { SearchState, UseFronticSearchOptions, UseSearchReturn, SearchUrlSlice, SearchUrlQuerySyncOptions } from './search.js';
|
|
15
15
|
export type { UseFronticClientOptions } from '../composables/useFronticClient.js';
|
|
16
16
|
export type { UseFronticBlockOptions } from '../composables/useFronticBlock.js';
|
|
@@ -157,11 +157,25 @@ export interface FronticOptions {
|
|
|
157
157
|
};
|
|
158
158
|
/**
|
|
159
159
|
* API secret for authenticating Fetch API requests
|
|
160
|
-
* Required for protected environments (e.g., development/staging)
|
|
161
|
-
* Stored in private runtime config (server-only, never exposed to browser)
|
|
162
160
|
*
|
|
163
|
-
*
|
|
164
|
-
*
|
|
161
|
+
* Only for a project whose Fetch API is protected. A project is readable by
|
|
162
|
+
* anyone holding its URL until a secret is created for it, and creating one
|
|
163
|
+
* is itself the switch — there is no separate toggle. That is a decision
|
|
164
|
+
* about the data (a B2B or partner catalogue, pricing worth scraping, a
|
|
165
|
+
* protect-by-default rule), not about an environment: the same project is
|
|
166
|
+
* protected or not wherever it is served from. Like any other project
|
|
167
|
+
* configuration it moves through Release Control, develop to preview to
|
|
168
|
+
* public, so a storefront against public keeps working until the release
|
|
169
|
+
* carrying the secret lands.
|
|
170
|
+
*
|
|
171
|
+
* Stored in private runtime config (server-only, never exposed to browser),
|
|
172
|
+
* and injected server-side on both paths: by the proxy for browser requests,
|
|
173
|
+
* by a Nitro plugin for SSR and server routes. Nothing enforces that —
|
|
174
|
+
* handing the value to the browser works and simply makes the protection
|
|
175
|
+
* meaningless.
|
|
176
|
+
*
|
|
177
|
+
* On a protected project a missing or wrong secret is 401 on every call at
|
|
178
|
+
* once, never partial.
|
|
165
179
|
*
|
|
166
180
|
* @example process.env.FRONTIC_FETCH_SECRET
|
|
167
181
|
*/
|
|
@@ -108,7 +108,17 @@ export interface RequestOptions {
|
|
|
108
108
|
* per-visitor URL would vary a request that is otherwise identical.
|
|
109
109
|
*/
|
|
110
110
|
requestUrl?: string | false;
|
|
111
|
-
|
|
111
|
+
/**
|
|
112
|
+
* Context token identifying the visitor's region and locale selection, sent
|
|
113
|
+
* as `fs-context`. The Nuxt wrapper fills it in from the context
|
|
114
|
+
* `useFronticContext` manages unless told otherwise.
|
|
115
|
+
*
|
|
116
|
+
* `false` is an instruction to that wrapper, not a value: it opts the call
|
|
117
|
+
* out, so the request resolves through `fs-domain` alone. Reach for it when
|
|
118
|
+
* the response goes behind a shared cache, where one visitor's context must
|
|
119
|
+
* not decide what the next one sees.
|
|
120
|
+
*/
|
|
121
|
+
contextKey?: string | false;
|
|
112
122
|
contextDomain?: string;
|
|
113
123
|
proxyUrl?: string;
|
|
114
124
|
secret?: string;
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* `cause` is non-enumerable, so it is absent from `console.error(err)` and from
|
|
7
7
|
* anything that serialises the error, and has to be walked explicitly.
|
|
8
8
|
*/
|
|
9
|
+
import { type FronticFetchErrorPayload, type FronticFetchOperation } from '../types/errors.js';
|
|
9
10
|
/**
|
|
10
11
|
* Flatten an error and its `cause` chain, outermost first.
|
|
11
12
|
*
|
|
@@ -36,3 +37,21 @@ export interface FronticFetchErrorDetail {
|
|
|
36
37
|
}
|
|
37
38
|
/** Describe a failure once, so every call site reports it the same way. */
|
|
38
39
|
export declare function toFetchErrorDetail(error: unknown): FronticFetchErrorDetail;
|
|
40
|
+
/**
|
|
41
|
+
* Build the `frontic:fetch:error` payload.
|
|
42
|
+
*
|
|
43
|
+
* Shared by both clients so a consumer's handler reads the same shape whichever
|
|
44
|
+
* runtime failed — the app composable announces it on the Nuxt app, the server
|
|
45
|
+
* client on the Nitro app, and neither should invent its own fields.
|
|
46
|
+
*/
|
|
47
|
+
export declare function toFronticFetchErrorPayload(operation: FronticFetchOperation, identity: {
|
|
48
|
+
name?: string;
|
|
49
|
+
slug?: string;
|
|
50
|
+
domain?: string;
|
|
51
|
+
}, request: {
|
|
52
|
+
contextDomain?: string;
|
|
53
|
+
contextKey?: string;
|
|
54
|
+
requestUrl?: string;
|
|
55
|
+
proxyUrl?: string;
|
|
56
|
+
targetUrl?: string;
|
|
57
|
+
}, error: unknown): FronticFetchErrorPayload;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { FRONTIC_FETCH_ERROR_VERSION } from "../types/errors.js";
|
|
1
2
|
const MAX_CAUSE_DEPTH = 5;
|
|
2
3
|
export function describeErrorChain(error) {
|
|
3
4
|
const chain = [];
|
|
@@ -36,3 +37,18 @@ export function toFetchErrorDetail(error) {
|
|
|
36
37
|
...statusOf(error) !== void 0 && { status: statusOf(error) }
|
|
37
38
|
};
|
|
38
39
|
}
|
|
40
|
+
export function toFronticFetchErrorPayload(operation, identity, request, error) {
|
|
41
|
+
return {
|
|
42
|
+
operation,
|
|
43
|
+
...identity.name !== void 0 && { name: identity.name },
|
|
44
|
+
...identity.slug !== void 0 && { slug: identity.slug },
|
|
45
|
+
...identity.domain !== void 0 && { domain: identity.domain },
|
|
46
|
+
...request.contextDomain && { contextDomain: request.contextDomain },
|
|
47
|
+
...request.contextKey && { contextKey: request.contextKey },
|
|
48
|
+
...request.requestUrl && { requestUrl: request.requestUrl },
|
|
49
|
+
...request.proxyUrl && { proxyUrl: request.proxyUrl },
|
|
50
|
+
...request.targetUrl && { targetUrl: request.targetUrl },
|
|
51
|
+
...toFetchErrorDetail(error),
|
|
52
|
+
version: FRONTIC_FETCH_ERROR_VERSION
|
|
53
|
+
};
|
|
54
|
+
}
|
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
* Query Key Utilities
|
|
3
|
-
*
|
|
4
|
-
* Shared building blocks for the per-entity query key factories.
|
|
5
|
-
*/
|
|
1
|
+
import type { MaybeRef } from 'vue';
|
|
6
2
|
/** Default stale time for all Frontic queries (5 minutes) */
|
|
7
3
|
export declare const DEFAULT_STALE_TIME: number;
|
|
8
4
|
/**
|
|
@@ -25,3 +21,12 @@ export declare function requestContextSlot(contextKey?: string, contextDomain?:
|
|
|
25
21
|
* not matter — the cache normalises objects order-insensitively.
|
|
26
22
|
*/
|
|
27
23
|
export declare function toKeyPart(value: Record<string, unknown>): Record<string, unknown>;
|
|
24
|
+
/**
|
|
25
|
+
* The token a query factory should use: its configured value, with an explicit
|
|
26
|
+
* opt-out normalised away. Factories reach for this rather than `toValue` so the
|
|
27
|
+
* cache key and the request are derived from one value — a key built from a
|
|
28
|
+
* different value than the request is what FSC-4523 was.
|
|
29
|
+
*/
|
|
30
|
+
export declare function contextKeyOf(config: {
|
|
31
|
+
contextKey?: MaybeRef<string | false | undefined>;
|
|
32
|
+
}): string | undefined;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { toValue } from "vue";
|
|
1
2
|
export const DEFAULT_STALE_TIME = 1e3 * 60 * 5;
|
|
2
3
|
export function requestContextSlot(contextKey, contextDomain) {
|
|
3
4
|
return { context: contextKey ?? null, domain: contextDomain ?? null };
|
|
@@ -16,3 +17,7 @@ function stripUndefined(value) {
|
|
|
16
17
|
}
|
|
17
18
|
return value;
|
|
18
19
|
}
|
|
20
|
+
export function contextKeyOf(config) {
|
|
21
|
+
const value = toValue(config.contextKey);
|
|
22
|
+
return value === false ? void 0 : value;
|
|
23
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Merge per-request options over a client's resolved defaults.
|
|
3
|
+
*
|
|
4
|
+
* Shared by both clients — the app composable and the Nitro one — because the
|
|
5
|
+
* merge rule is the contract, not an implementation detail: the query factories
|
|
6
|
+
* pass their whole option object, so absent values arrive as an explicit
|
|
7
|
+
* `undefined`. A plain spread would let those erase the defaults the client just
|
|
8
|
+
* resolved, which is how an auto-resolved contextDomain stopped reaching the
|
|
9
|
+
* wire (FSC-4523). Only keys with a value override.
|
|
10
|
+
*/
|
|
11
|
+
export declare function withDefaults<TDefaults extends object, TOptions extends object>(defaults: TDefaults, reqOptions?: TOptions): TDefaults & TOptions;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export function withDefaults(defaults, reqOptions) {
|
|
2
|
+
if (!reqOptions) return defaults;
|
|
3
|
+
const provided = Object.fromEntries(Object.entries(reqOptions).filter(([, value]) => value !== void 0));
|
|
4
|
+
const merged = { ...defaults, ...provided };
|
|
5
|
+
if (merged.requestUrl === false) delete merged.requestUrl;
|
|
6
|
+
if (merged.contextKey === false) delete merged.contextKey;
|
|
7
|
+
return merged;
|
|
8
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which cookie the context lives in, and whether the module manages it at all.
|
|
3
|
+
*
|
|
4
|
+
* Module config is only the default: `useFronticContext({ cookieName, disableContext })`
|
|
5
|
+
* overrides both per instance, and an app that does so writes its token under a
|
|
6
|
+
* different cell than this resolver would guess. So the composable records what
|
|
7
|
+
* it actually used, and every read goes through that.
|
|
8
|
+
*/
|
|
9
|
+
export interface FronticContextScope {
|
|
10
|
+
cookieName: string;
|
|
11
|
+
disableContext: boolean;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* A getter for the current context token.
|
|
15
|
+
*
|
|
16
|
+
* Calls `useCookie`/`useState`, so it belongs in setup — the same rule
|
|
17
|
+
* `resolveContextDomain` follows. The scope is read per call rather than
|
|
18
|
+
* captured: a `useFronticContext` that registers a custom cookie after this
|
|
19
|
+
* composable's setup still has to be observed, or its token never reaches a
|
|
20
|
+
* request.
|
|
21
|
+
*/
|
|
22
|
+
export declare function resolveContextKey(): () => string | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* Apply a per-call override to the resolved token.
|
|
25
|
+
*
|
|
26
|
+
* `false` opts the call out — the request then resolves through `fs-domain`
|
|
27
|
+
* alone. Kept here rather than inlined per composable so the query key and the
|
|
28
|
+
* request cannot drift: every composable reads the token through this one rule.
|
|
29
|
+
*/
|
|
30
|
+
export declare function resolveCallContextKey(override: string | false | undefined, auto: () => string | undefined): string | undefined;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { useCookie, useNuxtApp, useRuntimeConfig, useState } from "nuxt/app";
|
|
2
|
+
function registeredScope(nuxtApp) {
|
|
3
|
+
return nuxtApp._fronticContext?.scope;
|
|
4
|
+
}
|
|
5
|
+
export function resolveContextKey() {
|
|
6
|
+
const config = useRuntimeConfig().public.frontic;
|
|
7
|
+
const nuxtApp = useNuxtApp();
|
|
8
|
+
const configured = {
|
|
9
|
+
cookieName: config?.contextCookieName ?? "fs-context",
|
|
10
|
+
disableContext: config?.disableContext ?? false
|
|
11
|
+
};
|
|
12
|
+
const cells = /* @__PURE__ */ new Map();
|
|
13
|
+
function makeCells(cookieName) {
|
|
14
|
+
const cookie = useCookie(cookieName);
|
|
15
|
+
const token = useState(`frontic:context:${cookieName}:token`, () => cookie.value ?? null);
|
|
16
|
+
return { cookie, token };
|
|
17
|
+
}
|
|
18
|
+
function cellsFor(cookieName, inSetup) {
|
|
19
|
+
let cell = cells.get(cookieName);
|
|
20
|
+
if (!cell) {
|
|
21
|
+
cell = inSetup ? makeCells(cookieName) : nuxtApp.runWithContext(() => makeCells(cookieName));
|
|
22
|
+
cells.set(cookieName, cell);
|
|
23
|
+
}
|
|
24
|
+
return cell;
|
|
25
|
+
}
|
|
26
|
+
if (!configured.disableContext) cellsFor(configured.cookieName, true);
|
|
27
|
+
return () => {
|
|
28
|
+
const scope = registeredScope(nuxtApp) ?? configured;
|
|
29
|
+
if (scope.disableContext) return void 0;
|
|
30
|
+
const { cookie, token } = cellsFor(scope.cookieName, false);
|
|
31
|
+
return token.value ?? cookie.value ?? void 0;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export function resolveCallContextKey(override, auto) {
|
|
35
|
+
if (override === false) return void 0;
|
|
36
|
+
return override ?? auto();
|
|
37
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frontic/nuxt",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.1",
|
|
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",
|
|
@@ -35,6 +35,10 @@
|
|
|
35
35
|
},
|
|
36
36
|
"./types": {
|
|
37
37
|
"types": "./dist/runtime/types/index.d.ts"
|
|
38
|
+
},
|
|
39
|
+
"./server": {
|
|
40
|
+
"types": "./dist/runtime/server/client.d.ts",
|
|
41
|
+
"import": "./dist/runtime/server/client.js"
|
|
38
42
|
}
|
|
39
43
|
},
|
|
40
44
|
"main": "./dist/module.mjs",
|