2018-04-16 19:41:37 +00:00
|
|
|
export function fetchWithProgress(url, options: RequestInit, onProgress: ((this: XMLHttpRequest, ev: ProgressEvent) => any) | null): Promise<string> {
|
2018-04-16 19:41:05 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let xhr = new XMLHttpRequest()
|
2018-04-16 19:41:37 +00:00
|
|
|
xhr.open(options.method || "GET", url)
|
2018-04-16 19:41:05 +00:00
|
|
|
|
2018-04-16 19:41:37 +00:00
|
|
|
for(let k in options.headers || {}) {
|
|
|
|
xhr.setRequestHeader(k, options.headers[k])
|
2018-04-16 19:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
xhr.onload = e => resolve(xhr.responseText)
|
|
|
|
xhr.onerror = reject
|
|
|
|
|
|
|
|
if(xhr.upload && onProgress) {
|
|
|
|
xhr.upload.onprogress = onProgress
|
|
|
|
}
|
|
|
|
|
2018-04-16 19:41:37 +00:00
|
|
|
xhr.send(options.body)
|
2018-04-16 19:41:05 +00:00
|
|
|
})
|
|
|
|
}
|