11import { ApiResponse } from './ApiResponses' ;
22
3- export const SCEVENTS_API_URL = 'http://localhost:8002' ;
3+ export function getSCEventsBaseUrl ( ) {
4+ return (
5+ ( typeof process !== 'undefined' && process . env . REACT_APP_SCEVENTS_URL ) ||
6+ 'http://localhost:8002'
7+ ) ;
8+ }
49
5- export async function getAllSCEvents ( ) {
6- let status = new ApiResponse ( ) ;
10+ function eventsUrl ( path ) {
11+ const base = getSCEventsBaseUrl ( ) . replace ( / \/ $ / , '' ) ;
12+ const p = path . startsWith ( '/' ) ? path : `/${ path } ` ;
13+ return `${ base } ${ p } ` ;
14+ }
715
16+ async function readBodyAsJsonOrText ( res ) {
17+ const text = await res . text ( ) ;
18+ if ( ! text ) {
19+ return null ;
20+ }
821 try {
9- const url = new URL ( '/events/' , SCEVENTS_API_URL ) ;
10- const res = await fetch ( url . href , {
11- method : 'GET' ,
12- headers : {
13- 'Content-Type' : 'application/json' ,
14- } ,
15- } ) ;
22+ return JSON . parse ( text ) ;
23+ } catch {
24+ return text ;
25+ }
26+ }
1627
17- if ( res . ok ) {
18- const result = await res . json ( ) ;
19- status . responseData = result ;
20- } else {
28+ export async function getAllSCEvents ( ) {
29+ const status = new ApiResponse ( ) ;
30+ try {
31+ const res = await fetch ( eventsUrl ( '/events/' ) ) ;
32+ status . responseData = await readBodyAsJsonOrText ( res ) ;
33+ if ( ! res . ok ) {
2134 status . error = true ;
2235 }
2336 } catch ( err ) {
24- status . error = true ;
2537 status . responseData = err ;
38+ status . error = true ;
2639 }
40+ return status ;
41+ }
2742
43+ export async function getEventByID ( id ) {
44+ const status = new ApiResponse ( ) ;
45+ try {
46+ const res = await fetch ( eventsUrl ( `/events/${ id } ` ) ) ;
47+ status . responseData = await readBodyAsJsonOrText ( res ) ;
48+ if ( ! res . ok ) {
49+ status . error = true ;
50+ }
51+ } catch ( err ) {
52+ status . responseData = err ;
53+ status . error = true ;
54+ }
2855 return status ;
2956}
3057
3158export async function createSCEvent ( eventBody ) {
32- let status = new ApiResponse ( ) ;
59+ const status = new ApiResponse ( ) ;
3360 status . statusCode = null ;
3461 try {
35- const url = new URL ( '/events/' , SCEVENTS_API_URL ) ;
36- const res = await fetch ( url . href , {
62+ const url = eventsUrl ( '/events/' ) ;
63+ const res = await fetch ( url , {
3764 method : 'POST' ,
3865 headers : {
3966 'Content-Type' : 'application/json' ,
4067 } ,
4168 body : JSON . stringify ( eventBody ) ,
4269 } ) ;
4370 status . statusCode = res . status ;
44- const body = await res . json ( ) ;
71+ const body = await readBodyAsJsonOrText ( res ) ;
4572 if ( res . ok ) {
4673 status . responseData = body ;
4774 } else {
@@ -56,29 +83,3 @@ export async function createSCEvent(eventBody) {
5683 }
5784 return status ;
5885}
59-
60- export async function getEventByID ( id ) {
61- let status = new ApiResponse ( ) ;
62-
63- try {
64- const url = new URL ( `/events/${ id } ` , SCEVENTS_API_URL ) ;
65- const res = await fetch ( url . href , {
66- method : 'GET' ,
67- headers : {
68- 'Content-Type' : 'application/json' ,
69- } ,
70- } ) ;
71-
72- if ( res . ok ) {
73- const result = await res . json ( ) ;
74- status . responseData = result ;
75- } else {
76- status . error = true ;
77- }
78- } catch ( err ) {
79- status . error = true ;
80- status . responseData = err ;
81- }
82-
83- return status ;
84- }
0 commit comments