25 lines
1.4 KiB
JavaScript
25 lines
1.4 KiB
JavaScript
import { createRequestError } from './request-client.js'
|
|
import { normalizeOptionalResponseText } from './response-normalizers.js'
|
|
|
|
export const normalizeContentPasswordPayload = (password) => {
|
|
if (typeof password !== 'string') throw new TypeError('内容密码必须是字符串')
|
|
if (password.length < 8 || password.length > 128) throw new TypeError('内容密码长度必须为8至128个字符')
|
|
return { password }
|
|
}
|
|
|
|
export const normalizeContentAccessGrant = (value) => {
|
|
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
throw createRequestError('内容解锁响应无效', 'CONTENT_ACCESS_GRANT_INVALID')
|
|
}
|
|
const accessToken = normalizeOptionalResponseText(value.accessToken, 'accessToken', '内容解锁响应', 'CONTENT_ACCESS_GRANT_INVALID')
|
|
const expiresAt = normalizeOptionalResponseText(value.expiresAt, 'expiresAt', '内容解锁响应', 'CONTENT_ACCESS_GRANT_INVALID')
|
|
if (!accessToken || !expiresAt) throw createRequestError('内容解锁响应缺少授权信息', 'CONTENT_ACCESS_GRANT_INVALID')
|
|
return { accessToken, expiresAt }
|
|
}
|
|
|
|
export const contentAccessHeader = (accessToken) => {
|
|
if (accessToken === undefined || accessToken === null || accessToken === '') return {}
|
|
if (typeof accessToken !== 'string' || !accessToken.trim()) throw new TypeError('内容访问令牌无效')
|
|
return { 'X-Content-Access-Token': accessToken.trim() }
|
|
}
|