Skip to content

Commit 22f982d

Browse files
author
Simon he
committed
chore: update inital headers to application/json
1 parent f3e6532 commit 22f982d

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/VFetch.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ import type { BodyType, Cache, Credentials, IFetch, Method, Mode, Redirect, Retu
22
export 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

Comments
 (0)