33 lines
1.5 KiB
JavaScript
33 lines
1.5 KiB
JavaScript
import { assertPlainPayload } from './request-normalizers.js'
|
|
|
|
export const FEEDBACK_TYPE_OPTIONS = Object.freeze([
|
|
Object.freeze({ value: 'bug', label: '功能问题' }),
|
|
Object.freeze({ value: 'advice', label: '使用建议' }),
|
|
Object.freeze({ value: 'complaint', label: '投诉反馈' }),
|
|
Object.freeze({ value: 'other', label: '其他' })
|
|
])
|
|
|
|
const feedbackTypes = new Set(FEEDBACK_TYPE_OPTIONS.map(({ value }) => value))
|
|
|
|
export const normalizeFeedbackPayload = (payload) => {
|
|
const allowedFields = new Set(['feedbackType', 'feedbackContent', 'contactInfo'])
|
|
assertPlainPayload(payload, allowedFields, '反馈请求')
|
|
if (typeof payload.feedbackContent !== 'string' || !payload.feedbackContent.trim()) {
|
|
throw new TypeError('反馈内容必须是非空字符串')
|
|
}
|
|
const normalizedPayload = { feedbackContent: payload.feedbackContent.trim() }
|
|
for (const optionalField of ['feedbackType', 'contactInfo']) {
|
|
if (!Object.prototype.hasOwnProperty.call(payload, optionalField)) continue
|
|
if (typeof payload[optionalField] !== 'string') {
|
|
throw new TypeError(`反馈字段 ${optionalField} 必须是字符串`)
|
|
}
|
|
const normalizedText = payload[optionalField].trim()
|
|
if (!normalizedText) continue
|
|
if (optionalField === 'feedbackType' && !feedbackTypes.has(normalizedText)) {
|
|
throw new TypeError('feedbackType 必须为 advice、bug、complaint 或 other')
|
|
}
|
|
normalizedPayload[optionalField] = normalizedText
|
|
}
|
|
return normalizedPayload
|
|
}
|