Skip to content

Commit fa0f540

Browse files
committed
refactor: verbose 配置项重写为 logType
1 parent f1bfcc7 commit fa0f540

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ import { proxyLogger } from 'vite-plugin-proxy-logger'
4040
export default defineConfig({
4141
plugins: [
4242
proxyLogger()
43+
// proxyLogger({
44+
// showHeaders: true, // 显示请求头信息
45+
// logType: 'all', // 输出请求前和请求后的日志
46+
// })
4347
],
4448
server: {
4549
proxy: {
@@ -57,7 +61,7 @@ export default defineConfig({
5761

5862
| 选项 | 类型 | 默认值 | 描述 |
5963
|------|------|--------|------|
60-
| verbose | boolean | false | 是否显示完整日志 |
64+
| logType |'req' \| 'res' \| 'all'| 'res' | 'req':仅记录请求前的日志;'res':仅记录请求后的日志;'all':记录请求前和请求后的日志|
6165
| showHeaders | boolean | false | 是否显示请求头信息 |
6266
| showTiming | boolean | true | 是否显示响应时间 |
6367
| showProxyPath | boolean | true | 是否显示代理路径前缀 |

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vite-plugin-proxy-logger",
3-
"version": "1.0.3",
4-
"description": "A Vite plugin for logging proxy requests with enhanced features",
3+
"version": "1.0.4",
4+
"description": "一个用于在开发环境中记录查看代理请求的 Vite 插件。",
55
"main": "dist/index.cjs",
66
"module": "dist/index.mjs",
77
"types": "dist/index.d.ts",

src/index.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import type { IncomingMessage } from 'http';
33
import chalk from 'chalk';
44

55
export interface ProxyLoggerOptions {
6-
/**
7-
* 是否启用详细日志
8-
* @default false
6+
/**
7+
* 日志类型:'req'(仅请求前)、'res'(仅请求后)、'all'(全部)
8+
* @default 'res'
99
*/
10-
verbose?: boolean;
10+
logType?: 'req' | 'res' | 'all';
1111
/**
1212
* 是否显示请求头信息
1313
* @default false
@@ -46,7 +46,7 @@ interface ProxyLogInfo {
4646
}
4747

4848
const defaultOptions: ProxyLoggerOptions = {
49-
verbose: false,
49+
logType: 'res', // 默认显示请求后的日志
5050
showHeaders: false,
5151
showTiming: true,
5252
showProxyPath: true,
@@ -104,7 +104,7 @@ export function proxyLogger(options: ProxyLoggerOptions = {}): Plugin {
104104
const reqId = `${req.method}-${req.url}}`;
105105
requestTimes.set(reqId, Date.now());
106106

107-
if (opts.verbose) {
107+
if ((opts.logType === 'req' || opts.logType === 'all')) {
108108
const logInfo: ProxyLogInfo = {
109109
method: req.method || 'UNKNOWN',
110110
url: req.url || '',
@@ -126,18 +126,19 @@ export function proxyLogger(options: ProxyLoggerOptions = {}): Plugin {
126126
const duration = startTime ? Date.now() - startTime : undefined;
127127
requestTimes.delete(reqId);
128128

129-
const logInfo: ProxyLogInfo = {
130-
method: req.method || 'UNKNOWN',
131-
url: req.url || '',
132-
target: String(options.target || ''),
133-
proxyPath: opts.showProxyPath ? key : undefined,
134-
statusCode: proxyRes.statusCode,
135-
duration: opts.showTiming ? duration : undefined,
136-
headers: opts.showHeaders ? proxyRes.headers : undefined,
137-
timestamp: new Date(),
138-
};
139-
140-
console.log(formatLog(logInfo));
129+
if ((opts.logType === 'res' || opts.logType === 'all')) {
130+
const logInfo: ProxyLogInfo = {
131+
method: req.method || 'UNKNOWN',
132+
url: req.url || '',
133+
target: String(options.target || ''),
134+
proxyPath: opts.showProxyPath ? key : undefined,
135+
statusCode: proxyRes.statusCode,
136+
duration: opts.showTiming ? duration : undefined,
137+
headers: opts.showHeaders ? proxyRes.headers : undefined,
138+
timestamp: new Date(),
139+
};
140+
console.log(formatLog(logInfo));
141+
}
141142
});
142143

143144
// 错误处理

0 commit comments

Comments
 (0)