nuxt-gin-tools 0.2.9 → 0.2.11

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-gin-tools",
3
- "version": "0.2.9",
3
+ "version": "0.2.11",
4
4
  "description": "This project is used as a dependency for [nuxt-gin-starter](https://github.com/RapboyGao/nuxt-gin-starter.git)",
5
5
  "bin": {
6
6
  "nuxt-gin": "index.js"
@@ -19,12 +19,6 @@ export interface ServerConfigJson {
19
19
  ginPort: number;
20
20
  }
21
21
  export interface MyNuxtConfig {
22
- /**
23
- * 服务器基础路径
24
- * 用于指定服务器端路由的基础路径
25
- * 例如,如果服务器端路由为 /api,则 serverBasePath 为 /api
26
- */
27
- apiBasePath: string;
28
22
  /**
29
23
  * WebSocket 代理基础路径列表(可选)
30
24
  * 例如:["/ws-go", "/socket"]
@@ -24,46 +24,18 @@ exports.createDefaultConfig = createDefaultConfig;
24
24
  * });
25
25
  * ```
26
26
  */
27
- function createDefaultConfig({ serverConfig, apiBasePath, wsProxyBasePaths = [], }) {
28
- /**
29
- * 处理基础路径,去除协议和域名部分,只保留路径部分
30
- * 例如,将 "https://example.com/api-go" 转换为 "/api-go"
31
- */
32
- const thisBasePath = apiBasePath.replace(/^https?:[/]{2}[^/]+/, "");
33
- const normalizedProxyBasePath = thisBasePath.endsWith("/")
34
- ? thisBasePath.slice(0, -1)
35
- : thisBasePath;
36
- const normalizeBasePath = (rawPath) => {
37
- const trimmed = rawPath.replace(/^https?:[/]{2}[^/]+/, "");
38
- if (!trimmed)
39
- return "/";
40
- if (trimmed === "/")
41
- return "/";
42
- return trimmed.endsWith("/") ? trimmed.slice(0, -1) : trimmed;
43
- };
44
- const buildTarget = (basePath) => `http://localhost:${serverConfig.ginPort}${basePath}`;
45
- /**
46
- * 目标服务器的 URL
47
- * 格式为:http://localhost:ginPort/serverBasePath
48
- * 其中,ginPort 是从 serverConfig 中获取的 Gin 服务器的端口号
49
- * serverBasePath 是从 MyNuxtConfig 中获取的服务器基础路径
50
- */
51
- const target = buildTarget(normalizedProxyBasePath);
52
- const proxyEntries = {
53
- [normalizedProxyBasePath]: {
54
- target,
55
- changeOrigin: true,
56
- ws: true,
57
- },
58
- };
59
- for (const rawPath of wsProxyBasePaths) {
60
- const normalizedPath = normalizeBasePath(rawPath);
61
- proxyEntries[normalizedPath] = {
62
- target: buildTarget(normalizedPath),
63
- changeOrigin: true,
64
- ws: true,
65
- };
66
- }
27
+ function createDefaultConfig({ serverConfig }) {
28
+ // Proxy target should point to backend origin only.
29
+ // The matched route prefix (e.g. /api-go/v1, /ws-go) is preserved by proxy itself.
30
+ // If target also appends basePath, final upstream path becomes duplicated and can
31
+ // break websocket upgrades with ECONNRESET.
32
+ const buildTarget = () => `http://127.0.0.1:${serverConfig.ginPort}`;
33
+ const normalizedBaseUrl = serverConfig.baseUrl.endsWith("/")
34
+ ? serverConfig.baseUrl.slice(0, -1)
35
+ : serverConfig.baseUrl;
36
+ const escapedBaseUrl = normalizedBaseUrl.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
37
+ // 匹配所有“不以 baseUrl 开头”的路径,例如 baseUrl=/vue 时会匹配 /api、/ws、/health 等。
38
+ const nonBaseUrlProxyPattern = `^/(?!${escapedBaseUrl.slice(1)}(?:/|$)).*`;
67
39
  return {
68
40
  buildDir: "vue/.nuxt", // 设置构建目录为 "vue/.nuxt",表示 Nuxt 项目的构建输出将存放在该目录下
69
41
  srcDir: "vue", // 设置源代码目录为 "vue",表示 Nuxt 项目的源代码将存放在该目录下
@@ -105,17 +77,15 @@ function createDefaultConfig({ serverConfig, apiBasePath, wsProxyBasePaths = [],
105
77
  maxAge: "1728000",
106
78
  },
107
79
  },
108
- nitro: {
109
- output: {
110
- // 设置输出目录为 "vue/.nuxt", 表示 Nitro 的构建输出
111
- dir: "vue/.output",
112
- },
113
- // 定义代理规则,将匹配基础路径的请求代理到目标服务器
114
- devProxy: proxyEntries,
115
- },
116
80
  vite: {
117
81
  server: {
118
- proxy: proxyEntries,
82
+ proxy: {
83
+ [nonBaseUrlProxyPattern]: {
84
+ target: buildTarget(),
85
+ changeOrigin: true,
86
+ ws: true,
87
+ },
88
+ },
119
89
  },
120
90
  // 配置 Vite 插件
121
91
  plugins: [],