import { normalizeAppArticle, normalizeAppArticles, normalizeArticleCategoryOptions, normalizeArticleCreatePayload } from './family-article-contract.js' import { normalizeGenealogyPathId } from './genealogy-contract.js' import { normalizeResourcePathId } from './request-normalizers.js' import { contentAccessHeader, normalizeContentAccessGrant, normalizeContentPasswordPayload } from './protected-content-contract.js' import { requestStrict } from './request-client.js' const requestArticleContentProtection = async ({ genealogyId, articleId, method, data, requestOptions }) => { return requestStrict({ url: `/genealogy/app/genealogies/${genealogyId}/articles/${articleId}/content-protection`, method, ...(data === undefined ? {} : { data }) }, { requireData: false, requestController: requestOptions.requestController ?? null }) } export const familyArticleApi = { async createArticle(genealogyId, payload, requestOptions = {}) { const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId) return requestStrict({ url: `/genealogy/app/genealogies/${normalizedGenealogyId}/articles`, method: 'POST', data: normalizeArticleCreatePayload(payload) }, { requestController: requestOptions.requestController ?? null }) }, async getArticles(genealogyId, requestOptions = {}) { const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId) const articles = await requestStrict({ url: `/genealogy/app/genealogies/${normalizedGenealogyId}/articles`, method: 'GET' }, { requestController: requestOptions.requestController ?? null }) return normalizeAppArticles(articles, normalizedGenealogyId) }, async getArticleDetail(genealogyId, articleId, accessToken = '', requestOptions = {}) { const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId) const normalizedArticleId = normalizeResourcePathId(articleId, '谱文标识') const article = await requestStrict({ url: `/genealogy/app/genealogies/${normalizedGenealogyId}/articles/${normalizedArticleId}`, method: 'GET', header: contentAccessHeader(accessToken) }, { requestController: requestOptions.requestController ?? null }) return normalizeAppArticle(article, normalizedGenealogyId, normalizedArticleId) }, async updateArticle(genealogyId, articleId, payload, requestOptions = {}) { const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId) const normalizedArticleId = normalizeResourcePathId(articleId, '谱文标识') const article = await requestStrict({ url: `/genealogy/app/genealogies/${normalizedGenealogyId}/articles/${normalizedArticleId}`, method: 'PUT', data: normalizeArticleCreatePayload(payload) }, { requestController: requestOptions.requestController ?? null }) return normalizeAppArticle(article, normalizedGenealogyId, normalizedArticleId) }, async deleteArticle(genealogyId, articleId, requestOptions = {}) { const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId) const normalizedArticleId = normalizeResourcePathId(articleId, '谱文标识') await requestStrict({ url: `/genealogy/app/genealogies/${normalizedGenealogyId}/articles/${normalizedArticleId}`, method: 'DELETE' }, { requireData: false, requestController: requestOptions.requestController ?? null }) return null }, async setArticlePassword(genealogyId, articleId, password, requestOptions = {}) { const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId) const normalizedArticleId = normalizeResourcePathId(articleId, '谱文标识') const passwordPayload = normalizeContentPasswordPayload(password) await requestArticleContentProtection({ genealogyId: normalizedGenealogyId, articleId: normalizedArticleId, method: 'PUT', data: passwordPayload, requestOptions }) return null }, async unlockArticle(genealogyId, articleId, password, requestOptions = {}) { const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId) const normalizedArticleId = normalizeResourcePathId(articleId, '谱文标识') const passwordPayload = normalizeContentPasswordPayload(password) const accessGrant = await requestStrict({ url: `/genealogy/app/genealogies/${normalizedGenealogyId}/articles/${normalizedArticleId}/content-unlock`, method: 'POST', data: passwordPayload }, { requestController: requestOptions.requestController ?? null }) return normalizeContentAccessGrant(accessGrant) }, async disableArticlePassword(genealogyId, articleId, requestOptions = {}) { const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId) const normalizedArticleId = normalizeResourcePathId(articleId, '谱文标识') await requestArticleContentProtection({ genealogyId: normalizedGenealogyId, articleId: normalizedArticleId, method: 'DELETE', requestOptions }) return null }, async getArticleCategories(genealogyId, requestOptions = {}) { const normalizedGenealogyId = normalizeGenealogyPathId(genealogyId) const categories = await requestStrict({ url: `/genealogy/app/genealogies/${normalizedGenealogyId}/article-categories`, method: 'GET' }, { requestController: requestOptions.requestController ?? null }) return normalizeArticleCategoryOptions(categories) } }