@gylautorun/dev-proxy-cookie 1.0.2 → 1.0.4

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.
@@ -10,6 +10,7 @@ import type { IncomingMessage } from 'http';
10
10
  import * as path from 'path';
11
11
  import { CookieReader, watchCookieFile, shouldEnableWatch } from '../utils';
12
12
  import { applyDevCookieHeader } from './apply-dev-cookie-header';
13
+ import { applyDevAuthentications, type AuthenticationItem } from './apply-dev-authentications';
13
14
 
14
15
  /** Options for {@link createFileCookieGetter}. */
15
16
  export interface CreateFileCookieGetterOptions {
@@ -50,6 +51,19 @@ export interface VueProxyConfigOptions {
50
51
  getCookie?: () => string;
51
52
  /** 是否输出调试日志 */
52
53
  debug?: boolean;
54
+ /**
55
+ * 是否使用 Cookie 文件中的 Cookie 注入到请求中
56
+ * - true: 使用文件中的 Cookie(默认)
57
+ * - false: 不注入 Cookie,使用浏览器发送的 Cookie
58
+ *
59
+ * 当使用账号密码登录时,设置为 false,避免覆盖浏览器的登录 Cookie
60
+ */
61
+ useCookie?: boolean;
62
+ /**
63
+ * 自定义鉴权信息数组,每个元素是一个键值对对象,会被注入到请求头中
64
+ * 例如: [{ 'ticket': 'xxxx' }, { 'X-Custom-Token': 'yyyy' }]
65
+ */
66
+ authentications?: AuthenticationItem[];
53
67
  /** 自定义请求头 */
54
68
  headers?: Record<string, string>;
55
69
  /** 是否启用 WebSocket 代理 */
@@ -100,6 +114,8 @@ export function createVueProxyConfig(
100
114
  const {
101
115
  getCookie,
102
116
  debug = false,
117
+ useCookie = true,
118
+ authentications = [],
103
119
  headers = {},
104
120
  ws = false,
105
121
  changeOrigin = true,
@@ -114,13 +130,25 @@ export function createVueProxyConfig(
114
130
  secure,
115
131
  headers,
116
132
  onProxyReq: (proxyReq: any, req: IncomingMessage) => {
117
- const cookie = getCookie ? getCookie() : '';
118
- if (cookie) {
119
- applyDevCookieHeader(proxyReq, cookie);
133
+ const reqPath = req.url || '/';
134
+
135
+ if (useCookie) {
136
+ const cookie = getCookie ? getCookie() : '';
137
+ if (cookie) {
138
+ applyDevCookieHeader(proxyReq, cookie);
139
+ }
140
+ if (debug) {
141
+ console.log('[Proxy Request]', reqPath, req.method, cookie ? '(with cookie)' : '(no cookie)');
142
+ }
143
+ } else if (debug) {
144
+ console.log('[Proxy Request]', reqPath, req.method, '(useCookie is false, skipping cookie injection)');
120
145
  }
121
- if (debug) {
122
- const reqPath = req.url || '/';
123
- console.log('[Proxy Request]', reqPath, req.method, cookie ? '(with cookie)' : '(no cookie)');
146
+
147
+ if (authentications && authentications.length > 0) {
148
+ applyDevAuthentications(proxyReq, authentications);
149
+ if (debug) {
150
+ console.log('[Proxy Request]', reqPath, req.method, '(with authentications)');
151
+ }
124
152
  }
125
153
  },
126
154
  onError: customOnError || ((err: Error) => {
@@ -209,13 +237,13 @@ export interface AutoProxyConfigOptions extends VueProxyConfigOptions {
209
237
  * @returns Vue CLI 代理配置对象映射
210
238
  */
211
239
  export function createAutoProxyConfig(options: AutoProxyConfigOptions): Record<string, ProxyConfig> {
212
- const { target, ignorePaths = [], includePaths = [], additionalProxies = {}, getCookie, debug, headers } = options;
240
+ const { target, ignorePaths = [], includePaths = [], additionalProxies = {}, getCookie, debug, headers, useCookie = true, authentications = [] } = options;
213
241
 
214
242
  const result: Record<string, ProxyConfig> = {};
215
243
 
216
244
  if (includePaths.length > 0) {
217
245
  for (const proxyPath of includePaths) {
218
- result[proxyPath] = createVueProxyConfig(target, { getCookie, debug, headers });
246
+ result[proxyPath] = createVueProxyConfig(target, { getCookie, debug, headers, useCookie, authentications });
219
247
  }
220
248
  } else {
221
249
  const defaultProxy: ProxyConfig = {
@@ -231,12 +259,23 @@ export function createAutoProxyConfig(options: AutoProxyConfigOptions): Record<s
231
259
  return;
232
260
  }
233
261
 
234
- const cookie = getCookie ? getCookie() : '';
235
- if (cookie) {
236
- applyDevCookieHeader(proxyReq, cookie);
262
+ if (useCookie) {
263
+ const cookie = getCookie ? getCookie() : '';
264
+ if (cookie) {
265
+ applyDevCookieHeader(proxyReq, cookie);
266
+ }
267
+ if (debug) {
268
+ console.log('[Proxy Request]', reqPath, req.method, cookie ? '(with cookie)' : '(no cookie)');
269
+ }
270
+ } else if (debug) {
271
+ console.log('[Proxy Request]', reqPath, req.method, '(useCookie is false, skipping cookie injection)');
237
272
  }
238
- if (debug) {
239
- console.log('[Proxy Request]', reqPath, req.method, cookie ? '(with cookie)' : '(no cookie)');
273
+
274
+ if (authentications && authentications.length > 0) {
275
+ applyDevAuthentications(proxyReq, authentications);
276
+ if (debug) {
277
+ console.log('[Proxy Request]', reqPath, req.method, '(with authentications)');
278
+ }
240
279
  }
241
280
  },
242
281
  onError: (err: Error) => {
@@ -247,7 +286,7 @@ export function createAutoProxyConfig(options: AutoProxyConfigOptions): Record<s
247
286
  }
248
287
 
249
288
  for (const [proxyPath, proxyTarget] of Object.entries(additionalProxies)) {
250
- result[proxyPath] = createVueProxyConfig(proxyTarget, { getCookie, debug, headers });
289
+ result[proxyPath] = createVueProxyConfig(proxyTarget, { getCookie, debug, headers, useCookie, authentications });
251
290
  }
252
291
 
253
292
  return result;