@@ -2,7 +2,7 @@ import { createLogger } from '@sim/logger'
22import { type NextRequest , NextResponse } from 'next/server'
33import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
44import { validateAlphanumericId , validateJiraCloudId } from '@/lib/core/security/input-validation'
5- import { getConfluenceCloudId } from '@/tools/confluence/utils'
5+ import { cleanHtmlContent , getConfluenceCloudId } from '@/tools/confluence/utils'
66
77const logger = createLogger ( 'ConfluencePageVersionsAPI' )
88
@@ -55,42 +55,79 @@ export async function POST(request: NextRequest) {
5555 return NextResponse . json ( { error : cloudIdValidation . error } , { status : 400 } )
5656 }
5757
58- // If versionNumber is provided, get specific version
58+ // If versionNumber is provided, get specific version with page content
5959 if ( versionNumber !== undefined && versionNumber !== null ) {
60- const url = `https://api.atlassian.com/ex/confluence/${ cloudId } /wiki/api/v2/pages/${ pageId } /versions/${ versionNumber } `
60+ const versionUrl = `https://api.atlassian.com/ex/confluence/${ cloudId } /wiki/api/v2/pages/${ pageId } /versions/${ versionNumber } `
61+ const pageUrl = `https://api.atlassian.com/ex/confluence/${ cloudId } /wiki/api/v2/pages/${ pageId } ?version=${ versionNumber } &body-format=storage`
6162
6263 logger . info ( `Fetching version ${ versionNumber } for page ${ pageId } ` )
6364
64- const response = await fetch ( url , {
65- method : 'GET' ,
66- headers : {
67- Accept : 'application/json' ,
68- Authorization : `Bearer ${ accessToken } ` ,
69- } ,
70- } )
71-
72- if ( ! response . ok ) {
73- const errorData = await response . json ( ) . catch ( ( ) => null )
65+ const [ versionResponse , pageResponse ] = await Promise . all ( [
66+ fetch ( versionUrl , {
67+ method : 'GET' ,
68+ headers : {
69+ Accept : 'application/json' ,
70+ Authorization : `Bearer ${ accessToken } ` ,
71+ } ,
72+ } ) ,
73+ fetch ( pageUrl , {
74+ method : 'GET' ,
75+ headers : {
76+ Accept : 'application/json' ,
77+ Authorization : `Bearer ${ accessToken } ` ,
78+ } ,
79+ } ) ,
80+ ] )
81+
82+ if ( ! versionResponse . ok ) {
83+ const errorData = await versionResponse . json ( ) . catch ( ( ) => null )
7484 logger . error ( 'Confluence API error response:' , {
75- status : response . status ,
76- statusText : response . statusText ,
85+ status : versionResponse . status ,
86+ statusText : versionResponse . statusText ,
7787 error : JSON . stringify ( errorData , null , 2 ) ,
7888 } )
79- const errorMessage = errorData ?. message || `Failed to get page version (${ response . status } )`
80- return NextResponse . json ( { error : errorMessage } , { status : response . status } )
89+ const errorMessage =
90+ errorData ?. message || `Failed to get page version (${ versionResponse . status } )`
91+ return NextResponse . json ( { error : errorMessage } , { status : versionResponse . status } )
8192 }
8293
83- const data = await response . json ( )
94+ const versionData = await versionResponse . json ( )
95+
96+ let title : string | null = null
97+ let content : string | null = null
98+ let body : Record < string , unknown > | null = null
99+
100+ if ( pageResponse . ok ) {
101+ const pageData = await pageResponse . json ( )
102+ title = pageData . title ?? null
103+ body = pageData . body ?? null
104+
105+ const rawContent =
106+ pageData . body ?. storage ?. value ||
107+ pageData . body ?. view ?. value ||
108+ pageData . body ?. atlas_doc_format ?. value ||
109+ ''
110+ if ( rawContent ) {
111+ content = cleanHtmlContent ( rawContent )
112+ }
113+ } else {
114+ logger . warn (
115+ `Could not fetch page content for version ${ versionNumber } : ${ pageResponse . status } `
116+ )
117+ }
84118
85119 return NextResponse . json ( {
86120 version : {
87- number : data . number ,
88- message : data . message ?? null ,
89- minorEdit : data . minorEdit ?? false ,
90- authorId : data . authorId ?? null ,
91- createdAt : data . createdAt ?? null ,
121+ number : versionData . number ,
122+ message : versionData . message ?? null ,
123+ minorEdit : versionData . minorEdit ?? false ,
124+ authorId : versionData . authorId ?? null ,
125+ createdAt : versionData . createdAt ?? null ,
92126 } ,
93127 pageId,
128+ title,
129+ content,
130+ body,
94131 } )
95132 }
96133 // List all versions
0 commit comments