Socket
Socket
Sign inDemoInstall

m-downloads

Package Overview
Dependencies
0
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    m-downloads

Blob stream file download of native JS


Version published
Maintainers
1
Created

Readme

Source
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)
      })
    

Keywords

FAQs

Last updated on 11 Apr 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc