- 新增模板管理模块,包含模板列表和详情页面 - 重构用户认证逻辑,支持用户ID存储和API调用 - 新增排行榜API接口和类型定义 - 更新环境配置,添加开发环境配置 - 优化表格操作钩子,增强类型安全性 - 新增文件上传工具类和验证工具函数 - 更新路由配置,支持模板详情页面导航 - 修复登录表单默认值问题
65 lines
1.9 KiB
TypeScript
65 lines
1.9 KiB
TypeScript
import type { ProxyOptions } from 'vite'
|
|
import { consola } from 'consola'
|
|
import { bgRed, bgYellow, green, lightBlue } from 'kolorist'
|
|
import { createServiceConfig } from '../../src/utils/service'
|
|
|
|
/**
|
|
* 设置 HTTP 代理
|
|
*
|
|
* @param env - 当前环境变量
|
|
* @param enable - 是否启用 HTTP 代理
|
|
*/
|
|
export function createViteProxy(env: Env.ImportMeta, enable: boolean) {
|
|
const isEnableHttpProxy = enable && env.VITE_HTTP_PROXY === 'Y'
|
|
|
|
if (!isEnableHttpProxy)
|
|
return undefined
|
|
|
|
const isEnableProxyLog = env.VITE_PROXY_LOG === 'Y'
|
|
|
|
const { baseURL, proxyPattern, other } = createServiceConfig(env)
|
|
|
|
const proxy: Record<string, ProxyOptions> = createProxyItem({ baseURL, proxyPattern }, isEnableProxyLog)
|
|
|
|
other.forEach((item) => {
|
|
Object.assign(proxy, createProxyItem(item, isEnableProxyLog))
|
|
})
|
|
|
|
return proxy
|
|
}
|
|
|
|
/**
|
|
* 创建 HTTP 代理项
|
|
*
|
|
* @param item - 服务配置项
|
|
* @param enableLog - 是否启用日志记录
|
|
*/
|
|
function createProxyItem(item: App.Service.ServiceConfigItem, enableLog: boolean) {
|
|
const proxy: Record<string, ProxyOptions> = {}
|
|
|
|
proxy[item.proxyPattern] = {
|
|
target: item.baseURL,
|
|
changeOrigin: true,
|
|
configure: (_proxy, options) => {
|
|
_proxy.on('proxyReq', (_proxyReq, req, _res) => {
|
|
if (!enableLog)
|
|
return
|
|
|
|
const requestUrl = `${lightBlue('[代理地址]')}: ${bgYellow(` ${req.method} `)} ${green(`${item.proxyPattern}${req.url}`)}`
|
|
|
|
const proxyUrl = `${lightBlue('[真实请求地址]')}: ${green(`${options.target}${req.url}`)}`
|
|
|
|
consola.log(`${requestUrl}\n${proxyUrl}`)
|
|
})
|
|
_proxy.on('error', (_err, req, _res) => {
|
|
if (!enableLog)
|
|
return
|
|
consola.log(bgRed(`错误: ${req.method} `), green(`${options.target}${req.url}`))
|
|
})
|
|
},
|
|
rewrite: path => path.replace(new RegExp(`^${item.proxyPattern}`), ''),
|
|
}
|
|
|
|
return proxy
|
|
}
|