@@ -4,10 +4,9 @@ export const fetchWrapper = {
44
55async function ajax ( method , url , headers , params , options ) {
66 const base_header = new Headers ( headers ) ;
7- base_header . append ( "Content-Type" , "application/json" ) ;
87
9- if ( process . env . NODE_ENV != "production" ) {
10- base_header . append ( "x-debug " , "SuyPBUZPau1F4Jd" )
8+ if ( ! headers [ "Content-Type" ] ) {
9+ base_header . append ( "Content-Type " , "application/json" ) ;
1110 }
1211
1312 let requestOptions = < any > {
@@ -17,9 +16,13 @@ async function ajax(method, url, headers, params, options) {
1716
1817 if ( params ) {
1918 if ( method == 'GET' ) {
20- url += '?' + new URLSearchParams ( params )
19+ url += '?' + new URLSearchParams ( params )
2120 } else {
22- requestOptions . body = JSON . stringify ( params )
21+ if ( headers [ "Content-Type" ] && headers [ "Content-Type" ] == "application/x-www-form-urlencoded" ) {
22+ requestOptions . body = new URLSearchParams ( params )
23+ } else {
24+ requestOptions . body = JSON . stringify ( params )
25+ }
2326 }
2427 }
2528
@@ -29,11 +32,12 @@ async function ajax(method, url, headers, params, options) {
2932 requestOptions . headers = { ...requestOptions . headers , ...headers }
3033 }
3134 }
32- const response = await fetch ( url , requestOptions ) ;
33- return response
34- }
3535
36- async function handleResponse ( response ) {
37- const json = await response . json ( )
38- return json
39- }
36+ try {
37+ const response = await fetch ( url , requestOptions )
38+ return response
39+ } catch ( err ) {
40+ let r = new Response ( JSON . stringify ( { error : err . message } ) , { status : 500 , headers : new Headers ( { "Content-Type" : "application/json" } ) } )
41+ return r
42+ }
43+ }
0 commit comments