-
Notifications
You must be signed in to change notification settings - Fork 0
/
esm.js
135 lines (101 loc) · 4.17 KB
/
esm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
class Fetcher {
constructor (props) {
this.url = ( props && props.url ) ? props.url : ( ( window && window.location ) ? window.location.href : '' )
this.query = ( props && props.query ) ? props.query : null
this.params = ( props && props.params ) ? props.params : null
this.headers = ( props && props.headers ) ? props.headers : null
this.body = ( props && props.body ) ? props.body : null
this.mode = ( props && props.mode ) ? props.mode : null
this.cache = ( props && props.cache ) ? props.cache : null
this.type = ( props && props.type ) ? props.type : 'json'
this.contentType = ( props && props.contentType ) ? props.contentType : 'application/json'
}
async fetch (method = 'GET', props, body = false) {
let url = this.url
let query = null
if (props && props.query) query = props.query
else if (this.query) query = this.query
if (query) {
if (!url.endsWith('/')) url += '/'
url += query
}
let params = null
if (this.params && Object.keys(this.params).length > 0) {
params = { ...this.params }
}
if (props && props.params && Object.keys(props.params).length > 0) {
params = { ...params, ...props.params }
}
if (params && Object.keys(params).length > 0) {
url += `?${new URLSearchParams(params)}`
}
let options = { method }
let headers = {}
if (this.headers && Object.keys(this.headers).length > 0) {
headers = { ...this.headers }
}
if (props && props.headers && Object.keys(props.headers).length > 0) {
headers = { ...headers, ...props.headers }
}
if (Object.keys(headers).length > 0) {
options.headers = headers
}
if (body) {
let requestBody = {}
if (this.body && Object.keys(this.body).length > 0) {
requestBody = { ...this.body }
}
if (body && Object.keys(body).length > 0) {
requestBody = { ...requestBody, ...body }
}
if (requestBody && Object.keys(requestBody).length > 0) {
let contentType = null
if (props && props.contentType) query = props.contentType
else if (this.contentType) contentType = this.contentType
if (options.headers && options.headers['Content-Type']) {
contentType = options.headers['Content-Type']
}
if (contentType) {
if (contentType == 'multipart/form-data') {
options.body = new FormData(requestBody)
Object.entries(requestBody).forEach(([key, value]) =>
options.body.append(key, value)
)
} else if (contentType == 'application/x-www-form-urlencoded') {
options.body = new URLSearchParams(Object.entries(requestBody)).toString();
} else {
options.body = JSON.stringify(requestBody)
}
} else {
options.body = JSON.stringify(requestBody)
}
}
}
if (props && props.mode) options.mode = props.mode
else if (this.mode) options.mode = this.mode
if (props && props.cache) options.cache = props.cache
else if (this.cache) options.cache = this.cache
const response = await fetch(url, options)
let type = null
if (props && props.type) type = props.type
else if (this.type) type = this.type
if (type) return await response[type]()
return response
}
async get (props) {
return await this.fetch('GET', props)
}
async post (props) {
return await this.fetch('POST', props, true)
}
async put (props) {
return await this.fetch('PUT', props, true)
}
async delete (props) {
return await this.fetch('DELETE', props)
}
}
function fetcher (props) {
return new Fetcher(props)
}
export default fetcher