@frontic/nuxt 0.0.2 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/module.d.mts +87 -10
- package/dist/module.json +1 -1
- package/dist/module.mjs +117 -56
- package/dist/runtime/composables/blocks.d.ts +0 -0
- package/dist/runtime/composables/blocks.js +31 -0
- package/dist/runtime/composables/index.js +7 -0
- package/dist/runtime/composables/listings.js +15 -9
- package/dist/runtime/composables/pages.d.ts +0 -0
- package/dist/runtime/composables/pages.js +31 -0
- package/dist/runtime/composables/useFronticBlock.d.ts +0 -0
- package/dist/runtime/composables/useFronticBlock.js +31 -0
- package/dist/runtime/composables/useFronticClient.d.ts +0 -0
- package/dist/runtime/composables/useFronticClient.js +67 -0
- package/dist/runtime/composables/useFronticContext.d.ts +0 -0
- package/dist/runtime/composables/useFronticContext.js +77 -0
- package/dist/runtime/composables/useFronticListing.js +26 -201
- package/dist/runtime/composables/useFronticPage.d.ts +0 -0
- package/dist/runtime/composables/useFronticPage.js +74 -0
- package/dist/runtime/composables/useFronticSearch.d.ts +0 -0
- package/dist/runtime/composables/useFronticSearch.js +321 -0
- package/dist/runtime/server/api/frontic/[...path].d.ts +0 -0
- package/dist/runtime/server/api/frontic/[...path].js +21 -0
- package/dist/runtime/types/search.d.ts +0 -0
- package/dist/runtime/types/search.js +0 -0
- package/dist/runtime/utils/state-helpers.js +19 -19
- package/dist/types.d.mts +4 -2
- package/package.json +20 -20
- package/dist/module.cjs +0 -5
- package/dist/module.d.ts +0 -48
- package/dist/types.d.ts +0 -7
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Frontic
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/module.d.mts
CHANGED
|
@@ -2,8 +2,15 @@ import { NuxtModule } from '@nuxt/schema';
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Available Frontic composables that can be enabled/disabled
|
|
5
|
+
*
|
|
6
|
+
* - 'block': useFronticBlock - Fetch single blocks (products, categories)
|
|
7
|
+
* - 'listing': useFronticListing - Fetch listings with caching
|
|
8
|
+
* - 'search': useFronticSearch - Full-featured search with filters/sorting
|
|
9
|
+
* - 'page': useFronticPage - Dynamic page routing
|
|
10
|
+
* - 'context': useFronticContext - Locale/region management
|
|
11
|
+
* - 'client': useFronticClient - Low-level API client
|
|
5
12
|
*/
|
|
6
|
-
type FronticComposable = 'listing' | '
|
|
13
|
+
type FronticComposable = 'block' | 'listing' | 'search' | 'page' | 'context' | 'client';
|
|
7
14
|
/**
|
|
8
15
|
* Frontic composables configuration
|
|
9
16
|
* - true: Enable all composables (default)
|
|
@@ -26,23 +33,93 @@ interface FronticOptions {
|
|
|
26
33
|
*/
|
|
27
34
|
componentDir: string;
|
|
28
35
|
/**
|
|
29
|
-
* Configure which Frontic composables to
|
|
30
|
-
* - true: Enable all composables (default)
|
|
31
|
-
* - false or []: Disable all composables
|
|
32
|
-
* - string[]: Enable only specified composables
|
|
36
|
+
* Configure which Frontic composables to auto-import
|
|
33
37
|
*
|
|
34
|
-
*
|
|
38
|
+
* When a composable is disabled, it will not be auto-imported during the Nuxt build.
|
|
39
|
+
* This means you won't get type errors or warnings—the composable simply won't exist.
|
|
40
|
+
* Use this to reduce bundle size by excluding composables you don't need.
|
|
41
|
+
*
|
|
42
|
+
* Available composables:
|
|
43
|
+
* - 'block': useFronticBlock - Fetch single blocks
|
|
44
|
+
* - 'listing': useFronticListing - Fetch listings
|
|
45
|
+
* - 'search': useFronticSearch - Search with filters/sorting
|
|
46
|
+
* - 'page': useFronticPage - Dynamic page routing
|
|
47
|
+
* - 'context': useFronticContext - Locale/region management
|
|
48
|
+
* - 'client': useFronticClient - Low-level API client
|
|
35
49
|
*
|
|
36
50
|
* @example
|
|
37
|
-
* composables: true // Enable all
|
|
51
|
+
* composables: true // Enable all (default)
|
|
38
52
|
* composables: false // Disable all
|
|
39
|
-
* composables: ['listing', 'page'] // Enable specific ones
|
|
53
|
+
* composables: ['listing', 'page', 'client'] // Enable specific ones
|
|
40
54
|
*
|
|
41
55
|
* @default true
|
|
42
56
|
*/
|
|
43
57
|
composables?: FronticComposablesConfig;
|
|
58
|
+
/**
|
|
59
|
+
* Enable/configure the API proxy for CORS prevention
|
|
60
|
+
* - true: Enable proxy at default path '/api/stack'
|
|
61
|
+
* - false: Disable proxy
|
|
62
|
+
* - string: Enable proxy at custom path
|
|
63
|
+
*
|
|
64
|
+
* @default true
|
|
65
|
+
*/
|
|
66
|
+
proxy?: boolean | string;
|
|
67
|
+
/**
|
|
68
|
+
* Context domain for page slug construction
|
|
69
|
+
* Used by useFronticPage when auto-detecting slug from current route
|
|
70
|
+
*
|
|
71
|
+
* @example 'demo-shop.com'
|
|
72
|
+
*/
|
|
73
|
+
contextDomain?: string;
|
|
74
|
+
/**
|
|
75
|
+
* Cookie name for persisting the context token
|
|
76
|
+
* @default 'fs-context'
|
|
77
|
+
*/
|
|
78
|
+
contextCookieName?: string;
|
|
79
|
+
/**
|
|
80
|
+
* Cookie max age in seconds for context token
|
|
81
|
+
* @default 31536000 (1 year)
|
|
82
|
+
*/
|
|
83
|
+
contextCookieMaxAge?: number;
|
|
84
|
+
/**
|
|
85
|
+
* Disable automatic context fetching and cookie management in useFronticContext
|
|
86
|
+
*
|
|
87
|
+
* When true:
|
|
88
|
+
* - The context endpoint will NOT be called automatically on mount
|
|
89
|
+
* - No context cookie will be set automatically
|
|
90
|
+
* - You must manually call `refresh()` to fetch contexts
|
|
91
|
+
* - You are responsible for storing and managing the context token
|
|
92
|
+
*
|
|
93
|
+
* Use this when you want full control over context management,
|
|
94
|
+
* or when integrating with an existing locale/region system.
|
|
95
|
+
*
|
|
96
|
+
* @default false
|
|
97
|
+
*/
|
|
98
|
+
disableContext?: boolean;
|
|
99
|
+
/**
|
|
100
|
+
* Automatically redirect on 301 responses
|
|
101
|
+
* Can be overridden per-request in useFronticPage
|
|
102
|
+
* @default true
|
|
103
|
+
*/
|
|
104
|
+
redirectOn301?: boolean;
|
|
105
|
+
/**
|
|
106
|
+
* Throw a 404 error when page is not found
|
|
107
|
+
* Can be overridden per-request in useFronticPage
|
|
108
|
+
* @default true
|
|
109
|
+
*/
|
|
110
|
+
throwOn404?: boolean;
|
|
111
|
+
/**
|
|
112
|
+
* API secret for authenticating Fetch API requests
|
|
113
|
+
* Required for protected environments (e.g., development/staging)
|
|
114
|
+
* Stored in private runtime config (server-only, never exposed to browser)
|
|
115
|
+
*
|
|
116
|
+
* For client-side requests, the secret is automatically injected by the proxy
|
|
117
|
+
*
|
|
118
|
+
* @example process.env.FRONTSTACK_FETCH_SECRET
|
|
119
|
+
*/
|
|
120
|
+
fetchApiSecret?: string;
|
|
44
121
|
}
|
|
45
|
-
declare const module: NuxtModule<FronticOptions>;
|
|
122
|
+
declare const module$1: NuxtModule<FronticOptions>;
|
|
46
123
|
|
|
47
|
-
export { module as default };
|
|
124
|
+
export { module$1 as default };
|
|
48
125
|
export type { FronticComposable, FronticComposablesConfig, FronticOptions };
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,33 +1,28 @@
|
|
|
1
1
|
import { existsSync, readdirSync } from 'node:fs';
|
|
2
2
|
import { dirname, join } from 'node:path';
|
|
3
3
|
import { fileURLToPath } from 'node:url';
|
|
4
|
-
import { defineNuxtModule, createResolver, addImports, addComponentsDir, addComponentExports } from '@nuxt/kit';
|
|
4
|
+
import { defineNuxtModule, createResolver, addServerHandler, addImports, addComponentsDir, addComponentExports } from '@nuxt/kit';
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
// -- Unbuild CommonJS Shims --
|
|
9
|
-
import __cjs_url__ from 'url';
|
|
10
|
-
import __cjs_path__ from 'path';
|
|
11
|
-
import __cjs_mod__ from 'module';
|
|
12
|
-
const __filename = __cjs_url__.fileURLToPath(import.meta.url);
|
|
13
|
-
const __dirname = __cjs_path__.dirname(__filename);
|
|
14
|
-
const require = __cjs_mod__.createRequire(import.meta.url);
|
|
15
6
|
const _dirname = typeof __dirname !== "undefined" ? __dirname : dirname(fileURLToPath(import.meta.url));
|
|
16
|
-
const
|
|
7
|
+
const ALL_COMPOSABLES = ["block", "listing", "search", "page", "context", "client"];
|
|
17
8
|
function isComposableEnabled(config, name) {
|
|
18
|
-
if (config === true)
|
|
19
|
-
|
|
20
|
-
if (config
|
|
21
|
-
return false;
|
|
22
|
-
if (Array.isArray(config))
|
|
23
|
-
return config.includes(name);
|
|
9
|
+
if (config === true) return true;
|
|
10
|
+
if (config === false) return false;
|
|
11
|
+
if (Array.isArray(config)) return config.includes(name);
|
|
24
12
|
return true;
|
|
25
13
|
}
|
|
26
|
-
const module = defineNuxtModule({
|
|
14
|
+
const module$1 = defineNuxtModule({
|
|
27
15
|
defaults: {
|
|
28
16
|
prefix: "",
|
|
29
17
|
componentDir: "@/components/ui",
|
|
30
|
-
composables: true
|
|
18
|
+
composables: true,
|
|
19
|
+
proxy: true,
|
|
20
|
+
contextCookieName: "fs-context",
|
|
21
|
+
contextCookieMaxAge: 60 * 60 * 24 * 365,
|
|
22
|
+
// 1 year
|
|
23
|
+
disableContext: false,
|
|
24
|
+
redirectOn301: true,
|
|
25
|
+
throwOn404: true
|
|
31
26
|
},
|
|
32
27
|
meta: {
|
|
33
28
|
name: "frontic",
|
|
@@ -38,22 +33,46 @@ const module = defineNuxtModule({
|
|
|
38
33
|
}
|
|
39
34
|
},
|
|
40
35
|
async setup(options, nuxt) {
|
|
41
|
-
const {
|
|
36
|
+
const {
|
|
37
|
+
componentDir,
|
|
38
|
+
prefix,
|
|
39
|
+
composables: composablesConfig = true,
|
|
40
|
+
proxy: proxyConfig = true,
|
|
41
|
+
contextDomain,
|
|
42
|
+
contextCookieName = "fs-context",
|
|
43
|
+
contextCookieMaxAge = 60 * 60 * 24 * 365,
|
|
44
|
+
disableContext = false,
|
|
45
|
+
redirectOn301 = true,
|
|
46
|
+
throwOn404 = true,
|
|
47
|
+
fetchApiSecret
|
|
48
|
+
} = options;
|
|
42
49
|
const COMPONENT_DIR_PATH = componentDir;
|
|
43
50
|
const ROOT_DIR_PATH = nuxt.options.rootDir;
|
|
44
51
|
const { resolve, resolvePath } = createResolver(ROOT_DIR_PATH);
|
|
45
52
|
const { resolve: resolveModule } = createResolver(_dirname);
|
|
46
53
|
const runtimeDir = resolveModule("./runtime");
|
|
54
|
+
const proxyPath = typeof proxyConfig === "string" ? proxyConfig : proxyConfig !== false ? "/api/stack" : void 0;
|
|
55
|
+
if (proxyPath) {
|
|
56
|
+
addServerHandler({
|
|
57
|
+
route: `${proxyPath}/**`,
|
|
58
|
+
handler: resolveModule("./runtime/server/api/stack/[...path]")
|
|
59
|
+
});
|
|
60
|
+
}
|
|
47
61
|
const fronticClientDir = join(ROOT_DIR_PATH, ".frontstack");
|
|
48
62
|
const fronticClientPath = join(fronticClientDir, "generated-client.ts");
|
|
49
63
|
const hasFronticClient = existsSync(fronticClientPath);
|
|
64
|
+
const blockEnabled = isComposableEnabled(composablesConfig, "block");
|
|
50
65
|
const listingEnabled = isComposableEnabled(composablesConfig, "listing");
|
|
66
|
+
const searchEnabled = isComposableEnabled(composablesConfig, "search");
|
|
51
67
|
const pageEnabled = isComposableEnabled(composablesConfig, "page");
|
|
52
|
-
const blockEnabled = isComposableEnabled(composablesConfig, "block");
|
|
53
68
|
const contextEnabled = isComposableEnabled(composablesConfig, "context");
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
|
|
69
|
+
const clientEnabled = isComposableEnabled(composablesConfig, "client");
|
|
70
|
+
const anyComposableEnabled = ALL_COMPOSABLES.some((c) => isComposableEnabled(composablesConfig, c));
|
|
71
|
+
if (anyComposableEnabled && !hasFronticClient) {
|
|
72
|
+
const enabledList = ALL_COMPOSABLES.filter((c) => isComposableEnabled(composablesConfig, c)).map((c) => {
|
|
73
|
+
if (c === "client") return "useFronticClient";
|
|
74
|
+
return `useFrontic${c.charAt(0).toUpperCase() + c.slice(1)}`;
|
|
75
|
+
});
|
|
57
76
|
console.warn(
|
|
58
77
|
`
|
|
59
78
|
[@frontic/nuxt] Frontic client not found at .frontstack/generated-client.ts
|
|
@@ -66,44 +85,88 @@ frontic: { composables: false }
|
|
|
66
85
|
nuxt.hook("prepare:types", ({ tsConfig }) => {
|
|
67
86
|
tsConfig.compilerOptions ||= {};
|
|
68
87
|
tsConfig.compilerOptions.paths ||= {};
|
|
69
|
-
tsConfig.compilerOptions.paths["@
|
|
88
|
+
tsConfig.compilerOptions.paths["@frontic/stack/*"] = ["../.frontstack/*"];
|
|
70
89
|
});
|
|
71
|
-
if (
|
|
90
|
+
if (anyComposableEnabled) {
|
|
72
91
|
const composablesDir = resolve(runtimeDir, "composables");
|
|
73
92
|
const utilsDir = resolve(runtimeDir, "utils");
|
|
74
|
-
|
|
75
|
-
{ name: "
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
{ name: "
|
|
95
|
-
|
|
96
|
-
|
|
93
|
+
if (clientEnabled) {
|
|
94
|
+
addImports([{ name: "useFronticClient", from: join(composablesDir, "useFronticClient") }]);
|
|
95
|
+
}
|
|
96
|
+
if (blockEnabled) {
|
|
97
|
+
addImports([
|
|
98
|
+
{ name: "useFronticBlock", from: join(composablesDir, "useFronticBlock") },
|
|
99
|
+
{ name: "BLOCK_QUERY_KEYS", from: join(composablesDir, "blocks") },
|
|
100
|
+
{ name: "fetchBlockQuery", from: join(composablesDir, "blocks") },
|
|
101
|
+
{ name: "createBlockCacheKey", from: join(composablesDir, "blocks") }
|
|
102
|
+
]);
|
|
103
|
+
}
|
|
104
|
+
if (listingEnabled) {
|
|
105
|
+
addImports([
|
|
106
|
+
{ name: "useFronticListing", from: join(composablesDir, "useFronticListing") },
|
|
107
|
+
{ name: "LISTING_QUERY_KEYS", from: join(composablesDir, "listings") },
|
|
108
|
+
{ name: "fetchListingQuery", from: join(composablesDir, "listings") },
|
|
109
|
+
{ name: "createCacheKey", from: join(composablesDir, "listings") }
|
|
110
|
+
]);
|
|
111
|
+
}
|
|
112
|
+
if (searchEnabled) {
|
|
113
|
+
addImports([{ name: "useFronticSearch", from: join(composablesDir, "useFronticSearch") }]);
|
|
114
|
+
if (!listingEnabled) {
|
|
115
|
+
addImports([
|
|
116
|
+
{ name: "LISTING_QUERY_KEYS", from: join(composablesDir, "listings") },
|
|
117
|
+
{ name: "fetchListingQuery", from: join(composablesDir, "listings") },
|
|
118
|
+
{ name: "createCacheKey", from: join(composablesDir, "listings") }
|
|
119
|
+
]);
|
|
120
|
+
}
|
|
121
|
+
addImports([
|
|
122
|
+
{ name: "makeEqualsFilter", from: join(utilsDir, "filter-helpers") },
|
|
123
|
+
{ name: "makeLogicalFilter", from: join(utilsDir, "filter-helpers") },
|
|
124
|
+
{ name: "manageFilter", from: join(utilsDir, "filter-helpers") },
|
|
125
|
+
{ name: "removeFilter", from: join(utilsDir, "filter-helpers") },
|
|
126
|
+
{ name: "hasFilter", from: join(utilsDir, "filter-helpers") },
|
|
127
|
+
{ name: "getFilterValues", from: join(utilsDir, "filter-helpers") },
|
|
128
|
+
{ name: "makeFilterState", from: join(utilsDir, "state-helpers") },
|
|
129
|
+
{ name: "processAvailableFilters", from: join(utilsDir, "state-helpers") },
|
|
130
|
+
{ name: "processAvailableSorts", from: join(utilsDir, "state-helpers") },
|
|
131
|
+
{ name: "applyFilterConfig", from: join(utilsDir, "state-helpers") },
|
|
132
|
+
{ name: "countActiveFilters", from: join(utilsDir, "state-helpers") },
|
|
133
|
+
{ name: "parseSortString", from: join(utilsDir, "state-helpers") },
|
|
134
|
+
{ name: "isSortAllowed", from: join(utilsDir, "state-helpers") }
|
|
135
|
+
]);
|
|
136
|
+
}
|
|
137
|
+
if (pageEnabled) {
|
|
138
|
+
addImports([
|
|
139
|
+
{ name: "useFronticPage", from: join(composablesDir, "useFronticPage") },
|
|
140
|
+
{ name: "PAGE_QUERY_KEYS", from: join(composablesDir, "pages") },
|
|
141
|
+
{ name: "fetchPageQuery", from: join(composablesDir, "pages") },
|
|
142
|
+
{ name: "createPageCacheKey", from: join(composablesDir, "pages") }
|
|
143
|
+
]);
|
|
144
|
+
}
|
|
145
|
+
if (contextEnabled) {
|
|
146
|
+
addImports([{ name: "useFronticContext", from: join(composablesDir, "useFronticContext") }]);
|
|
147
|
+
}
|
|
97
148
|
}
|
|
98
149
|
nuxt.options.runtimeConfig.public.frontic = {
|
|
99
150
|
hasFronticClient,
|
|
151
|
+
proxyPath,
|
|
152
|
+
contextDomain,
|
|
153
|
+
contextCookieName,
|
|
154
|
+
contextCookieMaxAge,
|
|
155
|
+
disableContext,
|
|
156
|
+
redirectOn301,
|
|
157
|
+
throwOn404,
|
|
100
158
|
composables: {
|
|
159
|
+
block: blockEnabled,
|
|
101
160
|
listing: listingEnabled,
|
|
161
|
+
search: searchEnabled,
|
|
102
162
|
page: pageEnabled,
|
|
103
|
-
|
|
104
|
-
|
|
163
|
+
context: contextEnabled,
|
|
164
|
+
client: clientEnabled
|
|
105
165
|
}
|
|
106
166
|
};
|
|
167
|
+
nuxt.options.runtimeConfig.frontic = {
|
|
168
|
+
fetchApiSecret
|
|
169
|
+
};
|
|
107
170
|
const componentsPath = await resolvePath(COMPONENT_DIR_PATH);
|
|
108
171
|
addComponentsDir(
|
|
109
172
|
{
|
|
@@ -128,16 +191,14 @@ frontic: { composables: false }
|
|
|
128
191
|
priority: 1
|
|
129
192
|
});
|
|
130
193
|
} catch (err) {
|
|
131
|
-
if (err instanceof Error)
|
|
132
|
-
console.warn("Module error: ", err.message);
|
|
194
|
+
if (err instanceof Error) console.warn("Module error: ", err.message);
|
|
133
195
|
}
|
|
134
196
|
})
|
|
135
197
|
);
|
|
136
198
|
} catch (err) {
|
|
137
|
-
if (err instanceof Error)
|
|
138
|
-
console.warn(err.message);
|
|
199
|
+
if (err instanceof Error) console.warn(err.message);
|
|
139
200
|
}
|
|
140
201
|
}
|
|
141
202
|
});
|
|
142
203
|
|
|
143
|
-
export { module as default };
|
|
204
|
+
export { module$1 as default };
|
|
File without changes
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
function unwrapRef(ref) {
|
|
2
|
+
return typeof ref === "object" && ref !== null && "value" in ref ? ref.value : ref;
|
|
3
|
+
}
|
|
4
|
+
export const BLOCK_QUERY_KEYS = {
|
|
5
|
+
root: ["blocks"],
|
|
6
|
+
block: (blockName) => [...BLOCK_QUERY_KEYS.root, blockName],
|
|
7
|
+
detail: (blockName, key, contextKey) => [...BLOCK_QUERY_KEYS.block(blockName), key, contextKey ?? "default"]
|
|
8
|
+
};
|
|
9
|
+
export function fetchBlockQuery(config) {
|
|
10
|
+
return {
|
|
11
|
+
key: () => {
|
|
12
|
+
const key = unwrapRef(config.key);
|
|
13
|
+
const contextKey = unwrapRef(config.contextKey);
|
|
14
|
+
return BLOCK_QUERY_KEYS.detail(config.name, key, contextKey);
|
|
15
|
+
},
|
|
16
|
+
query: async () => {
|
|
17
|
+
const { useFronticClient } = await import("./useFronticClient.js");
|
|
18
|
+
const client = useFronticClient();
|
|
19
|
+
const key = unwrapRef(config.key);
|
|
20
|
+
const contextKey = unwrapRef(config.contextKey);
|
|
21
|
+
const contextDomain = unwrapRef(config.contextDomain);
|
|
22
|
+
const requestUrl = unwrapRef(config.requestUrl);
|
|
23
|
+
return await client.block(config.name, key, { contextKey, contextDomain, requestUrl });
|
|
24
|
+
},
|
|
25
|
+
staleTime: config.staleTime ?? 1e3 * 60 * 5
|
|
26
|
+
// 5 minutes default
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export function createBlockCacheKey(keyArray) {
|
|
30
|
+
return keyArray.map((part) => typeof part === "object" ? JSON.stringify(part) : String(part)).join("-");
|
|
31
|
+
}
|
|
@@ -1,2 +1,9 @@
|
|
|
1
|
+
export { useFronticClient } from "./useFronticClient.js";
|
|
2
|
+
export { useFronticSearch } from "./useFronticSearch.js";
|
|
1
3
|
export { useFronticListing } from "./useFronticListing.js";
|
|
2
4
|
export { fetchListingQuery, LISTING_QUERY_KEYS, createCacheKey } from "./listings.js";
|
|
5
|
+
export { useFronticBlock } from "./useFronticBlock.js";
|
|
6
|
+
export { fetchBlockQuery, BLOCK_QUERY_KEYS, createBlockCacheKey } from "./blocks.js";
|
|
7
|
+
export { useFronticPage } from "./useFronticPage.js";
|
|
8
|
+
export { fetchPageQuery, PAGE_QUERY_KEYS, createPageCacheKey } from "./pages.js";
|
|
9
|
+
export { useFronticContext } from "./useFronticContext.js";
|
|
@@ -13,26 +13,32 @@ export const LISTING_QUERY_KEYS = {
|
|
|
13
13
|
export function fetchListingQuery(config) {
|
|
14
14
|
return {
|
|
15
15
|
key: () => {
|
|
16
|
-
const params = unwrapRef(config.
|
|
17
|
-
const query = unwrapRef(config.query);
|
|
16
|
+
const params = unwrapRef(config.params);
|
|
17
|
+
const query = unwrapRef(config.query) ?? {};
|
|
18
18
|
const page = unwrapRef(config.page);
|
|
19
19
|
const queryWithPage = page ? { ...query, page } : query;
|
|
20
|
-
return LISTING_QUERY_KEYS.detail(config.
|
|
20
|
+
return LISTING_QUERY_KEYS.detail(config.name, params, queryWithPage);
|
|
21
21
|
},
|
|
22
22
|
query: async () => {
|
|
23
|
-
const params = unwrapRef(config.
|
|
24
|
-
const query = unwrapRef(config.query);
|
|
23
|
+
const params = unwrapRef(config.params);
|
|
24
|
+
const query = unwrapRef(config.query) ?? {};
|
|
25
25
|
const contextKey = unwrapRef(config.contextKey);
|
|
26
|
+
const contextDomain = unwrapRef(config.contextDomain);
|
|
27
|
+
const requestUrl = unwrapRef(config.requestUrl);
|
|
26
28
|
const page = unwrapRef(config.page);
|
|
27
29
|
const queryWithPage = page ? { ...query, page } : query;
|
|
28
30
|
try {
|
|
29
|
-
const {
|
|
30
|
-
|
|
31
|
+
const { useFronticClient } = await import("./useFronticClient.js");
|
|
32
|
+
const client = useFronticClient();
|
|
33
|
+
const result = await client.listing(config.name, params, {
|
|
31
34
|
query: queryWithPage,
|
|
32
|
-
contextKey
|
|
35
|
+
contextKey,
|
|
36
|
+
contextDomain,
|
|
37
|
+
requestUrl
|
|
33
38
|
});
|
|
39
|
+
return result;
|
|
34
40
|
} catch (error) {
|
|
35
|
-
console.error(`[@frontic/nuxt] Failed to fetch listing "${config.
|
|
41
|
+
console.error(`[@frontic/nuxt] Failed to fetch listing "${String(config.name)}". Make sure the Frontic client is generated.`);
|
|
36
42
|
throw error;
|
|
37
43
|
}
|
|
38
44
|
},
|
|
File without changes
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
function unwrapRef(ref) {
|
|
2
|
+
return typeof ref === "object" && ref !== null && "value" in ref ? ref.value : ref;
|
|
3
|
+
}
|
|
4
|
+
export const PAGE_QUERY_KEYS = {
|
|
5
|
+
root: ["pages"],
|
|
6
|
+
page: (slug) => [...PAGE_QUERY_KEYS.root, slug],
|
|
7
|
+
detail: (slug, contextKey) => [...PAGE_QUERY_KEYS.page(slug), contextKey ?? "default"]
|
|
8
|
+
};
|
|
9
|
+
export function fetchPageQuery(config) {
|
|
10
|
+
return {
|
|
11
|
+
key: () => {
|
|
12
|
+
const slug = unwrapRef(config.slug);
|
|
13
|
+
const contextKey = unwrapRef(config.contextKey);
|
|
14
|
+
return PAGE_QUERY_KEYS.detail(slug, contextKey);
|
|
15
|
+
},
|
|
16
|
+
query: async () => {
|
|
17
|
+
const { useFronticClient } = await import("./useFronticClient.js");
|
|
18
|
+
const client = useFronticClient();
|
|
19
|
+
const slug = unwrapRef(config.slug);
|
|
20
|
+
const contextKey = unwrapRef(config.contextKey);
|
|
21
|
+
const contextDomain = unwrapRef(config.contextDomain);
|
|
22
|
+
const requestUrl = unwrapRef(config.requestUrl);
|
|
23
|
+
return await client.page(slug, { contextKey, contextDomain, requestUrl });
|
|
24
|
+
},
|
|
25
|
+
staleTime: config.staleTime ?? 1e3 * 60 * 5
|
|
26
|
+
// 5 minutes default
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export function createPageCacheKey(keyArray) {
|
|
30
|
+
return keyArray.map((part) => typeof part === "object" ? JSON.stringify(part) : String(part)).join("-");
|
|
31
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { useQuery } from "@pinia/colada";
|
|
2
|
+
import { fetchBlockQuery } from "./blocks.js";
|
|
3
|
+
export function useFronticBlock(name, key, options) {
|
|
4
|
+
const blockQuery = fetchBlockQuery({
|
|
5
|
+
name,
|
|
6
|
+
key,
|
|
7
|
+
contextKey: options?.contextKey,
|
|
8
|
+
contextDomain: options?.contextDomain,
|
|
9
|
+
requestUrl: options?.requestUrl,
|
|
10
|
+
staleTime: options?.staleTime
|
|
11
|
+
});
|
|
12
|
+
const { data, status, refresh, refetch } = useQuery(blockQuery);
|
|
13
|
+
return {
|
|
14
|
+
/**
|
|
15
|
+
* The block data, typed based on the block name
|
|
16
|
+
*/
|
|
17
|
+
block: data,
|
|
18
|
+
/**
|
|
19
|
+
* Query status: 'pending' | 'error' | 'success'
|
|
20
|
+
*/
|
|
21
|
+
status,
|
|
22
|
+
/**
|
|
23
|
+
* Manually refresh the block data (uses cache if valid)
|
|
24
|
+
*/
|
|
25
|
+
refresh,
|
|
26
|
+
/**
|
|
27
|
+
* Force a fresh fetch of the block data, bypassing cache
|
|
28
|
+
*/
|
|
29
|
+
refetch
|
|
30
|
+
};
|
|
31
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { createClient } from "@frontic/stack/generated-client";
|
|
2
|
+
export function useFronticClient(options) {
|
|
3
|
+
const config = useRuntimeConfig();
|
|
4
|
+
const requestURL = useRequestURL();
|
|
5
|
+
const resolveProxyUrl = () => {
|
|
6
|
+
if (options?.proxy === false) return void 0;
|
|
7
|
+
if (options?.proxy === true) return config.public.frontic?.proxyPath;
|
|
8
|
+
if (typeof options?.proxy === "string") return options.proxy;
|
|
9
|
+
return import.meta.server ? void 0 : config.public.frontic?.proxyPath;
|
|
10
|
+
};
|
|
11
|
+
const resolveContextDomain = () => {
|
|
12
|
+
if (options?.contextDomain === false) return void 0;
|
|
13
|
+
if (typeof options?.contextDomain === "string") return options.contextDomain;
|
|
14
|
+
return config.public.frontic?.contextDomain;
|
|
15
|
+
};
|
|
16
|
+
const resolveRequestUrl = () => {
|
|
17
|
+
if (options?.requestUrl === false) return void 0;
|
|
18
|
+
if (typeof options?.requestUrl === "string") return options.requestUrl;
|
|
19
|
+
return requestURL.href;
|
|
20
|
+
};
|
|
21
|
+
const resolveSecret = () => {
|
|
22
|
+
if (typeof options?.secret === "string") return options.secret;
|
|
23
|
+
if (import.meta.server) {
|
|
24
|
+
return config.frontic?.fetchApiSecret;
|
|
25
|
+
}
|
|
26
|
+
return void 0;
|
|
27
|
+
};
|
|
28
|
+
const proxyUrl = resolveProxyUrl();
|
|
29
|
+
const contextDomain = resolveContextDomain();
|
|
30
|
+
const requestUrlValue = resolveRequestUrl();
|
|
31
|
+
const secret = resolveSecret();
|
|
32
|
+
const baseClient = createClient({ proxyUrl });
|
|
33
|
+
const client = {
|
|
34
|
+
block: (name, key, reqOptions) => baseClient.block(name, key, {
|
|
35
|
+
...contextDomain && { contextDomain },
|
|
36
|
+
...requestUrlValue && { requestUrl: requestUrlValue },
|
|
37
|
+
...secret && { secret },
|
|
38
|
+
...reqOptions
|
|
39
|
+
// Per-request options override defaults
|
|
40
|
+
}),
|
|
41
|
+
listing: (name, params, reqOptions) => baseClient.listing(name, params, {
|
|
42
|
+
...contextDomain && { contextDomain },
|
|
43
|
+
...requestUrlValue && { requestUrl: requestUrlValue },
|
|
44
|
+
...secret && { secret },
|
|
45
|
+
...reqOptions
|
|
46
|
+
}),
|
|
47
|
+
page: (slug, reqOptions) => baseClient.page(slug, {
|
|
48
|
+
...requestUrlValue && { requestUrl: requestUrlValue },
|
|
49
|
+
...secret && { secret },
|
|
50
|
+
...reqOptions
|
|
51
|
+
}),
|
|
52
|
+
context: (token, reqOptions) => baseClient.context(token, {
|
|
53
|
+
...secret && { secret },
|
|
54
|
+
...reqOptions
|
|
55
|
+
}),
|
|
56
|
+
contextList: (token, reqOptions) => baseClient.contextList(token, {
|
|
57
|
+
...contextDomain && { contextDomain },
|
|
58
|
+
...secret && { secret },
|
|
59
|
+
...reqOptions
|
|
60
|
+
}),
|
|
61
|
+
contextUpdate: (context, token, reqOptions) => baseClient.contextUpdate(context, token, {
|
|
62
|
+
...secret && { secret },
|
|
63
|
+
...reqOptions
|
|
64
|
+
})
|
|
65
|
+
};
|
|
66
|
+
return client;
|
|
67
|
+
}
|
|
File without changes
|