@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.
Files changed (78) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +274 -0
  3. package/dist/adapters/fetch.cjs +157 -0
  4. package/dist/adapters/fetch.d.ts +25 -0
  5. package/dist/adapters/fetch.mjs +148 -0
  6. package/dist/adapters/index.cjs +27 -0
  7. package/dist/adapters/index.d.ts +5 -0
  8. package/dist/adapters/index.mjs +2 -0
  9. package/dist/adapters/platform.cjs +394 -0
  10. package/dist/adapters/platform.d.ts +72 -0
  11. package/dist/adapters/platform.mjs +369 -0
  12. package/dist/cache/index.cjs +56 -0
  13. package/dist/cache/index.d.ts +21 -0
  14. package/dist/cache/index.mjs +14 -0
  15. package/dist/cache/indexedDB.cjs +188 -0
  16. package/dist/cache/indexedDB.d.ts +58 -0
  17. package/dist/cache/indexedDB.mjs +176 -0
  18. package/dist/cache/localStorage.cjs +158 -0
  19. package/dist/cache/localStorage.d.ts +58 -0
  20. package/dist/cache/localStorage.mjs +153 -0
  21. package/dist/cache/memory.cjs +112 -0
  22. package/dist/cache/memory.d.ts +71 -0
  23. package/dist/cache/memory.mjs +103 -0
  24. package/dist/graphql.cjs +255 -0
  25. package/dist/graphql.d.ts +192 -0
  26. package/dist/graphql.mjs +235 -0
  27. package/dist/http-cache.cjs +248 -0
  28. package/dist/http-cache.d.ts +156 -0
  29. package/dist/http-cache.mjs +233 -0
  30. package/dist/index.cjs +181 -0
  31. package/dist/index.d.ts +23 -0
  32. package/dist/index.mjs +16 -0
  33. package/dist/interceptors/debug.cjs +139 -0
  34. package/dist/interceptors/debug.d.ts +92 -0
  35. package/dist/interceptors/debug.mjs +130 -0
  36. package/dist/interceptors/index.cjs +38 -0
  37. package/dist/interceptors/index.d.ts +6 -0
  38. package/dist/interceptors/index.mjs +3 -0
  39. package/dist/interceptors/progress.cjs +185 -0
  40. package/dist/interceptors/progress.d.ts +97 -0
  41. package/dist/interceptors/progress.mjs +177 -0
  42. package/dist/interceptors/security.cjs +154 -0
  43. package/dist/interceptors/security.d.ts +83 -0
  44. package/dist/interceptors/security.mjs +134 -0
  45. package/dist/plugin.cjs +166 -0
  46. package/dist/plugin.d.ts +106 -0
  47. package/dist/plugin.mjs +163 -0
  48. package/dist/request.cjs +396 -0
  49. package/dist/request.d.ts +111 -0
  50. package/dist/request.mjs +339 -0
  51. package/dist/types.cjs +13 -0
  52. package/dist/types.d.ts +157 -0
  53. package/dist/types.mjs +7 -0
  54. package/dist/useAIStream.cjs +125 -0
  55. package/dist/useAIStream.d.ts +89 -0
  56. package/dist/useAIStream.mjs +108 -0
  57. package/dist/useLoadMore.cjs +136 -0
  58. package/dist/useLoadMore.d.ts +84 -0
  59. package/dist/useLoadMore.mjs +134 -0
  60. package/dist/usePagination.cjs +141 -0
  61. package/dist/usePagination.d.ts +89 -0
  62. package/dist/usePagination.mjs +132 -0
  63. package/dist/useQueue.cjs +243 -0
  64. package/dist/useQueue.d.ts +118 -0
  65. package/dist/useQueue.mjs +239 -0
  66. package/dist/useRequest.cjs +325 -0
  67. package/dist/useRequest.d.ts +126 -0
  68. package/dist/useRequest.mjs +329 -0
  69. package/dist/useRequestQueue.cjs +36 -0
  70. package/dist/useRequestQueue.d.ts +52 -0
  71. package/dist/useRequestQueue.mjs +27 -0
  72. package/dist/useSSE.cjs +241 -0
  73. package/dist/useSSE.d.ts +74 -0
  74. package/dist/useSSE.mjs +226 -0
  75. package/dist/websocket.cjs +325 -0
  76. package/dist/websocket.d.ts +163 -0
  77. package/dist/websocket.mjs +316 -0
  78. package/package.json +61 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024-present YH-UI Team
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,274 @@
1
+ # @yh-ui/request
2
+
3
+ <p align="center">
4
+ <img src="https://raw.githubusercontent.com/1079161148/yh-ui/main/docs/public/logo.svg" width="100" height="100" alt="YH-UI Logo">
5
+ </p>
6
+
7
+ <h3 align="center">YH-UI Request — 企业级请求管理 Hooks</h3>
8
+
9
+ <p align="center">
10
+ 响应式请求状态 · SSE 流式支持 · 自动取消 · 重试机制 · 缓存管理 · 完整 TypeScript
11
+ </p>
12
+
13
+ <p align="center">
14
+ <a href="https://www.npmjs.com/package/@yh-ui/request">
15
+ <img src="https://img.shields.io/npm/v/@yh-ui/request.svg?style=flat-square&colorB=409eff" alt="npm version">
16
+ </a>
17
+ <a href="https://www.npmjs.com/package/@yh-ui/request">
18
+ <img src="https://img.shields.io/npm/dm/@yh-ui/request.svg?style=flat-square&colorB=409eff" alt="npm downloads">
19
+ </a>
20
+ <a href="https://github.com/1079161148/yh-ui/blob/main/LICENSE">
21
+ <img src="https://img.shields.io/npm/l/@yh-ui/request.svg?style=flat-square" alt="license">
22
+ </a>
23
+ </p>
24
+
25
+ ---
26
+
27
+ ## ✨ 特性
28
+
29
+ - ⚡ **响应式状态** — `data`、`loading`、`error` 全部是 Vue 响应式引用,无需手动管理
30
+ - 🌊 **SSE 流式支持** — 原生支持 Server-Sent Events,完美配合 AI 流式输出
31
+ - 🔄 **自动重试** — 可配置指数退避重试策略
32
+ - 🚫 **自动取消** — 组件卸载时自动中止 fetch,防止内存泄漏
33
+ - 🗄 **请求缓存** — 内置 LRU 缓存,支持 TTL 过期策略
34
+ - 🔌 **拦截器** — 支持请求/响应拦截器,方便处理鉴权、日志等
35
+ - 🌐 **SSR 安全** — 服务端环境无副作用
36
+
37
+ ---
38
+
39
+ ## 📦 安装
40
+
41
+ ```bash
42
+ # pnpm(推荐)
43
+ pnpm add @yh-ui/request
44
+
45
+ # npm
46
+ npm install @yh-ui/request
47
+ ```
48
+
49
+ ---
50
+
51
+ ## 🔨 快速开始
52
+
53
+ ### `useRequest` — 基础请求
54
+
55
+ ```vue
56
+ <script setup lang="ts">
57
+ import { useRequest } from '@yh-ui/request'
58
+
59
+ interface User {
60
+ id: number
61
+ name: string
62
+ email: string
63
+ }
64
+
65
+ const { data, loading, error, execute, refresh } = useRequest<User[]>(
66
+ () => fetch('/api/users').then((r) => r.json()),
67
+ {
68
+ immediate: true, // 立即执行
69
+ initialData: [], // 初始值
70
+ onSuccess: (data) => console.log('成功:', data),
71
+ onError: (err) => console.error('失败:', err)
72
+ }
73
+ )
74
+ </script>
75
+
76
+ <template>
77
+ <div>
78
+ <div v-if="loading">加载中...</div>
79
+ <div v-if="error">错误: {{ error.message }}</div>
80
+ <ul v-if="data">
81
+ <li v-for="user in data" :key="user.id">{{ user.name }}</li>
82
+ </ul>
83
+ <button @click="refresh">刷新</button>
84
+ </div>
85
+ </template>
86
+ ```
87
+
88
+ ### `useRequest` — 高级配置
89
+
90
+ ```ts
91
+ import { useRequest } from '@yh-ui/request'
92
+
93
+ const { data, loading, execute } = useRequest(fetchUserData, {
94
+ immediate: false, // 手动触发
95
+ retry: 3, // 失败重试 3 次
96
+ retryDelay: 1000, // 重试间隔(支持指数退避)
97
+ cache: true, // 开启缓存
98
+ cacheTTL: 60000, // 缓存 60 秒
99
+ debounce: 300, // 防抖 300ms
100
+ throttle: 0, // 节流(ms),0 为关闭
101
+ timeout: 10000 // 请求超时 10s
102
+ })
103
+
104
+ // 手动触发
105
+ await execute()
106
+ ```
107
+
108
+ ### `useSSE` — Server-Sent Events 流式
109
+
110
+ ```vue
111
+ <script setup lang="ts">
112
+ import { ref } from 'vue'
113
+ import { useSSE } from '@yh-ui/request'
114
+
115
+ const messages = ref<string[]>([])
116
+
117
+ const { isConnected, connect, disconnect, error } = useSSE('/api/chat/stream', {
118
+ onMessage: (event) => {
119
+ const data = JSON.parse(event.data)
120
+ if (data.done) {
121
+ disconnect()
122
+ return
123
+ }
124
+ messages.value.push(data.content)
125
+ },
126
+ onOpen: () => console.log('连接建立'),
127
+ onError: (err) => console.error('SSE 错误:', err),
128
+ // 自动重连
129
+ reconnect: true,
130
+ reconnectInterval: 3000,
131
+ maxReconnectAttempts: 5
132
+ })
133
+ </script>
134
+
135
+ <template>
136
+ <div>
137
+ <div class="status">{{ isConnected ? '🟢 已连接' : '🔴 未连接' }}</div>
138
+ <button @click="connect">开始</button>
139
+ <button @click="disconnect">停止</button>
140
+ <div v-for="(msg, i) in messages" :key="i">{{ msg }}</div>
141
+ </div>
142
+ </template>
143
+ ```
144
+
145
+ ### `usePagination` — 分页请求
146
+
147
+ ```vue
148
+ <script setup lang="ts">
149
+ import { usePagination } from '@yh-ui/request'
150
+
151
+ const { data, loading, page, pageSize, total, prev, next, goTo, refresh } = usePagination(
152
+ ({ page, pageSize }) => fetch(`/api/list?page=${page}&size=${pageSize}`).then((r) => r.json()),
153
+ {
154
+ initialPage: 1,
155
+ initialPageSize: 20,
156
+ immediate: true
157
+ }
158
+ )
159
+ </script>
160
+
161
+ <template>
162
+ <div>
163
+ <table>
164
+ <tr v-for="item in data?.list" :key="item.id">
165
+ <td>{{ item.name }}</td>
166
+ </tr>
167
+ </table>
168
+ <div class="pagination">
169
+ <button @click="prev" :disabled="page === 1">上一页</button>
170
+ <span>{{ page }} / {{ Math.ceil(total / pageSize) }}</span>
171
+ <button @click="next">下一页</button>
172
+ </div>
173
+ </div>
174
+ </template>
175
+ ```
176
+
177
+ ### `useInfiniteScroll` — 无限滚动
178
+
179
+ ```ts
180
+ import { ref } from 'vue'
181
+ import { useInfiniteScroll } from '@yh-ui/request'
182
+
183
+ const containerRef = ref<HTMLElement>()
184
+
185
+ const { data, loading, hasMore, loadMore } = useInfiniteScroll(
186
+ (cursor) => fetch(`/api/feed?cursor=${cursor}`).then((r) => r.json()),
187
+ {
188
+ target: containerRef, // 自动监听此元素的滚动触底
189
+ threshold: 200, // 距离底部 200px 时触发
190
+ getCursor: (lastPage) => lastPage.nextCursor
191
+ }
192
+ )
193
+ ```
194
+
195
+ ---
196
+
197
+ ## ⚙️ 拦截器配置
198
+
199
+ ```ts
200
+ import { createRequestInstance } from '@yh-ui/request'
201
+
202
+ const request = createRequestInstance({
203
+ baseURL: 'https://api.example.com',
204
+ timeout: 10000,
205
+ // 请求拦截器
206
+ onRequest: (config) => {
207
+ config.headers['Authorization'] = `Bearer ${getToken()}`
208
+ return config
209
+ },
210
+ // 响应拦截器
211
+ onResponse: (response) => {
212
+ if (response.code !== 0) throw new Error(response.message)
213
+ return response.data
214
+ },
215
+ // 错误拦截器
216
+ onError: (error) => {
217
+ if (error.status === 401) router.push('/login')
218
+ }
219
+ })
220
+
221
+ // 在组件中使用
222
+ const { data } = useRequest(() => request.get('/users'))
223
+ ```
224
+
225
+ ---
226
+
227
+ ## 📚 API 参考
228
+
229
+ ### `useRequest(fetcher, options?)`
230
+
231
+ | 参数 | 类型 | 默认值 | 说明 |
232
+ | ------------- | ---------------------- | ------- | -------------------------- |
233
+ | `immediate` | `boolean` | `true` | 是否立即执行 |
234
+ | `initialData` | `T \| null` | `null` | 初始数据值 |
235
+ | `retry` | `number` | `0` | 失败重试次数 |
236
+ | `retryDelay` | `number` | `1000` | 重试延迟(ms) |
237
+ | `cache` | `boolean` | `false` | 是否开启缓存 |
238
+ | `cacheTTL` | `number` | `0` | 缓存有效期(ms),0 为永久 |
239
+ | `debounce` | `number` | `0` | 防抖延迟(ms) |
240
+ | `timeout` | `number` | `0` | 超时时间(ms),0 为不限制 |
241
+ | `onSuccess` | `(data: T) => void` | — | 成功回调 |
242
+ | `onError` | `(err: Error) => void` | — | 失败回调 |
243
+ | `onFinally` | `() => void` | — | 完成回调 |
244
+
245
+ ### 返回值
246
+
247
+ | 属性/方法 | 类型 | 说明 |
248
+ | ------------------ | -------------------- | -------------------- |
249
+ | `data` | `Ref<T \| null>` | 响应数据 |
250
+ | `loading` | `Ref<boolean>` | 加载状态 |
251
+ | `error` | `Ref<Error \| null>` | 错误信息 |
252
+ | `execute(...args)` | `Promise<T>` | 手动执行 |
253
+ | `refresh()` | `Promise<T>` | 使用上次参数重新执行 |
254
+ | `abort()` | `void` | 中止当前请求 |
255
+ | `reset()` | `void` | 重置状态到初始值 |
256
+
257
+ ---
258
+
259
+ ## ⚠️ 注意事项
260
+
261
+ - **自动中止**:组件卸载时会自动调用 `abort()`,无需手动清理
262
+ - **SSR**:`useSSE`、`useInfiniteScroll` 在服务端不会自动连接,仅在 `onMounted` 后激活
263
+ - **并发请求**:默认取消上一次未完成的请求(竞态条件保护),可通过 `cancelPrevious: false` 关闭
264
+
265
+ ---
266
+
267
+ ## 🔗 相关资源
268
+
269
+ - [📖 官方文档](https://1079161148.github.io/yh-ui/)
270
+ - [📦 GitHub 仓库](https://github.com/1079161148/yh-ui)
271
+
272
+ ## 📄 开源协议
273
+
274
+ MIT License © 2024-present YH-UI Team
@@ -0,0 +1,157 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.XHRAdapter = exports.FetchAdapter = void 0;
7
+ exports.getDefaultAdapter = getDefaultAdapter;
8
+ class FetchAdapter {
9
+ name = "fetch";
10
+ isSupported() {
11
+ return typeof fetch !== "undefined";
12
+ }
13
+ async request(config) {
14
+ const {
15
+ fullPath,
16
+ method = "GET",
17
+ headers = {},
18
+ data,
19
+ signal,
20
+ credentials,
21
+ responseType
22
+ } = config;
23
+ const fetchOptions = {
24
+ method,
25
+ headers,
26
+ credentials,
27
+ signal
28
+ };
29
+ if (data !== void 0 && method !== "GET") {
30
+ if (data instanceof FormData) {
31
+ fetchOptions.body = data;
32
+ } else if (data instanceof URLSearchParams) {
33
+ fetchOptions.body = data;
34
+ } else {
35
+ fetchOptions.body = JSON.stringify(data);
36
+ if (!headers["Content-Type"]) {
37
+ headers["Content-Type"] = "application/json";
38
+ }
39
+ }
40
+ }
41
+ const response = await fetch(fullPath, fetchOptions);
42
+ let responseData;
43
+ switch (responseType) {
44
+ case "text":
45
+ responseData = await response.text();
46
+ break;
47
+ case "blob":
48
+ responseData = await response.blob();
49
+ break;
50
+ case "arraybuffer":
51
+ responseData = await response.arrayBuffer();
52
+ break;
53
+ case "formdata":
54
+ responseData = await response.formData();
55
+ break;
56
+ case "json":
57
+ default:
58
+ const text = await response.text();
59
+ responseData = text ? JSON.parse(text) : null;
60
+ }
61
+ return {
62
+ data: responseData,
63
+ response,
64
+ config,
65
+ requestId: config.requestId
66
+ };
67
+ }
68
+ }
69
+ exports.FetchAdapter = FetchAdapter;
70
+ class XHRAdapter {
71
+ name = "xhr";
72
+ isSupported() {
73
+ return typeof XMLHttpRequest !== "undefined";
74
+ }
75
+ async request(config) {
76
+ return new Promise((resolve, reject) => {
77
+ const xhr = new XMLHttpRequest();
78
+ const {
79
+ fullPath,
80
+ method = "GET",
81
+ headers = {},
82
+ data,
83
+ timeout,
84
+ credentials,
85
+ responseType
86
+ } = config;
87
+ xhr.open(method, fullPath, true);
88
+ xhr.responseType = responseType;
89
+ Object.entries(headers).forEach(([key, value]) => {
90
+ xhr.setRequestHeader(key, value);
91
+ });
92
+ if (credentials) {
93
+ xhr.withCredentials = credentials === "include";
94
+ }
95
+ if (timeout) {
96
+ xhr.timeout = timeout;
97
+ }
98
+ xhr.ontimeout = () => {
99
+ reject(new Error("Request timeout"));
100
+ };
101
+ xhr.onload = () => {
102
+ if (xhr.status >= 200 && xhr.status < 300) {
103
+ let responseData;
104
+ if (responseType === "text") {
105
+ responseData = xhr.responseText;
106
+ } else if (responseType === "blob") {
107
+ responseData = xhr.response;
108
+ } else if (responseType === "arraybuffer") {
109
+ responseData = xhr.response;
110
+ } else {
111
+ try {
112
+ responseData = xhr.responseText ? JSON.parse(xhr.responseText) : null;
113
+ } catch {
114
+ responseData = xhr.responseText;
115
+ }
116
+ }
117
+ resolve({
118
+ data: responseData,
119
+ response: xhr,
120
+ config,
121
+ requestId: config.requestId
122
+ });
123
+ } else {
124
+ reject(new Error(`Request failed with status ${xhr.status}`));
125
+ }
126
+ };
127
+ xhr.onerror = () => {
128
+ reject(new Error("Network error"));
129
+ };
130
+ if (data) {
131
+ if (data instanceof FormData) {
132
+ xhr.send(data);
133
+ } else {
134
+ xhr.setRequestHeader("Content-Type", "application/json");
135
+ xhr.send(JSON.stringify(data));
136
+ }
137
+ } else {
138
+ xhr.send();
139
+ }
140
+ if (config.signal) {
141
+ config.signal.addEventListener("abort", () => {
142
+ xhr.abort();
143
+ });
144
+ }
145
+ });
146
+ }
147
+ }
148
+ exports.XHRAdapter = XHRAdapter;
149
+ function getDefaultAdapter() {
150
+ if (typeof fetch !== "undefined") {
151
+ return new FetchAdapter();
152
+ }
153
+ if (typeof XMLHttpRequest !== "undefined") {
154
+ return new XHRAdapter();
155
+ }
156
+ throw new Error("No HTTP adapter available");
157
+ }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * 适配器接口定义
3
+ * 适配器架构允许切换不同的 HTTP 实现
4
+ */
5
+ import type { InternalRequestOptions, RequestResponse, HttpAdapter } from '../types';
6
+ /**
7
+ * Fetch 适配器 - 浏览器/Node 18+ 环境
8
+ */
9
+ export declare class FetchAdapter implements HttpAdapter {
10
+ name: string;
11
+ isSupported(): boolean;
12
+ request<T>(config: InternalRequestOptions): Promise<RequestResponse<T>>;
13
+ }
14
+ /**
15
+ * XHR 适配器 - 传统浏览器
16
+ */
17
+ export declare class XHRAdapter implements HttpAdapter {
18
+ name: string;
19
+ isSupported(): boolean;
20
+ request<T>(config: InternalRequestOptions): Promise<RequestResponse<T>>;
21
+ }
22
+ /**
23
+ * 获取默认适配器
24
+ */
25
+ export declare function getDefaultAdapter(): HttpAdapter;
@@ -0,0 +1,148 @@
1
+ export class FetchAdapter {
2
+ name = "fetch";
3
+ isSupported() {
4
+ return typeof fetch !== "undefined";
5
+ }
6
+ async request(config) {
7
+ const {
8
+ fullPath,
9
+ method = "GET",
10
+ headers = {},
11
+ data,
12
+ signal,
13
+ credentials,
14
+ responseType
15
+ } = config;
16
+ const fetchOptions = {
17
+ method,
18
+ headers,
19
+ credentials,
20
+ signal
21
+ };
22
+ if (data !== void 0 && method !== "GET") {
23
+ if (data instanceof FormData) {
24
+ fetchOptions.body = data;
25
+ } else if (data instanceof URLSearchParams) {
26
+ fetchOptions.body = data;
27
+ } else {
28
+ fetchOptions.body = JSON.stringify(data);
29
+ if (!headers["Content-Type"]) {
30
+ headers["Content-Type"] = "application/json";
31
+ }
32
+ }
33
+ }
34
+ const response = await fetch(fullPath, fetchOptions);
35
+ let responseData;
36
+ switch (responseType) {
37
+ case "text":
38
+ responseData = await response.text();
39
+ break;
40
+ case "blob":
41
+ responseData = await response.blob();
42
+ break;
43
+ case "arraybuffer":
44
+ responseData = await response.arrayBuffer();
45
+ break;
46
+ case "formdata":
47
+ responseData = await response.formData();
48
+ break;
49
+ case "json":
50
+ default:
51
+ const text = await response.text();
52
+ responseData = text ? JSON.parse(text) : null;
53
+ }
54
+ return {
55
+ data: responseData,
56
+ response,
57
+ config,
58
+ requestId: config.requestId
59
+ };
60
+ }
61
+ }
62
+ export class XHRAdapter {
63
+ name = "xhr";
64
+ isSupported() {
65
+ return typeof XMLHttpRequest !== "undefined";
66
+ }
67
+ async request(config) {
68
+ return new Promise((resolve, reject) => {
69
+ const xhr = new XMLHttpRequest();
70
+ const {
71
+ fullPath,
72
+ method = "GET",
73
+ headers = {},
74
+ data,
75
+ timeout,
76
+ credentials,
77
+ responseType
78
+ } = config;
79
+ xhr.open(method, fullPath, true);
80
+ xhr.responseType = responseType;
81
+ Object.entries(headers).forEach(([key, value]) => {
82
+ xhr.setRequestHeader(key, value);
83
+ });
84
+ if (credentials) {
85
+ xhr.withCredentials = credentials === "include";
86
+ }
87
+ if (timeout) {
88
+ xhr.timeout = timeout;
89
+ }
90
+ xhr.ontimeout = () => {
91
+ reject(new Error("Request timeout"));
92
+ };
93
+ xhr.onload = () => {
94
+ if (xhr.status >= 200 && xhr.status < 300) {
95
+ let responseData;
96
+ if (responseType === "text") {
97
+ responseData = xhr.responseText;
98
+ } else if (responseType === "blob") {
99
+ responseData = xhr.response;
100
+ } else if (responseType === "arraybuffer") {
101
+ responseData = xhr.response;
102
+ } else {
103
+ try {
104
+ responseData = xhr.responseText ? JSON.parse(xhr.responseText) : null;
105
+ } catch {
106
+ responseData = xhr.responseText;
107
+ }
108
+ }
109
+ resolve({
110
+ data: responseData,
111
+ response: xhr,
112
+ config,
113
+ requestId: config.requestId
114
+ });
115
+ } else {
116
+ reject(new Error(`Request failed with status ${xhr.status}`));
117
+ }
118
+ };
119
+ xhr.onerror = () => {
120
+ reject(new Error("Network error"));
121
+ };
122
+ if (data) {
123
+ if (data instanceof FormData) {
124
+ xhr.send(data);
125
+ } else {
126
+ xhr.setRequestHeader("Content-Type", "application/json");
127
+ xhr.send(JSON.stringify(data));
128
+ }
129
+ } else {
130
+ xhr.send();
131
+ }
132
+ if (config.signal) {
133
+ config.signal.addEventListener("abort", () => {
134
+ xhr.abort();
135
+ });
136
+ }
137
+ });
138
+ }
139
+ }
140
+ export function getDefaultAdapter() {
141
+ if (typeof fetch !== "undefined") {
142
+ return new FetchAdapter();
143
+ }
144
+ if (typeof XMLHttpRequest !== "undefined") {
145
+ return new XHRAdapter();
146
+ }
147
+ throw new Error("No HTTP adapter available");
148
+ }
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ var _fetch = require("./fetch.cjs");
7
+ Object.keys(_fetch).forEach(function (key) {
8
+ if (key === "default" || key === "__esModule") return;
9
+ if (key in exports && exports[key] === _fetch[key]) return;
10
+ Object.defineProperty(exports, key, {
11
+ enumerable: true,
12
+ get: function () {
13
+ return _fetch[key];
14
+ }
15
+ });
16
+ });
17
+ var _platform = require("./platform.cjs");
18
+ Object.keys(_platform).forEach(function (key) {
19
+ if (key === "default" || key === "__esModule") return;
20
+ if (key in exports && exports[key] === _platform[key]) return;
21
+ Object.defineProperty(exports, key, {
22
+ enumerable: true,
23
+ get: function () {
24
+ return _platform[key];
25
+ }
26
+ });
27
+ });
@@ -0,0 +1,5 @@
1
+ /**
2
+ * 适配器导出
3
+ */
4
+ export * from './fetch';
5
+ export * from './platform';
@@ -0,0 +1,2 @@
1
+ export * from "./fetch.mjs";
2
+ export * from "./platform.mjs";