All files / src/utils FetchUtils.tsx

61.93% Statements 179/289
50.33% Branches 75/149
59.75% Functions 49/82
62.67% Lines 178/284

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837                                                                                                                                      65x     1057x 1057x                       1070x     1015x     1015x 1015x 1015x   323x 323x 323x     323x 323x     165x 35x     165x 105x 105x 105x 105x                                                                                                                         1x 1x 1x       4x 4x 4x 4x               3x       31x       1x     1x       29x         12x     12x 12x     12x 1x       12x 1x     12x       6x         2x 2x 2x                     1x 1x 1x                                   26x 26x 26x 26x       118x     26x 18x               18x 13x     13x 13x 8x     13x 13x 11x 11x 11x   2x 1x 1x     1x 1x 1x       13x       13x     13x 13x 13x             1x                               4x 4x 4x 4x 4x 4x                 4x       4x   4x                               1x 1x 1x 1x                 1x 1x   1x                                                             5x 5x 5x 5x     3x   3x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x     2x 2x         2x                                                                                         8x 8x                       10x           22x       672x 672x                 2x 2x           8x 8x 8x       1x             65x                                     5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x                           5x             65x 210x 210x 210x 23x           210x 193x       65x   65x 2x 2x 1x   1x 1x 1x 1x 1x 1x           1x 1x             65x                                         1x 1x 1x           1x 1x 1x 1x               1x 1x               1x 1x       5x 5x                                                 2x                       12x       1x               1x 1x                                       20x                                                                                                                                   65x      
import { ExcludeArrayColumnsFetchType, LogApiResponseToConsole, OnlyLabelFieldFetchType, SERVER_URL } from '@config/base';
import i18n from '@config/i18n';
import {
  AttachmentProps,
  ActionProps,
  DashboardProps,
  DashboardWidgetProps,
  DataApiResultProps,
  DeleteRecordResponseProps,
  DeletionProps,
  DynamicFilterResponseProps,
  EnumMetaProps,
  ExecResultsProps,
  ExtendDataNode,
  FetchListOfDomainDataProps,
  FetchSearchResultProps,
  FetchType,
  FormProps,
  FormType,
  GroupMetaProps,
  MessageSummaryResponseProps,
  ObjectMetaProps,
  RecordProps,
  SaveRecordProps,
  SaveRecordResponseProps,
  SearchConditions,
  TableMetaProps,
  ThemeInfoProps,
  UploadCsvResponseProps,
  ErrorResponseProps,
  WidgetDataResponseProps,
  WizardMetaProps,
  WizardStepResultProps,
  BaseResponseProps,
  RevisionCompareProps,
  RevisionCompareResult,
  ActionMode
} from '@props/RecordProps';
 
import {
  openErrorNotification,
  openInfoNotification,
  openWarningNotification,
  openWarningNotificationWithDescription
} from '@utils/NotificationUtils';
import { ValidateErrorEntity } from "rc-field-form/lib/interface";
import { getAccessToken, getRefreshToken, saveTokens } from "@utils/TokenUtils";
import { configureRefreshFetch, fetchJSON } from "refresh-fetch";
import merge from 'lodash/merge';
import { cachedFetch } from './CachedFetch';
import { capitalizeFirstLetter, typeWithPackageToSimpleType } from '@utils/StringUtils';
import { deleteLabelFromCache, getRawDomainName, pureObjectIsEmpty } from "@utils/ObjectUtils";
import {
  AvatarData, CreateActionProps, CronInformationProps, PopulateDomainDataProps,
  FetchRelateDomainProps, LoginResultProps, RefreshColumnMetaResultProps, RefreshDynamicColumnMetaResultProps,
  SaveDomainProps
} from "./FetchUtilsProps";
import { DynamicMenuProps } from '@kernel/RoutersConfig';
 
// TODO define a base interface for error type with 'errorType' field.
export interface FetchError {
  errorType: 'FetchError';
  code: number;
  message: string;
}
 
 
export const buildAuthorizationHeader = (): {
  [propName: string]: string;
} => {
  const accessToken = getAccessToken();
  return {
    'Authorization': "Bearer " + (accessToken == null ? "" : accessToken),
    'Content-Type': 'application/json'
  };
};
 
export async function requestUrlAndGetPromise(url: string, parameters: RequestInit, options?: {
  useCache?: boolean;
  parseResponse?: boolean;
  isRetry?: boolean;
  // eslint-disable-next-line  @typescript-eslint/no-explicit-any
}): Promise<any> {
  const params = { headers: buildAuthorizationHeader(), ...parameters };
  const {
    useCache, parseResponse, isRetry
  } = options || {
    useCache: true, parseResponse: true, isRetry: false
  };
  const isGetMethod = (parameters.method == null || parameters.method === 'GET');
  const cacheUsed = (isGetMethod && useCache !== false);
  const response = (cacheUsed) ? await
    cachedFetch(url, params) : await _fetch(url, params);
  Eif (parseResponse !== false) {
    return await response.text().then((text: string) => {
      Iif (LogApiResponseToConsole) {
        console.debug(`Response from ${url}: \n${text}`);
      }
      try {
        return JSON.parse(text);
      } catch (error) {
        // 仅在重试访问后台报错后,才打印错误日志
        if (isRetry) {
          console.error(`Failed to parse [${text}] from [${url}] with parameters [${JSON.stringify(params)}] to json: ${error}`);
        }
        // 如果使用 cache 的 response 出错,修改请求 options, 跳过 cache 往后台请求
        if (cacheUsed) {
          const skipCacheOptions = { ...options };
          skipCacheOptions.useCache = false;
          skipCacheOptions.isRetry = true;
          return requestUrlAndGetPromise(url, parameters, skipCacheOptions);
        }
      }
    });
  }
  return response;
}
 
export async function requestUrlAndGetPromiseThrowError<T>(url: string, parameters: RequestInit, options?: {
  useCache?: boolean;
  parseResponse?: boolean;
  isRetry?: boolean;
  // eslint-disable-next-line  @typescript-eslint/no-explicit-any
}): Promise<T> {
  const params = { headers: buildAuthorizationHeader(), ...parameters };
  const {
    useCache, parseResponse, isRetry
  } = options || {
    useCache: true, parseResponse: true, isRetry: false
  };
  const isGetMethod = (parameters.method == null || parameters.method === 'GET');
  const cacheUsed = (isGetMethod && useCache !== false);
  const response = (cacheUsed) ? await
    cachedFetch(url, params) : await _fetch(url, params);
  if (parseResponse !== false) {
    const resp = response as Response;
    return await resp.text().then((text: string) => {
      if (LogApiResponseToConsole) {
        console.debug(`Response from ${url}: \n${text}`);
      }
      let body = null;
      try {
        body = JSON.parse(text);
      } catch (error) {
        // 仅在重试访问后台报错后,才打印错误日志
        if (isRetry) {
          console.error(`Failed to parse [${text}] from [${url}] with parameters [${JSON.stringify(params)}] to json: ${error}`);
        }
        // 如果使用 cache 的 response 出错,修改请求 options, 跳过 cache 往后台请求
        if (cacheUsed) {
          const skipCacheOptions = { ...options };
          skipCacheOptions.useCache = false;
          skipCacheOptions.isRetry = true;
          return requestUrlAndGetPromise(url, parameters, skipCacheOptions);
        }
      }
      if (resp.ok) {
        return body;
      } else {
        throw {
          errorType: 'FetchError',
          code: resp.status,
          message: body?.message
        } as FetchError;
      }
    });
  }
  return response;
}
 
export async function fetchCronInformation(value: string): Promise<CronInformationProps> {
  const headers = buildAuthorizationHeader();
  const response = await _fetch(`${SERVER_URL}/cron/parse?q=${value}`, { headers });
  return await response.json();
}
 
export async function fetchTopMenus(): Promise<Array<DynamicMenuProps>> {
  const headers = buildAuthorizationHeader();
  const response = await _fetch(`${SERVER_URL}/menu/top`, { headers });
  if (response.ok) {
    return await response.json();
  } else E{
    console.warn("Error getting list of top menus: ", response);
    return await response.json();
  }
}
 
export async function fetchEnumOptions(enumType: string): Promise<Array<EnumMetaProps>> {
  return await requestUrlAndGetPromise(`${SERVER_URL}/enum/${enumType}`, {});
}
 
export async function fetchSelectOptions(dynamicFieldKey: string): Promise<Array<string>> {
  return await requestUrlAndGetPromise(`${SERVER_URL}/options/${dynamicFieldKey}`, {});
}
 
export async function searchObjectByKeyword(ownerClass: string, fieldName: string, type: string, keyword: string): Promise<Array<ObjectMetaProps>> {
  Iif (ownerClass == null) {
    return await requestUrlAndGetPromise(`${SERVER_URL}/search/${fieldName}/${type}?q=${keyword}`, {});
  }
  return await requestUrlAndGetPromise(`${SERVER_URL}/search/${ownerClass}/${fieldName}/${type}?q=${keyword}`, {});
}
 
export async function fetchDomainMeta(domainName: string, formId: number | undefined): Promise<Array<TableMetaProps>> {
  return await requestUrlAndGetPromise(`${SERVER_URL}/domain/${getRawDomainName(domainName)}/formFields/${formId}`, {});
}
 
export async function fetchCurrentValue(domainName: string, id: number, fetchType?: FetchType | null, formId?: number | null): Promise<RecordProps> {
 
  let url = `${SERVER_URL}/${getRawDomainName(domainName)}/${id}`;
 
  // Add fetchType and formId to the URL only if they are defined
  const queryParams = [];
  Iif (fetchType != null) {
    queryParams.push(`fetchType=${fetchType}`);
  }
  if (formId != null) {
    queryParams.push(`formId=${formId}`);
  }
 
  // Join the query parameters together and add to the URL
  if (queryParams.length > 0) {
    url += `?${queryParams.join('&')}`;
  }
 
  return requestUrlAndGetPromise(url, {});
}
 
export async function fetchCurrentValueNoRelationshipColumns(domainName: string, id: number, useCache?: boolean): Promise<RecordProps> {
  return requestUrlAndGetPromise(`${SERVER_URL}/${getRawDomainName(domainName)}/${id}?fetchType=${OnlyLabelFieldFetchType}`, { },
                                 { useCache: (useCache === false)? false : true });
}
 
export async function fetchCurrentValues(domainName: string, ids: Array<number>, fetchType?: FetchType, formId?: number): Promise<Array<RecordProps>> {
  const noIdsSupplied = (ids.length === 0 || (ids.length === 1 && ids[0] == null));
  const ft: FetchType = (fetchType == null) ? OnlyLabelFieldFetchType : ExcludeArrayColumnsFetchType;
  return noIdsSupplied ? await new Promise<Array<RecordProps>>((resolve) => resolve([])) :
    await requestUrlAndGetPromise(`${SERVER_URL}/multiple/${getRawDomainName(domainName)}/${ids}?fetchType=${ft}&formId=${formId}`, {});
}
 
export async function fetchListOfDomainData(props: FetchListOfDomainDataProps): Promise<DataApiResultProps> {
  const { formId, domainName, current, max, useCache } = props;
  const offset = max * (current - 1);
  return await requestUrlAndGetPromise(`${SERVER_URL}/${getRawDomainName(domainName)}?max=${max}&offset=${offset}&formId=${formId}`, {}, { useCache });
}
 
export async function fetchListOfRelateDomainData(props: FetchRelateDomainProps): Promise<DataApiResultProps> {
  const { formId, domainName, columnName, ownerId, current, max, useCache } = props;
  const offset = max * (current - 1);
  return await requestUrlAndGetPromise(`${SERVER_URL}/${getRawDomainName(domainName)}/${ownerId}/${columnName}?max=${max}&offset=${offset}&formId=${formId}`,
    {}, { useCache });
}
 
export async function populateDomainData<T extends SaveRecordProps>(props: PopulateDomainDataProps<T>): Promise<T[]> {
  const { formId, domainName, objects } = props;
  return requestUrlAndGetPromise(`${SERVER_URL}/batch/populate/${formId}/${getRawDomainName(domainName)}`, {
    method: 'POST',
    headers: buildAuthorizationHeader(),
    body: JSON.stringify(objects)
  }, { useCache: false });
}
 
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function saveDomain(props: SaveDomainProps): Promise<any> {
  const {
    domainName, values, url, method, successCallback, failCallback,
    skipInfoNotification
  } = props;
  const headers = buildAuthorizationHeader();
  const dn = i18n.t(`domainTitle:${capitalizeFirstLetter(typeWithPackageToSimpleType(domainName))}`);
  const parameters = {
    method, headers,
    // eslint-disable-next-line  @typescript-eslint/no-explicit-any
    body: JSON.stringify(values, (k: string, v: any) => {
      return v === undefined ? null : v;
    })
  };
  return await _fetch(url, parameters).then(response => {
    Iif (!response.ok) {
      response.json().then((errorInfo) => {
        const errorMsg = ("message" in errorInfo) ? errorInfo.message : JSON.stringify(errorInfo);
        failCallback(errorInfo);
        openErrorNotification(i18n.t('Save failed', { domainTitle: dn, msg: errorMsg }));
        return errorInfo;
      });
    } else {
      return response.json().then((r: SaveRecordResponseProps) => {
        Iif (Array.isArray(r.data)) {
          r.data.forEach((d) => deleteLabelFromCache(domainName, d.id));
        } else {
          const id = r?.data?.id;
          if (id) {
            deleteLabelFromCache(domainName, id);
          }
        }
        const msg = r.message;
        if (r.status === "success") {
          successCallback(r.data);
          Eif (!(skipInfoNotification === true)) {
            openInfoNotification(i18n.t('Save success', { domainTitle: dn }));
          }
        } else if (r.status === "warning") {
          successCallback(r.data);
          openWarningNotificationWithDescription(
            i18n.t('Save success with warnings', { domainTitle: dn, msg: '' }), msg
          );
        } else Eif (r.status === "error") {
          failCallback(r.data);
          openErrorNotification(
            msg, i18n.t('Save failed', { domainTitle: dn, msg: '' })
          );
        }
        return r.data;
      });
    }
  }).catch(error => {
    const msg = (error.status === 422) ?
      (error?.body?._embedded?.errors?.map((e: { message: string }) => e?.message)?.join("<br/>")) :
      (error.message == null || error.message === '') ? error?.body?.message : error?.message;
    console.warn(`Error update ${JSON.stringify(domainName)} with value ${JSON.stringify(values)} to server: ${JSON.stringify(error)}`);
    failCallback(error);
    openErrorNotification(msg, i18n.t('Save failed', { domainTitle: dn, msg: '' }));
  });
}
 
// noinspection JSUnusedLocalSymbols
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function onFinishFailed(errorInfo: ValidateErrorEntity): void {
  openErrorNotification(i18n.t('Save failed validation error'));
}
 
/**
 * 调用后台的唯一校验接口,校验用户输入的字段
 * 如果 id 参数为空,则表示是新建的校验,这种情况下,传递到后台的 id 会是 -1
 * @param props 校验的相关参数,包含 domain 名称,id,列名和用户输入的值
 */
export async function validConstrain(props: {
  domainName: string; id: number | undefined;
  column: TableMetaProps;
  value: string | number | RecordProps | null;
  record: SaveRecordProps | undefined;
  validationType: "validate" | "unique";
  formValues: SaveRecordProps;
}): Promise<{ valid: boolean; message?: string }> {
  const { domainName, id, column, value, record, validationType, formValues } = props;
  const { key: columnName } = column;
  const headers = buildAuthorizationHeader();
  const escapedValue = (value == null) ? '' : encodeURIComponent(typeof value === 'string' || typeof value === 'number' ? value : value.id);
  const url = `${SERVER_URL}/${validationType}/${getRawDomainName(domainName)}/${columnName}?create=${id == null}&id=${id == null ? -1 : id}&v=${escapedValue}`;
  return await _fetch(url, {
    headers,
    method: 'POST',
    body: JSON.stringify({
      record: record,
      unique: column.unique,
      formValues: formValues
    }),
  }).then(response => {
    Iif (!response.ok) {
      openErrorNotification(i18n.t('Valid unique constraint failed for field', { columnName }));
      throw Error(response.statusText);
    } else {
      return response;
    }
  }).then((response) => response.json());
}
 
export async function refreshColumnMeta(props: {
  domainName: string;
  currentObjValue: SaveRecordProps | undefined;
  sourceFieldName: string;
  destFieldName: string;
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  sourceFieldValue: string | number | Array<any> | undefined;
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  destFieldValue: string | number | Array<any> | undefined;
}): Promise<RefreshColumnMetaResultProps> {
  const {
    domainName, currentObjValue, sourceFieldValue, destFieldName,
    destFieldValue, sourceFieldName
  } = props;
  const url = `${SERVER_URL}/column/refresh/${getRawDomainName(domainName)}/${sourceFieldName}/${destFieldName}`;
  const headers = buildAuthorizationHeader();
  const parameters = {
    method: 'POST',
    body: JSON.stringify({
      object: currentObjValue,
      [sourceFieldName]: sourceFieldValue,
      [destFieldName]: destFieldValue,
    }),
    headers,
  };
  return await _fetch(url, parameters).then(response => {
    return response.json();
  }).then(json => {
    return json;
  });
}
 
export async function refreshDynamicColumnMeta(props: {
  domainName: string;
  currentObjValue: SaveRecordProps | undefined;
  sourceFieldName: string;
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  sourceFieldValue?: string | number | Array<any>;
}): Promise<RefreshDynamicColumnMetaResultProps> {
  const {
    domainName, currentObjValue, sourceFieldName
  } = props;
  const url = `${SERVER_URL}/column/refreshAll/${getRawDomainName(domainName)}/${sourceFieldName}`;
  const headers = buildAuthorizationHeader();
  const parameters = {
    method: 'POST',
    body: JSON.stringify(currentObjValue),
    headers,
  };
  return await _fetch(url, parameters).then(response => {
    return response.json();
  }).then(json => {
    return json;
  });
}
 
export async function deleteRecord(props: DeletionProps): Promise<void> {
  //FIXME add status and error message as a parameter to callback and move
  //display of notification to DeleteComponent
  const { domainName, id, callback } = props;
  const headers = buildAuthorizationHeader();
  const dn = i18n.t(`domainTitle:${capitalizeFirstLetter(typeWithPackageToSimpleType(domainName))}`);
  return await _fetch(`${SERVER_URL}/${getRawDomainName(domainName)}/${id}`, {
    method: 'DELETE', headers
  }).then(r => {
    return r.json();
  }).then((r: DeleteRecordResponseProps) => {
    if (r.status === "success") {
      const title = i18n.t('Delete success', { domainTitle: dn });
      callback({ id });
      openInfoNotification(title);
    } else if (r.status === "warning") {
      const title = i18n.t('Delete success with warnings', { domainTitle: dn });
      callback({ id });
      openWarningNotification(`${title}: ${r.message}`);
    } else Eif (r.status === "error") {
      const title = i18n.t("Delete failed", { domainTitle: dn });
      openErrorNotification(`${title}: ${r.message}`);
    }
  }).catch(error => {
    console.error(`Error delete domain ${getRawDomainName(domainName)} with id ${id}: ${error.message}: ${error.stack}`);
    const errorMessage = i18n.t("Delete failed with id", {
      domainTitle: dn,
      msg: error.message,
      id: id
    });
    openErrorNotification(errorMessage);
  });
}
 
export async function executeDynamicAction(
  props: {
    domainName: string;
    actionId: number;
    formValues: RecordProps;
    ids?: Array<number>;
    ownerClass?: string;
    ownerId?: number;
    columnNameInOwnerClass?: string;
    isFinalRound?: boolean;
    fineTuningResult?: string;
  }
): Promise<ExecResultsProps> {
  const {
    domainName, actionId, formValues, ids, fineTuningResult,
    ownerClass, ownerId, columnNameInOwnerClass, isFinalRound,
  } = props;
  const url = `${SERVER_URL}/execute/${actionId}/${getRawDomainName(domainName)}`;
  const isFinalRoundCnst = isFinalRound ?? true;
  const hasOwnerInfo = (columnNameInOwnerClass != null && ownerClass != null && ownerId != null);
  const body = hasOwnerInfo?
    { ids, formValues, ownerId, ownerClass, columnNameInOwnerClass, isFinalRound: isFinalRoundCnst, fineTuningResult } :
    { ids, formValues, isFinalRound: isFinalRoundCnst, fineTuningResult };
  return await requestUrlAndGetPromise(url, {
    method: 'POST', body: JSON.stringify(body)
  });
}
 
export async function fetchEnabledActionsForClass(
  props: {
    domainName: string;
    type: ActionMode;
    ids?: Array<number>;
    ownerClass?: string;
    ownerId?: number;
    columnNameInOwnerClass?: string;
  }
): Promise<Array<ActionProps>> {
  const {
    domainName, ids, type,
    ownerClass, ownerId, columnNameInOwnerClass
  } = props;
  return await requestUrlAndGetPromise(
    `${SERVER_URL}/actions/${getRawDomainName(domainName)}/${type}`, {
      method: 'POST', body: JSON.stringify({ ids, ownerClass, ownerId, columnNameInOwnerClass })
  });
}
 
//export async function executeAction(domainName: string, actionName, { parameters });
//export async function fetchActionByName(domainName: string, actionName);
 
export async function fetchCanUpdateDeleteObjects(
  domainName: string, ids?: Array<number>
): Promise<Record<number, Array<ActionProps>>> {
  return await requestUrlAndGetPromise(`${SERVER_URL}/actions/${getRawDomainName(domainName)}`, {
    method: 'POST', body: JSON.stringify(ids),
  });
}
 
export async function fetchCanCreate(domainName: string): Promise<CreateActionProps> {
  return requestUrlAndGetPromise(`${SERVER_URL}/actions/${getRawDomainName(domainName)}/canCreate`, {});
}
 
export async function fetchFormIdAndExtInfoByType(domainName: string, formType: FormType): Promise<FormProps> {
  const url = `${SERVER_URL}/form/${getRawDomainName(domainName)}/type/${formType}`;
  return requestUrlAndGetPromise(url, {});
}
 
export async function fetchFormIdAndExtInfoByName(domainName: string, formName: string): Promise<FormProps> {
  const url = `${SERVER_URL}/form/${getRawDomainName(domainName)}/name/${formName}`;
  return requestUrlAndGetPromise(url, {});
}
 
export async function fetchFormExtInfo(formId: number): Promise<FormProps> {
  const url = `${SERVER_URL}/form/${formId}`;
  return requestUrlAndGetPromise(url, {});
}
 
export async function fetchFormFieldGroups(
  domainName: string, formId: number, objectId?: number
): Promise<Array<GroupMetaProps>> {
  const baseUrl = `${SERVER_URL}/domain/${getRawDomainName(domainName)}/formGroups/${formId}`;
  const url = (objectId != null) ? `${baseUrl}/${objectId}` : baseUrl;
  return requestUrlAndGetPromise(url, {});
}
 
export async function validLogin(): Promise<LoginResultProps> {
  return requestUrlAndGetPromise(`${SERVER_URL}/api/validate`, { method: 'POST' });
}
 
export async function getMenu(parentMenuId: number): Promise<Array<DynamicMenuProps>> {
  return requestUrlAndGetPromise(`${SERVER_URL}/menu/sub/${parentMenuId}`, {});
}
 
const SEARCH_RESULT_CACHE = new Map<string, Promise<DataApiResultProps>>();
 
export async function fetchSearchResult(props: FetchSearchResultProps): Promise<DataApiResultProps> {
  const {
    domainName,
    ownerIds,
    ownerClass,
    ownerClassColumnName,
    searchConditions,
    current,
    max,
    fetchType,
    isFullTextSearch,
    sortDirection,
    sortField,
    fullTextConditions,
    columnNameInOwnerClass,
    queryJson,
    useCache,
  } = props;
  const endPoint = (isFullTextSearch) ? "fullTextSearch" : "search";
  const offset = current <= 0 ? 0 : max * (current - 1);
  const hasSortInfo = (sortField != null && sortDirection != null);
  const baseUrl = `${SERVER_URL}/${endPoint}/${getRawDomainName(domainName)}?max=${max}&offset=${offset}`;
  const u1 = (ownerClass != null) ? `${baseUrl}&ownerClass=${ownerClass}` : baseUrl;
  const u2 = (ownerIds != null && ownerIds.length > 0) ? `${u1}&ownerId=${ownerIds?.join(",")}` : u1;
  const u3 = (columnNameInOwnerClass != null) ? `${u2}&columnNameInOwnerClass=${columnNameInOwnerClass}` : u2;
  const u4 = (ownerClassColumnName != null) ? `${u3}&ownerClassColumnName=${ownerClassColumnName}` : u3;
  const u5 = (hasSortInfo) ? `${u4}&sortField=${sortField}&sortDirection=${sortDirection}` : u4;
  const url = (fetchType != null) ? `${u5}&fetchType=${fetchType}` : u5;
  Iif (useCache) {
    const key = `${url}:${JSON.stringify(searchConditions)}:${JSON.stringify(fullTextConditions)}:${JSON.stringify(queryJson)}`;
    const cached = SEARCH_RESULT_CACHE.get(key);
    if (cached) {
      return cached;
    }
    const promise = requestUrlAndGetPromise(url, {
      method: "POST",
      body: JSON.stringify({ searchConditions, fullTextConditions, queryJson }),
    });
    promise.then(() => setTimeout(() => SEARCH_RESULT_CACHE.delete(key), 1000));
    SEARCH_RESULT_CACHE.set(key, promise);
    return promise;
  }
  return await requestUrlAndGetPromise(url, {
    method: "POST",
    body: JSON.stringify({ searchConditions, fullTextConditions, queryJson }),
  });
}
 
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const fetchJSONWithToken = async <ResponseBody,>(url: string, options = {}): Promise<Response> => {
  const token = getAccessToken();
  let optionsWithToken = options;
  if (token != null) {
    optionsWithToken = merge({}, options, {
      headers: {
        Authorization: `Bearer ${token}`
      }
    });
  }
  const response = await fetchJSON(url, optionsWithToken);
  return response.response;
};
 
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const shouldRefreshToken = (error: any): boolean => error.status === 401;
 
export const refreshToken = async (): Promise<void> => {
  const refreshToken = getRefreshToken();
  if (refreshToken == null) {
    return;
  }
  const url = new URL(`${SERVER_URL}/oauth/access_token`);
  const searchParams = new URLSearchParams();
  searchParams.append('grant_type', 'refresh_token');
  searchParams.append('refresh_token', refreshToken);
  url.search = searchParams.toString();
  const response = await fetchJSONWithToken(url.toString(), {
    method: 'POST',
    headers: new Headers({
      'Content-Type': 'application/x-www-form-urlencoded'
    })
  });
  if (response.ok) {
    saveTokens(await response.json());
  } else E{
    console.error("Refresh token failed, response: ", response);
    throw new Error("Refresh token failed");
  }
};
 
const _fetch = configureRefreshFetch({
  fetch: fetchJSONWithToken,
  shouldRefreshToken: shouldRefreshToken,
  refreshToken: refreshToken
});
 
export async function downloadAttachment(id: number, preview?: boolean): Promise<Blob> {
  const endpoint = preview ? "preview" : "attachment";
  const url = `${SERVER_URL}/${endpoint}/${id}`;
  return requestUrlAndGetPromise(url, {
    method: 'GET',
    headers: {
      'Accept': 'application/octet-stream'
    }
  }, { useCache: false, parseResponse: false })
    .then(res => res.blob()) as Promise<Blob>;
}
 
export async function uploadCsvs(props: {
  data: FormData, domainName: string
}): Promise<UploadCsvResponseProps | ErrorResponseProps> {
  const { data, domainName } = props;
  const url = `${SERVER_URL}/import/${getRawDomainName(domainName)}/csv`;
  return uploadFiles({ data, url });
}
 
export async function uploadFiles(props: {
  data: FormData; url: string
}): Promise<UploadCsvResponseProps> {
  const { url, data } = props;
  const headers = buildAuthorizationHeader();
  delete headers['Content-Type'];
  const response = await fetch(
    url,
    {
      headers,
      method: "POST",
      body: data
    }
  );
  if (response.ok) {
    return await response.json();
  } else E{
    console.warn("Error uploading file: ", response);
    return await response.json();
  }
}
 
export function getWizardMeta(wizardId: number): Promise<WizardMetaProps> {
  const url = `${SERVER_URL}/wizard/${wizardId}`;
  return requestUrlAndGetPromise(url, {});
}
 
export function getWizardStepMeta(stepId: number): Promise<Array<TableMetaProps>> {
  const url = `${SERVER_URL}/wizard/step/${stepId}`;
  return requestUrlAndGetPromise(url, {});
}
 
export async function postWizardStep(props: {
  stepId: number;
  formValues: SaveRecordProps;
  isLastStep: boolean
}): Promise<WizardStepResultProps> {
  const { stepId, formValues, isLastStep } = props;
  const url = `${SERVER_URL}/wizard/step/${stepId}`;
  return await _fetch(url, {
    headers: buildAuthorizationHeader(),
    method: 'POST',
    body: JSON.stringify({ formValues, isLastStep }),
  }).then(response => {
    if (!response.ok) {
      openErrorNotification(i18n.t('Failed to handle wizard step', { stepId }));
      throw Error(response.statusText);
    } else {
      return response;
    }
  }).then((response) => response.json());
}
 
export async function getDashboards(): Promise<Array<DashboardProps>> {
  return requestUrlAndGetPromise(`${SERVER_URL}/dashboard/list`, {}, { useCache: false });
}
 
export async function getDashboardWidgets(dashboardId: number): Promise<Array<DashboardWidgetProps>> {
  return requestUrlAndGetPromise(`${SERVER_URL}/dashboard/meta/${dashboardId}`, {}, { useCache: false });
}
 
export async function getDashboardWidgetData(widgetId: number): Promise<WidgetDataResponseProps> {
  return requestUrlAndGetPromise(`${SERVER_URL}/dashboard/widget/data/${widgetId}`, {}, { useCache: false });
}
 
export async function getDynamicFilters(domainName: string, displayDefault: boolean): Promise<Array<DynamicFilterResponseProps>> {
  return requestUrlAndGetPromise(`${SERVER_URL}/filter/${getRawDomainName(domainName)}?displayDefault=${displayDefault}`, {}, { useCache: false });
}
 
export async function getHasDefaultFilters(domainName: string): Promise<{ result: boolean }> {
  return requestUrlAndGetPromise(`${SERVER_URL}/filter/hasDefault/${getRawDomainName(domainName)}`, {}, { useCache: false });
}
 
export async function fetchMessageSummary(username: string): Promise<MessageSummaryResponseProps> {
  return requestUrlAndGetPromise(`${SERVER_URL}/${username}/messageSummary`, {}, { useCache: false });
}
 
export async function fetchDynamicActionParameters(props: { actionId: number; }): Promise<Array<TableMetaProps>> {
  const { actionId } = props;
  return requestUrlAndGetPromise(`${SERVER_URL}/action/${actionId}/parameters`, {}, { useCache: false });
}
 
export async function fetchTree(
  domainName: string, key: string | number,
  searchConditions?: SearchConditions, useCache?: boolean
): Promise<Array<ExtendDataNode>> {
  // 如果搜索条件为空, 则调用 GET 请求(可以缓存), 否则调用 POST 请求
  if (searchConditions == null || pureObjectIsEmpty(searchConditions)) {
    return requestUrlAndGetPromise(`${SERVER_URL}/tree/${getRawDomainName(domainName)}/${key}`, {}, { useCache: useCache ?? true });
  } else {
    return requestUrlAndGetPromise(`${SERVER_URL}/tree/${getRawDomainName(domainName)}/${key}`, {
      method: 'POST',
      headers: buildAuthorizationHeader(),
      body: JSON.stringify({ searchConditions })
    });
  }
}
 
export async function fetchDynamicThemeInfo(): Promise<ThemeInfoProps> {
  return requestUrlAndGetPromise(`${SERVER_URL}/theme/info`, {}, { useCache: false });
}
 
export async function updateAttachmentsDisplaySequence(ids: Array<number>): Promise<BaseResponseProps> {
  return requestUrlAndGetPromise(`${SERVER_URL}/attachment/sort`, {
    method: 'POST',
    headers: buildAuthorizationHeader(),
    body: JSON.stringify(ids)
  }, { useCache: false });
}
 
export async function updateAttachmentDisplayName(id: number, displayName: string): Promise<BaseResponseProps> {
  return requestUrlAndGetPromise(`${SERVER_URL}/attachment/rename`, {
    method: 'POST',
    headers: buildAuthorizationHeader(),
    body: JSON.stringify({id, displayName})
  }, { useCache: false });
}
 
export async function deleteAttachment(meta: AttachmentProps): Promise<BaseResponseProps> {
  return requestUrlAndGetPromise(`${SERVER_URL}/attachment/delete`, {
    method: 'POST',
    headers: buildAuthorizationHeader(),
    body: JSON.stringify(meta)
  }, { useCache: false });
}
 
export async function fetchRevisionCompareResult(request: RevisionCompareProps): Promise<RevisionCompareResult> {
  const { columnName, domainName, sourceId, targetId } = request;
  const prefix = `${SERVER_URL}/diff/${getRawDomainName(domainName)}/`;
  const url = (sourceId == null)?
    `${prefix}${targetId}/${columnName}` : `${prefix}${sourceId}/${targetId}/${columnName}`;
  return requestUrlAndGetPromise(url,{
    method: 'GET',
    headers: buildAuthorizationHeader()
  },{ useCache: true });
}
 
export async function fetchDomainLabels(domainFullName: string, domainIds: string[]): Promise<Map<string, string>> {
  const idsStr = domainIds.join(',');
  const result = await requestUrlAndGetPromise(`${SERVER_URL}/multiple/${domainFullName}/${idsStr}?fetchType=only_label_field`, {
    headers: buildAuthorizationHeader()
  }) as { label: string, id: string; }[];
  const domainMap = new Map<string, string>();
  result.forEach(domain => {
      domainMap.set(domain.id, domain.label);
    }
  );
  return domainMap;
}
 
export async function createRelatedDomainObject(props: {
  mainDomainName: string;
  mainDomainId: number;
  columnName: string;
  relatedDomainName: string;
  object: SaveRecordProps;
}): Promise<BaseResponseProps> {
  const { mainDomainName, mainDomainId, columnName, relatedDomainName, object } = props;
  return requestUrlAndGetPromise(`${SERVER_URL}/updateRelated/${mainDomainName}/${mainDomainId}/${columnName}/${relatedDomainName}`, {
    method: 'POST',
    headers: buildAuthorizationHeader(),
    body: JSON.stringify(object)
  }, { useCache: false });
}
 
export const getAvatarForUser = async (userId: number): Promise<AvatarData> => {
  return await requestUrlAndGetPromise(`${SERVER_URL}/attachment/avatar/${userId}`, {});
};