-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch.js
More file actions
70 lines (62 loc) · 1.72 KB
/
fetch.js
File metadata and controls
70 lines (62 loc) · 1.72 KB
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
const $fetch = async function (path, p) {
const timeout = p.timeout || 10000 // 默认 10 秒超时
const config = {
method: p.method || 'GET',
}
if (p.body) {
config.body = JSON.stringify(p.body)
}
let q = ''
if (p.query && Object.prototype.toString.call(p.query) === '[object Object]') {
for (const key in p.query) {
if (Object.getOwnPropertyNames(p.query).includes(key)) {
q += `&${key}=${encodeURIComponent(p.query[key])}`
}
}
if (q.length) q = q.substring(1)
}
if (p.headers) {
config.headers = Object.assign(
{
'Content-Type': 'application/json',
},
p.headers,
)
const jsonREG = /pplication\/json/
if (!jsonREG.test(p.headers['Content-Type'])) {
config.body = p.body
}
} else {
config.headers = {
'Content-Type': 'application/json',
}
}
let url = path
if (q.length) {
url += url.indexOf('?') === -1 ? `?${q}` : `&${q}`
}
// 添加超时控制
const controller = new AbortController()
const timeoutId = setTimeout(() => controller.abort(), timeout)
config.signal = controller.signal
try {
const res = await fetch(url, config)
clearTimeout(timeoutId)
if (!res.ok) {
throw new Error(`HTTP error! status: ${res.status}`)
}
const result = await res.json()
return result
} catch (e) {
clearTimeout(timeoutId)
if (e.name === 'AbortError') {
throw new Error(`Request timeout after ${timeout}ms: ${url}`)
}
throw e
}
}
const temp = {
post: (path, data = {}, config = {}) => $fetch(path, { ...config, body: data, method: 'POST' }),
get: (path, query = {}, config = {}) => $fetch(path, { ...config, query, method: 'GET' }),
}
module.exports = temp