ConvertRgeistry.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. return {
  42. success: true,
  43. result: source?.split(',') || [],
  44. }
  45. return {
  46. success: false,
  47. convertFailMessage: `[${key}] 不是字符串类型`,
  48. };
  49. }
  50. })
  51. DataConverter.registerConverter({
  52. key: 'CommaArrayMerge',
  53. targetType: 'commaArrayMerge',
  54. converter: (source, key, type) => {
  55. if (source instanceof Array)
  56. return {
  57. success: true,
  58. result: source?.join(',') || '',
  59. }
  60. return {
  61. success: false,
  62. convertFailMessage: `[${key}] 不是数组类型`,
  63. };
  64. }
  65. })
  66. DataConverter.registerConverter({
  67. key: 'ForceArray',
  68. targetType: 'forceArray',
  69. converter: (source, key, type) => {
  70. if (source instanceof Array)
  71. return {
  72. success: true,
  73. result: source,
  74. }
  75. if (typeof source === 'object' && source !== null) {
  76. const arr = []
  77. for (const key in source) {
  78. arr.push((source as Record<string, any>)[key])
  79. }
  80. return {
  81. success: true,
  82. result: arr,
  83. }
  84. }
  85. if (typeof source === 'string')
  86. return {
  87. success: true,
  88. result: source.split(','),
  89. }
  90. return {
  91. success: false,
  92. convertFailMessage: `[${key}] 不是数组类型`,
  93. };
  94. }
  95. })
  96. }