@frontic/nuxt 1.3.1 → 1.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -25,7 +25,8 @@ function readGeneratorSchemaVersion(clientDir) {
|
|
|
25
25
|
function readFetchApiBaseUrl(clientPath) {
|
|
26
26
|
try {
|
|
27
27
|
const source = readFileSync(clientPath, "utf-8");
|
|
28
|
-
|
|
28
|
+
const url = /const servers = \[\s*\{[^}]*url:\s*'([^']+)'/.exec(source)?.[1];
|
|
29
|
+
return url && /^https?:$/.test(new URL(url).protocol) ? url : void 0;
|
|
29
30
|
} catch {
|
|
30
31
|
return void 0;
|
|
31
32
|
}
|
|
@@ -93,13 +94,19 @@ const module$1 = defineNuxtModule({
|
|
|
93
94
|
"\n[@frontic/nuxt] Your generated Frontic client is outdated for this module version.\nComposable name and response types will not resolve (parameters typed as `never`).\nRegenerate with the current CLI: npx @frontic/cli generate\n"
|
|
94
95
|
);
|
|
95
96
|
}
|
|
96
|
-
|
|
97
|
+
let proxyPath = typeof proxyConfig === "string" ? proxyConfig : proxyConfig !== false ? "/api/frontic" : void 0;
|
|
97
98
|
const proxyTimeout = options.proxyTimeout ?? 1e4;
|
|
99
|
+
const fetchApiBaseUrl = hasFronticClient ? readFetchApiBaseUrl(fronticClientPath) : void 0;
|
|
98
100
|
if (proxyPath && hasFronticClient) {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
101
|
+
if (fetchApiBaseUrl) {
|
|
102
|
+
addServerHandler({
|
|
103
|
+
route: `${proxyPath}/**`,
|
|
104
|
+
handler: resolveModule("./runtime/server/api/frontic/[...path]")
|
|
105
|
+
});
|
|
106
|
+
} else {
|
|
107
|
+
console.warn("\n[@frontic/nuxt] proxy is disabled: no Fetch API URL found in the generated client.\nRegenerate with the current CLI: npx @frontic/cli generate\n");
|
|
108
|
+
proxyPath = void 0;
|
|
109
|
+
}
|
|
103
110
|
}
|
|
104
111
|
if (fetchApiSecret && hasFronticClient) {
|
|
105
112
|
addServerPlugin(resolveModule("./runtime/server/plugins/fetch-secret"));
|
|
@@ -110,7 +117,7 @@ const module$1 = defineNuxtModule({
|
|
|
110
117
|
if (hasNuxtModule("@nuxtjs/sitemap", nuxt)) {
|
|
111
118
|
console.warn("\n[@frontic/nuxt] sitemap: true is ignored while @nuxtjs/sitemap is installed.\nBoth modules would claim /sitemap.xml. Disable one of them.\n");
|
|
112
119
|
} else {
|
|
113
|
-
sitemapBaseUrl =
|
|
120
|
+
sitemapBaseUrl = fetchApiBaseUrl;
|
|
114
121
|
if (!sitemapBaseUrl) {
|
|
115
122
|
console.warn("\n[@frontic/nuxt] sitemap: true but no Fetch API URL found in the generated client.\nRegenerate with the current CLI: npx @frontic/cli generate\n");
|
|
116
123
|
} else {
|
|
@@ -168,6 +175,13 @@ frontic: { composables: false }
|
|
|
168
175
|
nitroConfig.alias ||= {};
|
|
169
176
|
nitroConfig.alias["@frontic/stack"] = clientDirPath;
|
|
170
177
|
});
|
|
178
|
+
nuxt.hook("nitro:prepare:types", ({ references }) => {
|
|
179
|
+
if (!hasFronticClient) return;
|
|
180
|
+
for (const file of ["generated-types.d.ts", "fetch-api.d.ts"]) {
|
|
181
|
+
const path = join(clientDirPath, file);
|
|
182
|
+
if (existsSync(path)) references.push({ path });
|
|
183
|
+
}
|
|
184
|
+
});
|
|
171
185
|
nuxt.hook("prepare:types", ({ tsConfig }) => {
|
|
172
186
|
tsConfig.compilerOptions ||= {};
|
|
173
187
|
tsConfig.compilerOptions.paths ||= {};
|
|
@@ -313,6 +327,22 @@ declare module 'nitropack/types' {
|
|
|
313
327
|
export {}
|
|
314
328
|
`
|
|
315
329
|
});
|
|
330
|
+
addTypeTemplate(
|
|
331
|
+
{
|
|
332
|
+
filename: "types/frontic-fetch-error.nitro.d.ts",
|
|
333
|
+
getContents: () => `import type { FronticFetchErrorPayload } from '${errorsModule}'
|
|
334
|
+
|
|
335
|
+
declare module 'nitropack/types' {
|
|
336
|
+
interface NitroRuntimeHooks {
|
|
337
|
+
'frontic:fetch:error': (payload: FronticFetchErrorPayload) => void | Promise<void>
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
export {}
|
|
342
|
+
`
|
|
343
|
+
},
|
|
344
|
+
{ nitro: true }
|
|
345
|
+
);
|
|
316
346
|
}
|
|
317
347
|
nuxt.options.runtimeConfig.public.frontic = {
|
|
318
348
|
hasFronticClient,
|
|
@@ -335,6 +365,7 @@ export {}
|
|
|
335
365
|
};
|
|
336
366
|
nuxt.options.runtimeConfig.frontic = {
|
|
337
367
|
fetchApiSecret: fetchApiSecret ?? "",
|
|
368
|
+
fetchApiBaseUrl: fetchApiBaseUrl ?? "",
|
|
338
369
|
sitemapBaseUrl: sitemapBaseUrl ?? "",
|
|
339
370
|
sitemapDomains,
|
|
340
371
|
proxyTimeout
|
|
@@ -143,6 +143,7 @@ export function useFronticSearch(name, ...args) {
|
|
|
143
143
|
initialDataUpdatedAt: options?.initialDataUpdatedAt,
|
|
144
144
|
_client
|
|
145
145
|
};
|
|
146
|
+
const isEnabled = () => toValue(options?.enabled) !== false;
|
|
146
147
|
let data;
|
|
147
148
|
let status;
|
|
148
149
|
let asyncStatus;
|
|
@@ -225,18 +226,21 @@ export function useFronticSearch(name, ...args) {
|
|
|
225
226
|
if (loaded) emitListingResolved(loaded, "loadMore");
|
|
226
227
|
};
|
|
227
228
|
loadNextFn = async () => {
|
|
229
|
+
if (!isEnabled()) return flattenedData.value;
|
|
228
230
|
const pageParamBefore = edgePageParam("last");
|
|
229
231
|
await infiniteResult.loadNextPage();
|
|
230
232
|
emitLoadedPage("last", pageParamBefore);
|
|
231
233
|
return flattenedData.value;
|
|
232
234
|
};
|
|
233
235
|
loadPrevFn = async () => {
|
|
236
|
+
if (!isEnabled()) return flattenedData.value;
|
|
234
237
|
const pageParamBefore = edgePageParam("first");
|
|
235
238
|
await infiniteResult.loadPreviousPage();
|
|
236
239
|
emitLoadedPage("first", pageParamBefore);
|
|
237
240
|
return flattenedData.value;
|
|
238
241
|
};
|
|
239
242
|
loadPageFn = async (page) => {
|
|
243
|
+
if (!isEnabled()) return flattenedData.value;
|
|
240
244
|
const pagedQueryFactory = fetchListingQuery({
|
|
241
245
|
...queryConfig,
|
|
242
246
|
page: ref(page)
|
|
@@ -382,8 +386,9 @@ export function useFronticSearch(name, ...args) {
|
|
|
382
386
|
function resetPage() {
|
|
383
387
|
_page.value = void 0;
|
|
384
388
|
}
|
|
385
|
-
function withIntent(trigger, changedFilter) {
|
|
386
|
-
|
|
389
|
+
async function withIntent(trigger, changedFilter) {
|
|
390
|
+
if (!isEnabled()) return;
|
|
391
|
+
await runRefresh({ trigger, changedFilter });
|
|
387
392
|
}
|
|
388
393
|
const triggerSearchFromInput = useDebounceFn(() => {
|
|
389
394
|
const committed = searchTerm.value.trim().length > searchTermThreshold ? searchTerm.value : void 0;
|
|
@@ -3,8 +3,30 @@ import { useRuntimeConfig, useNitroApp } from "nitropack/runtime";
|
|
|
3
3
|
import { toFetchErrorDetail, isTimeoutError } from "../../../utils/describe-error.js";
|
|
4
4
|
import { FRONTIC_FETCH_ERROR_VERSION } from "../../../types/errors.js";
|
|
5
5
|
const DEFAULT_PROXY_TIMEOUT_MS = 1e4;
|
|
6
|
+
function originOf(url) {
|
|
7
|
+
try {
|
|
8
|
+
const { protocol, origin } = new URL(url);
|
|
9
|
+
return protocol === "http:" || protocol === "https:" ? origin : void 0;
|
|
10
|
+
} catch {
|
|
11
|
+
return void 0;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function resolveTarget(requestPath, proxyPath, origin) {
|
|
15
|
+
const prefix = proxyPath.replace(/\/+$/, "");
|
|
16
|
+
if (!prefix || !requestPath.startsWith(`${prefix}/`)) return void 0;
|
|
17
|
+
try {
|
|
18
|
+
const target = new URL(origin + requestPath.slice(prefix.length));
|
|
19
|
+
return target.origin === origin ? target.href : void 0;
|
|
20
|
+
} catch {
|
|
21
|
+
return void 0;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
6
24
|
export default defineEventHandler(async (event) => {
|
|
7
|
-
const
|
|
25
|
+
const config = useRuntimeConfig(event);
|
|
26
|
+
const baseUrl = config.frontic?.fetchApiBaseUrl;
|
|
27
|
+
const allowedOrigin = baseUrl ? originOf(baseUrl) : void 0;
|
|
28
|
+
const claimedTarget = getHeader(event, "fs-target-url");
|
|
29
|
+
let targetUrl = allowedOrigin ? resolveTarget(event.path ?? "", config.public?.frontic?.proxyPath ?? "", allowedOrigin) : void 0;
|
|
8
30
|
function report(error, status) {
|
|
9
31
|
const payload = {
|
|
10
32
|
operation: "proxy",
|
|
@@ -20,11 +42,30 @@ export default defineEventHandler(async (event) => {
|
|
|
20
42
|
void Promise.resolve(hooks.callHook("frontic:fetch:error", payload)).catch(() => {
|
|
21
43
|
});
|
|
22
44
|
}
|
|
45
|
+
if (!allowedOrigin) {
|
|
46
|
+
const misconfigured = createError({
|
|
47
|
+
statusCode: 500,
|
|
48
|
+
statusMessage: "Internal Server Error",
|
|
49
|
+
message: "Frontic proxy has no Fetch API URL configured"
|
|
50
|
+
});
|
|
51
|
+
report(misconfigured, 500);
|
|
52
|
+
throw misconfigured;
|
|
53
|
+
}
|
|
54
|
+
if (claimedTarget !== void 0 && originOf(claimedTarget) !== allowedOrigin) {
|
|
55
|
+
const forbidden = createError({
|
|
56
|
+
statusCode: 400,
|
|
57
|
+
statusMessage: "Bad Request",
|
|
58
|
+
message: "fs-target-url does not point at the Fetch API"
|
|
59
|
+
});
|
|
60
|
+
targetUrl = claimedTarget;
|
|
61
|
+
report(forbidden, 400);
|
|
62
|
+
throw forbidden;
|
|
63
|
+
}
|
|
23
64
|
if (!targetUrl) {
|
|
24
65
|
const badRequest = createError({
|
|
25
66
|
statusCode: 400,
|
|
26
67
|
statusMessage: "Bad Request",
|
|
27
|
-
message: "
|
|
68
|
+
message: "Request path is not under the Frontic proxy path"
|
|
28
69
|
});
|
|
29
70
|
report(badRequest, 400);
|
|
30
71
|
throw badRequest;
|
|
@@ -35,7 +76,6 @@ export default defineEventHandler(async (event) => {
|
|
|
35
76
|
delete event.node.req.headers["cookie"];
|
|
36
77
|
delete event.node.req.headers["authorization"];
|
|
37
78
|
}
|
|
38
|
-
const config = useRuntimeConfig(event);
|
|
39
79
|
const fetchApiSecret = config.frontic?.fetchApiSecret;
|
|
40
80
|
if (fetchApiSecret && !getHeader(event, "fs-secret") && event.node?.req?.headers) {
|
|
41
81
|
event.node.req.headers["fs-secret"] = fetchApiSecret;
|
|
@@ -43,7 +83,7 @@ export default defineEventHandler(async (event) => {
|
|
|
43
83
|
const configured = config.frontic?.proxyTimeout;
|
|
44
84
|
const timeout = typeof configured === "number" ? configured : DEFAULT_PROXY_TIMEOUT_MS;
|
|
45
85
|
const signal = timeout > 0 ? AbortSignal.timeout(timeout) : void 0;
|
|
46
|
-
return await proxyRequest(event, targetUrl,
|
|
86
|
+
return await proxyRequest(event, targetUrl, { fetchOptions: { redirect: "manual", ...signal && { signal } } });
|
|
47
87
|
} catch (error) {
|
|
48
88
|
const detail = toFetchErrorDetail(error);
|
|
49
89
|
const timedOut = isTimeoutError(error);
|
|
@@ -36,9 +36,9 @@ export interface FronticQueryOptions {
|
|
|
36
36
|
*/
|
|
37
37
|
gcTime?: number;
|
|
38
38
|
/**
|
|
39
|
-
* Whether the query fetches. While `false`, no request is made
|
|
40
|
-
*
|
|
41
|
-
* not known yet.
|
|
39
|
+
* Whether the query fetches. While `false`, no request is made except by an
|
|
40
|
+
* explicit `refresh()` or `refetch()` call; flipping it back to `true`
|
|
41
|
+
* refreshes. Useful when an input (e.g. a reactive key) is not known yet.
|
|
42
42
|
* @default true
|
|
43
43
|
*/
|
|
44
44
|
enabled?: MaybeRefOrGetter<boolean>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frontic/nuxt",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.3",
|
|
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",
|
|
@@ -56,7 +56,8 @@
|
|
|
56
56
|
"test": "vitest run",
|
|
57
57
|
"test:watch": "vitest watch",
|
|
58
58
|
"test:coverage": "vitest run --coverage",
|
|
59
|
-
"clean": "rm -rf dist .nuxt"
|
|
59
|
+
"clean": "rm -rf dist .nuxt",
|
|
60
|
+
"emit:docs-manifest": "node scripts/emit-docs-manifest.js"
|
|
60
61
|
},
|
|
61
62
|
"dependencies": {
|
|
62
63
|
"@nuxt/kit": "^4.5.2",
|