@@ -5,28 +5,12 @@ import {
55} from 'lucide-react' ;
66import toast from '../components/ui/Toast' ;
77import { timeAgo } from '../utils/formatters' ;
8- import { generateAvatar } from '../services/api' ;
8+ import api , { generateAvatar } from '../services/api' ;
99import socket from '../services/socket' ;
1010
11- const request = async ( endpoint , options = { } ) => {
12- const response = await fetch ( `/api/character${ endpoint } ` , {
13- headers : { 'Content-Type' : 'application/json' , ...options . headers } ,
14- ...options
15- } ) ;
16- let data = null ;
17- try { data = await response . json ( ) ; } catch { /* non-JSON body */ }
18- if ( ! response . ok ) {
19- const message = data ?. message || `Request failed with status ${ response . status } ` ;
20- const error = new Error ( message ) ;
21- error . status = response . status ;
22- throw error ;
23- }
24- return data ;
25- } ;
26-
27- const get = ( ) => request ( '' ) ;
28- const post = ( path , body ) => request ( path , { method : 'POST' , body : JSON . stringify ( body ) } ) ;
29- const put = ( body ) => request ( '' , { method : 'PUT' , body : JSON . stringify ( body ) } ) ;
11+ const charGet = ( ) => api . get ( '/character' ) ;
12+ const charPost = ( path , body ) => api . post ( `/character${ path } ` , body ) ;
13+ const charPut = ( body ) => api . put ( '/character' , body ) ;
3014
3115// D&D 5e XP thresholds (must match server)
3216const XP_THRESHOLDS = [
@@ -91,7 +75,7 @@ export default function CharacterSheet() {
9175 const load = useCallback ( async ( ) => {
9276 setLoadError ( null ) ;
9377 try {
94- const data = await get ( ) ;
78+ const data = await charGet ( ) ;
9579 if ( ! data || data . error ) {
9680 setLoadError ( 'Failed to load character data' ) ;
9781 return ;
@@ -143,7 +127,7 @@ export default function CharacterSheet() {
143127
144128 const handleDamage = async ( ) => {
145129 try {
146- const result = await post ( '/damage' , { diceNotation : dmgDice , description : dmgDesc || undefined } ) ;
130+ const result = await charPost ( '/damage' , { diceNotation : dmgDice , description : dmgDesc || undefined } ) ;
147131 setChar ( result . character ) ;
148132 setActiveAction ( null ) ;
149133 setDmgDice ( '1d6' ) ;
@@ -153,22 +137,22 @@ export default function CharacterSheet() {
153137
154138 const handleShortRest = async ( ) => {
155139 try {
156- const result = await post ( '/rest' , { type : 'short' } ) ;
140+ const result = await charPost ( '/rest' , { type : 'short' } ) ;
157141 setChar ( result . character ) ;
158142 } catch ( err ) { toast . error ( err . message || 'Failed to take short rest' ) ; }
159143 } ;
160144
161145 const handleLongRest = async ( ) => {
162146 try {
163- const result = await post ( '/rest' , { type : 'long' } ) ;
147+ const result = await charPost ( '/rest' , { type : 'long' } ) ;
164148 setChar ( result . character ) ;
165149 } catch ( err ) { toast . error ( err . message || 'Failed to take long rest' ) ; }
166150 } ;
167151
168152 const handleAddXp = async ( ) => {
169153 if ( ! xpAmount ) return ;
170154 try {
171- const result = await post ( '/xp' , { amount : Number ( xpAmount ) , source : 'manual' , description : xpDesc || undefined } ) ;
155+ const result = await charPost ( '/xp' , { amount : Number ( xpAmount ) , source : 'manual' , description : xpDesc || undefined } ) ;
172156 setChar ( result . character ) ;
173157 setActiveAction ( null ) ;
174158 setXpAmount ( '' ) ;
@@ -182,7 +166,7 @@ export default function CharacterSheet() {
182166 const body = { description : evtDesc } ;
183167 if ( evtXp ) body . xp = Number ( evtXp ) ;
184168 if ( evtDice ) body . diceNotation = evtDice ;
185- const result = await post ( '/event' , body ) ;
169+ const result = await charPost ( '/event' , body ) ;
186170 setChar ( result . character ) ;
187171 setActiveAction ( null ) ;
188172 setEvtDesc ( '' ) ;
@@ -194,7 +178,7 @@ export default function CharacterSheet() {
194178 const handleSync = async ( type ) => {
195179 setSyncing ( type ) ;
196180 try {
197- const result = await post ( `/sync/${ type } ` , { } ) ;
181+ const result = await charPost ( `/sync/${ type } ` , { } ) ;
198182 setChar ( result . character ) ;
199183 } catch ( err ) {
200184 toast . error ( err . message || `Failed to sync ${ type } ` ) ;
@@ -206,7 +190,7 @@ export default function CharacterSheet() {
206190 const handleNameSave = async ( ) => {
207191 try {
208192 if ( nameVal . trim ( ) && nameVal !== char . name ) {
209- const data = await put ( { name : nameVal . trim ( ) } ) ;
193+ const data = await charPut ( { name : nameVal . trim ( ) } ) ;
210194 setChar ( data ) ;
211195 }
212196 } catch ( err ) { toast . error ( err . message || 'Failed to save name' ) ; }
@@ -216,7 +200,7 @@ export default function CharacterSheet() {
216200 const handleClassSave = async ( ) => {
217201 try {
218202 if ( classVal . trim ( ) && classVal !== char . class ) {
219- const data = await put ( { class : classVal . trim ( ) } ) ;
203+ const data = await charPut ( { class : classVal . trim ( ) } ) ;
220204 setChar ( data ) ;
221205 }
222206 } catch ( err ) { toast . error ( err . message || 'Failed to save class' ) ; }
0 commit comments