@gz-fronted/gz-pc 0.1.3-beta.0 → 0.1.3-beta.2

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.
@@ -1,15 +1,16 @@
1
1
  import axios, { AxiosError, AxiosHeaders } from 'axios';
2
- import { Modal, message, Drawer, Button } from '@chenhui996/gg-ui';
3
- import { useRef, useSyncExternalStore, useEffect, useCallback } from 'react';
4
- import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
2
+ import { message, Drawer, Button, Modal, Tooltip, Tabs, Tag } from '@chenhui996/gg-ui';
3
+ import { useRef, useCallback, useEffect, useSyncExternalStore, useState } from 'react';
4
+ import { jsxs, Fragment, jsx } from 'react/jsx-runtime';
5
5
 
6
6
  // src/fetch/client.ts
7
7
 
8
- // src/fetch/debug.ts
8
+ // src/fetch/feedback/debug-store.ts
9
9
  var EMPTY_DEBUG_DRAWER_SNAPSHOT = {
10
10
  activeHostId: void 0,
11
11
  errors: [],
12
- open: false
12
+ open: false,
13
+ selectedErrorId: void 0
13
14
  };
14
15
  var debugErrorSequence = 0;
15
16
  var snapshot = EMPTY_DEBUG_DRAWER_SNAPSHOT;
@@ -24,29 +25,6 @@ var setSnapshot = (nextSnapshot) => {
24
25
  snapshot = nextSnapshot;
25
26
  emitChange();
26
27
  };
27
- var isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
28
- var getResponseDebugInfo = (responseData) => {
29
- if (!isRecord(responseData) || !isRecord(responseData.debug)) {
30
- return void 0;
31
- }
32
- return responseData.debug;
33
- };
34
- var isDebugEnabled = () => {
35
- if (typeof window === "undefined") {
36
- return false;
37
- }
38
- try {
39
- if (new URLSearchParams(window.location.search).get("showDebug") === "1") {
40
- return true;
41
- }
42
- } catch (e) {
43
- }
44
- try {
45
- return window.localStorage.getItem("showDebug") === "1";
46
- } catch (e) {
47
- return false;
48
- }
49
- };
50
28
  var subscribeDebugDrawer = (listener) => {
51
29
  listeners.add(listener);
52
30
  return () => listeners.delete(listener);
@@ -70,21 +48,20 @@ var registerDebugDrawerHost = (hostId) => {
70
48
  });
71
49
  };
72
50
  };
73
- var showFetchDebugError = (input) => {
51
+ var appendDebugError = (input) => {
74
52
  if (snapshot.activeHostId === void 0) {
75
53
  return false;
76
54
  }
77
55
  debugErrorSequence += 1;
56
+ const error = {
57
+ ...input,
58
+ id: `${Date.now()}-${debugErrorSequence}`
59
+ };
78
60
  setSnapshot({
79
61
  ...snapshot,
80
- errors: [
81
- ...snapshot.errors,
82
- {
83
- ...input,
84
- id: `${Date.now()}-${debugErrorSequence}`
85
- }
86
- ],
87
- open: true
62
+ errors: [error, ...snapshot.errors],
63
+ open: true,
64
+ selectedErrorId: error.id
88
65
  });
89
66
  return true;
90
67
  };
@@ -94,15 +71,646 @@ var closeDebugDrawer = () => {
94
71
  }
95
72
  };
96
73
  var removeDebugError = (id) => {
74
+ var _a, _b, _c, _d;
75
+ const removedIndex = snapshot.errors.findIndex((error) => error.id === id);
76
+ if (removedIndex === -1) {
77
+ return;
78
+ }
79
+ const errors = snapshot.errors.filter((error) => error.id !== id);
80
+ const selectedErrorId = snapshot.selectedErrorId === id ? (_c = (_a = errors[removedIndex]) == null ? void 0 : _a.id) != null ? _c : (_b = errors[removedIndex - 1]) == null ? void 0 : _b.id : snapshot.selectedErrorId && errors.some((error) => error.id === snapshot.selectedErrorId) ? snapshot.selectedErrorId : (_d = errors[0]) == null ? void 0 : _d.id;
97
81
  setSnapshot({
98
82
  ...snapshot,
99
- errors: snapshot.errors.filter((error) => error.id !== id)
83
+ errors,
84
+ selectedErrorId
100
85
  });
101
86
  };
87
+ var selectDebugError = (id) => {
88
+ if (snapshot.selectedErrorId === id || !snapshot.errors.some((error) => error.id === id)) {
89
+ return;
90
+ }
91
+ setSnapshot({ ...snapshot, selectedErrorId: id });
92
+ };
102
93
  var clearDebugErrors = () => {
103
94
  if (snapshot.errors.length > 0) {
104
- setSnapshot({ ...snapshot, errors: [] });
95
+ setSnapshot({ ...snapshot, errors: [], selectedErrorId: void 0 });
96
+ }
97
+ };
98
+ var feedbackHosts = /* @__PURE__ */ new Map();
99
+ var activeHostId;
100
+ var registerFeedbackHost = (hostId, host) => {
101
+ feedbackHosts.set(hostId, host);
102
+ activeHostId != null ? activeHostId : activeHostId = hostId;
103
+ return () => {
104
+ feedbackHosts.delete(hostId);
105
+ if (activeHostId !== hostId) {
106
+ return;
107
+ }
108
+ const nextHost = feedbackHosts.keys().next();
109
+ activeHostId = nextHost.done ? void 0 : nextHost.value;
110
+ };
111
+ };
112
+ var showFeedbackErrorMessage = (content) => {
113
+ const activeHost = activeHostId === void 0 ? void 0 : feedbackHosts.get(activeHostId);
114
+ if (activeHost) {
115
+ activeHost.showErrorMessage(content);
116
+ return;
117
+ }
118
+ message.error(content);
119
+ };
120
+ var showFeedbackSuccessMessage = (content) => {
121
+ const activeHost = activeHostId === void 0 ? void 0 : feedbackHosts.get(activeHostId);
122
+ if (activeHost == null ? void 0 : activeHost.showSuccessMessage) {
123
+ activeHost.showSuccessMessage(content);
124
+ return;
125
+ }
126
+ message.success(content);
127
+ };
128
+ var EMPTY_DEBUG_VALUE = "(无)";
129
+ var formatDebugValue = (value) => {
130
+ if (typeof value === "string") {
131
+ return value;
132
+ }
133
+ if (value === void 0 || value === null) {
134
+ return EMPTY_DEBUG_VALUE;
135
+ }
136
+ try {
137
+ const serialized = JSON.stringify(value, null, 2);
138
+ return serialized != null ? serialized : "(无法序列化)";
139
+ } catch (e) {
140
+ return "(无法序列化)";
141
+ }
142
+ };
143
+ var formatCompleteDebugResponse = (error) => error.responseData === void 0 ? void 0 : formatDebugValue(error.responseData);
144
+ var copyDebugText = async (text) => {
145
+ var _a;
146
+ try {
147
+ if (typeof navigator !== "undefined" && ((_a = navigator.clipboard) == null ? void 0 : _a.writeText)) {
148
+ await navigator.clipboard.writeText(text);
149
+ return true;
150
+ }
151
+ } catch (e) {
152
+ }
153
+ if (typeof document === "undefined" || !document.body || typeof document.execCommand !== "function") {
154
+ return false;
155
+ }
156
+ const textarea = document.createElement("textarea");
157
+ textarea.value = text;
158
+ textarea.setAttribute("readonly", "");
159
+ textarea.style.position = "fixed";
160
+ textarea.style.opacity = "0";
161
+ document.body.appendChild(textarea);
162
+ let copied = false;
163
+ try {
164
+ textarea.focus();
165
+ textarea.select();
166
+ copied = document.execCommand("copy");
167
+ } catch (e) {
168
+ copied = false;
169
+ } finally {
170
+ document.body.removeChild(textarea);
105
171
  }
172
+ return copied;
173
+ };
174
+ var codeBlockStyle = {
175
+ background: "var(--gz-color-bg-field, rgba(127, 127, 127, 0.08))",
176
+ border: "0",
177
+ borderRadius: 6,
178
+ boxSizing: "border-box",
179
+ fontFamily: "ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace",
180
+ lineHeight: 1.5,
181
+ margin: 0,
182
+ minHeight: 0,
183
+ overflow: "auto",
184
+ overflowWrap: "anywhere",
185
+ padding: 16,
186
+ whiteSpace: "pre-wrap"
187
+ };
188
+ var methodTagColors = {
189
+ DELETE: "red",
190
+ GET: "green",
191
+ PATCH: "purple",
192
+ POST: "gold",
193
+ PUT: "blue"
194
+ };
195
+ var getHttpMethodTagColor = (method) => {
196
+ const normalizedMethod = method == null ? void 0 : method.toUpperCase();
197
+ return normalizedMethod === void 0 ? void 0 : methodTagColors[normalizedMethod];
198
+ };
199
+ var HttpMethodTag = ({ method }) => {
200
+ var _a;
201
+ const normalizedMethod = (_a = method == null ? void 0 : method.toUpperCase()) != null ? _a : "--";
202
+ const color = getHttpMethodTagColor(method);
203
+ return color ? /* @__PURE__ */ jsx(Tag, { color, children: normalizedMethod }) : /* @__PURE__ */ jsx(Tag, { children: normalizedMethod });
204
+ };
205
+ var DebugCodeBlock = ({ onCopy, value }) => {
206
+ const canCopy = value !== void 0 && value !== null;
207
+ return /* @__PURE__ */ jsxs(
208
+ "div",
209
+ {
210
+ style: {
211
+ height: "100%",
212
+ minHeight: 0,
213
+ overflow: "auto",
214
+ position: "relative"
215
+ },
216
+ children: [
217
+ canCopy ? /* @__PURE__ */ jsx(Tooltip, { title: "复制", children: /* @__PURE__ */ jsx(
218
+ Button,
219
+ {
220
+ "aria-label": "复制",
221
+ size: "middle",
222
+ style: {
223
+ paddingInline: 12,
224
+ position: "absolute",
225
+ right: 8,
226
+ top: 8,
227
+ zIndex: 1
228
+ },
229
+ type: "primary",
230
+ onClick: onCopy,
231
+ children: "复制"
232
+ }
233
+ ) }) : null,
234
+ /* @__PURE__ */ jsx(
235
+ "pre",
236
+ {
237
+ style: {
238
+ ...codeBlockStyle,
239
+ paddingRight: canCopy ? 88 : codeBlockStyle.padding
240
+ },
241
+ children: formatDebugValue(value)
242
+ }
243
+ )
244
+ ]
245
+ }
246
+ );
247
+ };
248
+ var DebugErrorListItem = ({ error, index, selected, total }) => {
249
+ var _a, _b;
250
+ const handleRemove = (event) => {
251
+ event.stopPropagation();
252
+ removeDebugError(error.id);
253
+ };
254
+ const handleKeyDown = (event) => {
255
+ if (event.key === "Enter" || event.key === " ") {
256
+ event.preventDefault();
257
+ selectDebugError(error.id);
258
+ }
259
+ };
260
+ return /* @__PURE__ */ jsxs(
261
+ "div",
262
+ {
263
+ "aria-selected": selected,
264
+ role: "button",
265
+ tabIndex: 0,
266
+ title: error.url,
267
+ onClick: () => selectDebugError(error.id),
268
+ onKeyDown: handleKeyDown,
269
+ style: {
270
+ background: selected ? "var(--gz-color-bg-field, rgba(127, 127, 127, 0.12))" : "transparent",
271
+ borderRadius: 6,
272
+ cursor: "pointer",
273
+ display: "flex",
274
+ flexDirection: "column",
275
+ gap: 6,
276
+ margin: "0 8px",
277
+ minWidth: 0,
278
+ padding: "10px 8px",
279
+ position: "relative"
280
+ },
281
+ children: [
282
+ /* @__PURE__ */ jsx(
283
+ "span",
284
+ {
285
+ "aria-hidden": "true",
286
+ style: {
287
+ background: selected ? "var(--gz-color-primary, var(--gz-color-primary-border, rgba(127, 127, 127, 0.24)))" : "transparent",
288
+ borderRadius: 2,
289
+ bottom: 8,
290
+ left: 0,
291
+ pointerEvents: "none",
292
+ position: "absolute",
293
+ top: 8,
294
+ width: 3
295
+ }
296
+ }
297
+ ),
298
+ /* @__PURE__ */ jsxs(
299
+ "div",
300
+ {
301
+ style: {
302
+ alignItems: "center",
303
+ display: "flex",
304
+ gap: 8,
305
+ minWidth: 0
306
+ },
307
+ children: [
308
+ /* @__PURE__ */ jsxs(
309
+ "span",
310
+ {
311
+ style: { color: "var(--gz-color-text-secondary)", flex: "0 0 auto" },
312
+ children: [
313
+ "#",
314
+ total - index
315
+ ]
316
+ }
317
+ ),
318
+ /* @__PURE__ */ jsx("span", { style: { flex: "0 0 58px", minWidth: 58 }, children: /* @__PURE__ */ jsx(HttpMethodTag, { method: error.method }) }),
319
+ /* @__PURE__ */ jsx(
320
+ "span",
321
+ {
322
+ style: {
323
+ display: "block",
324
+ flex: "1 1 auto",
325
+ minWidth: 0,
326
+ overflow: "hidden"
327
+ },
328
+ children: /* @__PURE__ */ jsx(Tooltip, { title: (_a = error.url) != null ? _a : "--", children: /* @__PURE__ */ jsx(
329
+ "code",
330
+ {
331
+ style: {
332
+ color: "var(--gz-color-text-secondary)",
333
+ direction: "rtl",
334
+ display: "block",
335
+ minWidth: 0,
336
+ overflow: "hidden",
337
+ textAlign: "left",
338
+ textOverflow: "ellipsis",
339
+ whiteSpace: "nowrap"
340
+ },
341
+ children: (_b = error.url) != null ? _b : "--"
342
+ }
343
+ ) })
344
+ }
345
+ ),
346
+ /* @__PURE__ */ jsx("span", { style: { flex: "0 0 24px", width: 24 }, children: /* @__PURE__ */ jsx(Tooltip, { title: "关闭此条", children: /* @__PURE__ */ jsx(
347
+ Button,
348
+ {
349
+ "aria-label": "关闭此条",
350
+ size: "small",
351
+ style: { padding: 0, width: 24 },
352
+ type: "text",
353
+ onClick: handleRemove,
354
+ children: "×"
355
+ }
356
+ ) }) })
357
+ ]
358
+ }
359
+ ),
360
+ /* @__PURE__ */ jsx(
361
+ "div",
362
+ {
363
+ style: {
364
+ display: "-webkit-box",
365
+ overflow: "hidden",
366
+ overflowWrap: "anywhere",
367
+ WebkitBoxOrient: "vertical",
368
+ WebkitLineClamp: 2
369
+ },
370
+ children: error.message
371
+ }
372
+ )
373
+ ]
374
+ }
375
+ );
376
+ };
377
+ var debugTabKeys = ["request", "stackTrace", "mdc"];
378
+ var isDebugTabKey = (value) => debugTabKeys.includes(value);
379
+ var DebugErrorDetails = ({
380
+ error
381
+ }) => {
382
+ var _a, _b;
383
+ const [activeTab, setActiveTab] = useState("request");
384
+ const values = {
385
+ mdc: error.debug.mdc,
386
+ request: error.debug.request,
387
+ stackTrace: error.debug.stackTrace
388
+ };
389
+ const handleCopy = async (value) => {
390
+ if (value === void 0 || value === null) {
391
+ return;
392
+ }
393
+ const copied = await copyDebugText(formatDebugValue(value));
394
+ if (copied) {
395
+ showFeedbackSuccessMessage("已复制");
396
+ return;
397
+ }
398
+ showFeedbackErrorMessage("复制失败");
399
+ };
400
+ const handleCopyCompleteResponse = async () => {
401
+ const responseText = formatCompleteDebugResponse(error);
402
+ if (responseText === void 0) {
403
+ return;
404
+ }
405
+ const copied = await copyDebugText(responseText);
406
+ if (copied) {
407
+ showFeedbackSuccessMessage("完整响应信息已复制");
408
+ return;
409
+ }
410
+ showFeedbackErrorMessage("复制失败");
411
+ };
412
+ return /* @__PURE__ */ jsxs(
413
+ "div",
414
+ {
415
+ style: {
416
+ display: "flex",
417
+ flexDirection: "column",
418
+ height: "100%",
419
+ minHeight: 0,
420
+ minWidth: 0
421
+ },
422
+ children: [
423
+ /* @__PURE__ */ jsxs("div", { style: { flex: "0 0 auto", marginBottom: 16, minWidth: 0 }, children: [
424
+ /* @__PURE__ */ jsxs(
425
+ "div",
426
+ {
427
+ style: {
428
+ alignItems: "flex-start",
429
+ display: "flex",
430
+ gap: 8,
431
+ minWidth: 0
432
+ },
433
+ children: [
434
+ /* @__PURE__ */ jsx(
435
+ "strong",
436
+ {
437
+ style: {
438
+ display: "block",
439
+ flex: "1 1 auto",
440
+ fontSize: 16,
441
+ minWidth: 0,
442
+ overflowWrap: "anywhere"
443
+ },
444
+ children: error.message
445
+ }
446
+ ),
447
+ error.responseData === void 0 ? null : /* @__PURE__ */ jsx(Tooltip, { title: "复制完整响应信息", children: /* @__PURE__ */ jsx(
448
+ Button,
449
+ {
450
+ "aria-label": "复制完整响应信息",
451
+ size: "middle",
452
+ style: { flex: "0 0 auto", paddingInline: 12 },
453
+ type: "primary",
454
+ onClick: () => {
455
+ void handleCopyCompleteResponse();
456
+ },
457
+ children: "复制完整响应"
458
+ }
459
+ ) })
460
+ ]
461
+ }
462
+ ),
463
+ /* @__PURE__ */ jsxs(
464
+ "div",
465
+ {
466
+ style: {
467
+ alignItems: "center",
468
+ display: "flex",
469
+ gap: 8,
470
+ marginTop: 8,
471
+ minWidth: 0
472
+ },
473
+ children: [
474
+ /* @__PURE__ */ jsx(HttpMethodTag, { method: error.method }),
475
+ /* @__PURE__ */ jsx(
476
+ "code",
477
+ {
478
+ style: {
479
+ color: "var(--gz-color-text-secondary)",
480
+ display: "block",
481
+ minWidth: 0,
482
+ overflow: "hidden",
483
+ textOverflow: "ellipsis",
484
+ whiteSpace: "nowrap"
485
+ },
486
+ title: (_a = error.url) != null ? _a : "--",
487
+ children: (_b = error.url) != null ? _b : "--"
488
+ }
489
+ )
490
+ ]
491
+ }
492
+ )
493
+ ] }),
494
+ /* @__PURE__ */ jsx(
495
+ Tabs,
496
+ {
497
+ activeKey: activeTab,
498
+ items: [
499
+ {
500
+ children: /* @__PURE__ */ jsx(
501
+ DebugCodeBlock,
502
+ {
503
+ onCopy: () => {
504
+ void handleCopy(values.request);
505
+ },
506
+ value: values.request
507
+ }
508
+ ),
509
+ key: "request",
510
+ label: "Request"
511
+ },
512
+ {
513
+ children: /* @__PURE__ */ jsx(
514
+ DebugCodeBlock,
515
+ {
516
+ onCopy: () => {
517
+ void handleCopy(values.stackTrace);
518
+ },
519
+ value: values.stackTrace
520
+ }
521
+ ),
522
+ key: "stackTrace",
523
+ label: "Stack Trace"
524
+ },
525
+ {
526
+ children: /* @__PURE__ */ jsx(
527
+ DebugCodeBlock,
528
+ {
529
+ onCopy: () => {
530
+ void handleCopy(values.mdc);
531
+ },
532
+ value: values.mdc
533
+ }
534
+ ),
535
+ key: "mdc",
536
+ label: "MDC"
537
+ }
538
+ ],
539
+ style: {
540
+ display: "flex",
541
+ flexDirection: "column",
542
+ height: "100%",
543
+ minHeight: 0
544
+ },
545
+ onChange: (key) => {
546
+ if (isDebugTabKey(key)) {
547
+ setActiveTab(key);
548
+ }
549
+ }
550
+ }
551
+ )
552
+ ]
553
+ }
554
+ );
555
+ };
556
+ var DebugErrorDrawer = () => {
557
+ var _a, _b;
558
+ const hostIdRef = useRef(void 0);
559
+ (_a = hostIdRef.current) != null ? _a : hostIdRef.current = /* @__PURE__ */ Symbol("gz-debug-feedback-drawer-host");
560
+ const hostId = hostIdRef.current;
561
+ const snapshot3 = useSyncExternalStore(
562
+ subscribeDebugDrawer,
563
+ getDebugDrawerSnapshot,
564
+ getDebugDrawerServerSnapshot
565
+ );
566
+ useEffect(() => registerDebugDrawerHost(hostId), [hostId]);
567
+ if (snapshot3.activeHostId !== hostId) {
568
+ return null;
569
+ }
570
+ const selectedError = (_b = snapshot3.errors.find((error) => error.id === snapshot3.selectedErrorId)) != null ? _b : snapshot3.errors[0];
571
+ return /* @__PURE__ */ jsx(
572
+ Drawer,
573
+ {
574
+ open: snapshot3.open,
575
+ placement: "right",
576
+ styles: { body: { overflow: "hidden", padding: 0 } },
577
+ title: `Debug Error (${snapshot3.errors.length})`,
578
+ extra: /* @__PURE__ */ jsx(
579
+ Button,
580
+ {
581
+ disabled: snapshot3.errors.length === 0,
582
+ size: "middle",
583
+ style: { paddingInline: 12 },
584
+ type: "default",
585
+ onClick: clearDebugErrors,
586
+ children: "清空记录"
587
+ }
588
+ ),
589
+ width: "min(calc(100vw - 32px), clamp(720px, 80vw, 1120px))",
590
+ onClose: closeDebugDrawer,
591
+ children: /* @__PURE__ */ jsxs(
592
+ "div",
593
+ {
594
+ style: {
595
+ display: "flex",
596
+ height: "100%",
597
+ minHeight: 0,
598
+ minWidth: 0
599
+ },
600
+ children: [
601
+ /* @__PURE__ */ jsx(
602
+ "aside",
603
+ {
604
+ "aria-label": "Debug Error 列表",
605
+ style: {
606
+ borderRight: "1px solid var(--gz-color-border, rgba(127, 127, 127, 0.24))",
607
+ flex: "0 1 clamp(220px, 24vw, 320px)",
608
+ minHeight: 0,
609
+ overflowY: "auto",
610
+ padding: "6px 0",
611
+ width: "clamp(220px, 24vw, 320px)"
612
+ },
613
+ children: snapshot3.errors.length > 0 ? snapshot3.errors.map((error, index) => /* @__PURE__ */ jsx(
614
+ DebugErrorListItem,
615
+ {
616
+ error,
617
+ index,
618
+ selected: error.id === (selectedError == null ? void 0 : selectedError.id),
619
+ total: snapshot3.errors.length
620
+ },
621
+ error.id
622
+ )) : /* @__PURE__ */ jsx(
623
+ "div",
624
+ {
625
+ style: {
626
+ color: "var(--gz-color-text-secondary)",
627
+ padding: 16,
628
+ textAlign: "center"
629
+ },
630
+ children: "暂无 Debug Error"
631
+ }
632
+ )
633
+ }
634
+ ),
635
+ /* @__PURE__ */ jsx(
636
+ "main",
637
+ {
638
+ "aria-label": "Debug Error 详情",
639
+ style: {
640
+ flex: "1 1 auto",
641
+ minHeight: 0,
642
+ minWidth: 0,
643
+ overflow: "hidden",
644
+ padding: 20
645
+ },
646
+ children: selectedError ? /* @__PURE__ */ jsx(DebugErrorDetails, { error: selectedError }, selectedError.id) : /* @__PURE__ */ jsx("div", { style: { color: "var(--gz-color-text-secondary)" }, children: "暂无 Debug Error" })
647
+ }
648
+ )
649
+ ]
650
+ }
651
+ )
652
+ }
653
+ );
654
+ };
655
+ DebugErrorDrawer.displayName = "DebugErrorDrawer";
656
+ var DebugErrorDrawer_default = DebugErrorDrawer;
657
+ var GzDebugFeedbackProvider = ({ children }) => {
658
+ var _a;
659
+ const hostIdRef = useRef(void 0);
660
+ (_a = hostIdRef.current) != null ? _a : hostIdRef.current = /* @__PURE__ */ Symbol("gz-debug-feedback-host");
661
+ const hostId = hostIdRef.current;
662
+ const [messageApi, messageContextHolder] = message.useMessage();
663
+ const showErrorMessage = useCallback(
664
+ (content) => {
665
+ void messageApi.error(content);
666
+ },
667
+ [messageApi]
668
+ );
669
+ const showSuccessMessage = useCallback(
670
+ (content) => {
671
+ void messageApi.success(content);
672
+ },
673
+ [messageApi]
674
+ );
675
+ useEffect(
676
+ () => registerFeedbackHost(hostId, { showErrorMessage, showSuccessMessage }),
677
+ [hostId, showErrorMessage, showSuccessMessage]
678
+ );
679
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
680
+ messageContextHolder,
681
+ /* @__PURE__ */ jsx(DebugErrorDrawer_default, {}),
682
+ children
683
+ ] });
684
+ };
685
+ GzDebugFeedbackProvider.displayName = "GzDebugFeedbackProvider";
686
+ var GzDebugFeedbackProvider_default = GzDebugFeedbackProvider;
687
+
688
+ // src/fetch/feedback/report-debug-error.ts
689
+ var isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
690
+ var getDebugInfo = (value) => isRecord(value) ? value : void 0;
691
+ var getResponseDebugInfo = (responseData) => isRecord(responseData) ? getDebugInfo(responseData.debug) : void 0;
692
+ var isDebugEnabled = () => {
693
+ if (typeof window === "undefined") {
694
+ return false;
695
+ }
696
+ try {
697
+ if (new URLSearchParams(window.location.search).get("showDebug") === "1") {
698
+ return true;
699
+ }
700
+ } catch (e) {
701
+ }
702
+ try {
703
+ return window.localStorage.getItem("showDebug") === "1";
704
+ } catch (e) {
705
+ return false;
706
+ }
707
+ };
708
+ var reportDebugError = (input) => {
709
+ const debug = getDebugInfo(input.debug);
710
+ if (debug === void 0 || !isDebugEnabled()) {
711
+ return false;
712
+ }
713
+ return appendDebugError({ ...input, debug });
106
714
  };
107
715
  var GzFetchError = class extends Error {
108
716
  constructor(message3, options) {
@@ -165,28 +773,6 @@ function toGzFetchError(error) {
165
773
  originalError: error
166
774
  });
167
775
  }
168
- var feedbackHosts = /* @__PURE__ */ new Map();
169
- var activeHostId;
170
- var registerFeedbackHost = (hostId, host) => {
171
- feedbackHosts.set(hostId, host);
172
- activeHostId != null ? activeHostId : activeHostId = hostId;
173
- return () => {
174
- feedbackHosts.delete(hostId);
175
- if (activeHostId !== hostId) {
176
- return;
177
- }
178
- const nextHost = feedbackHosts.keys().next();
179
- activeHostId = nextHost.done ? void 0 : nextHost.value;
180
- };
181
- };
182
- var showFetchErrorMessage = (content) => {
183
- const activeHost = activeHostId === void 0 ? void 0 : feedbackHosts.get(activeHostId);
184
- if (activeHost) {
185
- activeHost.showErrorMessage(content);
186
- return;
187
- }
188
- message.error(content);
189
- };
190
776
 
191
777
  // src/fetch/middleware.ts
192
778
  async function runRequestMiddlewares(initialConfig, middlewares) {
@@ -379,15 +965,15 @@ function createGzFetch(options = {}) {
379
965
  await runErrorMiddlewares(error, { config: currentConfig }, middlewares);
380
966
  const shouldShowError = (_a2 = currentConfig.showErrorMessage) != null ? _a2 : showInstanceErrorMessage;
381
967
  if (shouldShowError && error.type !== "CANCELED_ERROR" && !shouldHandleUnauthorized) {
382
- const debugInfo = error.type === "HTTP_ERROR" && error.status !== 401 && isDebugEnabled() ? getResponseDebugInfo(error.responseData) : void 0;
383
- const didShowDebugError = debugInfo === void 0 ? false : showFetchDebugError({
384
- debug: debugInfo,
968
+ const didShowDebugError = error.type === "HTTP_ERROR" && error.status !== 401 ? reportDebugError({
969
+ debug: getResponseDebugInfo(error.responseData),
385
970
  message: error.message,
386
971
  method: currentConfig.method,
972
+ responseData: error.responseData,
387
973
  url: currentConfig.url
388
- });
974
+ }) : false;
389
975
  if (!didShowDebugError) {
390
- showFetchErrorMessage(error.message);
976
+ showFeedbackErrorMessage(error.message);
391
977
  }
392
978
  }
393
979
  throw error;
@@ -423,122 +1009,7 @@ async function gzFetch(config) {
423
1009
  }
424
1010
  return defaultClient(config);
425
1011
  }
426
- var EMPTY_DEBUG_VALUE = "(无)";
427
- var formatDebugValue = (value) => {
428
- if (typeof value === "string") {
429
- return value;
430
- }
431
- if (value === void 0 || value === null) {
432
- return EMPTY_DEBUG_VALUE;
433
- }
434
- try {
435
- const serialized = JSON.stringify(value, null, 2);
436
- return serialized != null ? serialized : "(无法序列化)";
437
- } catch (e) {
438
- return "(无法序列化)";
439
- }
440
- };
441
- var codeStyle = {
442
- background: "rgba(127, 127, 127, 0.08)",
443
- borderRadius: 6,
444
- margin: "8px 0 0",
445
- overflowWrap: "anywhere",
446
- overflowX: "auto",
447
- padding: 12,
448
- whiteSpace: "pre-wrap"
449
- };
450
- var DebugSection = ({
451
- title,
452
- value
453
- }) => /* @__PURE__ */ jsxs("div", { style: { marginTop: 16 }, children: [
454
- /* @__PURE__ */ jsx("strong", { children: title }),
455
- /* @__PURE__ */ jsx("pre", { style: codeStyle, children: formatDebugValue(value) })
456
- ] });
457
- var DebugError = ({ error }) => /* @__PURE__ */ jsxs(
458
- "section",
459
- {
460
- style: {
461
- border: "1px solid rgba(127, 127, 127, 0.24)",
462
- borderRadius: 8,
463
- padding: 16
464
- },
465
- children: [
466
- /* @__PURE__ */ jsxs(
467
- "div",
468
- {
469
- style: {
470
- alignItems: "flex-start",
471
- display: "flex",
472
- gap: 12,
473
- justifyContent: "space-between"
474
- },
475
- children: [
476
- /* @__PURE__ */ jsxs("div", { style: { minWidth: 0 }, children: [
477
- /* @__PURE__ */ jsx("strong", { children: error.message }),
478
- error.method && error.url ? /* @__PURE__ */ jsx("div", { style: { marginTop: 6, overflowWrap: "anywhere" }, children: /* @__PURE__ */ jsxs("code", { children: [
479
- error.method,
480
- " ",
481
- error.url
482
- ] }) }) : null
483
- ] }),
484
- /* @__PURE__ */ jsx(
485
- Button,
486
- {
487
- danger: true,
488
- size: "small",
489
- type: "text",
490
- onClick: () => removeDebugError(error.id),
491
- children: "删除"
492
- }
493
- )
494
- ]
495
- }
496
- ),
497
- /* @__PURE__ */ jsx(DebugSection, { title: "Request", value: error.debug.request }),
498
- /* @__PURE__ */ jsx(DebugSection, { title: "Stack Trace", value: error.debug.stackTrace }),
499
- /* @__PURE__ */ jsx(DebugSection, { title: "MDC", value: error.debug.mdc })
500
- ]
501
- }
502
- );
503
- var GzFetchDebugDrawer = () => {
504
- var _a;
505
- const hostIdRef = useRef(void 0);
506
- (_a = hostIdRef.current) != null ? _a : hostIdRef.current = /* @__PURE__ */ Symbol("gz-fetch-debug-drawer-host");
507
- const hostId = hostIdRef.current;
508
- const snapshot3 = useSyncExternalStore(
509
- subscribeDebugDrawer,
510
- getDebugDrawerSnapshot,
511
- getDebugDrawerServerSnapshot
512
- );
513
- useEffect(() => registerDebugDrawerHost(hostId), [hostId]);
514
- if (snapshot3.activeHostId !== hostId) {
515
- return null;
516
- }
517
- return /* @__PURE__ */ jsx(
518
- Drawer,
519
- {
520
- extra: /* @__PURE__ */ jsx(
521
- Button,
522
- {
523
- disabled: snapshot3.errors.length === 0,
524
- size: "small",
525
- onClick: clearDebugErrors,
526
- children: "全部清除"
527
- }
528
- ),
529
- open: snapshot3.open,
530
- placement: "right",
531
- styles: { body: { overflowY: "auto" } },
532
- title: `Debug Error (${snapshot3.errors.length})`,
533
- width: "min(760px, 100vw)",
534
- onClose: closeDebugDrawer,
535
- children: /* @__PURE__ */ jsx("div", { style: { display: "grid", gap: 16 }, children: snapshot3.errors.map((error) => /* @__PURE__ */ jsx(DebugError, { error }, error.id)) })
536
- }
537
- );
538
- };
539
- GzFetchDebugDrawer.displayName = "GzFetchDebugDrawer";
540
- var GzFetchDebugDrawer_default = GzFetchDebugDrawer;
541
- var GzFetchUnauthorizedModal = () => {
1012
+ var GzFetchUnauthorizedModalContent = () => {
542
1013
  var _a, _b, _c;
543
1014
  const hostIdRef = useRef(void 0);
544
1015
  (_a = hostIdRef.current) != null ? _a : hostIdRef.current = /* @__PURE__ */ Symbol("gz-fetch-unauthorized-modal-host");
@@ -573,32 +1044,16 @@ var GzFetchUnauthorizedModal = () => {
573
1044
  }
574
1045
  );
575
1046
  };
576
- GzFetchUnauthorizedModal.displayName = "GzFetchUnauthorizedModal";
577
- var GzFetchUnauthorizedModal_default = GzFetchUnauthorizedModal;
578
- var GzFetchFeedbackProvider = ({ children }) => {
579
- var _a;
580
- const hostIdRef = useRef(void 0);
581
- (_a = hostIdRef.current) != null ? _a : hostIdRef.current = /* @__PURE__ */ Symbol("gz-fetch-feedback-host");
582
- const hostId = hostIdRef.current;
583
- const [messageApi, messageContextHolder] = message.useMessage();
584
- const showErrorMessage = useCallback(
585
- (content) => {
586
- void messageApi.error(content);
587
- },
588
- [messageApi]
589
- );
590
- useEffect(
591
- () => registerFeedbackHost(hostId, { showErrorMessage }),
592
- [hostId, showErrorMessage]
593
- );
594
- return /* @__PURE__ */ jsxs(Fragment, { children: [
595
- messageContextHolder,
596
- /* @__PURE__ */ jsx(GzFetchDebugDrawer_default, {}),
597
- /* @__PURE__ */ jsx(GzFetchUnauthorizedModal_default, {}),
598
- children
599
- ] });
600
- };
1047
+ GzFetchUnauthorizedModalContent.displayName = "GzFetchUnauthorizedModalContent";
1048
+ var GzFetchUnauthorizedModalContent_default = GzFetchUnauthorizedModalContent;
1049
+ var GzFetchFeedbackProvider = ({ children }) => /* @__PURE__ */ jsxs(GzDebugFeedbackProvider_default, { children: [
1050
+ /* @__PURE__ */ jsx(GzFetchUnauthorizedModalContent_default, {}),
1051
+ children
1052
+ ] });
601
1053
  GzFetchFeedbackProvider.displayName = "GzFetchFeedbackProvider";
602
1054
  var GzFetchFeedbackProvider_default = GzFetchFeedbackProvider;
1055
+ var GzFetchUnauthorizedModal = () => /* @__PURE__ */ jsx(GzFetchUnauthorizedModalContent_default, {});
1056
+ GzFetchUnauthorizedModal.displayName = "GzFetchUnauthorizedModal";
1057
+ var GzFetchUnauthorizedModal_default = GzFetchUnauthorizedModal;
603
1058
 
604
- export { GzFetchError, GzFetchFeedbackProvider_default as GzFetchFeedbackProvider, GzFetchUnauthorizedModal_default as GzFetchUnauthorizedModal, configureGzFetch, createGzFetch, getResponseMessage, gzFetch, toGzFetchError };
1059
+ export { GzDebugFeedbackProvider_default as GzDebugFeedbackProvider, GzFetchError, GzFetchFeedbackProvider_default as GzFetchFeedbackProvider, GzFetchUnauthorizedModal_default as GzFetchUnauthorizedModal, configureGzFetch, createGzFetch, getResponseMessage, gzFetch, reportDebugError, toGzFetchError };