This is an automated email from the ASF dual-hosted git repository.
dockerzhang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/inlong.git
The following commit(s) were added to refs/heads/master by this push:
new 29a6b60c7 [INLONG-7582][Dashboard] Support responseParse config (#7583)
29a6b60c7 is described below
commit 29a6b60c72899f62528b779f73d849cebc1033f2
Author: Daniel <[email protected]>
AuthorDate: Mon Mar 13 18:44:10 2023 +0800
[INLONG-7582][Dashboard] Support responseParse config (#7583)
---
inlong-dashboard/src/configs/default/conf.ts | 2 ++
inlong-dashboard/src/utils/request.ts | 20 +++++++++++++++++---
2 files changed, 19 insertions(+), 3 deletions(-)
diff --git a/inlong-dashboard/src/configs/default/conf.ts
b/inlong-dashboard/src/configs/default/conf.ts
index 646a2057d..443662b23 100644
--- a/inlong-dashboard/src/configs/default/conf.ts
+++ b/inlong-dashboard/src/configs/default/conf.ts
@@ -21,6 +21,7 @@ import i18n from '@/i18n';
import Provider from '@/components/Provider';
import Layout from '@/components/Layout';
import { message } from 'antd';
+import type { SuccessResponse } from '@/utils/request';
const conf = {
title: '',
@@ -35,6 +36,7 @@ const conf = {
AppLayout: Layout,
requestPrefix: '/inlong/manager/api',
requestErrorAlert: (msg: string) => message.error(msg),
+ responseParse: (res: any): SuccessResponse => res,
};
export default conf;
diff --git a/inlong-dashboard/src/utils/request.ts
b/inlong-dashboard/src/utils/request.ts
index 6fc70528d..17c8d7c48 100644
--- a/inlong-dashboard/src/utils/request.ts
+++ b/inlong-dashboard/src/utils/request.ts
@@ -29,9 +29,16 @@ export interface FetchOptions extends RequestOptionsInit {
fetchType?: 'FETCH' | 'JSONP';
}
+export interface SuccessResponse {
+ success: boolean;
+ data: any;
+ errMsg: string;
+}
+
export interface RequestOptions extends FetchOptions {
// Do not use global request error prompt (http request is successful, but
the returned result has a backend error)
noGlobalError?: boolean;
+ responseParse?: (res: any) => SuccessResponse;
}
const extendRequest = extend({
@@ -68,15 +75,22 @@ const fetch = (options: FetchOptions) => {
};
export default function request(_options: RequestOptions | string) {
- const options = typeof _options === 'string' ? { url: _options } : _options;
+ const opts = typeof _options === 'string' ? { url: _options } : _options;
+ const { noGlobalError, responseParse, ...options } = opts;
nprogress.start();
return fetch(options)
.then((response: any) => {
- const { success, data, errMsg: message } = response;
+ const {
+ success,
+ data,
+ errMsg: message,
+ } = responseParse
+ ? responseParse(response)
+ : config.responseParse?.(response) || (response as SuccessResponse);
// Request 200, but the result is wrong
if (!success) {
- if (options.noGlobalError) {
+ if (noGlobalError) {
return Promise.resolve(response);
}