@@ -2,12 +2,15 @@ import type { BodyType, Cache, Credentials, IFetch, Method, Mode, Redirect, Retu
22export class VFetch {
33 url : string
44 method : Method = 'GET'
5- headers : Record < string , string > = { }
6- credentials : Credentials = 'omit'
5+ headers : Record < string , string > = {
6+ 'Content-Type' : 'application/json' ,
7+ }
8+
9+ credentials : Credentials = 'include'
710 body : any
811 timeout = 0
912 returnType : ReturnType = 'json'
10- bodyType : BodyType = 'form '
13+ bodyType : BodyType = 'json '
1114 firstThen ?: ( response : Response ) => Response
1215 result : Promise < any >
1316 cache : Cache = 'default'
@@ -38,18 +41,17 @@ export class VFetch {
3841
3942 set ( target : keyof VFetch , value : Record < string , string > = { } ) {
4043 Object . keys ( value ) . forEach ( ( key ) => {
41- if ( this [ target ] && ! this [ target ] [ key ] )
42- this [ target ] [ key ] = value [ key ]
44+ this [ target ] [ key ] = value [ key ]
4345 } )
4446 }
4547
46- async then ( callback : Function ) {
48+ async then ( successCallback : Function , errorCallback : Function ) {
4749 try {
4850 const result = await this . result
49- callback ( result )
51+ successCallback ( result )
5052 }
51- catch ( error : any ) {
52- throw new Error ( error )
53+ catch ( error ) {
54+ errorCallback ( error )
5355 }
5456 }
5557
@@ -64,6 +66,7 @@ export class VFetch {
6466 this . body = this . bodyToString
6567 }
6668 else if ( this . bodyType === 'file' ) {
69+ this . set ( 'headers' , { 'Content-Type' : 'multipart/form-data' } )
6770 this . body = Object . keys ( this . body ! ) . reduce ( ( result , key ) => {
6871 result . append ( key , this . body ! [ key ] )
6972 return result
0 commit comments