33
44export  default  async  function  bitsFetch ( data ,  action ,  queryParam  =  null ,  method  =  'POST' ,  signal )  { 
55  const  uri  =  new  URL ( typeof  btcbi  ===  'undefined'  ? bitFromsFront ?. ajaxURL  : btcbi . ajaxURL ) 
6-   uri . searchParams . append ( 'action' ,  `btcbi_${ action }  ) 
7-   uri . searchParams . append ( '_ajax_nonce' ,  typeof  btcbi  ===  'undefined'  ? ''  : btcbi . nonce ) 
6+ 
7+   if  ( method . toLowerCase ( )  ===  'get' )  { 
8+     uri . searchParams . append ( 'action' ,  `btcbi_${ action }  ) 
9+     uri . searchParams . append ( '_ajax_nonce' ,  typeof  btcbi  ===  'undefined'  ? ''  : btcbi . nonce ) 
10+   } 
811  // append query params in url 
912  if  ( queryParam )  { 
1013    for  ( const  key  in  queryParam )  { 
@@ -21,11 +24,25 @@ export default async function bitsFetch(data, action, queryParam = null, method
2124  } 
2225
2326  if  ( method . toLowerCase ( )  ===  'post' )  { 
24-     options . body  =  data  instanceof  FormData  ? data  : JSON . stringify ( data ) 
27+     /** 
28+      * @type  FormData 
29+      */ 
30+     let  formData 
31+     if  ( ! ( data  instanceof  FormData ) )  { 
32+       formData  =  new  FormData ( ) 
33+       formData . set ( 'data' ,  JSON . stringify ( data ) ) 
34+     }  else  { 
35+       formData  =  data 
36+     } 
37+ 
38+     formData . set ( 'action' ,  `btcbi_${ action }  ) 
39+     formData . set ( '_ajax_nonce' ,  typeof  btcbi  ===  'undefined'  ? ''  : btcbi . nonce ) 
40+ 
41+     options . body  =  formData 
2542  } 
2643  const  response  =  await  fetch ( uri ,  options ) 
27-     . then ( res  =>  res . text ( ) ) 
28-     . then ( res  =>  { 
44+     . then ( ( res )  =>  res . text ( ) ) 
45+     . then ( ( res )  =>  { 
2946      try  { 
3047        return  JSON . parse ( res ) 
3148      }  catch  ( error )  { 
0 commit comments