|
| 1 | +interface DocumentData { |
| 2 | + documentId: string |
| 3 | + title: string |
| 4 | + description?: string |
| 5 | + slug: string |
| 6 | + isPrivate: boolean |
| 7 | + [key: string]: any |
| 8 | +} |
| 9 | + |
| 10 | +interface DocumentResponse { |
| 11 | + data: DocumentData |
| 12 | +} |
| 13 | + |
| 14 | +interface Session { |
| 15 | + user: { |
| 16 | + id: string |
| 17 | + } |
| 18 | + access_token?: string |
| 19 | +} |
| 20 | + |
| 21 | +interface DocumentWithClientId extends DocumentData { |
| 22 | + docClientId: string |
| 23 | +} |
| 24 | + |
| 25 | +class DocumentFetchError extends Error { |
| 26 | + constructor( |
| 27 | + message: string, |
| 28 | + public status?: number, |
| 29 | + public originalError?: Error |
| 30 | + ) { |
| 31 | + super(message) |
| 32 | + this.name = 'DocumentFetchError' |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +export async function fetchDocument( |
| 37 | + slug: string | undefined, |
| 38 | + session?: Session | null |
| 39 | +): Promise<DocumentWithClientId | null> { |
| 40 | + if (!slug?.trim()) return null |
| 41 | + |
| 42 | + if (!process.env.NEXT_PUBLIC_RESTAPI_URL) throw new DocumentFetchError('API URL not configured') |
| 43 | + |
| 44 | + try { |
| 45 | + const baseAPIUrl = `${process.env.NEXT_PUBLIC_RESTAPI_URL}/documents/${encodeURIComponent(slug)}` |
| 46 | + const url = session?.user?.id |
| 47 | + ? `${baseAPIUrl}?userId=${encodeURIComponent(session.user.id)}` |
| 48 | + : baseAPIUrl |
| 49 | + |
| 50 | + const headers: Record<string, string> = { |
| 51 | + 'Content-Type': 'application/json' |
| 52 | + } |
| 53 | + |
| 54 | + if (session?.access_token) { |
| 55 | + headers.token = session.access_token |
| 56 | + } |
| 57 | + |
| 58 | + const response = await fetch(url, { |
| 59 | + headers, |
| 60 | + method: 'GET' |
| 61 | + }) |
| 62 | + |
| 63 | + if (!response.ok) { |
| 64 | + throw new DocumentFetchError( |
| 65 | + `Failed to fetch document: ${response.statusText}`, |
| 66 | + response.status |
| 67 | + ) |
| 68 | + } |
| 69 | + |
| 70 | + const responseData: DocumentResponse = await response.json() |
| 71 | + |
| 72 | + if (!responseData?.data) { |
| 73 | + return null |
| 74 | + } |
| 75 | + |
| 76 | + const { data } = responseData |
| 77 | + |
| 78 | + // Validate required fields |
| 79 | + if (!data.documentId || typeof data.isPrivate !== 'boolean') { |
| 80 | + throw new DocumentFetchError('Invalid document data received') |
| 81 | + } |
| 82 | + |
| 83 | + const visibility = data.isPrivate ? 'private' : 'public' |
| 84 | + const docClientId = `${visibility}.${data.documentId}` |
| 85 | + |
| 86 | + return { |
| 87 | + ...data, |
| 88 | + docClientId |
| 89 | + } |
| 90 | + } catch (error) { |
| 91 | + if (error instanceof DocumentFetchError) { |
| 92 | + throw error |
| 93 | + } |
| 94 | + |
| 95 | + throw new DocumentFetchError( |
| 96 | + 'Network error while fetching document', |
| 97 | + undefined, |
| 98 | + error instanceof Error ? error : new Error(String(error)) |
| 99 | + ) |
| 100 | + } |
| 101 | +} |
0 commit comments