This repository has been archived by the owner on Jan 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (48 loc) · 1.51 KB
/
index.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
import Swal from 'sweetalert2'
import request from 'axios'
import { isString } from 'util';
const bind = (fn, swalConfig = {}) => {
return (...args) => {
const {
start = () => Swal({
title: 'please wait',
text: 'starting request...',
onBeforeOpen: () => {
Swal.showLoading()
},
}),
progress = progressEvent => {
Swal.getContent().textContent = `progress ${((progressEvent.loaded * 100) / progressEvent.total).toFixed()}%`
},
end = () => Swal.close()
} = swalConfig
start()
const lastArgs = args[args.length - 1]
const newArgs = isString(lastArgs)
|| (typeof URLSearchParams != 'undefined' && lastArgs instanceof URLSearchParams)
|| (typeof FormData != 'undefined' && lastArgs instanceof FormData)
? [lastArgs, {}] : [...args]
const axiosConfig = newArgs[newArgs.length - 1]
const { onUploadProgress = () => { }, onDownloadProgress = () => { } } = axiosConfig
newArgs[newArgs.length - 1] = {
...axiosConfig,
onUploadProgress: (e) => {
onUploadProgress(e)
progress(e)
},
onDownloadProgress: (e) => {
onDownloadProgress(e)
progress(e)
}
}
return fn(...newArgs).then(x => {
end()
return x
})
}
}
export default (config) => {
const swalRequest = bind(request, config);
['get', 'head', 'post', 'put', 'delete', 'connect', 'options', 'trace', 'patch'].forEach(k => swalRequest[k] = bind(request[k], config))
return swalRequest
}