nuxt-gin-tools 0.2.7 → 0.2.8

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.7",
3
+ "version": "0.2.8",
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"
@@ -25,6 +25,12 @@ export interface MyNuxtConfig {
25
25
  * 例如,如果服务器端路由为 /api,则 serverBasePath 为 /api
26
26
  */
27
27
  apiBasePath: string;
28
+ /**
29
+ * WebSocket 代理基础路径列表(可选)
30
+ * 例如:["/ws-go", "/socket"]
31
+ * 会同时注入 Nitro devProxy 与 Vite proxy,默认启用 ws。
32
+ */
33
+ wsProxyBasePaths?: string[];
28
34
  /**
29
35
  * 服务器配置
30
36
  * 包含服务器端的相关配置,如端口号、基础路径等
@@ -54,4 +60,67 @@ export interface MyNuxtConfig {
54
60
  * });
55
61
  * ```
56
62
  */
57
- export function createDefaultConfig({ serverConfig, apiBasePath }: MyNuxtConfig): NuxtConfig;
63
+ export function createDefaultConfig(config: MyNuxtConfig): NuxtConfig;
64
+ srcDir: string;
65
+ ssr: boolean;
66
+ /**
67
+ * 配置实验性功能
68
+ * 禁用 payloadExtraction 功能,该功能可能用于提取页面的有效负载数据
69
+ * 这里禁用它可能是为了避免某些兼容性问题或特定的项目需求
70
+ */
71
+ experimental: {
72
+ payloadExtraction: boolean;
73
+ };
74
+ devtools: {
75
+ timeline: {
76
+ enabled: boolean;
77
+ };
78
+ };
79
+ /**
80
+ * 配置应用的基础 URL
81
+ * 从 server.config.json 文件中获取 baseUrl 作为应用的基础 URL
82
+ * 这个基础 URL 将用于构建应用的路由和请求地址
83
+ */
84
+ app: {
85
+ baseURL: string;
86
+ };
87
+ /**
88
+ * 配置开发服务器的端口号
89
+ * 从 server.config.json 文件中获取 nuxtPort 作为开发服务器的端口号
90
+ * 启动开发服务器时,将使用该端口号进行监听
91
+ */
92
+ devServer: {
93
+ port: number;
94
+ cors: {
95
+ origin: string;
96
+ methods: string[];
97
+ allowHeaders: string[];
98
+ credentials: boolean;
99
+ maxAge: string;
100
+ };
101
+ };
102
+ nitro: {
103
+ output: {
104
+ dir: string;
105
+ };
106
+ devProxy: Record<string, {
107
+ target: string;
108
+ changeOrigin: boolean;
109
+ ws: boolean;
110
+ }>;
111
+ };
112
+ vite: {
113
+ server: {
114
+ proxy: Record<string, {
115
+ target: string;
116
+ changeOrigin: boolean;
117
+ ws: boolean;
118
+ }>;
119
+ };
120
+ plugins: never[];
121
+ esbuild: {
122
+ jsxFactory: string;
123
+ jsxFragment: string;
124
+ };
125
+ };
126
+ };
@@ -24,7 +24,7 @@ exports.createDefaultConfig = createDefaultConfig;
24
24
  * });
25
25
  * ```
26
26
  */
27
- function createDefaultConfig({ serverConfig, apiBasePath }) {
27
+ function createDefaultConfig({ serverConfig, apiBasePath, wsProxyBasePaths = [], }) {
28
28
  /**
29
29
  * 处理基础路径,去除协议和域名部分,只保留路径部分
30
30
  * 例如,将 "https://example.com/api-go" 转换为 "/api-go"
@@ -33,13 +33,37 @@ function createDefaultConfig({ serverConfig, apiBasePath }) {
33
33
  const normalizedProxyBasePath = thisBasePath.endsWith("/")
34
34
  ? thisBasePath.slice(0, -1)
35
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}`;
36
45
  /**
37
46
  * 目标服务器的 URL
38
47
  * 格式为:http://localhost:ginPort/serverBasePath
39
48
  * 其中,ginPort 是从 serverConfig 中获取的 Gin 服务器的端口号
40
49
  * serverBasePath 是从 MyNuxtConfig 中获取的服务器基础路径
41
50
  */
42
- const target = `http://localhost:${serverConfig.ginPort}${normalizedProxyBasePath}`;
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
+ }
43
67
  return {
44
68
  buildDir: "vue/.nuxt", // 设置构建目录为 "vue/.nuxt",表示 Nuxt 项目的构建输出将存放在该目录下
45
69
  srcDir: "vue", // 设置源代码目录为 "vue",表示 Nuxt 项目的源代码将存放在该目录下
@@ -86,27 +110,12 @@ function createDefaultConfig({ serverConfig, apiBasePath }) {
86
110
  // 设置输出目录为 "vue/.nuxt", 表示 Nitro 的构建输出
87
111
  dir: "vue/.output",
88
112
  },
89
- devProxy: {
90
- // 定义代理规则,将匹配 thisBasePath 的请求代理到目标服务器
91
- [normalizedProxyBasePath]: {
92
- // 目标服务器的 URL
93
- target: target,
94
- // 是否改变请求的源,设置为 true 可以避免跨域问题
95
- changeOrigin: true,
96
- // 启用 WebSocket 代理
97
- ws: true,
98
- },
99
- },
113
+ // 定义代理规则,将匹配基础路径的请求代理到目标服务器
114
+ devProxy: proxyEntries,
100
115
  },
101
116
  vite: {
102
117
  server: {
103
- proxy: {
104
- [normalizedProxyBasePath]: {
105
- target,
106
- changeOrigin: true,
107
- ws: true,
108
- },
109
- },
118
+ proxy: proxyEntries,
110
119
  },
111
120
  // 配置 Vite 插件
112
121
  plugins: [],