UNPKG

prd-comment-server

Version:

fetch confluence page comments

73 lines (61 loc) 1.86 kB
/** * 获取指定页面的评论列表 * @param {string} pageId - Confluence 页面 ID * @param {string} token - Bearer token * @param {string} baseUrl - Confluence 基础 URL * @returns {Promise<Object>} 评论数据 */ declare global { var fetch: any; } export async function fetchComments( pageId: string, token: string, baseUrl: string ) { try { const allComments: string[] = []; let start = 0; const limit = 25; // 每页获取25条评论 let hasMore = true; baseUrl = baseUrl.replace(/\/$/, ""); while (hasMore) { const url = `${baseUrl}/rest/api/content/${pageId}/child/comment?expand=body.view&limit=${limit}&start=${start}`; const response = await fetch(url, { headers: { Accept: "application/json", Authorization: `Bearer ${token}`, "Content-Type": "application/json", }, method: "GET", }); if (!(response as any).ok) { throw new Error(`HTTP error! status: ${(response as any).status}`); } const data = await (response as any).json(); if (data.results.length === 0) { hasMore = false; break; } const results = data.results?.filter((c: any) => c.extensions?.location === "footer") || []; // 处理当前页的评论 const pageComments = results.map( (item: any, idx: number) => `评论 ${start + idx + 1}:\n${item.body.view.value}` ); allComments.push(...pageComments); // 检查是否还有更多评论 if (data.results.length < limit) { hasMore = false; } else { start += limit; } } return allComments.join("\n"); } catch (error) { // 由于 Node.js 环境下没有 console 对象,改用 process.stderr 输出错误信息 throw error; } }