@yh-ui/request 0.1.21
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/LICENSE +21 -0
- package/README.md +274 -0
- package/dist/adapters/fetch.cjs +157 -0
- package/dist/adapters/fetch.d.ts +25 -0
- package/dist/adapters/fetch.mjs +148 -0
- package/dist/adapters/index.cjs +27 -0
- package/dist/adapters/index.d.ts +5 -0
- package/dist/adapters/index.mjs +2 -0
- package/dist/adapters/platform.cjs +394 -0
- package/dist/adapters/platform.d.ts +72 -0
- package/dist/adapters/platform.mjs +369 -0
- package/dist/cache/index.cjs +56 -0
- package/dist/cache/index.d.ts +21 -0
- package/dist/cache/index.mjs +14 -0
- package/dist/cache/indexedDB.cjs +188 -0
- package/dist/cache/indexedDB.d.ts +58 -0
- package/dist/cache/indexedDB.mjs +176 -0
- package/dist/cache/localStorage.cjs +158 -0
- package/dist/cache/localStorage.d.ts +58 -0
- package/dist/cache/localStorage.mjs +153 -0
- package/dist/cache/memory.cjs +112 -0
- package/dist/cache/memory.d.ts +71 -0
- package/dist/cache/memory.mjs +103 -0
- package/dist/graphql.cjs +255 -0
- package/dist/graphql.d.ts +192 -0
- package/dist/graphql.mjs +235 -0
- package/dist/http-cache.cjs +248 -0
- package/dist/http-cache.d.ts +156 -0
- package/dist/http-cache.mjs +233 -0
- package/dist/index.cjs +181 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.mjs +16 -0
- package/dist/interceptors/debug.cjs +139 -0
- package/dist/interceptors/debug.d.ts +92 -0
- package/dist/interceptors/debug.mjs +130 -0
- package/dist/interceptors/index.cjs +38 -0
- package/dist/interceptors/index.d.ts +6 -0
- package/dist/interceptors/index.mjs +3 -0
- package/dist/interceptors/progress.cjs +185 -0
- package/dist/interceptors/progress.d.ts +97 -0
- package/dist/interceptors/progress.mjs +177 -0
- package/dist/interceptors/security.cjs +154 -0
- package/dist/interceptors/security.d.ts +83 -0
- package/dist/interceptors/security.mjs +134 -0
- package/dist/plugin.cjs +166 -0
- package/dist/plugin.d.ts +106 -0
- package/dist/plugin.mjs +163 -0
- package/dist/request.cjs +396 -0
- package/dist/request.d.ts +111 -0
- package/dist/request.mjs +339 -0
- package/dist/types.cjs +13 -0
- package/dist/types.d.ts +157 -0
- package/dist/types.mjs +7 -0
- package/dist/useAIStream.cjs +125 -0
- package/dist/useAIStream.d.ts +89 -0
- package/dist/useAIStream.mjs +108 -0
- package/dist/useLoadMore.cjs +136 -0
- package/dist/useLoadMore.d.ts +84 -0
- package/dist/useLoadMore.mjs +134 -0
- package/dist/usePagination.cjs +141 -0
- package/dist/usePagination.d.ts +89 -0
- package/dist/usePagination.mjs +132 -0
- package/dist/useQueue.cjs +243 -0
- package/dist/useQueue.d.ts +118 -0
- package/dist/useQueue.mjs +239 -0
- package/dist/useRequest.cjs +325 -0
- package/dist/useRequest.d.ts +126 -0
- package/dist/useRequest.mjs +329 -0
- package/dist/useRequestQueue.cjs +36 -0
- package/dist/useRequestQueue.d.ts +52 -0
- package/dist/useRequestQueue.mjs +27 -0
- package/dist/useSSE.cjs +241 -0
- package/dist/useSSE.d.ts +74 -0
- package/dist/useSSE.mjs +226 -0
- package/dist/websocket.cjs +325 -0
- package/dist/websocket.d.ts +163 -0
- package/dist/websocket.mjs +316 -0
- package/package.json +61 -0
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
export class HttpCache {
|
|
2
|
+
cache = /* @__PURE__ */ new Map();
|
|
3
|
+
options;
|
|
4
|
+
constructor(options = {}) {
|
|
5
|
+
this.options = {
|
|
6
|
+
enabled: options.enabled ?? true,
|
|
7
|
+
etagHeader: options.etagHeader ?? "ETag",
|
|
8
|
+
lastModifiedHeader: options.lastModifiedHeader ?? "Last-Modified",
|
|
9
|
+
maxAge: options.maxAge ?? 5 * 60 * 1e3,
|
|
10
|
+
// 5 分钟
|
|
11
|
+
staleWhileRevalidate: options.staleWhileRevalidate ?? false,
|
|
12
|
+
staleTime: options.staleTime ?? 60 * 1e3
|
|
13
|
+
// 1 分钟
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* 从响应中提取缓存信息
|
|
18
|
+
*/
|
|
19
|
+
extractCacheInfo(response) {
|
|
20
|
+
const etag = response.headers.get(this.options.etagHeader) || void 0;
|
|
21
|
+
const lastModified = response.headers.get(this.options.lastModifiedHeader) || void 0;
|
|
22
|
+
return { etag, lastModified };
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* 生成缓存键
|
|
26
|
+
*/
|
|
27
|
+
getCacheKey(config) {
|
|
28
|
+
return `${config.method}:${config.fullPath || config.url}`;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* 获取缓存
|
|
32
|
+
*/
|
|
33
|
+
get(key) {
|
|
34
|
+
const entry = this.cache.get(key);
|
|
35
|
+
if (!entry) return void 0;
|
|
36
|
+
const now = Date.now();
|
|
37
|
+
if (entry.expireTime && now > entry.expireTime) {
|
|
38
|
+
this.cache.delete(key);
|
|
39
|
+
return void 0;
|
|
40
|
+
}
|
|
41
|
+
return entry;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* 设置缓存
|
|
45
|
+
*/
|
|
46
|
+
set(key, data, response) {
|
|
47
|
+
const { etag, lastModified } = this.extractCacheInfo(response);
|
|
48
|
+
const now = Date.now();
|
|
49
|
+
const cacheControl = response.headers.get("Cache-Control");
|
|
50
|
+
let maxAge = this.options.maxAge;
|
|
51
|
+
if (cacheControl) {
|
|
52
|
+
const maxAgeMatch = cacheControl.match(/max-age=(\d+)/);
|
|
53
|
+
if (maxAgeMatch) {
|
|
54
|
+
maxAge = parseInt(maxAgeMatch[1], 10) * 1e3;
|
|
55
|
+
}
|
|
56
|
+
if (cacheControl.includes("no-cache")) {
|
|
57
|
+
maxAge = 0;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const entry = {
|
|
61
|
+
etag,
|
|
62
|
+
lastModified,
|
|
63
|
+
data,
|
|
64
|
+
expireTime: maxAge > 0 ? now + maxAge : now,
|
|
65
|
+
createTime: now
|
|
66
|
+
};
|
|
67
|
+
this.cache.set(key, entry);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* 构建条件请求头
|
|
71
|
+
*/
|
|
72
|
+
buildConditionalHeaders(config) {
|
|
73
|
+
const key = this.getCacheKey(config);
|
|
74
|
+
const entry = this.get(key);
|
|
75
|
+
const headers = {};
|
|
76
|
+
if (entry) {
|
|
77
|
+
if (entry.etag) {
|
|
78
|
+
headers["If-None-Match"] = entry.etag;
|
|
79
|
+
}
|
|
80
|
+
if (entry.lastModified) {
|
|
81
|
+
headers["If-Modified-Since"] = entry.lastModified;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return headers;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* 处理条件响应
|
|
88
|
+
*/
|
|
89
|
+
handleConditionalResponse(config, response, data) {
|
|
90
|
+
const key = this.getCacheKey(config);
|
|
91
|
+
const entry = this.get(key);
|
|
92
|
+
if (response.status === 304) {
|
|
93
|
+
if (entry) {
|
|
94
|
+
return {
|
|
95
|
+
isModified: false,
|
|
96
|
+
data: entry.data,
|
|
97
|
+
entry
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
this.set(key, data, response);
|
|
102
|
+
return {
|
|
103
|
+
isModified: true,
|
|
104
|
+
data
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* 获取缓存数据(可能返回过期数据用于 stale-while-revalidate)
|
|
109
|
+
*/
|
|
110
|
+
getWithFallback(key) {
|
|
111
|
+
const entry = this.get(key);
|
|
112
|
+
if (!entry) return void 0;
|
|
113
|
+
const now = Date.now();
|
|
114
|
+
const isStale = !!(entry.expireTime && now > entry.expireTime);
|
|
115
|
+
return {
|
|
116
|
+
data: entry.data,
|
|
117
|
+
isStale
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* 删除缓存
|
|
122
|
+
*/
|
|
123
|
+
delete(key) {
|
|
124
|
+
return this.cache.delete(key);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* 清空缓存
|
|
128
|
+
*/
|
|
129
|
+
clear() {
|
|
130
|
+
this.cache.clear();
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* 获取缓存大小
|
|
134
|
+
*/
|
|
135
|
+
size() {
|
|
136
|
+
return this.cache.size;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* 清理过期缓存
|
|
140
|
+
*/
|
|
141
|
+
cleanup() {
|
|
142
|
+
const now = Date.now();
|
|
143
|
+
for (const [key, entry] of this.cache.entries()) {
|
|
144
|
+
if (entry.expireTime && now > entry.expireTime) {
|
|
145
|
+
this.cache.delete(key);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
export function createHttpCacheInterceptor(options = {}) {
|
|
151
|
+
const cache = new HttpCache(options);
|
|
152
|
+
return {
|
|
153
|
+
/**
|
|
154
|
+
* 请求拦截 - 添加条件请求头
|
|
155
|
+
*/
|
|
156
|
+
onRequest: (config) => {
|
|
157
|
+
if (!options.enabled) return config;
|
|
158
|
+
if (config.method !== "GET") return config;
|
|
159
|
+
const conditionalHeaders = cache.buildConditionalHeaders(config);
|
|
160
|
+
return {
|
|
161
|
+
...config,
|
|
162
|
+
headers: {
|
|
163
|
+
...config.headers,
|
|
164
|
+
...conditionalHeaders
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
},
|
|
168
|
+
/**
|
|
169
|
+
* 响应拦截 - 处理条件响应
|
|
170
|
+
*/
|
|
171
|
+
onResponse: (response) => {
|
|
172
|
+
if (!options.enabled) return response;
|
|
173
|
+
if (response.config.method !== "GET") return response;
|
|
174
|
+
if (response.response.status === 304) {
|
|
175
|
+
const key = `${response.config.method}:${response.config.fullPath || response.config.url}`;
|
|
176
|
+
const entry = cache.get(key);
|
|
177
|
+
if (entry) {
|
|
178
|
+
return {
|
|
179
|
+
...response,
|
|
180
|
+
data: entry.data
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
cache.set(
|
|
185
|
+
`${response.config.method}:${response.config.fullPath || response.config.url}`,
|
|
186
|
+
response.data,
|
|
187
|
+
response.response
|
|
188
|
+
);
|
|
189
|
+
return response;
|
|
190
|
+
},
|
|
191
|
+
/**
|
|
192
|
+
* 获取缓存实例
|
|
193
|
+
*/
|
|
194
|
+
getCache: () => cache
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
export const httpCache = new HttpCache();
|
|
198
|
+
export function createConditionalRequestInterceptor(options = {}) {
|
|
199
|
+
return createHttpCacheInterceptor({ ...options });
|
|
200
|
+
}
|
|
201
|
+
export function parseCacheControl(header) {
|
|
202
|
+
if (!header)
|
|
203
|
+
return {
|
|
204
|
+
noCache: false,
|
|
205
|
+
noStore: false,
|
|
206
|
+
mustRevalidate: false,
|
|
207
|
+
isPublic: false,
|
|
208
|
+
isPrivate: false
|
|
209
|
+
};
|
|
210
|
+
const result = {
|
|
211
|
+
noCache: false,
|
|
212
|
+
noStore: false,
|
|
213
|
+
mustRevalidate: false,
|
|
214
|
+
isPublic: false,
|
|
215
|
+
isPrivate: false
|
|
216
|
+
};
|
|
217
|
+
const maxAgeMatch = header.match(/max-age=(\d+)/);
|
|
218
|
+
if (maxAgeMatch) {
|
|
219
|
+
result.maxAge = parseInt(maxAgeMatch[1], 10);
|
|
220
|
+
}
|
|
221
|
+
if (header.includes("no-cache")) result.noCache = true;
|
|
222
|
+
if (header.includes("no-store")) result.noStore = true;
|
|
223
|
+
if (header.includes("must-revalidate")) result.mustRevalidate = true;
|
|
224
|
+
if (header.includes("public")) result.isPublic = true;
|
|
225
|
+
if (header.includes("private")) result.isPrivate = true;
|
|
226
|
+
return result;
|
|
227
|
+
}
|
|
228
|
+
export function isResponseCacheable(response) {
|
|
229
|
+
const cacheControl = parseCacheControl(response.headers.get("Cache-Control"));
|
|
230
|
+
if (cacheControl.noStore) return false;
|
|
231
|
+
if (cacheControl.isPublic || cacheControl.isPrivate) return true;
|
|
232
|
+
return response.ok;
|
|
233
|
+
}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
var _request = require("./request.cjs");
|
|
7
|
+
Object.keys(_request).forEach(function (key) {
|
|
8
|
+
if (key === "default" || key === "__esModule") return;
|
|
9
|
+
if (key in exports && exports[key] === _request[key]) return;
|
|
10
|
+
Object.defineProperty(exports, key, {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
get: function () {
|
|
13
|
+
return _request[key];
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
var _types = require("./types.cjs");
|
|
18
|
+
Object.keys(_types).forEach(function (key) {
|
|
19
|
+
if (key === "default" || key === "__esModule") return;
|
|
20
|
+
if (key in exports && exports[key] === _types[key]) return;
|
|
21
|
+
Object.defineProperty(exports, key, {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
get: function () {
|
|
24
|
+
return _types[key];
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
var _useRequest = require("./useRequest.cjs");
|
|
29
|
+
Object.keys(_useRequest).forEach(function (key) {
|
|
30
|
+
if (key === "default" || key === "__esModule") return;
|
|
31
|
+
if (key in exports && exports[key] === _useRequest[key]) return;
|
|
32
|
+
Object.defineProperty(exports, key, {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
get: function () {
|
|
35
|
+
return _useRequest[key];
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
var _useSSE = require("./useSSE.cjs");
|
|
40
|
+
Object.keys(_useSSE).forEach(function (key) {
|
|
41
|
+
if (key === "default" || key === "__esModule") return;
|
|
42
|
+
if (key in exports && exports[key] === _useSSE[key]) return;
|
|
43
|
+
Object.defineProperty(exports, key, {
|
|
44
|
+
enumerable: true,
|
|
45
|
+
get: function () {
|
|
46
|
+
return _useSSE[key];
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
var _useAIStream = require("./useAIStream.cjs");
|
|
51
|
+
Object.keys(_useAIStream).forEach(function (key) {
|
|
52
|
+
if (key === "default" || key === "__esModule") return;
|
|
53
|
+
if (key in exports && exports[key] === _useAIStream[key]) return;
|
|
54
|
+
Object.defineProperty(exports, key, {
|
|
55
|
+
enumerable: true,
|
|
56
|
+
get: function () {
|
|
57
|
+
return _useAIStream[key];
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
var _usePagination = require("./usePagination.cjs");
|
|
62
|
+
Object.keys(_usePagination).forEach(function (key) {
|
|
63
|
+
if (key === "default" || key === "__esModule") return;
|
|
64
|
+
if (key in exports && exports[key] === _usePagination[key]) return;
|
|
65
|
+
Object.defineProperty(exports, key, {
|
|
66
|
+
enumerable: true,
|
|
67
|
+
get: function () {
|
|
68
|
+
return _usePagination[key];
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
var _useLoadMore = require("./useLoadMore.cjs");
|
|
73
|
+
Object.keys(_useLoadMore).forEach(function (key) {
|
|
74
|
+
if (key === "default" || key === "__esModule") return;
|
|
75
|
+
if (key in exports && exports[key] === _useLoadMore[key]) return;
|
|
76
|
+
Object.defineProperty(exports, key, {
|
|
77
|
+
enumerable: true,
|
|
78
|
+
get: function () {
|
|
79
|
+
return _useLoadMore[key];
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
var _useQueue = require("./useQueue.cjs");
|
|
84
|
+
Object.keys(_useQueue).forEach(function (key) {
|
|
85
|
+
if (key === "default" || key === "__esModule") return;
|
|
86
|
+
if (key in exports && exports[key] === _useQueue[key]) return;
|
|
87
|
+
Object.defineProperty(exports, key, {
|
|
88
|
+
enumerable: true,
|
|
89
|
+
get: function () {
|
|
90
|
+
return _useQueue[key];
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
var _useRequestQueue = require("./useRequestQueue.cjs");
|
|
95
|
+
Object.keys(_useRequestQueue).forEach(function (key) {
|
|
96
|
+
if (key === "default" || key === "__esModule") return;
|
|
97
|
+
if (key in exports && exports[key] === _useRequestQueue[key]) return;
|
|
98
|
+
Object.defineProperty(exports, key, {
|
|
99
|
+
enumerable: true,
|
|
100
|
+
get: function () {
|
|
101
|
+
return _useRequestQueue[key];
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
var _plugin = require("./plugin.cjs");
|
|
106
|
+
Object.keys(_plugin).forEach(function (key) {
|
|
107
|
+
if (key === "default" || key === "__esModule") return;
|
|
108
|
+
if (key in exports && exports[key] === _plugin[key]) return;
|
|
109
|
+
Object.defineProperty(exports, key, {
|
|
110
|
+
enumerable: true,
|
|
111
|
+
get: function () {
|
|
112
|
+
return _plugin[key];
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
var _adapters = require("./adapters/index.cjs");
|
|
117
|
+
Object.keys(_adapters).forEach(function (key) {
|
|
118
|
+
if (key === "default" || key === "__esModule") return;
|
|
119
|
+
if (key in exports && exports[key] === _adapters[key]) return;
|
|
120
|
+
Object.defineProperty(exports, key, {
|
|
121
|
+
enumerable: true,
|
|
122
|
+
get: function () {
|
|
123
|
+
return _adapters[key];
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
var _cache = require("./cache/index.cjs");
|
|
128
|
+
Object.keys(_cache).forEach(function (key) {
|
|
129
|
+
if (key === "default" || key === "__esModule") return;
|
|
130
|
+
if (key in exports && exports[key] === _cache[key]) return;
|
|
131
|
+
Object.defineProperty(exports, key, {
|
|
132
|
+
enumerable: true,
|
|
133
|
+
get: function () {
|
|
134
|
+
return _cache[key];
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
var _interceptors = require("./interceptors/index.cjs");
|
|
139
|
+
Object.keys(_interceptors).forEach(function (key) {
|
|
140
|
+
if (key === "default" || key === "__esModule") return;
|
|
141
|
+
if (key in exports && exports[key] === _interceptors[key]) return;
|
|
142
|
+
Object.defineProperty(exports, key, {
|
|
143
|
+
enumerable: true,
|
|
144
|
+
get: function () {
|
|
145
|
+
return _interceptors[key];
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
var _graphql = require("./graphql.cjs");
|
|
150
|
+
Object.keys(_graphql).forEach(function (key) {
|
|
151
|
+
if (key === "default" || key === "__esModule") return;
|
|
152
|
+
if (key in exports && exports[key] === _graphql[key]) return;
|
|
153
|
+
Object.defineProperty(exports, key, {
|
|
154
|
+
enumerable: true,
|
|
155
|
+
get: function () {
|
|
156
|
+
return _graphql[key];
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
var _websocket = require("./websocket.cjs");
|
|
161
|
+
Object.keys(_websocket).forEach(function (key) {
|
|
162
|
+
if (key === "default" || key === "__esModule") return;
|
|
163
|
+
if (key in exports && exports[key] === _websocket[key]) return;
|
|
164
|
+
Object.defineProperty(exports, key, {
|
|
165
|
+
enumerable: true,
|
|
166
|
+
get: function () {
|
|
167
|
+
return _websocket[key];
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
var _httpCache = require("./http-cache.cjs");
|
|
172
|
+
Object.keys(_httpCache).forEach(function (key) {
|
|
173
|
+
if (key === "default" || key === "__esModule") return;
|
|
174
|
+
if (key in exports && exports[key] === _httpCache[key]) return;
|
|
175
|
+
Object.defineProperty(exports, key, {
|
|
176
|
+
enumerable: true,
|
|
177
|
+
get: function () {
|
|
178
|
+
return _httpCache[key];
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
});
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* YH-UI Request Hooks
|
|
3
|
+
*
|
|
4
|
+
* Enterprise-grade HTTP request hooks for Vue 3
|
|
5
|
+
*
|
|
6
|
+
* @package @yh-ui/request
|
|
7
|
+
*/
|
|
8
|
+
export * from './request';
|
|
9
|
+
export * from './types';
|
|
10
|
+
export * from './useRequest';
|
|
11
|
+
export * from './useSSE';
|
|
12
|
+
export * from './useAIStream';
|
|
13
|
+
export * from './usePagination';
|
|
14
|
+
export * from './useLoadMore';
|
|
15
|
+
export * from './useQueue';
|
|
16
|
+
export * from './useRequestQueue';
|
|
17
|
+
export * from './plugin';
|
|
18
|
+
export * from './adapters';
|
|
19
|
+
export * from './cache';
|
|
20
|
+
export * from './interceptors';
|
|
21
|
+
export * from './graphql';
|
|
22
|
+
export * from './websocket';
|
|
23
|
+
export * from './http-cache';
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export * from "./request.mjs";
|
|
2
|
+
export * from "./types.mjs";
|
|
3
|
+
export * from "./useRequest.mjs";
|
|
4
|
+
export * from "./useSSE.mjs";
|
|
5
|
+
export * from "./useAIStream.mjs";
|
|
6
|
+
export * from "./usePagination.mjs";
|
|
7
|
+
export * from "./useLoadMore.mjs";
|
|
8
|
+
export * from "./useQueue.mjs";
|
|
9
|
+
export * from "./useRequestQueue.mjs";
|
|
10
|
+
export * from "./plugin.mjs";
|
|
11
|
+
export * from "./adapters/index.mjs";
|
|
12
|
+
export * from "./cache/index.mjs";
|
|
13
|
+
export * from "./interceptors/index.mjs";
|
|
14
|
+
export * from "./graphql.mjs";
|
|
15
|
+
export * from "./websocket.mjs";
|
|
16
|
+
export * from "./http-cache.mjs";
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.DebugLogger = void 0;
|
|
7
|
+
exports.createDebugInterceptor = createDebugInterceptor;
|
|
8
|
+
exports.debugLogger = void 0;
|
|
9
|
+
function createDebugInterceptor(options = {}) {
|
|
10
|
+
const {
|
|
11
|
+
enabled = false,
|
|
12
|
+
level = "log",
|
|
13
|
+
logRequestBody = true,
|
|
14
|
+
logResponseBody = true,
|
|
15
|
+
sanitize,
|
|
16
|
+
logger
|
|
17
|
+
} = options;
|
|
18
|
+
const loggers = {
|
|
19
|
+
log: console.log,
|
|
20
|
+
warn: console.warn,
|
|
21
|
+
error: console.error
|
|
22
|
+
};
|
|
23
|
+
const log = logger || loggers[level];
|
|
24
|
+
const createDebugInfo = (config, response, error) => {
|
|
25
|
+
const info = {
|
|
26
|
+
requestId: config.requestId || "",
|
|
27
|
+
url: config.fullPath || config.url || "",
|
|
28
|
+
method: config.method || "GET",
|
|
29
|
+
timestamp: Date.now()
|
|
30
|
+
};
|
|
31
|
+
if (logRequestBody) {
|
|
32
|
+
info.headers = config.headers;
|
|
33
|
+
info.data = config.data;
|
|
34
|
+
}
|
|
35
|
+
if (response) {
|
|
36
|
+
info.status = response.response.status;
|
|
37
|
+
info.duration = response.duration;
|
|
38
|
+
if (logResponseBody) {
|
|
39
|
+
info.response = response.data;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (error) {
|
|
43
|
+
info.error = error.message;
|
|
44
|
+
info.status = error.response?.status;
|
|
45
|
+
}
|
|
46
|
+
return info;
|
|
47
|
+
};
|
|
48
|
+
const printDebug = info => {
|
|
49
|
+
if (!enabled) return;
|
|
50
|
+
const sanitizedInfo = sanitize ? sanitize(info) : info;
|
|
51
|
+
const prefix = `[YH-Request] ${sanitizedInfo.method} ${sanitizedInfo.requestId}`;
|
|
52
|
+
const status = sanitizedInfo.status ? ` [${sanitizedInfo.status}]` : "";
|
|
53
|
+
const duration = sanitizedInfo.duration ? ` (${sanitizedInfo.duration}ms)` : "";
|
|
54
|
+
if (sanitizedInfo.error) {
|
|
55
|
+
log({
|
|
56
|
+
...sanitizedInfo,
|
|
57
|
+
message: `${prefix}${status}${duration} - ${sanitizedInfo.error}`
|
|
58
|
+
});
|
|
59
|
+
} else {
|
|
60
|
+
log({
|
|
61
|
+
...sanitizedInfo,
|
|
62
|
+
message: `${prefix}${status}${duration}`
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
return {
|
|
67
|
+
/**
|
|
68
|
+
* 请求拦截
|
|
69
|
+
*/
|
|
70
|
+
onRequest: config => {
|
|
71
|
+
if (!enabled) return config;
|
|
72
|
+
const info = createDebugInfo(config);
|
|
73
|
+
printDebug({
|
|
74
|
+
...info,
|
|
75
|
+
message: `[YH-Request] \u2192 ${info.method} ${info.url}`
|
|
76
|
+
});
|
|
77
|
+
return config;
|
|
78
|
+
},
|
|
79
|
+
/**
|
|
80
|
+
* 响应拦截
|
|
81
|
+
*/
|
|
82
|
+
onResponse: response => {
|
|
83
|
+
if (!enabled) return response;
|
|
84
|
+
const info = createDebugInfo(response.config, response);
|
|
85
|
+
const duration = Date.now() - info.timestamp;
|
|
86
|
+
info.duration = duration;
|
|
87
|
+
printDebug(info);
|
|
88
|
+
return response;
|
|
89
|
+
},
|
|
90
|
+
/**
|
|
91
|
+
* 错误拦截
|
|
92
|
+
*/
|
|
93
|
+
onError: error => {
|
|
94
|
+
if (!enabled) return error;
|
|
95
|
+
const info = createDebugInfo(error.config || {}, void 0, error);
|
|
96
|
+
printDebug(info);
|
|
97
|
+
return error;
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
class DebugLogger {
|
|
102
|
+
logs = [];
|
|
103
|
+
maxLogs = 100;
|
|
104
|
+
/**
|
|
105
|
+
* 添加日志
|
|
106
|
+
*/
|
|
107
|
+
addLog(info) {
|
|
108
|
+
this.logs.push(info);
|
|
109
|
+
if (this.logs.length > this.maxLogs) {
|
|
110
|
+
this.logs.shift();
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* 获取所有日志
|
|
115
|
+
*/
|
|
116
|
+
getLogs() {
|
|
117
|
+
return [...this.logs];
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* 清除日志
|
|
121
|
+
*/
|
|
122
|
+
clear() {
|
|
123
|
+
this.logs = [];
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* 根据请求 ID 查找日志
|
|
127
|
+
*/
|
|
128
|
+
findByRequestId(requestId) {
|
|
129
|
+
return this.logs.filter(log => log.requestId === requestId);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* 导出日志
|
|
133
|
+
*/
|
|
134
|
+
export() {
|
|
135
|
+
return JSON.stringify(this.logs, null, 2);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
exports.DebugLogger = DebugLogger;
|
|
139
|
+
const debugLogger = exports.debugLogger = new DebugLogger();
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 调试拦截器
|
|
3
|
+
* 提供请求/响应的调试信息
|
|
4
|
+
*/
|
|
5
|
+
import type { InternalRequestOptions, RequestResponse, RequestError } from '../types';
|
|
6
|
+
/** 调试日志级别 */
|
|
7
|
+
export type DebugLevel = 'log' | 'warn' | 'error';
|
|
8
|
+
/** 调试信息 */
|
|
9
|
+
export interface DebugInfo {
|
|
10
|
+
/** 请求 ID */
|
|
11
|
+
requestId: string;
|
|
12
|
+
/** 请求 URL */
|
|
13
|
+
url: string;
|
|
14
|
+
/** 请求方法 */
|
|
15
|
+
method: string;
|
|
16
|
+
/** 请求头 */
|
|
17
|
+
headers?: Record<string, string>;
|
|
18
|
+
/** 请求体 */
|
|
19
|
+
data?: unknown;
|
|
20
|
+
/** 响应数据 */
|
|
21
|
+
response?: unknown;
|
|
22
|
+
/** 响应状态 */
|
|
23
|
+
status?: number;
|
|
24
|
+
/** 耗时 (ms) */
|
|
25
|
+
duration?: number;
|
|
26
|
+
/** 错误信息 */
|
|
27
|
+
error?: string;
|
|
28
|
+
/** 时间戳 */
|
|
29
|
+
timestamp: number;
|
|
30
|
+
/** 日志消息 */
|
|
31
|
+
message?: string;
|
|
32
|
+
}
|
|
33
|
+
/** 调试配置 */
|
|
34
|
+
export interface DebugInterceptorOptions {
|
|
35
|
+
/** 是否启用调试 */
|
|
36
|
+
enabled?: boolean;
|
|
37
|
+
/** 日志级别 */
|
|
38
|
+
level?: DebugLevel;
|
|
39
|
+
/** 是否打印请求体 */
|
|
40
|
+
logRequestBody?: boolean;
|
|
41
|
+
/** 是否打印响应体 */
|
|
42
|
+
logResponseBody?: boolean;
|
|
43
|
+
/** 脱敏处理 */
|
|
44
|
+
sanitize?: (info: DebugInfo) => DebugInfo;
|
|
45
|
+
/** 自定义日志输出 */
|
|
46
|
+
logger?: (info: DebugInfo) => void;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* 创建调试拦截器
|
|
50
|
+
*/
|
|
51
|
+
export declare function createDebugInterceptor(options?: DebugInterceptorOptions): {
|
|
52
|
+
/**
|
|
53
|
+
* 请求拦截
|
|
54
|
+
*/
|
|
55
|
+
onRequest: (config: InternalRequestOptions) => InternalRequestOptions;
|
|
56
|
+
/**
|
|
57
|
+
* 响应拦截
|
|
58
|
+
*/
|
|
59
|
+
onResponse: <T>(response: RequestResponse<T>) => RequestResponse<T>;
|
|
60
|
+
/**
|
|
61
|
+
* 错误拦截
|
|
62
|
+
*/
|
|
63
|
+
onError: (error: RequestError) => RequestError;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* 调试日志工具
|
|
67
|
+
*/
|
|
68
|
+
export declare class DebugLogger {
|
|
69
|
+
private logs;
|
|
70
|
+
private maxLogs;
|
|
71
|
+
/**
|
|
72
|
+
* 添加日志
|
|
73
|
+
*/
|
|
74
|
+
addLog(info: DebugInfo): void;
|
|
75
|
+
/**
|
|
76
|
+
* 获取所有日志
|
|
77
|
+
*/
|
|
78
|
+
getLogs(): DebugInfo[];
|
|
79
|
+
/**
|
|
80
|
+
* 清除日志
|
|
81
|
+
*/
|
|
82
|
+
clear(): void;
|
|
83
|
+
/**
|
|
84
|
+
* 根据请求 ID 查找日志
|
|
85
|
+
*/
|
|
86
|
+
findByRequestId(requestId: string): DebugInfo[];
|
|
87
|
+
/**
|
|
88
|
+
* 导出日志
|
|
89
|
+
*/
|
|
90
|
+
export(): string;
|
|
91
|
+
}
|
|
92
|
+
export declare const debugLogger: DebugLogger;
|