@gz-fronted/gz-pc 0.1.1-beta.2 → 0.1.1-beta.4

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
@@ -313,22 +313,26 @@ configureGzFetch({
313
313
  | `modalMessage` | `string` | `当前登录状态已失效,请重新登录。` |
314
314
  | `onUnauthorized` | `() => void` | 无;确认后使用 `window.location.assign` |
315
315
 
316
- 为了让弹窗继承宿主项目当前的 gg-ui 主题,需要在应用根部的 `ConfigProvider` 内挂载一次
317
- `GzFetchUnauthorizedModal`:
316
+ 为了让普通错误消息和 401 弹窗都继承宿主项目当前的 gg-ui 主题,需要在应用根部的
317
+ `ConfigProvider` 内挂载一次 `GzFetchFeedbackProvider`:
318
318
 
319
319
  ```tsx
320
- import { GzFetchUnauthorizedModal } from '@gz-fronted/gz-pc/fetch';
320
+ import { GzFetchFeedbackProvider } from '@gz-fronted/gz-pc/fetch';
321
321
  import { ConfigProvider } from '@chenhui996/gg-ui';
322
322
 
323
323
  <ConfigProvider themeMode={themeMode}>
324
- <GzFetchUnauthorizedModal />
325
- <App />
324
+ <GzFetchFeedbackProvider>
325
+ <App />
326
+ </GzFetchFeedbackProvider>
326
327
  </ConfigProvider>;
327
328
  ```
328
329
 
329
- 该组件使用受控 gg-ui `Modal`,不会调用脱离 React 上下文的静态
330
- `Modal.confirm`,因此主题切换时会继续读取宿主 `ConfigProvider` 的主题 Token,
331
- 无需复制亮色或暗色样式。业务页面不需要维护 401 状态或重复渲染弹窗。
330
+ 该 Provider 使用 `message.useMessage()` 和受控 gg-ui `Modal`,不会在已接入的项目中
331
+ 调用脱离 React 上下文的静态反馈 API。因此主题切换时,普通错误消息和 401 弹窗都会
332
+ 继续读取宿主 `ConfigProvider` 的主题 Token,无需复制亮色或暗色样式。业务页面不需要
333
+ 维护反馈状态。未挂载该 Provider 的已有项目仍会回退到原来的静态 `message.error`,
334
+ 不会产生 breaking change。`GzFetchUnauthorizedModal` 继续保留为兼容 API,但新项目
335
+ 优先使用统一 Provider。
332
336
 
333
337
  所有由同一份 gz-pc 包创建的默认客户端和独立客户端共享一个 401 管理器。首个 401
334
338
  打开弹窗,后续并发 401 不会创建新弹窗;取消或弹窗关闭动画结束后会释放状态,后续
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- var ggUi = require('@chenhui996/gg-ui');
4
3
  var axios = require('axios');
4
+ var ggUi = require('@chenhui996/gg-ui');
5
5
  var react = require('react');
6
6
  var jsxRuntime = require('react/jsx-runtime');
7
7
 
@@ -11,8 +11,8 @@ var axios__default = /*#__PURE__*/_interopDefault(axios);
11
11
 
12
12
  // src/fetch/client.ts
13
13
  var GzFetchError = class extends Error {
14
- constructor(message2, options) {
15
- super(message2);
14
+ constructor(message3, options) {
15
+ super(message3);
16
16
  this.name = "GzFetchError";
17
17
  this.type = options.type;
18
18
  this.status = options.status;
@@ -71,6 +71,28 @@ function toGzFetchError(error) {
71
71
  originalError: error
72
72
  });
73
73
  }
74
+ var feedbackHosts = /* @__PURE__ */ new Map();
75
+ var activeHostId;
76
+ var registerFeedbackHost = (hostId, host) => {
77
+ feedbackHosts.set(hostId, host);
78
+ activeHostId != null ? activeHostId : activeHostId = hostId;
79
+ return () => {
80
+ feedbackHosts.delete(hostId);
81
+ if (activeHostId !== hostId) {
82
+ return;
83
+ }
84
+ const nextHost = feedbackHosts.keys().next();
85
+ activeHostId = nextHost.done ? void 0 : nextHost.value;
86
+ };
87
+ };
88
+ var showFetchErrorMessage = (content) => {
89
+ const activeHost = activeHostId === void 0 ? void 0 : feedbackHosts.get(activeHostId);
90
+ if (activeHost) {
91
+ activeHost.showErrorMessage(content);
92
+ return;
93
+ }
94
+ ggUi.message.error(content);
95
+ };
74
96
 
75
97
  // src/fetch/middleware.ts
76
98
  async function runRequestMiddlewares(initialConfig, middlewares) {
@@ -263,7 +285,7 @@ function createGzFetch(options = {}) {
263
285
  await runErrorMiddlewares(error, { config: currentConfig }, middlewares);
264
286
  const shouldShowError = (_a2 = currentConfig.showErrorMessage) != null ? _a2 : showInstanceErrorMessage;
265
287
  if (shouldShowError && error.type !== "CANCELED_ERROR" && !shouldHandleUnauthorized) {
266
- ggUi.message.error(error.message);
288
+ showFetchErrorMessage(error.message);
267
289
  }
268
290
  throw error;
269
291
  }
@@ -335,8 +357,33 @@ var GzFetchUnauthorizedModal = () => {
335
357
  };
336
358
  GzFetchUnauthorizedModal.displayName = "GzFetchUnauthorizedModal";
337
359
  var GzFetchUnauthorizedModal_default = GzFetchUnauthorizedModal;
360
+ var GzFetchFeedbackProvider = ({ children }) => {
361
+ var _a;
362
+ const hostIdRef = react.useRef(void 0);
363
+ (_a = hostIdRef.current) != null ? _a : hostIdRef.current = /* @__PURE__ */ Symbol("gz-fetch-feedback-host");
364
+ const hostId = hostIdRef.current;
365
+ const [messageApi, messageContextHolder] = ggUi.message.useMessage();
366
+ const showErrorMessage = react.useCallback(
367
+ (content) => {
368
+ void messageApi.error(content);
369
+ },
370
+ [messageApi]
371
+ );
372
+ react.useEffect(
373
+ () => registerFeedbackHost(hostId, { showErrorMessage }),
374
+ [hostId, showErrorMessage]
375
+ );
376
+ return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
377
+ messageContextHolder,
378
+ /* @__PURE__ */ jsxRuntime.jsx(GzFetchUnauthorizedModal_default, {}),
379
+ children
380
+ ] });
381
+ };
382
+ GzFetchFeedbackProvider.displayName = "GzFetchFeedbackProvider";
383
+ var GzFetchFeedbackProvider_default = GzFetchFeedbackProvider;
338
384
 
339
385
  exports.GzFetchError = GzFetchError;
386
+ exports.GzFetchFeedbackProvider = GzFetchFeedbackProvider_default;
340
387
  exports.GzFetchUnauthorizedModal = GzFetchUnauthorizedModal_default;
341
388
  exports.configureGzFetch = configureGzFetch;
342
389
  exports.createGzFetch = createGzFetch;
@@ -1,5 +1,5 @@
1
1
  import { AxiosHeaders } from 'axios';
2
- import { FC } from 'react';
2
+ import { FC, PropsWithChildren } from 'react';
3
3
 
4
4
  type GzFetchErrorType = 'HTTP_ERROR' | 'NETWORK_ERROR' | 'TIMEOUT_ERROR' | 'CANCELED_ERROR' | 'UNKNOWN_ERROR';
5
5
  interface GzFetchErrorOptions {
@@ -86,6 +86,9 @@ declare function createGzFetch(options?: CreateGzFetchOptions): GzFetchClient;
86
86
  declare function configureGzFetch(options: CreateGzFetchOptions): void;
87
87
  declare function gzFetch<TResponse, TRequestParams = unknown>(config: GzRequestConfig<TRequestParams>): Promise<TResponse>;
88
88
 
89
+ declare const GzFetchFeedbackProvider: FC<PropsWithChildren>;
90
+
91
+ /** @deprecated 请使用 GzFetchFeedbackProvider 统一接入请求反馈。 */
89
92
  declare const GzFetchUnauthorizedModal: FC;
90
93
 
91
- export { type CreateGzFetchOptions, type GzErrorContext, type GzFetch, type GzFetchAuthOptions, type GzFetchClient, GzFetchError, type GzFetchErrorOptions, type GzFetchErrorType, type GzFetchMiddleware, GzFetchUnauthorizedModal, type GzFetchUnauthorizedOptions, type GzInternalRequestConfig, type GzRequestConfig, type GzRequestHeaders, type GzRequestMethod, type GzRequestOptions, type GzRequestParams, type GzResponseContext, type GzResponseType, configureGzFetch, createGzFetch, getResponseMessage, gzFetch, toGzFetchError };
94
+ export { type CreateGzFetchOptions, type GzErrorContext, type GzFetch, type GzFetchAuthOptions, type GzFetchClient, GzFetchError, type GzFetchErrorOptions, type GzFetchErrorType, GzFetchFeedbackProvider, type GzFetchMiddleware, GzFetchUnauthorizedModal, type GzFetchUnauthorizedOptions, type GzInternalRequestConfig, type GzRequestConfig, type GzRequestHeaders, type GzRequestMethod, type GzRequestOptions, type GzRequestParams, type GzResponseContext, type GzResponseType, configureGzFetch, createGzFetch, getResponseMessage, gzFetch, toGzFetchError };
@@ -1,5 +1,5 @@
1
1
  import { AxiosHeaders } from 'axios';
2
- import { FC } from 'react';
2
+ import { FC, PropsWithChildren } from 'react';
3
3
 
4
4
  type GzFetchErrorType = 'HTTP_ERROR' | 'NETWORK_ERROR' | 'TIMEOUT_ERROR' | 'CANCELED_ERROR' | 'UNKNOWN_ERROR';
5
5
  interface GzFetchErrorOptions {
@@ -86,6 +86,9 @@ declare function createGzFetch(options?: CreateGzFetchOptions): GzFetchClient;
86
86
  declare function configureGzFetch(options: CreateGzFetchOptions): void;
87
87
  declare function gzFetch<TResponse, TRequestParams = unknown>(config: GzRequestConfig<TRequestParams>): Promise<TResponse>;
88
88
 
89
+ declare const GzFetchFeedbackProvider: FC<PropsWithChildren>;
90
+
91
+ /** @deprecated 请使用 GzFetchFeedbackProvider 统一接入请求反馈。 */
89
92
  declare const GzFetchUnauthorizedModal: FC;
90
93
 
91
- export { type CreateGzFetchOptions, type GzErrorContext, type GzFetch, type GzFetchAuthOptions, type GzFetchClient, GzFetchError, type GzFetchErrorOptions, type GzFetchErrorType, type GzFetchMiddleware, GzFetchUnauthorizedModal, type GzFetchUnauthorizedOptions, type GzInternalRequestConfig, type GzRequestConfig, type GzRequestHeaders, type GzRequestMethod, type GzRequestOptions, type GzRequestParams, type GzResponseContext, type GzResponseType, configureGzFetch, createGzFetch, getResponseMessage, gzFetch, toGzFetchError };
94
+ export { type CreateGzFetchOptions, type GzErrorContext, type GzFetch, type GzFetchAuthOptions, type GzFetchClient, GzFetchError, type GzFetchErrorOptions, type GzFetchErrorType, GzFetchFeedbackProvider, type GzFetchMiddleware, GzFetchUnauthorizedModal, type GzFetchUnauthorizedOptions, type GzInternalRequestConfig, type GzRequestConfig, type GzRequestHeaders, type GzRequestMethod, type GzRequestOptions, type GzRequestParams, type GzResponseContext, type GzResponseType, configureGzFetch, createGzFetch, getResponseMessage, gzFetch, toGzFetchError };
@@ -1,12 +1,12 @@
1
- import { Modal, message } from '@chenhui996/gg-ui';
2
1
  import axios, { AxiosError, AxiosHeaders } from 'axios';
3
- import { useRef, useSyncExternalStore, useEffect } from 'react';
4
- import { jsx } from 'react/jsx-runtime';
2
+ import { Modal, message } from '@chenhui996/gg-ui';
3
+ import { useRef, useSyncExternalStore, useEffect, useCallback } from 'react';
4
+ import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
5
5
 
6
6
  // src/fetch/client.ts
7
7
  var GzFetchError = class extends Error {
8
- constructor(message2, options) {
9
- super(message2);
8
+ constructor(message3, options) {
9
+ super(message3);
10
10
  this.name = "GzFetchError";
11
11
  this.type = options.type;
12
12
  this.status = options.status;
@@ -65,6 +65,28 @@ function toGzFetchError(error) {
65
65
  originalError: error
66
66
  });
67
67
  }
68
+ var feedbackHosts = /* @__PURE__ */ new Map();
69
+ var activeHostId;
70
+ var registerFeedbackHost = (hostId, host) => {
71
+ feedbackHosts.set(hostId, host);
72
+ activeHostId != null ? activeHostId : activeHostId = hostId;
73
+ return () => {
74
+ feedbackHosts.delete(hostId);
75
+ if (activeHostId !== hostId) {
76
+ return;
77
+ }
78
+ const nextHost = feedbackHosts.keys().next();
79
+ activeHostId = nextHost.done ? void 0 : nextHost.value;
80
+ };
81
+ };
82
+ var showFetchErrorMessage = (content) => {
83
+ const activeHost = activeHostId === void 0 ? void 0 : feedbackHosts.get(activeHostId);
84
+ if (activeHost) {
85
+ activeHost.showErrorMessage(content);
86
+ return;
87
+ }
88
+ message.error(content);
89
+ };
68
90
 
69
91
  // src/fetch/middleware.ts
70
92
  async function runRequestMiddlewares(initialConfig, middlewares) {
@@ -257,7 +279,7 @@ function createGzFetch(options = {}) {
257
279
  await runErrorMiddlewares(error, { config: currentConfig }, middlewares);
258
280
  const shouldShowError = (_a2 = currentConfig.showErrorMessage) != null ? _a2 : showInstanceErrorMessage;
259
281
  if (shouldShowError && error.type !== "CANCELED_ERROR" && !shouldHandleUnauthorized) {
260
- message.error(error.message);
282
+ showFetchErrorMessage(error.message);
261
283
  }
262
284
  throw error;
263
285
  }
@@ -329,5 +351,29 @@ var GzFetchUnauthorizedModal = () => {
329
351
  };
330
352
  GzFetchUnauthorizedModal.displayName = "GzFetchUnauthorizedModal";
331
353
  var GzFetchUnauthorizedModal_default = GzFetchUnauthorizedModal;
354
+ var GzFetchFeedbackProvider = ({ children }) => {
355
+ var _a;
356
+ const hostIdRef = useRef(void 0);
357
+ (_a = hostIdRef.current) != null ? _a : hostIdRef.current = /* @__PURE__ */ Symbol("gz-fetch-feedback-host");
358
+ const hostId = hostIdRef.current;
359
+ const [messageApi, messageContextHolder] = message.useMessage();
360
+ const showErrorMessage = useCallback(
361
+ (content) => {
362
+ void messageApi.error(content);
363
+ },
364
+ [messageApi]
365
+ );
366
+ useEffect(
367
+ () => registerFeedbackHost(hostId, { showErrorMessage }),
368
+ [hostId, showErrorMessage]
369
+ );
370
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
371
+ messageContextHolder,
372
+ /* @__PURE__ */ jsx(GzFetchUnauthorizedModal_default, {}),
373
+ children
374
+ ] });
375
+ };
376
+ GzFetchFeedbackProvider.displayName = "GzFetchFeedbackProvider";
377
+ var GzFetchFeedbackProvider_default = GzFetchFeedbackProvider;
332
378
 
333
- export { GzFetchError, GzFetchUnauthorizedModal_default as GzFetchUnauthorizedModal, configureGzFetch, createGzFetch, getResponseMessage, gzFetch, toGzFetchError };
379
+ export { GzFetchError, GzFetchFeedbackProvider_default as GzFetchFeedbackProvider, GzFetchUnauthorizedModal_default as GzFetchUnauthorizedModal, configureGzFetch, createGzFetch, getResponseMessage, gzFetch, toGzFetchError };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gz-fronted/gz-pc",
3
- "version": "0.1.1-beta.2",
3
+ "version": "0.1.1-beta.4",
4
4
  "description": "Shared fetch, hooks and utility primitives for GZ frontend applications.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -42,16 +42,6 @@
42
42
  },
43
43
  "./package.json": "./package.json"
44
44
  },
45
- "scripts": {
46
- "build": "node scripts/build.mjs",
47
- "lint": "eslint .",
48
- "typecheck": "tsc --noEmit",
49
- "test": "vitest run",
50
- "test:watch": "vitest",
51
- "verify:exports": "node tests/exports/smoke.mjs",
52
- "prepublishOnly": "npm run build",
53
- "prepare": "husky"
54
- },
55
45
  "dependencies": {
56
46
  "ahooks": "^3.9.7",
57
47
  "axios": "^1.13.6"
@@ -95,5 +85,13 @@
95
85
  "*.{json,md,yml,yaml,css,less}": [
96
86
  "prettier --write"
97
87
  ]
88
+ },
89
+ "scripts": {
90
+ "build": "node scripts/build.mjs",
91
+ "lint": "eslint .",
92
+ "typecheck": "tsc --noEmit",
93
+ "test": "vitest run",
94
+ "test:watch": "vitest",
95
+ "verify:exports": "node tests/exports/smoke.mjs"
98
96
  }
99
- }
97
+ }