|
| 1 | +import { NextResponse } from 'next/server' |
| 2 | + |
| 3 | +export async function POST(request: Request) { |
| 4 | + try { |
| 5 | + const { domain, accessToken, title, limit = 50 } = await request.json() |
| 6 | + |
| 7 | + if (!domain) { |
| 8 | + return NextResponse.json({ error: 'Domain is required' }, { status: 400 }) |
| 9 | + } |
| 10 | + |
| 11 | + if (!accessToken) { |
| 12 | + return NextResponse.json({ error: 'Access token is required' }, { status: 400 }) |
| 13 | + } |
| 14 | + |
| 15 | + // Build the URL with query parameters |
| 16 | + const baseUrl = `https://${domain}/wiki/api/v2/pages` |
| 17 | + const queryParams = new URLSearchParams() |
| 18 | + |
| 19 | + if (limit) { |
| 20 | + queryParams.append('limit', limit.toString()) |
| 21 | + } |
| 22 | + |
| 23 | + if (title) { |
| 24 | + queryParams.append('title', title) |
| 25 | + } |
| 26 | + |
| 27 | + const queryString = queryParams.toString() |
| 28 | + const url = queryString ? `${baseUrl}?${queryString}` : baseUrl |
| 29 | + |
| 30 | + console.log(`Fetching Confluence pages from: ${url}`) |
| 31 | + |
| 32 | + // Make the request to Confluence API |
| 33 | + const response = await fetch(url, { |
| 34 | + method: 'GET', |
| 35 | + headers: { |
| 36 | + 'Content-Type': 'application/json', |
| 37 | + Authorization: `Bearer ${accessToken}`, |
| 38 | + }, |
| 39 | + }) |
| 40 | + |
| 41 | + if (!response.ok) { |
| 42 | + console.error(`Confluence API error: ${response.status} ${response.statusText}`) |
| 43 | + let errorMessage |
| 44 | + |
| 45 | + try { |
| 46 | + const errorData = await response.json() |
| 47 | + errorMessage = errorData.message || `Failed to fetch Confluence pages (${response.status})` |
| 48 | + console.error('Error details:', errorData) |
| 49 | + } catch (e) { |
| 50 | + errorMessage = `Failed to fetch Confluence pages: ${response.status} ${response.statusText}` |
| 51 | + } |
| 52 | + |
| 53 | + return NextResponse.json({ error: errorMessage }, { status: response.status }) |
| 54 | + } |
| 55 | + |
| 56 | + const data = await response.json() |
| 57 | + console.log('Confluence API response:', JSON.stringify(data, null, 2).substring(0, 300) + '...') |
| 58 | + console.log(`Found ${data.results?.length || 0} pages`) |
| 59 | + |
| 60 | + if (data.results && data.results.length > 0) { |
| 61 | + console.log('First few pages:') |
| 62 | + data.results.slice(0, 3).forEach((page: any) => { |
| 63 | + console.log(`- ${page.id}: ${page.title}`) |
| 64 | + }) |
| 65 | + } |
| 66 | + |
| 67 | + return NextResponse.json({ |
| 68 | + files: data.results.map((page: any) => ({ |
| 69 | + id: page.id, |
| 70 | + name: page.title, |
| 71 | + mimeType: 'confluence/page', |
| 72 | + url: page._links?.webui || '', |
| 73 | + modifiedTime: page.version?.createdAt || '', |
| 74 | + spaceId: page.spaceId, |
| 75 | + webViewLink: page._links?.webui || '', |
| 76 | + })), |
| 77 | + }) |
| 78 | + } catch (error) { |
| 79 | + console.error('Error fetching Confluence pages:', error) |
| 80 | + return NextResponse.json( |
| 81 | + { error: (error as Error).message || 'Internal server error' }, |
| 82 | + { status: 500 } |
| 83 | + ) |
| 84 | + } |
| 85 | +} |
0 commit comments