ConvertRgeistry.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import {
  2. DATA_MODEL_ERROR_REQUIRED_KEY_MISSING,
  3. DATA_MODEL_ERROR_TRY_CONVERT_BAD_TYPE,
  4. DATA_MODEL_ERROR_PRINT_SOURCE,
  5. DATA_MODEL_ERROR_ARRAY_REQUIRED_KEY_MISSING,
  6. DATA_MODEL_ERROR_ARRAY_IS_NOT_ARRAY,
  7. DATA_MODEL_ERROR_MUST_PROVIDE_SIDE,
  8. DATA_MODEL_ERROR_REQUIRED_KEY_NULL,
  9. DataConverter,
  10. DataErrorFormatUtils,
  11. defaultDataErrorFormatHandler
  12. } from "@imengyu/js-request-transform";
  13. function setErrorFormatter() {
  14. DataErrorFormatUtils.setFormatHandler((error, data) => {
  15. switch (error) {
  16. case DATA_MODEL_ERROR_REQUIRED_KEY_MISSING:
  17. return `字段 ${data.sourceKey} 必填但未提供。 来源 ${data.source}; 对象 ${data.objectName} ${data.serverKey ? ('服务器应传字段: ' + data.serverKey) : ''}`;
  18. case DATA_MODEL_ERROR_TRY_CONVERT_BAD_TYPE:
  19. return `尝试将 ${data.sourceType} 转换为 ${data.targetType}。`;
  20. case DATA_MODEL_ERROR_PRINT_SOURCE:
  21. return `来源: ${data.objectName}.`;
  22. case DATA_MODEL_ERROR_ARRAY_REQUIRED_KEY_MISSING:
  23. return `转换数组模型失败: 需要的字段 ${data.sourceKey} 未提供。`
  24. case DATA_MODEL_ERROR_ARRAY_IS_NOT_ARRAY:
  25. return `转换数组模型失败: 需要的字段 ${data.sourceKey} 不是数组类型。`
  26. case DATA_MODEL_ERROR_MUST_PROVIDE_SIDE:
  27. return `转换字段 ${data.key} 失败: 必须提供 ${data.direction} 侧数据。`;
  28. case DATA_MODEL_ERROR_REQUIRED_KEY_NULL:
  29. return `转换字段 ${data.key} 失败: 必填字段 ${data.key} 未提供或者为 null。`;
  30. }
  31. return defaultDataErrorFormatHandler(error, data);
  32. });
  33. }
  34. export function registryConvert() {
  35. setErrorFormatter();
  36. DataConverter.registerConverter({
  37. key: 'SplitCommaArray',
  38. targetType: 'splitCommaArray',
  39. converter: (source, key, type) => {
  40. if (typeof source === 'string') {
  41. if (source === '')
  42. return { success: true, result: [] }
  43. return {
  44. success: true,
  45. result: source?.split(',') || [],
  46. }
  47. }
  48. return {
  49. success: false,
  50. convertFailMessage: `[${key}] 不是字符串类型`,
  51. };
  52. }
  53. })
  54. DataConverter.registerConverter({
  55. key: 'ArrayNumber',
  56. targetType: 'arrayInt',
  57. converter: (source, key, type) => {
  58. if (source instanceof Array) {
  59. const result = source.map((p) => {
  60. if (typeof p === 'number') return p;
  61. return parseInt(p);
  62. });
  63. return {
  64. success: true,
  65. result,
  66. }
  67. }
  68. return {
  69. success: false,
  70. convertFailMessage: `[${key}] 不是数组类型`,
  71. };
  72. }
  73. })
  74. DataConverter.registerConverter({
  75. key: 'CommaArrayMerge',
  76. targetType: 'commaArrayMerge',
  77. converter: (source, key, type) => {
  78. if (source instanceof Array)
  79. return {
  80. success: true,
  81. result: source?.join(',') || '',
  82. }
  83. return {
  84. success: false,
  85. convertFailMessage: `[${key}] 不是数组类型`,
  86. };
  87. }
  88. })
  89. DataConverter.registerConverter({
  90. key: 'ForceArray',
  91. targetType: 'forceArray',
  92. converter: (source, key, type) => {
  93. if (source instanceof Array)
  94. return {
  95. success: true,
  96. result: source,
  97. }
  98. if (typeof source === 'object' && source !== null) {
  99. const arr = []
  100. for (const key in source) {
  101. arr.push((source as Record<string, any>)[key])
  102. }
  103. return {
  104. success: true,
  105. result: arr,
  106. }
  107. }
  108. if (typeof source === 'string')
  109. return {
  110. success: true,
  111. result: source.split(','),
  112. }
  113. return {
  114. success: false,
  115. convertFailMessage: `[${key}] 不是数组类型`,
  116. };
  117. }
  118. })
  119. }