@@ -12,6 +12,78 @@ interface ApiRequestOptions {
1212 expectJson ?: boolean ;
1313}
1414
15+ // Transform functions to convert database column names to TypeScript property names
16+ function transformCollection ( collection : any ) {
17+ if ( ! collection ) return collection ;
18+ return {
19+ id : collection . id ,
20+ name : collection . name ,
21+ description : collection . description ,
22+ userId : collection . userid || collection . userId , // Handle both formats
23+ createdAt : collection . createdat || collection . createdAt , // Handle both formats
24+ updatedAt : collection . updatedat || collection . updatedAt , // Handle both formats
25+ } ;
26+ }
27+
28+ function transformSnippet ( snippet : any ) {
29+ if ( ! snippet ) return snippet ;
30+ return {
31+ id : snippet . id ,
32+ title : snippet . title ,
33+ description : snippet . description ,
34+ code : snippet . code ,
35+ language : snippet . language ,
36+ tags : snippet . tags ,
37+ userId : snippet . userid || snippet . userId ,
38+ createdAt : snippet . createdat || snippet . createdAt ,
39+ updatedAt : snippet . updatedat || snippet . updatedAt ,
40+ viewCount : snippet . viewcount || snippet . viewCount ,
41+ isFavorite : snippet . isfavorite || snippet . isFavorite ,
42+ shareId : snippet . shareid || snippet . shareId ,
43+ isPublic : snippet . ispublic || snippet . isPublic ,
44+ } ;
45+ }
46+
47+ function transformComment ( comment : any ) {
48+ if ( ! comment ) return comment ;
49+ return {
50+ id : comment . id ,
51+ snippetId : comment . snippetid || comment . snippetId ,
52+ content : comment . content ,
53+ userId : comment . userid || comment . userId ,
54+ createdAt : comment . createdat || comment . createdAt ,
55+ updatedAt : comment . updatedat || comment . updatedAt ,
56+ } ;
57+ }
58+
59+ // Transform API responses based on URL patterns
60+ function transformApiResponse ( url : string , data : any ) {
61+ if ( ! data ) return data ;
62+
63+ // Handle array responses
64+ if ( Array . isArray ( data ) ) {
65+ if ( url . includes ( '/collections' ) ) {
66+ return data . map ( transformCollection ) ;
67+ } else if ( url . includes ( '/snippets' ) ) {
68+ return data . map ( transformSnippet ) ;
69+ } else if ( url . includes ( '/comments' ) ) {
70+ return data . map ( transformComment ) ;
71+ }
72+ return data ;
73+ }
74+
75+ // Handle single object responses
76+ if ( url . includes ( '/collections' ) ) {
77+ return transformCollection ( data ) ;
78+ } else if ( url . includes ( '/snippets' ) ) {
79+ return transformSnippet ( data ) ;
80+ } else if ( url . includes ( '/comments' ) ) {
81+ return transformComment ( data ) ;
82+ }
83+
84+ return data ;
85+ }
86+
1587export async function apiRequest < T = any > (
1688 method : string ,
1789 url : string ,
@@ -79,7 +151,10 @@ export async function apiRequest<T = any>(
79151 throw new Error ( json . message || `API error: ${ res . status } ` ) ;
80152 }
81153 console . log ( `[apiRequest] ${ method } ${ url } completed successfully` ) ;
82- return json ;
154+
155+ // Transform the response before returning
156+ const transformedJson = transformApiResponse ( url , json ) ;
157+ return transformedJson ;
83158 } else {
84159 // Not JSON, likely an error page
85160 if ( ! res . ok ) {
@@ -114,7 +189,8 @@ export const getQueryFn: <T>(options: {
114189 headers [ "Authorization" ] = `Bearer ${ token } ` ;
115190 }
116191
117- const res = await fetch ( queryKey [ 0 ] as string , {
192+ const url = queryKey [ 0 ] as string ;
193+ const res = await fetch ( url , {
118194 headers,
119195 credentials : "include" ,
120196 } ) ;
@@ -124,7 +200,10 @@ export const getQueryFn: <T>(options: {
124200 }
125201
126202 await throwIfResNotOk ( res ) ;
127- return await res . json ( ) ;
203+ const json = await res . json ( ) ;
204+
205+ // Transform the response before returning
206+ return transformApiResponse ( url , json ) ;
128207 } ;
129208
130209export const queryClient = new QueryClient ( {
0 commit comments