-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcustomFetch.ts
More file actions
64 lines (59 loc) · 1.87 KB
/
Copy pathcustomFetch.ts
File metadata and controls
64 lines (59 loc) · 1.87 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
import { useToaster } from '@/components/modules/toats/index'
const fetchCache = new Map()
export function customFetch(url: string, options = {}, needDebounce = true, needError = true) {
const now = Date.now()
if (needDebounce) {
if (fetchCache.has(url)) {
const lastFetchTime = fetchCache.get(url)
if (now - lastFetchTime < 1200) {
return Promise.resolve(null)
}
}
fetchCache.set(url, now)
}
const host = `${window.location.origin}${window.location.pathname === '/' ? '' : window.location.pathname}`
return window
.fetch(`${host}${url}`, options)
.then(response => {
if (response.status === 404) {
useToaster.error(
'You may be missing dependencies at the moment. For details, please refer to the ComfyUI logs.'
)
}
return response.json()
})
.then(data => {
const { code, message } = data
if (code !== 20000) {
if (needError) {
if (fetchCache.has(message)) {
const lastFetchTime = fetchCache.get(message)
if (now - lastFetchTime < 2500) {
return
}
}
fetchCache.set(message, now)
// 检查options中是否有shieldError字段,如果有,记录URL以确保同一URL的错误只提示一次
const hasShieldError = options && (options as any).shieldError
const errorCacheKey = `error_${url}`
if (hasShieldError) {
if (!fetchCache.has(errorCacheKey)) {
fetchCache.set(errorCacheKey, true)
useToaster.error(message)
}
} else {
useToaster.error(message)
}
throw new Error(message)
} else {
return data
}
// return
}
return data
})
.catch(error => {
console.error('Fetch error:', error)
throw error
})
}