1- import { NextRequest , NextResponse } from 'next/server' ;
1+ import { NextRequest } from 'next/server' ;
22import { withAuth , AuthenticatedApiHandler } from '@/lib/withAuth' ;
33import { hasApiAccess } from '@/lib/userService' ;
44import { HTTP_METHODS } from '@/constants/httpMethods' ;
5- import { sendForbidden , sendInternalError , proxyResponse } from '@/utils/apiResponses' ;
5+ import { sendForbidden , sendInternalError , proxyResponse , sendError } from '@/utils/apiResponses' ;
66
7- const API_BASE_URL = process . env . API_BASE_URL || 'http://localhost:5000' ;
7+ const API_BASE_URL = process . env . API_BASE_URL ;
88
99const getHandler : AuthenticatedApiHandler = async ( req : NextRequest , context , auth ) => {
1010 try {
@@ -21,15 +21,12 @@ const getHandler: AuthenticatedApiHandler = async (req: NextRequest, context, au
2121 } ,
2222 } ) ;
2323
24+ const data = await response . json ( ) ;
25+
2426 if ( ! response . ok ) {
25- const data = await response . json ( ) ;
26- return NextResponse . json (
27- { success : false , error : data . error || 'Failed to fetch user' } ,
28- { status : response . status }
29- ) ;
27+ return sendError ( response . status , data . error || 'Failed to fetch user' ) ;
3028 }
3129
32- const data = await response . json ( ) ;
3330 return proxyResponse ( data ) ;
3431 } catch ( error ) {
3532 console . error ( 'Error fetching user:' , error ) ;
@@ -55,15 +52,12 @@ const putHandler: AuthenticatedApiHandler = async (req: NextRequest, context, au
5552 body : JSON . stringify ( body ) ,
5653 } ) ;
5754
55+ const data = await response . json ( ) ;
56+
5857 if ( ! response . ok ) {
59- const data = await response . json ( ) ;
60- return NextResponse . json (
61- { success : false , error : data . error || 'Failed to update user' } ,
62- { status : response . status }
63- ) ;
58+ return sendError ( response . status , data . error || 'Failed to update user' ) ;
6459 }
6560
66- const data = await response . json ( ) ;
6761 return proxyResponse ( data ) ;
6862 } catch ( error ) {
6963 console . error ( 'Error updating user:' , error ) ;
@@ -86,15 +80,12 @@ const deleteHandler: AuthenticatedApiHandler = async (req: NextRequest, context,
8680 } ,
8781 } ) ;
8882
83+ const data = await response . json ( ) ;
84+
8985 if ( ! response . ok ) {
90- const data = await response . json ( ) ;
91- return NextResponse . json (
92- { success : false , error : data . error || 'Failed to delete user' } ,
93- { status : response . status }
94- ) ;
86+ return sendError ( response . status , data . error || 'Failed to delete user' ) ;
9587 }
9688
97- const data = await response . json ( ) ;
9889 return proxyResponse ( data ) ;
9990 } catch ( error ) {
10091 console . error ( 'Error deleting user:' , error ) ;
0 commit comments