@@ -19,25 +19,29 @@ const serializeParams = (params: RequestParams | null) => {
1919 return searchParams . toString ( )
2020}
2121
22- const createUrl = (
23- path : string ,
24- params : RequestParams | null ,
25- ) => {
22+ const createUrl = ( path : string , params : RequestParams | null ) => {
2623 const serializedParams = serializeParams ( params )
2724 const queryString = serializedParams ? `?${ serializedParams } ` : ''
2825 return `${ path } ${ queryString } `
2926}
3027
31- export const request = ( path : string , options ?: RequestOptions ) => {
28+ export const request = async ( path : string , options ?: RequestOptions ) => {
3229 const { params = null , ...requestInit } = options ?? { }
3330
3431 const url = createUrl ( path , params )
3532
36- return window . fetch ( url , {
37- ...requestInit ,
38- headers : {
39- 'Content-Type' : 'application/json' ,
40- ...requestInit ?. headers ,
41- } ,
42- } )
33+ try {
34+ return await window . fetch ( url , {
35+ ...requestInit ,
36+ headers : {
37+ 'Content-Type' : 'application/json' ,
38+ ...requestInit ?. headers ,
39+ } ,
40+ } )
41+ } catch ( e ) {
42+ return new Response ( null , {
43+ status : 500 ,
44+ statusText : `Unknown error` ,
45+ } )
46+ }
4347}
0 commit comments