2.3.0 • Published 3 years ago
m-downloads v2.3.0
Installing
Using npm
npm install m-downloads
Using yarn
yarn add m-downloads
Use examples
Proxy interception
import Download from 'm-downloads' const m = new Map() m.set('POST', (url, options) => { const { data = {}, headers = {}, ...config } = options Object.assign(config, { url, body: data ? JSON.stringify(data) : null, headers: { ...headers, 'Content-Type': 'application/json; charset=utf-8', 'Authorization': localStorage.getItem('AUTH-TOKEN'), }, }) return config }) m.set('GET', (url, options) => ({ url, ...options })) const handler = { construct(target, [url, options] = args) { const method = options.method ? options.method.toUpperCase() : 'GET' return new target(m.get(method)(url, options)) } } const ProxyDownload = new Proxy(Download, handler) export default ProxyDownload
GET request
const downloader = new Download('/api/download', { filename: 'Custom file name' }) downloader .catch(error => { console.log(error) }) .finally(() => console.log('A successful or failed operation, such as the handling of loading'))
Note: if a custom file name is specified, the filename value in the response header Content-Disposition will be ignored
POST request
const downloader = new Download('/api/download', { method: 'POST', data: { id: 5, type: 1 }, }) downloader .catch(error => { console.log(error) }) .finally(() => console.log('A successful or failed operation, such as the handling of loading'))
Get download progress
const downloader = new Download('/api/download', { getProgress(percentage) { console.log(`Download progress:${percentage}`) } }) downloader .catch(error => { console.log(error) }) .finally(() => console.log('A successful or failed operation, such as the handling of loading'))
Note: you need to read the Content-Length field of the server response header to get the total size of the downloaded file
Capturing JSON errors in server response
{ "status": 500, "msg": "The amount of data is too large. It is recommended to export splitting conditions in batches", }
const downloader = new Download('/api/download', { method: 'POST', data: { id: 5, type: 1 }, }) downloader .catch(error => { console.log('errorMsg', error.statusText) })
2.3.0
3 years ago
2.2.0
3 years ago
2.1.9
3 years ago
2.1.8
3 years ago
2.1.6
3 years ago
2.1.5
3 years ago
2.1.4
3 years ago
2.1.2
3 years ago
2.1.1
3 years ago
2.1.3
3 years ago
2.0.1
3 years ago
2.0.0
3 years ago
1.6.2
3 years ago
1.6.1
3 years ago
1.6.0
3 years ago
1.5.9
3 years ago
1.5.8
3 years ago
1.5.7
3 years ago
1.4.7
3 years ago
1.4.6
3 years ago
1.4.5
3 years ago
1.4.3
3 years ago
1.4.2
3 years ago
1.4.1
3 years ago
1.4.0
3 years ago
1.3.9
3 years ago
1.3.8
3 years ago
1.3.6
3 years ago
1.3.5
3 years ago
1.3.2
3 years ago
1.2.2
3 years ago
1.1.2
3 years ago
1.0.2
3 years ago
1.0.1
3 years ago
1.0.0
3 years ago