Files
jiapuapp/services/api/business-dictionary-contract.js
T

45 lines
1.6 KiB
JavaScript

import { createRequestError } from './request-client.js'
import {
normalizeOptionalNonnegativeInteger,
normalizeOptionalNumericId,
normalizeResponseText
} from './response-normalizers.js'
const supportedDictionaryTypes = new Set([
'gen_ceremony_type',
'gen_growth_record_type'
])
export const normalizeBusinessDictionaryType = (dictType) => {
if (!supportedDictionaryTypes.has(dictType)) {
throw new TypeError('当前页面不支持该业务字典')
}
return dictType
}
export const normalizeBusinessDictionaryOptions = (dictType, value) => {
normalizeBusinessDictionaryType(dictType)
if (!Array.isArray(value)) {
throw createRequestError('业务字典响应不是列表', 'BUSINESS_DICTIONARY_RESPONSE_INVALID')
}
const values = new Set()
return value.map((item) => {
if (!item || typeof item !== 'object' || Array.isArray(item)) {
throw createRequestError('业务字典响应包含无效条目', 'BUSINESS_DICTIONARY_RESPONSE_INVALID')
}
const optionValue = normalizeResponseText(item.value, 'value')
const label = normalizeResponseText(item.label, 'label')
if (!optionValue || !label || values.has(optionValue)) {
throw createRequestError('业务字典响应缺少稳定选项', 'BUSINESS_DICTIONARY_RESPONSE_INVALID')
}
values.add(optionValue)
return {
code: normalizeOptionalNumericId(item.code, '业务字典编码', 'BUSINESS_DICTIONARY_RESPONSE_INVALID'),
value: optionValue,
label,
sort: normalizeOptionalNonnegativeInteger(item.sort, '业务字典排序值', 'BUSINESS_DICTIONARY_RESPONSE_INVALID'),
default: item.default === true
}
})
}