@done-coding/request-ofetch 1.2.3 → 1.2.4-alpha.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 CHANGED
@@ -22,19 +22,19 @@ pnpm add @done-coding/request-ofetch ofetch
22
22
  ### 基础配置
23
23
 
24
24
  ```typescript
25
- import { ofetch } from 'ofetch';
26
- import { createRequest } from '@done-coding/request-ofetch';
25
+ import { ofetch } from "ofetch";
26
+ import { createRequest } from "@done-coding/request-ofetch";
27
27
 
28
28
  // 创建请求实例
29
29
  const request = createRequest({
30
- basePath: 'https://api.example.com',
30
+ basePath: "https://api.example.com",
31
31
  timeout: 5000,
32
32
  // 必须传入 ofetch 实例
33
33
  Fetch: ofetch,
34
34
  // 业务数据处理函数
35
- getBusinessCode: (res) => res.data.code,
36
- getBusinessMsg: (res) => res.data.message,
37
- getBusinessData: (res) => res.data.data,
35
+ getBusinessCode: (res) => res.code,
36
+ getBusinessMsg: (res) => res.message,
37
+ getBusinessData: (res) => res.data,
38
38
  // 业务成功状态码列表
39
39
  businessSuccessCodeList: [200],
40
40
  // 请求拦截器
@@ -47,42 +47,42 @@ const request = createRequest({
47
47
  if (error.isBusinessError) {
48
48
  return {
49
49
  newError: error,
50
- canShowErrorToast: true
50
+ canShowErrorToast: true,
51
51
  };
52
52
  }
53
53
  return {
54
54
  newError: error,
55
- canShowErrorToast: true
55
+ canShowErrorToast: true,
56
56
  };
57
- }
57
+ },
58
58
  });
59
59
 
60
60
  // 发起请求
61
61
  const response = await request({
62
- url: '/api/data',
63
- method: 'GET',
64
- params: { id: 1 }
62
+ url: "/api/data",
63
+ method: "GET",
64
+ params: { id: 1 },
65
65
  });
66
66
  ```
67
67
 
68
68
  ### 高级配置
69
69
 
70
70
  ```typescript
71
- import { ofetch } from 'ofetch';
72
- import { createRequest } from '@done-coding/request-ofetch';
71
+ import { ofetch } from "ofetch";
72
+ import { createRequest } from "@done-coding/request-ofetch";
73
73
 
74
74
  // 创建 ofetch 实例
75
75
  const ofetchInstance = ofetch.create({
76
- baseURL: 'https://api.example.com',
76
+ baseURL: "https://api.example.com",
77
77
  timeout: 5000,
78
78
  headers: {
79
- 'Content-Type': 'application/json'
80
- }
79
+ "Content-Type": "application/json",
80
+ },
81
81
  });
82
82
 
83
83
  // 创建请求实例
84
84
  const request = createRequest({
85
- basePath: 'https://api.example.com',
85
+ basePath: "https://api.example.com",
86
86
  timeout: 5000,
87
87
  // 必须传入 ofetch 实例
88
88
  ofetch: ofetchInstance,
@@ -90,8 +90,8 @@ const request = createRequest({
90
90
  instanceCreateConfig: {
91
91
  // 可以传入 ofetch.create 的配置项
92
92
  headers: {
93
- 'Authorization': 'Bearer token'
94
- }
93
+ Authorization: "Bearer token",
94
+ },
95
95
  },
96
96
  // 业务数据处理函数
97
97
  getBusinessCode: (res) => res.code,
@@ -111,12 +111,12 @@ const request = createRequest({
111
111
  if (error.isBusinessError) {
112
112
  return {
113
113
  newError: error,
114
- canShowErrorToast: true
114
+ canShowErrorToast: true,
115
115
  };
116
116
  }
117
117
  return {
118
118
  newError: error,
119
- canShowErrorToast: true
119
+ canShowErrorToast: true,
120
120
  };
121
121
  },
122
122
  // 错误提示
@@ -125,11 +125,11 @@ const request = createRequest({
125
125
  },
126
126
  // UI配置
127
127
  uiConfig: {
128
- errorToast: true
128
+ errorToast: true,
129
129
  },
130
130
  // 缓存配置
131
131
  cacheConfig: {
132
- useCache: true
132
+ useCache: true,
133
133
  },
134
134
  // 缓存选项配置
135
135
  cache: {
@@ -142,22 +142,59 @@ const request = createRequest({
142
142
  getCache: async (key) => {
143
143
  // 实现缓存获取逻辑
144
144
  return undefined;
145
- }
146
- }
145
+ },
146
+ },
147
147
  });
148
148
 
149
149
  // 发起请求
150
150
  const response = await request({
151
- url: '/api/data',
152
- method: 'GET',
151
+ url: "/api/data",
152
+ method: "GET",
153
153
  params: { id: 1 },
154
154
  // 请求级别缓存配置
155
155
  cacheConfig: {
156
- useCache: true
157
- }
156
+ useCache: true,
157
+ },
158
158
  });
159
159
  ```
160
160
 
161
+ ## 标准错误与兼容配置
162
+
163
+ **默认关闭标准错误解析,保留 1.2.3 的非成功 HTTP 行为**:不调用业务 getter,返回 `RequestFailResultNetwork`,`isBusinessError=false`,`code` 为 HTTP 状态码。Axios / ofetch 的 `message` 保留 `statusText`(包括空字符串,不补 `HTTP <status>`);uni 保留 `errMsg`,仅在其为 `undefined` 时使用状态码字符串。新增的 `httpStatus`、`kind` 等是附加信息。2xx 的成功与业务失败判定不变。
164
+
165
+ 需要标准解析时,从当前包导入 `getErrorFromResponse`,在已有配置中加一行 `getErrorFromResponse: getErrorFromResponse`(也可用同名属性简写):
166
+
167
+ ```ts
168
+ import { getErrorFromResponse } from "@done-coding/request-ofetch";
169
+
170
+ const options = {
171
+ // ...原有配置(业务 getter 等)
172
+ getErrorFromResponse: getErrorFromResponse,
173
+ };
174
+ ```
175
+
176
+ 显式开启后,非成功 HTTP 才使用 `getBusinessCode` / `getBusinessMsg` 读取包体业务码、文案;`error.retryable`、`error.outcome`(`not_executed` / `unknown`)、`error.fields` 原样保留合法类型。标准失败包体示例:
177
+
178
+ ```json
179
+ {
180
+ "code": "ORDER_NOT_FOUND",
181
+ "message": "订单不存在",
182
+ "data": null,
183
+ "error": { "retryable": false, "outcome": "not_executed", "fields": [] },
184
+ "requestId": "req-1"
185
+ }
186
+ ```
187
+
188
+ 可选的 `getErrorFromResponse(response, getters)` 仅处理非成功 HTTP:`response` 为 `{ status, statusText, body }`;三个 `getters` 接收包体,并自动适配到现有业务 getter 的入参。标准实现作为同名函数导出,必须传入配置才生效。特殊包体可覆盖此配置,通过 `getters.getBusinessCode(body)`、`getters.getBusinessMsg(body)`、`getters.getBusinessData(body)` 复用取值方法,返回 `{ code?, message?, retryable?, outcome?, fields? }`。显式配置的解析器返回 `undefined`、业务码缺失或抛错时按 HTTP 状态码兜底;此时文案缺失使用 `statusText` 或 `HTTP <status>`(uni 没有标准 statusText)。
189
+
190
+ `beforeError` 与最终错误都可读取 `httpStatus`、`retryable`、`outcome`、`fields`、`requestId`、`cause` 和 `kind`:`http` 为非成功 HTTP,`business` 为成功 HTTP 下业务失败,`network` 为无响应或传输失败。`code` 仍是业务码或兜底码,`message` 仍是提示文案;取得业务码时 `isBusinessError=true`,HTTP 兜底与网络错误为 `false`。不要仅凭 `isBusinessError=false` 判断断网,应查看 `kind`。
191
+
192
+ 保留独立 bug 修复:ofetch 已收到 HTTP 响应时优先使用该响应,即使 `navigator.onLine=false`,也不再报 `NO_RESPONSE_OFFLINE`。这与是否开启标准解析无关,是相对 1.2.3 的已知差异。
193
+
194
+ HTTP 200 的旧后端继续只按已有 `businessSuccessCodeList` / `isSuccessBusinessCode` 判断成功,不调用新解析器;成功 HTTP 默认包含整个 2xx 区间,201 的成功包体业务码仍为 200。成功结果的可选 `_response` 保留 `{ status, statusText, headers }`(含 Location 等响应头),`_raw` 保持原形状;无响应错误不伪造 `httpStatus`。`retryable` 不会触发自动重试。
195
+
196
+ ofetch 从 `response.status` / `statusText` / `_data` 取得失败响应;现有业务 getter 仍直接接收包体,成功 `_raw` 也是包体。
197
+
161
198
  ## 特性
162
199
 
163
200
  - 🔄 **统一的请求处理流程**:基于 base 包的统一请求处理流程
@@ -187,4 +224,20 @@ pnpm test
187
224
 
188
225
  ## 许可证
189
226
 
190
- MIT
227
+ MIT
228
+
229
+ <!-- repo-map:start -->
230
+
231
+ ## 目录导航(自动维护,勿手改段内 — 由项目初始化脚本重跑刷新)
232
+
233
+ - `src/`:源码
234
+ - `__tests__/`:单元测试(MSW / Vitest)
235
+ - `package.json`:公开出口、依赖与包脚本
236
+ - `vite.config.ts`:ESM / CJS / 声明构建与 Vitest 配置
237
+ - `tsconfig.json`:TypeScript 配置
238
+ - `index.html`:本地调试宿主
239
+
240
+ > 用途:供协作者/AI 按图定位,免遍历目录树。结构变更后重跑初始化刷新。
241
+ > (模块根的本段展开该模块**内部**结构;深层大目录 ≥8 项或继续分叉则嵌套列出,叶子小目录不展。)
242
+
243
+ <!-- repo-map:end -->