gd-web-core 0.0.16 → 0.0.18

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.
@@ -54,6 +54,8 @@ interface RequestOptions {
54
54
  transformResponse?: (res: ResponseData, config: RequestConfig) => any // 自定义响应处理
55
55
  useAuthToken?: boolean // 是否启用 Authorization token
56
56
  getToken?: () => string | null // 获取 token 的函数
57
+ authTokenHeader?: string // 自定义 Authorization header 名称,默认 `Authorization`
58
+ authTokenFormat?: (token: string) => string // 自定义 Authorization 格式
57
59
  }
58
60
  ```
59
61
 
@@ -86,6 +88,36 @@ configureRequest({
86
88
  useAuthToken: true,
87
89
  getToken: () => localStorage.getItem('token')
88
90
  })
91
+
92
+ // 自定义 Authorization header 名称
93
+ configureRequest({
94
+ useAuthToken: true,
95
+ authTokenHeader: 'QSS-Authorization' // 自定义 header 名称
96
+ })
97
+
98
+ // 自定义 Authorization 格式
99
+ configureRequest({
100
+ useAuthToken: true,
101
+ authTokenFormat: (token) => `Bearer ${token}` // 默认格式
102
+ })
103
+
104
+ // 其他格式示例
105
+ configureRequest({
106
+ useAuthToken: true,
107
+ authTokenFormat: (token) => `QSS ${token}` // 自定义前缀
108
+ })
109
+
110
+ configureRequest({
111
+ useAuthToken: true,
112
+ authTokenFormat: (token) => token // 无前缀
113
+ })
114
+
115
+ // 同时自定义 header 名称和格式
116
+ configureRequest({
117
+ useAuthToken: true,
118
+ authTokenHeader: 'X-Auth-Token',
119
+ authTokenFormat: (token) => `${token}`
120
+ })
89
121
  ```
90
122
 
91
123
  ---
@@ -152,7 +184,7 @@ const data = await request.get('/api/users', {
152
184
  1. **请求拦截器**
153
185
  - 自动显示 loading(可通过 `hideLoading: true` 禁用)
154
186
  - 自动添加时间戳参数 `_t` 防止缓存
155
- - 自动添加 Authorization token(当配置 `useAuthToken: true` 时)
187
+ - 自动添加 Authorization token(当配置 `useAuthToken: true` 时,可通过 `authTokenFormat` 自定义格式)
156
188
 
157
189
  2. **响应拦截器**
158
190
  - 自动移除 pending
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gd-web-core",
3
- "version": "0.0.16",
3
+ "version": "0.0.18",
4
4
  "description": "Vue 3 基座能力封装库 - 组件与工具函数",
5
5
  "type": "module",
6
6
  "files": [
@@ -12,7 +12,7 @@ export interface RequestConfig extends AxiosRequestConfig {
12
12
  hideErrMsg?: boolean;
13
13
  /** 是否隐藏 loading */
14
14
  hideLoading?: boolean;
15
- useToken?: boolean;
15
+ useToken?: string;
16
16
  }
17
17
 
18
18
  // 定义通用的响应结构
@@ -62,6 +62,10 @@ export interface RequestOptions {
62
62
  useAuthToken?: boolean;
63
63
  /** 获取 token 的函数,默认从 localStorage 获取 */
64
64
  getToken?: () => string | null;
65
+ /** 自定义 Authorization header 名称,默认 `Authorization` */
66
+ authTokenHeader?: string;
67
+ /** 自定义 Authorization 格式,默认 `Bearer ${token}` */
68
+ authTokenFormat?: (token: string) => string;
65
69
  }
66
70
 
67
71
  interface CustomAxiosInstance extends AxiosInstance {
@@ -199,7 +203,9 @@ request.interceptors.request.use(
199
203
  if (!config.headers) {
200
204
  config.headers = {} as any;
201
205
  }
202
- (config.headers as any).Authorization = `Bearer ${token}`;
206
+ const headerName = globalOptions.authTokenHeader || 'Authorization';
207
+ const format = globalOptions.authTokenFormat || ((t: string) => `Bearer ${t}`);
208
+ (config.headers as any)[headerName] = format(token);
203
209
  }
204
210
  }
205
211