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

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
  }
@@ -303,40 +325,60 @@ var GzFetchUnauthorizedModal = () => {
303
325
  const hostIdRef = react.useRef(void 0);
304
326
  (_a = hostIdRef.current) != null ? _a : hostIdRef.current = /* @__PURE__ */ Symbol("gz-fetch-unauthorized-modal-host");
305
327
  const hostId = hostIdRef.current;
328
+ const [messageApi, messageContextHolder] = ggUi.message.useMessage();
329
+ const showErrorMessage = react.useCallback(
330
+ (content) => {
331
+ void messageApi.error(content);
332
+ },
333
+ [messageApi]
334
+ );
306
335
  const snapshot2 = react.useSyncExternalStore(
307
336
  subscribeUnauthorized,
308
337
  getUnauthorizedSnapshot,
309
338
  getUnauthorizedServerSnapshot
310
339
  );
311
340
  react.useEffect(() => registerUnauthorizedModalHost(hostId), [hostId]);
312
- if (snapshot2.activeHostId !== hostId) {
313
- return null;
314
- }
341
+ react.useEffect(
342
+ () => registerFeedbackHost(hostId, { showErrorMessage }),
343
+ [hostId, showErrorMessage]
344
+ );
315
345
  const handleConfirm = () => {
316
346
  const options = confirmUnauthorizedModal();
317
347
  if (options) {
318
348
  executeUnauthorizedAction(options);
319
349
  }
320
350
  };
321
- return /* @__PURE__ */ jsxRuntime.jsx(
322
- ggUi.Modal,
323
- {
324
- afterClose: completeUnauthorizedModal,
325
- cancelText: "取消",
326
- closable: true,
327
- okText: "重新登录",
328
- open: snapshot2.phase === "open",
329
- title: (_b = snapshot2.options) == null ? void 0 : _b.modalTitle,
330
- onCancel: cancelUnauthorizedModal,
331
- onOk: handleConfirm,
332
- children: (_c = snapshot2.options) == null ? void 0 : _c.modalMessage
333
- }
334
- );
351
+ return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
352
+ messageContextHolder,
353
+ snapshot2.activeHostId === hostId && /* @__PURE__ */ jsxRuntime.jsx(
354
+ ggUi.Modal,
355
+ {
356
+ afterClose: completeUnauthorizedModal,
357
+ cancelText: "取消",
358
+ closable: true,
359
+ okText: "重新登录",
360
+ open: snapshot2.phase === "open",
361
+ title: (_b = snapshot2.options) == null ? void 0 : _b.modalTitle,
362
+ onCancel: cancelUnauthorizedModal,
363
+ onOk: handleConfirm,
364
+ children: (_c = snapshot2.options) == null ? void 0 : _c.modalMessage
365
+ }
366
+ )
367
+ ] });
335
368
  };
336
369
  GzFetchUnauthorizedModal.displayName = "GzFetchUnauthorizedModal";
337
370
  var GzFetchUnauthorizedModal_default = GzFetchUnauthorizedModal;
371
+ var GzFetchFeedbackProvider = ({ children }) => {
372
+ return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
373
+ /* @__PURE__ */ jsxRuntime.jsx(GzFetchUnauthorizedModal_default, {}),
374
+ children
375
+ ] });
376
+ };
377
+ GzFetchFeedbackProvider.displayName = "GzFetchFeedbackProvider";
378
+ var GzFetchFeedbackProvider_default = GzFetchFeedbackProvider;
338
379
 
339
380
  exports.GzFetchError = GzFetchError;
381
+ exports.GzFetchFeedbackProvider = GzFetchFeedbackProvider_default;
340
382
  exports.GzFetchUnauthorizedModal = GzFetchUnauthorizedModal_default;
341
383
  exports.configureGzFetch = configureGzFetch;
342
384
  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,8 @@ 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
+
89
91
  declare const GzFetchUnauthorizedModal: FC;
90
92
 
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 };
93
+ 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,8 @@ 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
+
89
91
  declare const GzFetchUnauthorizedModal: FC;
90
92
 
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 };
93
+ 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 { message, Modal } from '@chenhui996/gg-ui';
3
+ import { useRef, useCallback, useSyncExternalStore, useEffect } from 'react';
4
+ import { jsxs, Fragment, jsx } 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
  }
@@ -297,37 +319,56 @@ var GzFetchUnauthorizedModal = () => {
297
319
  const hostIdRef = useRef(void 0);
298
320
  (_a = hostIdRef.current) != null ? _a : hostIdRef.current = /* @__PURE__ */ Symbol("gz-fetch-unauthorized-modal-host");
299
321
  const hostId = hostIdRef.current;
322
+ const [messageApi, messageContextHolder] = message.useMessage();
323
+ const showErrorMessage = useCallback(
324
+ (content) => {
325
+ void messageApi.error(content);
326
+ },
327
+ [messageApi]
328
+ );
300
329
  const snapshot2 = useSyncExternalStore(
301
330
  subscribeUnauthorized,
302
331
  getUnauthorizedSnapshot,
303
332
  getUnauthorizedServerSnapshot
304
333
  );
305
334
  useEffect(() => registerUnauthorizedModalHost(hostId), [hostId]);
306
- if (snapshot2.activeHostId !== hostId) {
307
- return null;
308
- }
335
+ useEffect(
336
+ () => registerFeedbackHost(hostId, { showErrorMessage }),
337
+ [hostId, showErrorMessage]
338
+ );
309
339
  const handleConfirm = () => {
310
340
  const options = confirmUnauthorizedModal();
311
341
  if (options) {
312
342
  executeUnauthorizedAction(options);
313
343
  }
314
344
  };
315
- return /* @__PURE__ */ jsx(
316
- Modal,
317
- {
318
- afterClose: completeUnauthorizedModal,
319
- cancelText: "取消",
320
- closable: true,
321
- okText: "重新登录",
322
- open: snapshot2.phase === "open",
323
- title: (_b = snapshot2.options) == null ? void 0 : _b.modalTitle,
324
- onCancel: cancelUnauthorizedModal,
325
- onOk: handleConfirm,
326
- children: (_c = snapshot2.options) == null ? void 0 : _c.modalMessage
327
- }
328
- );
345
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
346
+ messageContextHolder,
347
+ snapshot2.activeHostId === hostId && /* @__PURE__ */ jsx(
348
+ Modal,
349
+ {
350
+ afterClose: completeUnauthorizedModal,
351
+ cancelText: "取消",
352
+ closable: true,
353
+ okText: "重新登录",
354
+ open: snapshot2.phase === "open",
355
+ title: (_b = snapshot2.options) == null ? void 0 : _b.modalTitle,
356
+ onCancel: cancelUnauthorizedModal,
357
+ onOk: handleConfirm,
358
+ children: (_c = snapshot2.options) == null ? void 0 : _c.modalMessage
359
+ }
360
+ )
361
+ ] });
329
362
  };
330
363
  GzFetchUnauthorizedModal.displayName = "GzFetchUnauthorizedModal";
331
364
  var GzFetchUnauthorizedModal_default = GzFetchUnauthorizedModal;
365
+ var GzFetchFeedbackProvider = ({ children }) => {
366
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
367
+ /* @__PURE__ */ jsx(GzFetchUnauthorizedModal_default, {}),
368
+ children
369
+ ] });
370
+ };
371
+ GzFetchFeedbackProvider.displayName = "GzFetchFeedbackProvider";
372
+ var GzFetchFeedbackProvider_default = GzFetchFeedbackProvider;
332
373
 
333
- export { GzFetchError, GzFetchUnauthorizedModal_default as GzFetchUnauthorizedModal, configureGzFetch, createGzFetch, getResponseMessage, gzFetch, toGzFetchError };
374
+ 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.3",
4
4
  "description": "Shared fetch, hooks and utility primitives for GZ frontend applications.",
5
5
  "license": "MIT",
6
6
  "type": "module",