@done-coding/request-ofetch 1.1.1 → 1.2.0

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/README.md ADDED
@@ -0,0 +1,190 @@
1
+ # @done-coding/request-ofetch
2
+
3
+ `@done-coding/request-ofetch` 是基于 [@done-coding/request-base](../base/README.md) 的 ofetch 请求封装,提供了统一的请求处理流程和错误处理机制。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ # 使用 npm
9
+ npm install @done-coding/request-ofetch ofetch
10
+
11
+ # 使用 yarn
12
+ yarn add @done-coding/request-ofetch ofetch
13
+
14
+ # 使用 pnpm
15
+ pnpm add @done-coding/request-ofetch ofetch
16
+ ```
17
+
18
+ > 注意:如果项目中已经安装了 `ofetch`,可以跳过安装 `ofetch` 依赖。
19
+
20
+ ## 使用方法
21
+
22
+ ### 基础配置
23
+
24
+ ```typescript
25
+ import { ofetch } from 'ofetch';
26
+ import { createRequest } from '@done-coding/request-ofetch';
27
+
28
+ // 创建请求实例
29
+ const request = createRequest({
30
+ basePath: 'https://api.example.com',
31
+ timeout: 5000,
32
+ // 必须传入 ofetch 实例
33
+ Fetch: ofetch,
34
+ // 业务数据处理函数
35
+ getBusinessCode: (res) => res.data.code,
36
+ getBusinessMsg: (res) => res.data.message,
37
+ getBusinessData: (res) => res.data.data,
38
+ // 业务成功状态码列表
39
+ businessSuccessCodeList: [200],
40
+ // 请求拦截器
41
+ beforeRequest: (config) => {
42
+ // 可以修改请求配置
43
+ return config;
44
+ },
45
+ // 错误处理
46
+ beforeError: (error, { apiConfig, extendConfig }) => {
47
+ if (error.isBusinessError) {
48
+ return {
49
+ newError: error,
50
+ canShowErrorToast: true
51
+ };
52
+ }
53
+ return {
54
+ newError: error,
55
+ canShowErrorToast: true
56
+ };
57
+ }
58
+ });
59
+
60
+ // 发起请求
61
+ const response = await request({
62
+ url: '/api/data',
63
+ method: 'GET',
64
+ params: { id: 1 }
65
+ });
66
+ ```
67
+
68
+ ### 高级配置
69
+
70
+ ```typescript
71
+ import { ofetch } from 'ofetch';
72
+ import { createRequest } from '@done-coding/request-ofetch';
73
+
74
+ // 创建 ofetch 实例
75
+ const ofetchInstance = ofetch.create({
76
+ baseURL: 'https://api.example.com',
77
+ timeout: 5000,
78
+ headers: {
79
+ 'Content-Type': 'application/json'
80
+ }
81
+ });
82
+
83
+ // 创建请求实例
84
+ const request = createRequest({
85
+ basePath: 'https://api.example.com',
86
+ timeout: 5000,
87
+ // 必须传入 ofetch 实例
88
+ ofetch: ofetchInstance,
89
+ // 实例创建选项
90
+ instanceCreateConfig: {
91
+ // 可以传入 ofetch.create 的配置项
92
+ headers: {
93
+ 'Authorization': 'Bearer token'
94
+ }
95
+ },
96
+ // 业务数据处理函数
97
+ getBusinessCode: (res) => res.code,
98
+ getBusinessMsg: (res) => res.message,
99
+ getBusinessData: (res) => res.data,
100
+ // 业务成功状态码列表
101
+ businessSuccessCodeList: [200],
102
+ // 网络成功状态码列表
103
+ networkSuccessCodeList: [200, 201, 202],
104
+ // 请求拦截器
105
+ beforeRequest: (config) => {
106
+ // 可以修改请求配置
107
+ return config;
108
+ },
109
+ // 错误处理
110
+ beforeError: (error, { apiConfig, extendConfig }) => {
111
+ if (error.isBusinessError) {
112
+ return {
113
+ newError: error,
114
+ canShowErrorToast: true
115
+ };
116
+ }
117
+ return {
118
+ newError: error,
119
+ canShowErrorToast: true
120
+ };
121
+ },
122
+ // 错误提示
123
+ showToast: (message) => {
124
+ console.error(message);
125
+ },
126
+ // UI配置
127
+ uiConfig: {
128
+ errorToast: true
129
+ },
130
+ // 缓存配置
131
+ cacheConfig: {
132
+ useCache: true
133
+ },
134
+ // 缓存选项配置
135
+ cache: {
136
+ getKey: (config) => `${config.url}-${JSON.stringify(config.params)}`,
137
+ setCache: async (key, data, config) => {
138
+ // 实现缓存存储逻辑
139
+ // data: 请求结果
140
+ // config: 缓存配置
141
+ },
142
+ getCache: async (key) => {
143
+ // 实现缓存获取逻辑
144
+ return undefined;
145
+ }
146
+ }
147
+ });
148
+
149
+ // 发起请求
150
+ const response = await request({
151
+ url: '/api/data',
152
+ method: 'GET',
153
+ params: { id: 1 },
154
+ // 请求级别缓存配置
155
+ cacheConfig: {
156
+ useCache: true
157
+ }
158
+ });
159
+ ```
160
+
161
+ ## 特性
162
+
163
+ - 🔄 **统一的请求处理流程**:基于 base 包的统一请求处理流程
164
+ - 📦 **统一的数据结构**:所有请求响应都会被统一处理,确保进入 then 的数据结构一致
165
+ - ✅ **自动的状态码处理**:自动处理业务状态码,只有在成功状态下才会进入 then
166
+ - 🛡️ **统一的错误处理**:所有错误都会被统一捕获和处理,进入 catch 的错误都是经过处理的
167
+ - 🔍 **可预测的结果**:保证请求结果的可预测性,减少重复的状态码判断
168
+ - 🔄 **请求重试机制**:支持请求失败后的自动重试
169
+ - 🧠 **缓存支持**:内置缓存机制,支持自定义缓存策略
170
+ - 🐛 **调试支持**:内置调试功能,支持详细的请求生命周期日志
171
+
172
+ ## 开发
173
+
174
+ ```bash
175
+ # 安装依赖
176
+ pnpm install
177
+
178
+ # 开发模式运行
179
+ pnpm dev
180
+
181
+ # 构建
182
+ pnpm build
183
+
184
+ # 测试
185
+ pnpm test
186
+ ```
187
+
188
+ ## 许可证
189
+
190
+ MIT
package/es/index.mjs CHANGED
File without changes
package/lib/index.cjs CHANGED
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@done-coding/request-ofetch",
3
- "version": "1.1.1",
3
+ "version": "1.2.0",
4
4
  "description": "请求ofetch配置 支持nuxt",
5
5
  "main": "lib/index.cjs",
6
6
  "module": "es/index.mjs",
@@ -59,12 +59,12 @@
59
59
  },
60
60
  "sideEffects": [],
61
61
  "dependencies": {
62
- "@done-coding/request-base": "^1.1.1",
62
+ "@done-coding/request-base": "^1.2.0",
63
63
  "lodash.get": "^4.4.2",
64
64
  "lodash.omit": "^4.5.0"
65
65
  },
66
66
  "peerDependencies": {
67
67
  "ofetch": "^1.3.4"
68
68
  },
69
- "gitHead": "0e8508bf47281f67f9026669ae4a7079fbaa6110"
69
+ "gitHead": "56a2c5c34bdb49db2bb6dea755c3b4df51138a5e"
70
70
  }
package/types/index.d.ts CHANGED
File without changes
package/types/main.d.ts CHANGED
File without changes
package/types/types.d.ts CHANGED
File without changes