@@ -19,25 +19,29 @@ const serializeParams = (params: RequestParams | null) => {
19
19
return searchParams . toString ( )
20
20
}
21
21
22
- const createUrl = (
23
- path : string ,
24
- params : RequestParams | null ,
25
- ) => {
22
+ const createUrl = ( path : string , params : RequestParams | null ) => {
26
23
const serializedParams = serializeParams ( params )
27
24
const queryString = serializedParams ? `?${ serializedParams } ` : ''
28
25
return `${ path } ${ queryString } `
29
26
}
30
27
31
- export const request = ( path : string , options ?: RequestOptions ) => {
28
+ export const request = async ( path : string , options ?: RequestOptions ) => {
32
29
const { params = null , ...requestInit } = options ?? { }
33
30
34
31
const url = createUrl ( path , params )
35
32
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
+ }
43
47
}
0 commit comments