@gz-fronted/gz-pc 0.1.3-beta.1 → 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.
- package/README.md +52 -5
- package/dist/fetch/index.cjs +478 -443
- package/dist/fetch/index.d.cts +23 -1
- package/dist/fetch/index.d.ts +23 -1
- package/dist/fetch/index.js +480 -447
- package/package.json +99 -99
package/dist/fetch/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import axios, { AxiosError, AxiosHeaders } from 'axios';
|
|
2
|
-
import {
|
|
3
|
-
import { useRef,
|
|
4
|
-
import {
|
|
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: [],
|
|
@@ -25,29 +25,6 @@ var setSnapshot = (nextSnapshot) => {
|
|
|
25
25
|
snapshot = nextSnapshot;
|
|
26
26
|
emitChange();
|
|
27
27
|
};
|
|
28
|
-
var isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
29
|
-
var getResponseDebugInfo = (responseData) => {
|
|
30
|
-
if (!isRecord(responseData) || !isRecord(responseData.debug)) {
|
|
31
|
-
return void 0;
|
|
32
|
-
}
|
|
33
|
-
return responseData.debug;
|
|
34
|
-
};
|
|
35
|
-
var isDebugEnabled = () => {
|
|
36
|
-
if (typeof window === "undefined") {
|
|
37
|
-
return false;
|
|
38
|
-
}
|
|
39
|
-
try {
|
|
40
|
-
if (new URLSearchParams(window.location.search).get("showDebug") === "1") {
|
|
41
|
-
return true;
|
|
42
|
-
}
|
|
43
|
-
} catch (e) {
|
|
44
|
-
}
|
|
45
|
-
try {
|
|
46
|
-
return window.localStorage.getItem("showDebug") === "1";
|
|
47
|
-
} catch (e) {
|
|
48
|
-
return false;
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
28
|
var subscribeDebugDrawer = (listener) => {
|
|
52
29
|
listeners.add(listener);
|
|
53
30
|
return () => listeners.delete(listener);
|
|
@@ -71,7 +48,7 @@ var registerDebugDrawerHost = (hostId) => {
|
|
|
71
48
|
});
|
|
72
49
|
};
|
|
73
50
|
};
|
|
74
|
-
var
|
|
51
|
+
var appendDebugError = (input) => {
|
|
75
52
|
if (snapshot.activeHostId === void 0) {
|
|
76
53
|
return false;
|
|
77
54
|
}
|
|
@@ -118,67 +95,6 @@ var clearDebugErrors = () => {
|
|
|
118
95
|
setSnapshot({ ...snapshot, errors: [], selectedErrorId: void 0 });
|
|
119
96
|
}
|
|
120
97
|
};
|
|
121
|
-
var GzFetchError = class extends Error {
|
|
122
|
-
constructor(message3, options) {
|
|
123
|
-
super(message3);
|
|
124
|
-
this.name = "GzFetchError";
|
|
125
|
-
this.type = options.type;
|
|
126
|
-
this.status = options.status;
|
|
127
|
-
this.responseData = options.responseData;
|
|
128
|
-
this.originalError = options.originalError;
|
|
129
|
-
}
|
|
130
|
-
};
|
|
131
|
-
function getResponseMessage(data) {
|
|
132
|
-
if (typeof data !== "object" || data === null || !("msg" in data)) {
|
|
133
|
-
return void 0;
|
|
134
|
-
}
|
|
135
|
-
const { msg } = data;
|
|
136
|
-
return typeof msg === "string" && msg.length > 0 ? msg : void 0;
|
|
137
|
-
}
|
|
138
|
-
var DEFAULT_HTTP_ERROR_MESSAGE = "请求失败,请稍后重试";
|
|
139
|
-
var DEFAULT_NETWORK_ERROR_MESSAGE = "网络异常,请检查网络连接";
|
|
140
|
-
var DEFAULT_TIMEOUT_ERROR_MESSAGE = "请求超时,请稍后重试";
|
|
141
|
-
var DEFAULT_CANCELED_ERROR_MESSAGE = "请求已取消";
|
|
142
|
-
var DEFAULT_UNKNOWN_ERROR_MESSAGE = "请求失败,请稍后重试";
|
|
143
|
-
function toGzFetchError(error) {
|
|
144
|
-
var _a;
|
|
145
|
-
if (error instanceof GzFetchError) {
|
|
146
|
-
return error;
|
|
147
|
-
}
|
|
148
|
-
if (axios.isCancel(error)) {
|
|
149
|
-
return new GzFetchError(DEFAULT_CANCELED_ERROR_MESSAGE, {
|
|
150
|
-
type: "CANCELED_ERROR",
|
|
151
|
-
originalError: error
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
if (axios.isAxiosError(error)) {
|
|
155
|
-
if (error.code === AxiosError.ECONNABORTED || error.code === AxiosError.ETIMEDOUT) {
|
|
156
|
-
return new GzFetchError(DEFAULT_TIMEOUT_ERROR_MESSAGE, {
|
|
157
|
-
type: "TIMEOUT_ERROR",
|
|
158
|
-
originalError: error
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
if (error.response) {
|
|
162
|
-
return new GzFetchError(
|
|
163
|
-
(_a = getResponseMessage(error.response.data)) != null ? _a : DEFAULT_HTTP_ERROR_MESSAGE,
|
|
164
|
-
{
|
|
165
|
-
type: "HTTP_ERROR",
|
|
166
|
-
status: error.response.status,
|
|
167
|
-
responseData: error.response.data,
|
|
168
|
-
originalError: error
|
|
169
|
-
}
|
|
170
|
-
);
|
|
171
|
-
}
|
|
172
|
-
return new GzFetchError(DEFAULT_NETWORK_ERROR_MESSAGE, {
|
|
173
|
-
type: "NETWORK_ERROR",
|
|
174
|
-
originalError: error
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
return new GzFetchError(DEFAULT_UNKNOWN_ERROR_MESSAGE, {
|
|
178
|
-
type: "UNKNOWN_ERROR",
|
|
179
|
-
originalError: error
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
98
|
var feedbackHosts = /* @__PURE__ */ new Map();
|
|
183
99
|
var activeHostId;
|
|
184
100
|
var registerFeedbackHost = (hostId, host) => {
|
|
@@ -193,7 +109,7 @@ var registerFeedbackHost = (hostId, host) => {
|
|
|
193
109
|
activeHostId = nextHost.done ? void 0 : nextHost.value;
|
|
194
110
|
};
|
|
195
111
|
};
|
|
196
|
-
var
|
|
112
|
+
var showFeedbackErrorMessage = (content) => {
|
|
197
113
|
const activeHost = activeHostId === void 0 ? void 0 : feedbackHosts.get(activeHostId);
|
|
198
114
|
if (activeHost) {
|
|
199
115
|
activeHost.showErrorMessage(content);
|
|
@@ -201,7 +117,7 @@ var showFetchErrorMessage = (content) => {
|
|
|
201
117
|
}
|
|
202
118
|
message.error(content);
|
|
203
119
|
};
|
|
204
|
-
var
|
|
120
|
+
var showFeedbackSuccessMessage = (content) => {
|
|
205
121
|
const activeHost = activeHostId === void 0 ? void 0 : feedbackHosts.get(activeHostId);
|
|
206
122
|
if (activeHost == null ? void 0 : activeHost.showSuccessMessage) {
|
|
207
123
|
activeHost.showSuccessMessage(content);
|
|
@@ -209,309 +125,72 @@ var showFetchSuccessMessage = (content) => {
|
|
|
209
125
|
}
|
|
210
126
|
message.success(content);
|
|
211
127
|
};
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
for (const middleware of middlewares) {
|
|
217
|
-
if (middleware.onRequest) {
|
|
218
|
-
config = await middleware.onRequest(config);
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
return config;
|
|
222
|
-
}
|
|
223
|
-
async function runResponseMiddlewares(initialData, context, middlewares) {
|
|
224
|
-
let data = initialData;
|
|
225
|
-
for (const middleware of middlewares) {
|
|
226
|
-
if (middleware.onResponse) {
|
|
227
|
-
data = await middleware.onResponse(data, context);
|
|
228
|
-
}
|
|
128
|
+
var EMPTY_DEBUG_VALUE = "(无)";
|
|
129
|
+
var formatDebugValue = (value) => {
|
|
130
|
+
if (typeof value === "string") {
|
|
131
|
+
return value;
|
|
229
132
|
}
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
async function runErrorMiddlewares(error, context, middlewares) {
|
|
233
|
-
var _a;
|
|
234
|
-
for (const middleware of middlewares) {
|
|
235
|
-
await ((_a = middleware.onError) == null ? void 0 : _a.call(middleware, error, context));
|
|
133
|
+
if (value === void 0 || value === null) {
|
|
134
|
+
return EMPTY_DEBUG_VALUE;
|
|
236
135
|
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
var DEFAULT_UNAUTHORIZED_MODAL_MESSAGE = "当前登录状态已失效,请重新登录。";
|
|
243
|
-
var IDLE_SNAPSHOT = { phase: "idle" };
|
|
244
|
-
var snapshot2 = IDLE_SNAPSHOT;
|
|
245
|
-
var listeners2 = /* @__PURE__ */ new Set();
|
|
246
|
-
var modalHosts = /* @__PURE__ */ new Set();
|
|
247
|
-
var emitChange2 = () => {
|
|
248
|
-
for (const listener of listeners2) {
|
|
249
|
-
listener();
|
|
136
|
+
try {
|
|
137
|
+
const serialized = JSON.stringify(value, null, 2);
|
|
138
|
+
return serialized != null ? serialized : "(无法序列化)";
|
|
139
|
+
} catch (e) {
|
|
140
|
+
return "(无法序列化)";
|
|
250
141
|
}
|
|
251
142
|
};
|
|
252
|
-
var
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
enabled: (_a = options == null ? void 0 : options.enabled) != null ? _a : false,
|
|
260
|
-
loginUrl: (_b = options == null ? void 0 : options.loginUrl) != null ? _b : DEFAULT_UNAUTHORIZED_LOGIN_URL,
|
|
261
|
-
modalTitle: (_c = options == null ? void 0 : options.modalTitle) != null ? _c : DEFAULT_UNAUTHORIZED_MODAL_TITLE,
|
|
262
|
-
modalMessage: (_d = options == null ? void 0 : options.modalMessage) != null ? _d : DEFAULT_UNAUTHORIZED_MODAL_MESSAGE,
|
|
263
|
-
...(options == null ? void 0 : options.onUnauthorized) === void 0 ? {} : { onUnauthorized: options.onUnauthorized }
|
|
264
|
-
};
|
|
265
|
-
};
|
|
266
|
-
var getUnauthorizedSnapshot = () => snapshot2;
|
|
267
|
-
var getUnauthorizedServerSnapshot = () => IDLE_SNAPSHOT;
|
|
268
|
-
var subscribeUnauthorized = (listener) => {
|
|
269
|
-
listeners2.add(listener);
|
|
270
|
-
return () => listeners2.delete(listener);
|
|
271
|
-
};
|
|
272
|
-
var registerUnauthorizedModalHost = (hostId) => {
|
|
273
|
-
modalHosts.add(hostId);
|
|
274
|
-
if (snapshot2.activeHostId === void 0) {
|
|
275
|
-
setSnapshot2({ ...snapshot2, activeHostId: hostId });
|
|
276
|
-
}
|
|
277
|
-
return () => {
|
|
278
|
-
modalHosts.delete(hostId);
|
|
279
|
-
if (snapshot2.activeHostId !== hostId) {
|
|
280
|
-
return;
|
|
281
|
-
}
|
|
282
|
-
const nextHost = modalHosts.values().next();
|
|
283
|
-
const nextHostId = nextHost.done ? void 0 : nextHost.value;
|
|
284
|
-
if (snapshot2.phase === "closing") {
|
|
285
|
-
setSnapshot2(
|
|
286
|
-
nextHostId === void 0 ? IDLE_SNAPSHOT : { activeHostId: nextHostId, phase: "idle" }
|
|
287
|
-
);
|
|
288
|
-
return;
|
|
289
|
-
}
|
|
290
|
-
if (nextHostId === void 0) {
|
|
291
|
-
setSnapshot2(
|
|
292
|
-
snapshot2.options === void 0 ? { phase: snapshot2.phase } : { options: snapshot2.options, phase: snapshot2.phase }
|
|
293
|
-
);
|
|
294
|
-
return;
|
|
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;
|
|
295
150
|
}
|
|
296
|
-
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
var openUnauthorizedModal = (options) => {
|
|
300
|
-
if (!options.enabled || snapshot2.phase !== "idle") {
|
|
151
|
+
} catch (e) {
|
|
152
|
+
}
|
|
153
|
+
if (typeof document === "undefined" || !document.body || typeof document.execCommand !== "function") {
|
|
301
154
|
return false;
|
|
302
155
|
}
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
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);
|
|
313
171
|
}
|
|
314
|
-
|
|
315
|
-
setSnapshot2({ ...snapshot2, phase: "closing" });
|
|
316
|
-
return options;
|
|
172
|
+
return copied;
|
|
317
173
|
};
|
|
318
|
-
var
|
|
319
|
-
var
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
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"
|
|
327
187
|
};
|
|
328
|
-
var
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
};
|
|
335
|
-
|
|
336
|
-
// src/fetch/client.ts
|
|
337
|
-
var DEFAULT_TIMEOUT = 15e3;
|
|
338
|
-
var DEFAULT_AUTH_HEADER = "Authorization";
|
|
339
|
-
var DEFAULT_VALIDATE_STATUS = (status) => status >= 200 && status < 300;
|
|
340
|
-
function toHeaderRecord(headers) {
|
|
341
|
-
const result = {};
|
|
342
|
-
for (const [name, value] of Object.entries(headers)) {
|
|
343
|
-
if (value !== void 0) {
|
|
344
|
-
result[name] = String(value);
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
return result;
|
|
348
|
-
}
|
|
349
|
-
function createGzFetch(options = {}) {
|
|
350
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
351
|
-
const middlewares = (_a = options.middlewares) != null ? _a : [];
|
|
352
|
-
const showInstanceErrorMessage = (_b = options.showErrorMessage) != null ? _b : true;
|
|
353
|
-
const headerName = (_d = (_c = options.auth) == null ? void 0 : _c.headerName) != null ? _d : DEFAULT_AUTH_HEADER;
|
|
354
|
-
const formatToken = (_f = (_e = options.auth) == null ? void 0 : _e.formatToken) != null ? _f : ((token) => `Bearer ${token}`);
|
|
355
|
-
const unauthorizedOptions = resolveUnauthorizedOptions(options.unauthorized);
|
|
356
|
-
const instance = axios.create({
|
|
357
|
-
...options.baseURL === void 0 ? {} : { baseURL: options.baseURL },
|
|
358
|
-
timeout: (_g = options.timeout) != null ? _g : DEFAULT_TIMEOUT,
|
|
359
|
-
validateStatus: (_h = options.validateStatus) != null ? _h : DEFAULT_VALIDATE_STATUS
|
|
360
|
-
});
|
|
361
|
-
async function request(initialConfig) {
|
|
362
|
-
var _a2;
|
|
363
|
-
let currentConfig = initialConfig;
|
|
364
|
-
try {
|
|
365
|
-
const headers = AxiosHeaders.from(initialConfig.headers);
|
|
366
|
-
if (!initialConfig.skipAuth && options.getToken) {
|
|
367
|
-
const token = await options.getToken();
|
|
368
|
-
if (token) {
|
|
369
|
-
headers.set(headerName, formatToken(token));
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
currentConfig = await runRequestMiddlewares(
|
|
373
|
-
{ ...initialConfig, headers },
|
|
374
|
-
middlewares
|
|
375
|
-
);
|
|
376
|
-
const usesRequestBody = currentConfig.method === "POST" || currentConfig.method === "PUT" || currentConfig.method === "DELETE" && !currentConfig.paramsInUrl;
|
|
377
|
-
const axiosConfig = {
|
|
378
|
-
url: currentConfig.url,
|
|
379
|
-
method: currentConfig.method,
|
|
380
|
-
...!usesRequestBody || currentConfig.params === void 0 ? {} : { data: currentConfig.params },
|
|
381
|
-
...currentConfig.headers === void 0 ? {} : { headers: currentConfig.headers },
|
|
382
|
-
...usesRequestBody || currentConfig.params === void 0 ? {} : { params: currentConfig.params },
|
|
383
|
-
...currentConfig.timeout === void 0 ? {} : { timeout: currentConfig.timeout },
|
|
384
|
-
...currentConfig.responseType === void 0 ? {} : { responseType: currentConfig.responseType },
|
|
385
|
-
...currentConfig.signal === void 0 ? {} : { signal: currentConfig.signal },
|
|
386
|
-
...currentConfig.withCredentials === void 0 ? {} : { withCredentials: currentConfig.withCredentials }
|
|
387
|
-
};
|
|
388
|
-
const response = await instance.request(axiosConfig);
|
|
389
|
-
const context = {
|
|
390
|
-
config: currentConfig,
|
|
391
|
-
status: response.status,
|
|
392
|
-
headers: toHeaderRecord(response.headers)
|
|
393
|
-
};
|
|
394
|
-
return await runResponseMiddlewares(response.data, context, middlewares);
|
|
395
|
-
} catch (originalError) {
|
|
396
|
-
const error = toGzFetchError(originalError);
|
|
397
|
-
const shouldHandleUnauthorized = unauthorizedOptions.enabled && error.type === "HTTP_ERROR" && error.status === 401;
|
|
398
|
-
if (shouldHandleUnauthorized) {
|
|
399
|
-
openUnauthorizedModal(unauthorizedOptions);
|
|
400
|
-
}
|
|
401
|
-
await runErrorMiddlewares(error, { config: currentConfig }, middlewares);
|
|
402
|
-
const shouldShowError = (_a2 = currentConfig.showErrorMessage) != null ? _a2 : showInstanceErrorMessage;
|
|
403
|
-
if (shouldShowError && error.type !== "CANCELED_ERROR" && !shouldHandleUnauthorized) {
|
|
404
|
-
const debugInfo = error.type === "HTTP_ERROR" && error.status !== 401 && isDebugEnabled() ? getResponseDebugInfo(error.responseData) : void 0;
|
|
405
|
-
const didShowDebugError = debugInfo === void 0 ? false : showFetchDebugError({
|
|
406
|
-
debug: debugInfo,
|
|
407
|
-
message: error.message,
|
|
408
|
-
method: currentConfig.method,
|
|
409
|
-
responseData: error.responseData,
|
|
410
|
-
url: currentConfig.url
|
|
411
|
-
});
|
|
412
|
-
if (!didShowDebugError) {
|
|
413
|
-
showFetchErrorMessage(error.message);
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
throw error;
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
const gzFetch2 = ((config) => request(config));
|
|
420
|
-
gzFetch2.get = (url, config = {}) => gzFetch2({ ...config, url, method: "GET" });
|
|
421
|
-
gzFetch2.post = (url, params, config = {}) => gzFetch2({
|
|
422
|
-
...config,
|
|
423
|
-
url,
|
|
424
|
-
method: "POST",
|
|
425
|
-
...params === void 0 ? {} : { params }
|
|
426
|
-
});
|
|
427
|
-
gzFetch2.put = (url, params, config = {}) => gzFetch2({
|
|
428
|
-
...config,
|
|
429
|
-
url,
|
|
430
|
-
method: "PUT",
|
|
431
|
-
...params === void 0 ? {} : { params }
|
|
432
|
-
});
|
|
433
|
-
gzFetch2.delete = (url, config = {}) => gzFetch2({ ...config, url, method: "DELETE" });
|
|
434
|
-
return gzFetch2;
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
// src/fetch/default-client.ts
|
|
438
|
-
var NOT_CONFIGURED_MESSAGE = "gzFetch 尚未配置,请先在应用初始化时调用 configureGzFetch。";
|
|
439
|
-
var defaultClient;
|
|
440
|
-
function configureGzFetch(options) {
|
|
441
|
-
defaultClient = createGzFetch(options);
|
|
442
|
-
}
|
|
443
|
-
async function gzFetch(config) {
|
|
444
|
-
if (!defaultClient) {
|
|
445
|
-
throw new Error(NOT_CONFIGURED_MESSAGE);
|
|
446
|
-
}
|
|
447
|
-
return defaultClient(config);
|
|
448
|
-
}
|
|
449
|
-
var EMPTY_DEBUG_VALUE = "(无)";
|
|
450
|
-
var formatDebugValue = (value) => {
|
|
451
|
-
if (typeof value === "string") {
|
|
452
|
-
return value;
|
|
453
|
-
}
|
|
454
|
-
if (value === void 0 || value === null) {
|
|
455
|
-
return EMPTY_DEBUG_VALUE;
|
|
456
|
-
}
|
|
457
|
-
try {
|
|
458
|
-
const serialized = JSON.stringify(value, null, 2);
|
|
459
|
-
return serialized != null ? serialized : "(无法序列化)";
|
|
460
|
-
} catch (e) {
|
|
461
|
-
return "(无法序列化)";
|
|
462
|
-
}
|
|
463
|
-
};
|
|
464
|
-
var formatCompleteDebugResponse = (error) => formatDebugValue(error.responseData);
|
|
465
|
-
var copyDebugText = async (text) => {
|
|
466
|
-
var _a;
|
|
467
|
-
try {
|
|
468
|
-
if (typeof navigator !== "undefined" && ((_a = navigator.clipboard) == null ? void 0 : _a.writeText)) {
|
|
469
|
-
await navigator.clipboard.writeText(text);
|
|
470
|
-
return true;
|
|
471
|
-
}
|
|
472
|
-
} catch (e) {
|
|
473
|
-
}
|
|
474
|
-
if (typeof document === "undefined" || !document.body || typeof document.execCommand !== "function") {
|
|
475
|
-
return false;
|
|
476
|
-
}
|
|
477
|
-
const textarea = document.createElement("textarea");
|
|
478
|
-
textarea.value = text;
|
|
479
|
-
textarea.setAttribute("readonly", "");
|
|
480
|
-
textarea.style.position = "fixed";
|
|
481
|
-
textarea.style.opacity = "0";
|
|
482
|
-
document.body.appendChild(textarea);
|
|
483
|
-
let copied = false;
|
|
484
|
-
try {
|
|
485
|
-
textarea.focus();
|
|
486
|
-
textarea.select();
|
|
487
|
-
copied = document.execCommand("copy");
|
|
488
|
-
} catch (e) {
|
|
489
|
-
copied = false;
|
|
490
|
-
} finally {
|
|
491
|
-
document.body.removeChild(textarea);
|
|
492
|
-
}
|
|
493
|
-
return copied;
|
|
494
|
-
};
|
|
495
|
-
var codeBlockStyle = {
|
|
496
|
-
background: "var(--gz-color-bg-field, rgba(127, 127, 127, 0.08))",
|
|
497
|
-
border: "0",
|
|
498
|
-
borderRadius: 6,
|
|
499
|
-
boxSizing: "border-box",
|
|
500
|
-
fontFamily: "ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace",
|
|
501
|
-
lineHeight: 1.5,
|
|
502
|
-
margin: 0,
|
|
503
|
-
minHeight: 0,
|
|
504
|
-
overflow: "auto",
|
|
505
|
-
overflowWrap: "anywhere",
|
|
506
|
-
padding: 16,
|
|
507
|
-
whiteSpace: "pre-wrap"
|
|
508
|
-
};
|
|
509
|
-
var methodTagColors = {
|
|
510
|
-
DELETE: "red",
|
|
511
|
-
GET: "green",
|
|
512
|
-
PATCH: "purple",
|
|
513
|
-
POST: "gold",
|
|
514
|
-
PUT: "blue"
|
|
188
|
+
var methodTagColors = {
|
|
189
|
+
DELETE: "red",
|
|
190
|
+
GET: "green",
|
|
191
|
+
PATCH: "purple",
|
|
192
|
+
POST: "gold",
|
|
193
|
+
PUT: "blue"
|
|
515
194
|
};
|
|
516
195
|
var getHttpMethodTagColor = (method) => {
|
|
517
196
|
const normalizedMethod = method == null ? void 0 : method.toUpperCase();
|
|
@@ -626,10 +305,16 @@ var DebugErrorListItem = ({ error, index, selected, total }) => {
|
|
|
626
305
|
minWidth: 0
|
|
627
306
|
},
|
|
628
307
|
children: [
|
|
629
|
-
/* @__PURE__ */ jsxs(
|
|
630
|
-
"
|
|
631
|
-
|
|
632
|
-
|
|
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
|
+
),
|
|
633
318
|
/* @__PURE__ */ jsx("span", { style: { flex: "0 0 58px", minWidth: 58 }, children: /* @__PURE__ */ jsx(HttpMethodTag, { method: error.method }) }),
|
|
634
319
|
/* @__PURE__ */ jsx(
|
|
635
320
|
"span",
|
|
@@ -691,10 +376,11 @@ var DebugErrorListItem = ({ error, index, selected, total }) => {
|
|
|
691
376
|
};
|
|
692
377
|
var debugTabKeys = ["request", "stackTrace", "mdc"];
|
|
693
378
|
var isDebugTabKey = (value) => debugTabKeys.includes(value);
|
|
694
|
-
var DebugErrorDetails = ({
|
|
379
|
+
var DebugErrorDetails = ({
|
|
380
|
+
error
|
|
381
|
+
}) => {
|
|
695
382
|
var _a, _b;
|
|
696
383
|
const [activeTab, setActiveTab] = useState("request");
|
|
697
|
-
const canCopyCompleteResponse = error.responseData !== void 0 && error.responseData !== null;
|
|
698
384
|
const values = {
|
|
699
385
|
mdc: error.debug.mdc,
|
|
700
386
|
request: error.debug.request,
|
|
@@ -706,18 +392,22 @@ var DebugErrorDetails = ({ error }) => {
|
|
|
706
392
|
}
|
|
707
393
|
const copied = await copyDebugText(formatDebugValue(value));
|
|
708
394
|
if (copied) {
|
|
709
|
-
|
|
395
|
+
showFeedbackSuccessMessage("已复制");
|
|
710
396
|
return;
|
|
711
397
|
}
|
|
712
|
-
|
|
398
|
+
showFeedbackErrorMessage("复制失败");
|
|
713
399
|
};
|
|
714
400
|
const handleCopyCompleteResponse = async () => {
|
|
715
|
-
const
|
|
401
|
+
const responseText = formatCompleteDebugResponse(error);
|
|
402
|
+
if (responseText === void 0) {
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
const copied = await copyDebugText(responseText);
|
|
716
406
|
if (copied) {
|
|
717
|
-
|
|
407
|
+
showFeedbackSuccessMessage("完整响应信息已复制");
|
|
718
408
|
return;
|
|
719
409
|
}
|
|
720
|
-
|
|
410
|
+
showFeedbackErrorMessage("复制失败");
|
|
721
411
|
};
|
|
722
412
|
return /* @__PURE__ */ jsxs(
|
|
723
413
|
"div",
|
|
@@ -734,7 +424,12 @@ var DebugErrorDetails = ({ error }) => {
|
|
|
734
424
|
/* @__PURE__ */ jsxs(
|
|
735
425
|
"div",
|
|
736
426
|
{
|
|
737
|
-
style: {
|
|
427
|
+
style: {
|
|
428
|
+
alignItems: "flex-start",
|
|
429
|
+
display: "flex",
|
|
430
|
+
gap: 8,
|
|
431
|
+
minWidth: 0
|
|
432
|
+
},
|
|
738
433
|
children: [
|
|
739
434
|
/* @__PURE__ */ jsx(
|
|
740
435
|
"strong",
|
|
@@ -749,7 +444,7 @@ var DebugErrorDetails = ({ error }) => {
|
|
|
749
444
|
children: error.message
|
|
750
445
|
}
|
|
751
446
|
),
|
|
752
|
-
|
|
447
|
+
error.responseData === void 0 ? null : /* @__PURE__ */ jsx(Tooltip, { title: "复制完整响应信息", children: /* @__PURE__ */ jsx(
|
|
753
448
|
Button,
|
|
754
449
|
{
|
|
755
450
|
"aria-label": "复制完整响应信息",
|
|
@@ -761,7 +456,7 @@ var DebugErrorDetails = ({ error }) => {
|
|
|
761
456
|
},
|
|
762
457
|
children: "复制完整响应"
|
|
763
458
|
}
|
|
764
|
-
) })
|
|
459
|
+
) })
|
|
765
460
|
]
|
|
766
461
|
}
|
|
767
462
|
),
|
|
@@ -841,7 +536,12 @@ var DebugErrorDetails = ({ error }) => {
|
|
|
841
536
|
label: "MDC"
|
|
842
537
|
}
|
|
843
538
|
],
|
|
844
|
-
style: {
|
|
539
|
+
style: {
|
|
540
|
+
display: "flex",
|
|
541
|
+
flexDirection: "column",
|
|
542
|
+
height: "100%",
|
|
543
|
+
minHeight: 0
|
|
544
|
+
},
|
|
845
545
|
onChange: (key) => {
|
|
846
546
|
if (isDebugTabKey(key)) {
|
|
847
547
|
setActiveTab(key);
|
|
@@ -853,10 +553,10 @@ var DebugErrorDetails = ({ error }) => {
|
|
|
853
553
|
}
|
|
854
554
|
);
|
|
855
555
|
};
|
|
856
|
-
var
|
|
556
|
+
var DebugErrorDrawer = () => {
|
|
857
557
|
var _a, _b;
|
|
858
558
|
const hostIdRef = useRef(void 0);
|
|
859
|
-
(_a = hostIdRef.current) != null ? _a : hostIdRef.current = /* @__PURE__ */ Symbol("gz-
|
|
559
|
+
(_a = hostIdRef.current) != null ? _a : hostIdRef.current = /* @__PURE__ */ Symbol("gz-debug-feedback-drawer-host");
|
|
860
560
|
const hostId = hostIdRef.current;
|
|
861
561
|
const snapshot3 = useSyncExternalStore(
|
|
862
562
|
subscribeDebugDrawer,
|
|
@@ -952,49 +652,12 @@ var GzFetchDebugDrawer = () => {
|
|
|
952
652
|
}
|
|
953
653
|
);
|
|
954
654
|
};
|
|
955
|
-
|
|
956
|
-
var
|
|
957
|
-
var
|
|
958
|
-
var _a, _b, _c;
|
|
959
|
-
const hostIdRef = useRef(void 0);
|
|
960
|
-
(_a = hostIdRef.current) != null ? _a : hostIdRef.current = /* @__PURE__ */ Symbol("gz-fetch-unauthorized-modal-host");
|
|
961
|
-
const hostId = hostIdRef.current;
|
|
962
|
-
const snapshot3 = useSyncExternalStore(
|
|
963
|
-
subscribeUnauthorized,
|
|
964
|
-
getUnauthorizedSnapshot,
|
|
965
|
-
getUnauthorizedServerSnapshot
|
|
966
|
-
);
|
|
967
|
-
useEffect(() => registerUnauthorizedModalHost(hostId), [hostId]);
|
|
968
|
-
if (snapshot3.activeHostId !== hostId) {
|
|
969
|
-
return null;
|
|
970
|
-
}
|
|
971
|
-
const handleConfirm = () => {
|
|
972
|
-
const options = confirmUnauthorizedModal();
|
|
973
|
-
if (options) {
|
|
974
|
-
executeUnauthorizedAction(options);
|
|
975
|
-
}
|
|
976
|
-
};
|
|
977
|
-
return /* @__PURE__ */ jsx(
|
|
978
|
-
Modal,
|
|
979
|
-
{
|
|
980
|
-
afterClose: completeUnauthorizedModal,
|
|
981
|
-
cancelText: "取消",
|
|
982
|
-
closable: true,
|
|
983
|
-
okText: "重新登录",
|
|
984
|
-
open: snapshot3.phase === "open",
|
|
985
|
-
title: (_b = snapshot3.options) == null ? void 0 : _b.modalTitle,
|
|
986
|
-
onCancel: cancelUnauthorizedModal,
|
|
987
|
-
onOk: handleConfirm,
|
|
988
|
-
children: (_c = snapshot3.options) == null ? void 0 : _c.modalMessage
|
|
989
|
-
}
|
|
990
|
-
);
|
|
991
|
-
};
|
|
992
|
-
GzFetchUnauthorizedModal.displayName = "GzFetchUnauthorizedModal";
|
|
993
|
-
var GzFetchUnauthorizedModal_default = GzFetchUnauthorizedModal;
|
|
994
|
-
var GzFetchFeedbackProvider = ({ children }) => {
|
|
655
|
+
DebugErrorDrawer.displayName = "DebugErrorDrawer";
|
|
656
|
+
var DebugErrorDrawer_default = DebugErrorDrawer;
|
|
657
|
+
var GzDebugFeedbackProvider = ({ children }) => {
|
|
995
658
|
var _a;
|
|
996
659
|
const hostIdRef = useRef(void 0);
|
|
997
|
-
(_a = hostIdRef.current) != null ? _a : hostIdRef.current = /* @__PURE__ */ Symbol("gz-
|
|
660
|
+
(_a = hostIdRef.current) != null ? _a : hostIdRef.current = /* @__PURE__ */ Symbol("gz-debug-feedback-host");
|
|
998
661
|
const hostId = hostIdRef.current;
|
|
999
662
|
const [messageApi, messageContextHolder] = message.useMessage();
|
|
1000
663
|
const showErrorMessage = useCallback(
|
|
@@ -1015,12 +678,382 @@ var GzFetchFeedbackProvider = ({ children }) => {
|
|
|
1015
678
|
);
|
|
1016
679
|
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
1017
680
|
messageContextHolder,
|
|
1018
|
-
/* @__PURE__ */ jsx(
|
|
1019
|
-
/* @__PURE__ */ jsx(GzFetchUnauthorizedModal_default, {}),
|
|
681
|
+
/* @__PURE__ */ jsx(DebugErrorDrawer_default, {}),
|
|
1020
682
|
children
|
|
1021
683
|
] });
|
|
1022
684
|
};
|
|
1023
|
-
|
|
1024
|
-
var
|
|
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 });
|
|
714
|
+
};
|
|
715
|
+
var GzFetchError = class extends Error {
|
|
716
|
+
constructor(message3, options) {
|
|
717
|
+
super(message3);
|
|
718
|
+
this.name = "GzFetchError";
|
|
719
|
+
this.type = options.type;
|
|
720
|
+
this.status = options.status;
|
|
721
|
+
this.responseData = options.responseData;
|
|
722
|
+
this.originalError = options.originalError;
|
|
723
|
+
}
|
|
724
|
+
};
|
|
725
|
+
function getResponseMessage(data) {
|
|
726
|
+
if (typeof data !== "object" || data === null || !("msg" in data)) {
|
|
727
|
+
return void 0;
|
|
728
|
+
}
|
|
729
|
+
const { msg } = data;
|
|
730
|
+
return typeof msg === "string" && msg.length > 0 ? msg : void 0;
|
|
731
|
+
}
|
|
732
|
+
var DEFAULT_HTTP_ERROR_MESSAGE = "请求失败,请稍后重试";
|
|
733
|
+
var DEFAULT_NETWORK_ERROR_MESSAGE = "网络异常,请检查网络连接";
|
|
734
|
+
var DEFAULT_TIMEOUT_ERROR_MESSAGE = "请求超时,请稍后重试";
|
|
735
|
+
var DEFAULT_CANCELED_ERROR_MESSAGE = "请求已取消";
|
|
736
|
+
var DEFAULT_UNKNOWN_ERROR_MESSAGE = "请求失败,请稍后重试";
|
|
737
|
+
function toGzFetchError(error) {
|
|
738
|
+
var _a;
|
|
739
|
+
if (error instanceof GzFetchError) {
|
|
740
|
+
return error;
|
|
741
|
+
}
|
|
742
|
+
if (axios.isCancel(error)) {
|
|
743
|
+
return new GzFetchError(DEFAULT_CANCELED_ERROR_MESSAGE, {
|
|
744
|
+
type: "CANCELED_ERROR",
|
|
745
|
+
originalError: error
|
|
746
|
+
});
|
|
747
|
+
}
|
|
748
|
+
if (axios.isAxiosError(error)) {
|
|
749
|
+
if (error.code === AxiosError.ECONNABORTED || error.code === AxiosError.ETIMEDOUT) {
|
|
750
|
+
return new GzFetchError(DEFAULT_TIMEOUT_ERROR_MESSAGE, {
|
|
751
|
+
type: "TIMEOUT_ERROR",
|
|
752
|
+
originalError: error
|
|
753
|
+
});
|
|
754
|
+
}
|
|
755
|
+
if (error.response) {
|
|
756
|
+
return new GzFetchError(
|
|
757
|
+
(_a = getResponseMessage(error.response.data)) != null ? _a : DEFAULT_HTTP_ERROR_MESSAGE,
|
|
758
|
+
{
|
|
759
|
+
type: "HTTP_ERROR",
|
|
760
|
+
status: error.response.status,
|
|
761
|
+
responseData: error.response.data,
|
|
762
|
+
originalError: error
|
|
763
|
+
}
|
|
764
|
+
);
|
|
765
|
+
}
|
|
766
|
+
return new GzFetchError(DEFAULT_NETWORK_ERROR_MESSAGE, {
|
|
767
|
+
type: "NETWORK_ERROR",
|
|
768
|
+
originalError: error
|
|
769
|
+
});
|
|
770
|
+
}
|
|
771
|
+
return new GzFetchError(DEFAULT_UNKNOWN_ERROR_MESSAGE, {
|
|
772
|
+
type: "UNKNOWN_ERROR",
|
|
773
|
+
originalError: error
|
|
774
|
+
});
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
// src/fetch/middleware.ts
|
|
778
|
+
async function runRequestMiddlewares(initialConfig, middlewares) {
|
|
779
|
+
let config = initialConfig;
|
|
780
|
+
for (const middleware of middlewares) {
|
|
781
|
+
if (middleware.onRequest) {
|
|
782
|
+
config = await middleware.onRequest(config);
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
return config;
|
|
786
|
+
}
|
|
787
|
+
async function runResponseMiddlewares(initialData, context, middlewares) {
|
|
788
|
+
let data = initialData;
|
|
789
|
+
for (const middleware of middlewares) {
|
|
790
|
+
if (middleware.onResponse) {
|
|
791
|
+
data = await middleware.onResponse(data, context);
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
return data;
|
|
795
|
+
}
|
|
796
|
+
async function runErrorMiddlewares(error, context, middlewares) {
|
|
797
|
+
var _a;
|
|
798
|
+
for (const middleware of middlewares) {
|
|
799
|
+
await ((_a = middleware.onError) == null ? void 0 : _a.call(middleware, error, context));
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
// src/fetch/unauthorized.ts
|
|
804
|
+
var DEFAULT_UNAUTHORIZED_LOGIN_URL = "/login";
|
|
805
|
+
var DEFAULT_UNAUTHORIZED_MODAL_TITLE = "登录失效";
|
|
806
|
+
var DEFAULT_UNAUTHORIZED_MODAL_MESSAGE = "当前登录状态已失效,请重新登录。";
|
|
807
|
+
var IDLE_SNAPSHOT = { phase: "idle" };
|
|
808
|
+
var snapshot2 = IDLE_SNAPSHOT;
|
|
809
|
+
var listeners2 = /* @__PURE__ */ new Set();
|
|
810
|
+
var modalHosts = /* @__PURE__ */ new Set();
|
|
811
|
+
var emitChange2 = () => {
|
|
812
|
+
for (const listener of listeners2) {
|
|
813
|
+
listener();
|
|
814
|
+
}
|
|
815
|
+
};
|
|
816
|
+
var setSnapshot2 = (nextSnapshot) => {
|
|
817
|
+
snapshot2 = nextSnapshot;
|
|
818
|
+
emitChange2();
|
|
819
|
+
};
|
|
820
|
+
var resolveUnauthorizedOptions = (options) => {
|
|
821
|
+
var _a, _b, _c, _d;
|
|
822
|
+
return {
|
|
823
|
+
enabled: (_a = options == null ? void 0 : options.enabled) != null ? _a : false,
|
|
824
|
+
loginUrl: (_b = options == null ? void 0 : options.loginUrl) != null ? _b : DEFAULT_UNAUTHORIZED_LOGIN_URL,
|
|
825
|
+
modalTitle: (_c = options == null ? void 0 : options.modalTitle) != null ? _c : DEFAULT_UNAUTHORIZED_MODAL_TITLE,
|
|
826
|
+
modalMessage: (_d = options == null ? void 0 : options.modalMessage) != null ? _d : DEFAULT_UNAUTHORIZED_MODAL_MESSAGE,
|
|
827
|
+
...(options == null ? void 0 : options.onUnauthorized) === void 0 ? {} : { onUnauthorized: options.onUnauthorized }
|
|
828
|
+
};
|
|
829
|
+
};
|
|
830
|
+
var getUnauthorizedSnapshot = () => snapshot2;
|
|
831
|
+
var getUnauthorizedServerSnapshot = () => IDLE_SNAPSHOT;
|
|
832
|
+
var subscribeUnauthorized = (listener) => {
|
|
833
|
+
listeners2.add(listener);
|
|
834
|
+
return () => listeners2.delete(listener);
|
|
835
|
+
};
|
|
836
|
+
var registerUnauthorizedModalHost = (hostId) => {
|
|
837
|
+
modalHosts.add(hostId);
|
|
838
|
+
if (snapshot2.activeHostId === void 0) {
|
|
839
|
+
setSnapshot2({ ...snapshot2, activeHostId: hostId });
|
|
840
|
+
}
|
|
841
|
+
return () => {
|
|
842
|
+
modalHosts.delete(hostId);
|
|
843
|
+
if (snapshot2.activeHostId !== hostId) {
|
|
844
|
+
return;
|
|
845
|
+
}
|
|
846
|
+
const nextHost = modalHosts.values().next();
|
|
847
|
+
const nextHostId = nextHost.done ? void 0 : nextHost.value;
|
|
848
|
+
if (snapshot2.phase === "closing") {
|
|
849
|
+
setSnapshot2(
|
|
850
|
+
nextHostId === void 0 ? IDLE_SNAPSHOT : { activeHostId: nextHostId, phase: "idle" }
|
|
851
|
+
);
|
|
852
|
+
return;
|
|
853
|
+
}
|
|
854
|
+
if (nextHostId === void 0) {
|
|
855
|
+
setSnapshot2(
|
|
856
|
+
snapshot2.options === void 0 ? { phase: snapshot2.phase } : { options: snapshot2.options, phase: snapshot2.phase }
|
|
857
|
+
);
|
|
858
|
+
return;
|
|
859
|
+
}
|
|
860
|
+
setSnapshot2({ ...snapshot2, activeHostId: nextHostId });
|
|
861
|
+
};
|
|
862
|
+
};
|
|
863
|
+
var openUnauthorizedModal = (options) => {
|
|
864
|
+
if (!options.enabled || snapshot2.phase !== "idle") {
|
|
865
|
+
return false;
|
|
866
|
+
}
|
|
867
|
+
setSnapshot2({
|
|
868
|
+
...snapshot2.activeHostId === void 0 ? {} : { activeHostId: snapshot2.activeHostId },
|
|
869
|
+
options,
|
|
870
|
+
phase: "open"
|
|
871
|
+
});
|
|
872
|
+
return true;
|
|
873
|
+
};
|
|
874
|
+
var beginCloseUnauthorizedModal = () => {
|
|
875
|
+
if (snapshot2.phase !== "open" || snapshot2.options === void 0) {
|
|
876
|
+
return void 0;
|
|
877
|
+
}
|
|
878
|
+
const { options } = snapshot2;
|
|
879
|
+
setSnapshot2({ ...snapshot2, phase: "closing" });
|
|
880
|
+
return options;
|
|
881
|
+
};
|
|
882
|
+
var cancelUnauthorizedModal = () => beginCloseUnauthorizedModal() !== void 0;
|
|
883
|
+
var confirmUnauthorizedModal = () => beginCloseUnauthorizedModal();
|
|
884
|
+
var completeUnauthorizedModal = () => {
|
|
885
|
+
if (snapshot2.phase !== "closing") {
|
|
886
|
+
return;
|
|
887
|
+
}
|
|
888
|
+
setSnapshot2(
|
|
889
|
+
snapshot2.activeHostId === void 0 ? IDLE_SNAPSHOT : { activeHostId: snapshot2.activeHostId, phase: "idle" }
|
|
890
|
+
);
|
|
891
|
+
};
|
|
892
|
+
var executeUnauthorizedAction = (options, navigate = (loginUrl) => window.location.assign(loginUrl)) => {
|
|
893
|
+
if (options.onUnauthorized) {
|
|
894
|
+
options.onUnauthorized();
|
|
895
|
+
return;
|
|
896
|
+
}
|
|
897
|
+
navigate(options.loginUrl);
|
|
898
|
+
};
|
|
899
|
+
|
|
900
|
+
// src/fetch/client.ts
|
|
901
|
+
var DEFAULT_TIMEOUT = 15e3;
|
|
902
|
+
var DEFAULT_AUTH_HEADER = "Authorization";
|
|
903
|
+
var DEFAULT_VALIDATE_STATUS = (status) => status >= 200 && status < 300;
|
|
904
|
+
function toHeaderRecord(headers) {
|
|
905
|
+
const result = {};
|
|
906
|
+
for (const [name, value] of Object.entries(headers)) {
|
|
907
|
+
if (value !== void 0) {
|
|
908
|
+
result[name] = String(value);
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
return result;
|
|
912
|
+
}
|
|
913
|
+
function createGzFetch(options = {}) {
|
|
914
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
915
|
+
const middlewares = (_a = options.middlewares) != null ? _a : [];
|
|
916
|
+
const showInstanceErrorMessage = (_b = options.showErrorMessage) != null ? _b : true;
|
|
917
|
+
const headerName = (_d = (_c = options.auth) == null ? void 0 : _c.headerName) != null ? _d : DEFAULT_AUTH_HEADER;
|
|
918
|
+
const formatToken = (_f = (_e = options.auth) == null ? void 0 : _e.formatToken) != null ? _f : ((token) => `Bearer ${token}`);
|
|
919
|
+
const unauthorizedOptions = resolveUnauthorizedOptions(options.unauthorized);
|
|
920
|
+
const instance = axios.create({
|
|
921
|
+
...options.baseURL === void 0 ? {} : { baseURL: options.baseURL },
|
|
922
|
+
timeout: (_g = options.timeout) != null ? _g : DEFAULT_TIMEOUT,
|
|
923
|
+
validateStatus: (_h = options.validateStatus) != null ? _h : DEFAULT_VALIDATE_STATUS
|
|
924
|
+
});
|
|
925
|
+
async function request(initialConfig) {
|
|
926
|
+
var _a2;
|
|
927
|
+
let currentConfig = initialConfig;
|
|
928
|
+
try {
|
|
929
|
+
const headers = AxiosHeaders.from(initialConfig.headers);
|
|
930
|
+
if (!initialConfig.skipAuth && options.getToken) {
|
|
931
|
+
const token = await options.getToken();
|
|
932
|
+
if (token) {
|
|
933
|
+
headers.set(headerName, formatToken(token));
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
currentConfig = await runRequestMiddlewares(
|
|
937
|
+
{ ...initialConfig, headers },
|
|
938
|
+
middlewares
|
|
939
|
+
);
|
|
940
|
+
const usesRequestBody = currentConfig.method === "POST" || currentConfig.method === "PUT" || currentConfig.method === "DELETE" && !currentConfig.paramsInUrl;
|
|
941
|
+
const axiosConfig = {
|
|
942
|
+
url: currentConfig.url,
|
|
943
|
+
method: currentConfig.method,
|
|
944
|
+
...!usesRequestBody || currentConfig.params === void 0 ? {} : { data: currentConfig.params },
|
|
945
|
+
...currentConfig.headers === void 0 ? {} : { headers: currentConfig.headers },
|
|
946
|
+
...usesRequestBody || currentConfig.params === void 0 ? {} : { params: currentConfig.params },
|
|
947
|
+
...currentConfig.timeout === void 0 ? {} : { timeout: currentConfig.timeout },
|
|
948
|
+
...currentConfig.responseType === void 0 ? {} : { responseType: currentConfig.responseType },
|
|
949
|
+
...currentConfig.signal === void 0 ? {} : { signal: currentConfig.signal },
|
|
950
|
+
...currentConfig.withCredentials === void 0 ? {} : { withCredentials: currentConfig.withCredentials }
|
|
951
|
+
};
|
|
952
|
+
const response = await instance.request(axiosConfig);
|
|
953
|
+
const context = {
|
|
954
|
+
config: currentConfig,
|
|
955
|
+
status: response.status,
|
|
956
|
+
headers: toHeaderRecord(response.headers)
|
|
957
|
+
};
|
|
958
|
+
return await runResponseMiddlewares(response.data, context, middlewares);
|
|
959
|
+
} catch (originalError) {
|
|
960
|
+
const error = toGzFetchError(originalError);
|
|
961
|
+
const shouldHandleUnauthorized = unauthorizedOptions.enabled && error.type === "HTTP_ERROR" && error.status === 401;
|
|
962
|
+
if (shouldHandleUnauthorized) {
|
|
963
|
+
openUnauthorizedModal(unauthorizedOptions);
|
|
964
|
+
}
|
|
965
|
+
await runErrorMiddlewares(error, { config: currentConfig }, middlewares);
|
|
966
|
+
const shouldShowError = (_a2 = currentConfig.showErrorMessage) != null ? _a2 : showInstanceErrorMessage;
|
|
967
|
+
if (shouldShowError && error.type !== "CANCELED_ERROR" && !shouldHandleUnauthorized) {
|
|
968
|
+
const didShowDebugError = error.type === "HTTP_ERROR" && error.status !== 401 ? reportDebugError({
|
|
969
|
+
debug: getResponseDebugInfo(error.responseData),
|
|
970
|
+
message: error.message,
|
|
971
|
+
method: currentConfig.method,
|
|
972
|
+
responseData: error.responseData,
|
|
973
|
+
url: currentConfig.url
|
|
974
|
+
}) : false;
|
|
975
|
+
if (!didShowDebugError) {
|
|
976
|
+
showFeedbackErrorMessage(error.message);
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
throw error;
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
const gzFetch2 = ((config) => request(config));
|
|
983
|
+
gzFetch2.get = (url, config = {}) => gzFetch2({ ...config, url, method: "GET" });
|
|
984
|
+
gzFetch2.post = (url, params, config = {}) => gzFetch2({
|
|
985
|
+
...config,
|
|
986
|
+
url,
|
|
987
|
+
method: "POST",
|
|
988
|
+
...params === void 0 ? {} : { params }
|
|
989
|
+
});
|
|
990
|
+
gzFetch2.put = (url, params, config = {}) => gzFetch2({
|
|
991
|
+
...config,
|
|
992
|
+
url,
|
|
993
|
+
method: "PUT",
|
|
994
|
+
...params === void 0 ? {} : { params }
|
|
995
|
+
});
|
|
996
|
+
gzFetch2.delete = (url, config = {}) => gzFetch2({ ...config, url, method: "DELETE" });
|
|
997
|
+
return gzFetch2;
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
// src/fetch/default-client.ts
|
|
1001
|
+
var NOT_CONFIGURED_MESSAGE = "gzFetch 尚未配置,请先在应用初始化时调用 configureGzFetch。";
|
|
1002
|
+
var defaultClient;
|
|
1003
|
+
function configureGzFetch(options) {
|
|
1004
|
+
defaultClient = createGzFetch(options);
|
|
1005
|
+
}
|
|
1006
|
+
async function gzFetch(config) {
|
|
1007
|
+
if (!defaultClient) {
|
|
1008
|
+
throw new Error(NOT_CONFIGURED_MESSAGE);
|
|
1009
|
+
}
|
|
1010
|
+
return defaultClient(config);
|
|
1011
|
+
}
|
|
1012
|
+
var GzFetchUnauthorizedModalContent = () => {
|
|
1013
|
+
var _a, _b, _c;
|
|
1014
|
+
const hostIdRef = useRef(void 0);
|
|
1015
|
+
(_a = hostIdRef.current) != null ? _a : hostIdRef.current = /* @__PURE__ */ Symbol("gz-fetch-unauthorized-modal-host");
|
|
1016
|
+
const hostId = hostIdRef.current;
|
|
1017
|
+
const snapshot3 = useSyncExternalStore(
|
|
1018
|
+
subscribeUnauthorized,
|
|
1019
|
+
getUnauthorizedSnapshot,
|
|
1020
|
+
getUnauthorizedServerSnapshot
|
|
1021
|
+
);
|
|
1022
|
+
useEffect(() => registerUnauthorizedModalHost(hostId), [hostId]);
|
|
1023
|
+
if (snapshot3.activeHostId !== hostId) {
|
|
1024
|
+
return null;
|
|
1025
|
+
}
|
|
1026
|
+
const handleConfirm = () => {
|
|
1027
|
+
const options = confirmUnauthorizedModal();
|
|
1028
|
+
if (options) {
|
|
1029
|
+
executeUnauthorizedAction(options);
|
|
1030
|
+
}
|
|
1031
|
+
};
|
|
1032
|
+
return /* @__PURE__ */ jsx(
|
|
1033
|
+
Modal,
|
|
1034
|
+
{
|
|
1035
|
+
afterClose: completeUnauthorizedModal,
|
|
1036
|
+
cancelText: "取消",
|
|
1037
|
+
closable: true,
|
|
1038
|
+
okText: "重新登录",
|
|
1039
|
+
open: snapshot3.phase === "open",
|
|
1040
|
+
title: (_b = snapshot3.options) == null ? void 0 : _b.modalTitle,
|
|
1041
|
+
onCancel: cancelUnauthorizedModal,
|
|
1042
|
+
onOk: handleConfirm,
|
|
1043
|
+
children: (_c = snapshot3.options) == null ? void 0 : _c.modalMessage
|
|
1044
|
+
}
|
|
1045
|
+
);
|
|
1046
|
+
};
|
|
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
|
+
] });
|
|
1053
|
+
GzFetchFeedbackProvider.displayName = "GzFetchFeedbackProvider";
|
|
1054
|
+
var GzFetchFeedbackProvider_default = GzFetchFeedbackProvider;
|
|
1055
|
+
var GzFetchUnauthorizedModal = () => /* @__PURE__ */ jsx(GzFetchUnauthorizedModalContent_default, {});
|
|
1056
|
+
GzFetchUnauthorizedModal.displayName = "GzFetchUnauthorizedModal";
|
|
1057
|
+
var GzFetchUnauthorizedModal_default = GzFetchUnauthorizedModal;
|
|
1025
1058
|
|
|
1026
|
-
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 };
|