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