@gz-fronted/gz-pc 0.1.0 → 0.1.1-beta.1

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,8 +22,9 @@ Axios 和 ahooks 是 gz-pc 的普通 dependencies,安装 gz-pc 时会自动安
22
22
 
23
23
  ## 请求协议
24
24
 
25
- gz-fetch **只有在 HTTP Status 为 200 时才认为请求成功**。201、204 以及其他
26
- 所有状态都会进入统一错误处理。
25
+ gz-fetch 默认将所有 HTTP `2xx` 状态视为成功,包括 200、201、202、204 和 206。
26
+ 非 `2xx` 状态进入统一错误处理并抛出 `GzFetchError`,错误中保留 `status` 和
27
+ `responseData`。
27
28
 
28
29
  成功时直接返回后端原始的 `response.data`,不会返回 `AxiosResponse`,也不会:
29
30
 
@@ -31,6 +32,29 @@ gz-fetch **只有在 HTTP Status 为 200 时才认为请求成功**。201、204
31
32
  - 自动解包 `data`
32
33
  - 转换分页、字段、日期或任何服务端数据
33
34
 
35
+ 例如后端返回以下内容时,调用方取得的是包含 `code`、`data`、`message` 的完整
36
+ 对象:
37
+
38
+ ```ts
39
+ interface CreateOrderResponse {
40
+ code: number;
41
+ data: {
42
+ orderId: string;
43
+ };
44
+ message: string;
45
+ }
46
+
47
+ const response = await gzFetch<CreateOrderResponse, CreateOrderParams>({
48
+ url: '/order/create',
49
+ method: 'POST',
50
+ params,
51
+ });
52
+
53
+ response.code;
54
+ response.data.orderId;
55
+ response.message;
56
+ ```
57
+
34
58
  ```ts
35
59
  import { configureGzFetch, gzFetch } from '@gz-fronted/gz-pc/fetch';
36
60
 
@@ -105,6 +129,7 @@ function gzFetch<TResponse, TRequestParams = unknown>(
105
129
  | `skipAuth` | `boolean` | 否 | 为 `true` 时跳过 Token 注入 |
106
130
  | `responseType` | `'json' \| 'blob' \| 'text'` | 否 | 响应数据类型,默认 `json` |
107
131
  | `signal` | `AbortSignal` | 否 | 取消请求的标准 AbortSignal |
132
+ | `withCredentials` | `boolean` | 否 | 是否携带跨域 Cookie 等凭证 |
108
133
 
109
134
  请求入参字段统一使用 `params`,业务代码不需要区分 Axios 的 `params` 和 `data`:
110
135
 
@@ -112,6 +137,8 @@ function gzFetch<TResponse, TRequestParams = unknown>(
112
137
  - POST、PUT:`params` 转为 Request Body。
113
138
 
114
139
  业务请求配置不支持 `data`,也不得混用 `params` 和 `data`。
140
+ POST、PUT 的 `params` 支持 JSON 对象、`FormData`、`URLSearchParams` 和 `Blob`;
141
+ 自定义请求头通过 `headers` 传入。
115
142
 
116
143
  完整示例:
117
144
 
@@ -153,15 +180,37 @@ const result = await gzFetch<SaveResult, SaveParams>({
153
180
  | ------------------ | ----------------------------------------------------------- | ----------------- | -------------------------- |
154
181
  | `baseURL` | `string` | 无 | 所有业务请求的基础地址 |
155
182
  | `timeout` | `number` | `15000` | 默认请求超时时间,单位毫秒 |
183
+ | `validateStatus` | `(status: number) => boolean` | 所有 `2xx` | 自定义 HTTP 成功状态范围 |
156
184
  | `getToken` | `() => string \| undefined \| Promise<string \| undefined>` | 无 | 每次请求前动态获取 Token |
157
185
  | `showErrorMessage` | `boolean` | `true` | 实例级错误提示开关 |
158
186
  | `auth.headerName` | `string` | `Authorization` | Token 请求头名称 |
159
187
  | `auth.formatToken` | `(token: string) => string` | `Bearer ${token}` | Token 格式化函数 |
160
188
  | `middlewares` | `readonly GzFetchMiddleware[]` | `[]` | 请求、响应和错误中间件 |
161
189
 
162
- 当前稳定 API 没有开放 Axios 的 `withCredentials`、`paramsSerializer`、
163
- `onUploadProgress`、`validateStatus` 等配置,也暂不支持 `PATCH`。业务代码只应传入
164
- 上表列出的字段;如果后续确有通用场景,再通过 gz-pc 统一扩展类型和请求核心。
190
+ 需要覆盖默认成功状态时,只在应用初始化层配置:
191
+
192
+ ```ts
193
+ configureGzFetch({
194
+ validateStatus: (status) => status === 200 || status === 304,
195
+ });
196
+ ```
197
+
198
+ 当前稳定 API 没有开放 Axios 的 `paramsSerializer`、`onUploadProgress` 等配置,也
199
+ 暂不支持 `PATCH`。业务代码只应传入上表列出的字段;如果后续确有通用场景,再
200
+ 通过 gz-pc 统一扩展类型和请求核心。
201
+
202
+ ### 跨域凭证
203
+
204
+ 需要浏览器携带跨域 Cookie 等凭证时,在单次请求中开启 `withCredentials`:
205
+
206
+ ```ts
207
+ await gzFetch<QueryPresetResponse, QueryPresetParams>({
208
+ url: '/queryFilterTemplate/list',
209
+ method: 'POST',
210
+ params,
211
+ withCredentials: true,
212
+ });
213
+ ```
165
214
 
166
215
  ## Token
167
216
 
@@ -196,7 +245,7 @@ configureGzFetch({
196
245
 
197
246
  ## 错误提示
198
247
 
199
- HTTP 非 200 时仅尝试读取响应体的 `msg`。没有有效 `msg` 时使用默认文案,最终
248
+ HTTP 非 `2xx` 时仅尝试读取响应体的 `msg`。没有有效 `msg` 时使用默认文案,最终
200
249
  抛出 `GzFetchError`。
201
250
 
202
251
  错误类型包括:
@@ -99,6 +99,7 @@ async function runErrorMiddlewares(error, context, middlewares) {
99
99
  // src/fetch/client.ts
100
100
  var DEFAULT_TIMEOUT = 15e3;
101
101
  var DEFAULT_AUTH_HEADER = "Authorization";
102
+ var DEFAULT_VALIDATE_STATUS = (status) => status >= 200 && status < 300;
102
103
  function toHeaderRecord(headers) {
103
104
  const result = {};
104
105
  for (const [name, value] of Object.entries(headers)) {
@@ -109,7 +110,7 @@ function toHeaderRecord(headers) {
109
110
  return result;
110
111
  }
111
112
  function createGzFetch(options = {}) {
112
- var _a, _b, _c, _d, _e, _f, _g;
113
+ var _a, _b, _c, _d, _e, _f, _g, _h;
113
114
  const middlewares = (_a = options.middlewares) != null ? _a : [];
114
115
  const showInstanceErrorMessage = (_b = options.showErrorMessage) != null ? _b : true;
115
116
  const headerName = (_d = (_c = options.auth) == null ? void 0 : _c.headerName) != null ? _d : DEFAULT_AUTH_HEADER;
@@ -117,7 +118,7 @@ function createGzFetch(options = {}) {
117
118
  const instance = axios__default.default.create({
118
119
  ...options.baseURL === void 0 ? {} : { baseURL: options.baseURL },
119
120
  timeout: (_g = options.timeout) != null ? _g : DEFAULT_TIMEOUT,
120
- validateStatus: (status) => status === 200
121
+ validateStatus: (_h = options.validateStatus) != null ? _h : DEFAULT_VALIDATE_STATUS
121
122
  });
122
123
  async function request(initialConfig) {
123
124
  var _a2;
@@ -143,7 +144,8 @@ function createGzFetch(options = {}) {
143
144
  ...usesRequestBody || currentConfig.params === void 0 ? {} : { params: currentConfig.params },
144
145
  ...currentConfig.timeout === void 0 ? {} : { timeout: currentConfig.timeout },
145
146
  ...currentConfig.responseType === void 0 ? {} : { responseType: currentConfig.responseType },
146
- ...currentConfig.signal === void 0 ? {} : { signal: currentConfig.signal }
147
+ ...currentConfig.signal === void 0 ? {} : { signal: currentConfig.signal },
148
+ ...currentConfig.withCredentials === void 0 ? {} : { withCredentials: currentConfig.withCredentials }
147
149
  };
148
150
  const response = await instance.request(axiosConfig);
149
151
  const context = {
@@ -31,6 +31,7 @@ interface GzRequestConfig<TRequestParams = unknown> {
31
31
  skipAuth?: boolean;
32
32
  responseType?: GzResponseType;
33
33
  signal?: AbortSignal;
34
+ withCredentials?: boolean;
34
35
  }
35
36
  interface GzInternalRequestConfig extends Omit<GzRequestConfig<unknown>, 'headers'> {
36
37
  headers?: GzRequestHeaders;
@@ -56,6 +57,7 @@ interface GzFetchAuthOptions {
56
57
  interface CreateGzFetchOptions {
57
58
  baseURL?: string;
58
59
  timeout?: number;
60
+ validateStatus?: (status: number) => boolean;
59
61
  getToken?: () => string | undefined | Promise<string | undefined>;
60
62
  showErrorMessage?: boolean;
61
63
  auth?: GzFetchAuthOptions;
@@ -31,6 +31,7 @@ interface GzRequestConfig<TRequestParams = unknown> {
31
31
  skipAuth?: boolean;
32
32
  responseType?: GzResponseType;
33
33
  signal?: AbortSignal;
34
+ withCredentials?: boolean;
34
35
  }
35
36
  interface GzInternalRequestConfig extends Omit<GzRequestConfig<unknown>, 'headers'> {
36
37
  headers?: GzRequestHeaders;
@@ -56,6 +57,7 @@ interface GzFetchAuthOptions {
56
57
  interface CreateGzFetchOptions {
57
58
  baseURL?: string;
58
59
  timeout?: number;
60
+ validateStatus?: (status: number) => boolean;
59
61
  getToken?: () => string | undefined | Promise<string | undefined>;
60
62
  showErrorMessage?: boolean;
61
63
  auth?: GzFetchAuthOptions;
@@ -93,6 +93,7 @@ async function runErrorMiddlewares(error, context, middlewares) {
93
93
  // src/fetch/client.ts
94
94
  var DEFAULT_TIMEOUT = 15e3;
95
95
  var DEFAULT_AUTH_HEADER = "Authorization";
96
+ var DEFAULT_VALIDATE_STATUS = (status) => status >= 200 && status < 300;
96
97
  function toHeaderRecord(headers) {
97
98
  const result = {};
98
99
  for (const [name, value] of Object.entries(headers)) {
@@ -103,7 +104,7 @@ function toHeaderRecord(headers) {
103
104
  return result;
104
105
  }
105
106
  function createGzFetch(options = {}) {
106
- var _a, _b, _c, _d, _e, _f, _g;
107
+ var _a, _b, _c, _d, _e, _f, _g, _h;
107
108
  const middlewares = (_a = options.middlewares) != null ? _a : [];
108
109
  const showInstanceErrorMessage = (_b = options.showErrorMessage) != null ? _b : true;
109
110
  const headerName = (_d = (_c = options.auth) == null ? void 0 : _c.headerName) != null ? _d : DEFAULT_AUTH_HEADER;
@@ -111,7 +112,7 @@ function createGzFetch(options = {}) {
111
112
  const instance = axios.create({
112
113
  ...options.baseURL === void 0 ? {} : { baseURL: options.baseURL },
113
114
  timeout: (_g = options.timeout) != null ? _g : DEFAULT_TIMEOUT,
114
- validateStatus: (status) => status === 200
115
+ validateStatus: (_h = options.validateStatus) != null ? _h : DEFAULT_VALIDATE_STATUS
115
116
  });
116
117
  async function request(initialConfig) {
117
118
  var _a2;
@@ -137,7 +138,8 @@ function createGzFetch(options = {}) {
137
138
  ...usesRequestBody || currentConfig.params === void 0 ? {} : { params: currentConfig.params },
138
139
  ...currentConfig.timeout === void 0 ? {} : { timeout: currentConfig.timeout },
139
140
  ...currentConfig.responseType === void 0 ? {} : { responseType: currentConfig.responseType },
140
- ...currentConfig.signal === void 0 ? {} : { signal: currentConfig.signal }
141
+ ...currentConfig.signal === void 0 ? {} : { signal: currentConfig.signal },
142
+ ...currentConfig.withCredentials === void 0 ? {} : { withCredentials: currentConfig.withCredentials }
141
143
  };
142
144
  const response = await instance.request(axiosConfig);
143
145
  const context = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gz-fronted/gz-pc",
3
- "version": "0.1.0",
3
+ "version": "0.1.1-beta.1",
4
4
  "description": "Shared fetch, hooks and utility primitives for GZ frontend applications.",
5
5
  "license": "MIT",
6
6
  "type": "module",