11import type { Complex , ComplexInput } from '../types/complex' ;
22
3- // 仮のユーザーID (認証基盤実装時に置き換える)
43const DUMMY_USER_ID = 'user-test-123' ;
5-
64const API_BASE_URL = '/api/v1' ;
75
86const getAuthHeaders = ( ) => ( {
97 'Content-Type' : 'application/json' ,
108 'X-User-ID' : DUMMY_USER_ID ,
119} ) ;
1210
13- /* コンプレックス一覧取得 */
11+ /* Get all complexes */
1412export const fetchComplexes = async ( ) : Promise < Complex [ ] > => {
1513 const response = await fetch ( `${ API_BASE_URL } /complexes` , {
1614 headers : getAuthHeaders ( ) ,
@@ -19,18 +17,36 @@ export const fetchComplexes = async (): Promise<Complex[]> => {
1917 return response . json ( ) ;
2018} ;
2119
22- /* 新しいコンプレックスを登録 */
20+ /* Create new complex */
2321export const createComplex = async ( data : ComplexInput ) : Promise < Complex > => {
2422 const response = await fetch ( `${ API_BASE_URL } /complexes` , {
2523 method : 'POST' ,
2624 headers : getAuthHeaders ( ) ,
2725 body : JSON . stringify ( data ) ,
2826 } ) ;
29- if ( ! response . ok ) throw new Error ( 'Failed to create complex' ) ;
27+ if ( ! response . ok ) {
28+ let errorDetails = '' ;
29+ try {
30+ const responseText = await response . text ( ) ;
31+ if ( responseText ) {
32+ try {
33+ const jsonData = JSON . parse ( responseText ) ;
34+ errorDetails = `: ${ JSON . stringify ( jsonData ) } ` ;
35+ } catch ( jsonError ) {
36+ errorDetails = `: ${ responseText } ` ;
37+ }
38+ }
39+ } catch ( readError ) {
40+ errorDetails = ': Failed to read error response body' ;
41+ }
42+ throw new Error (
43+ `Failed to create complex (status: ${ response . status } )${ errorDetails } `
44+ ) ;
45+ }
3046 return response . json ( ) ;
3147} ;
3248
33- // コンプレックス削除 (ComplexesPage.tsxで使用していたダミー関数を置き換え)
49+ /* Delete complex */
3450export const deleteComplex = async ( id : number ) : Promise < void > => {
3551 const response = await fetch ( `${ API_BASE_URL } /complexes/${ id } ` , {
3652 method : 'DELETE' ,
@@ -39,4 +55,10 @@ export const deleteComplex = async (id: number): Promise<void> => {
3955 if ( ! response . ok ) throw new Error ( 'Failed to delete complex' ) ;
4056} ;
4157
42- // TODO: 他のAPI関数 (fetchComplex, updateComplex, fetchGoals, createGoalなど) もここに追加
58+ /*
59+ * TODO: 他のAPI関数もここに追加
60+ * - fetchComplex
61+ * - updateComplex
62+ * - fetchGoals
63+ * - createGoal
64+ */
0 commit comments