@frontic/nuxt 1.3.2 → 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
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "frontic",
3
3
  "configKey": "frontic",
4
- "version": "1.3.2",
4
+ "version": "1.3.3",
5
5
  "compatibility": {
6
6
  "nuxt": ">=4.0.0"
7
7
  },
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
- return /const servers = \[\s*\{[^}]*url:\s*'([^']+)'/.exec(source)?.[1];
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
- const proxyPath = typeof proxyConfig === "string" ? proxyConfig : proxyConfig !== false ? "/api/frontic" : void 0;
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
- addServerHandler({
100
- route: `${proxyPath}/**`,
101
- handler: resolveModule("./runtime/server/api/frontic/[...path]")
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 = readFetchApiBaseUrl(fronticClientPath);
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 {
@@ -358,6 +365,7 @@ export {}
358
365
  };
359
366
  nuxt.options.runtimeConfig.frontic = {
360
367
  fetchApiSecret: fetchApiSecret ?? "",
368
+ fetchApiBaseUrl: fetchApiBaseUrl ?? "",
361
369
  sitemapBaseUrl: sitemapBaseUrl ?? "",
362
370
  sitemapDomains,
363
371
  proxyTimeout
@@ -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 targetUrl = getHeader(event, "fs-target-url");
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: "Missing fs-target-url header"
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, signal ? { fetchOptions: { signal } } : void 0);
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);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@frontic/nuxt",
3
- "version": "1.3.2",
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",