Skip to content

Axios and Electron #552

Closed
Closed

Description

@paul-piro

I'm trying to integrate the library with Electron desktop apps, but the problem is XHR and Node are available. Which one it will be used?

My goal is to user XHR on the development and when we build the production to use node request.

Is that possible?

Activity

  1. nickuraltsev commented on Nov 25, 2016

    @nickuraltsev
    Contributor

    By default, axios uses the following code to choose between the XHR and Node.js adapters:

    function getDefaultAdapter() {
      var adapter;
      if (typeof XMLHttpRequest !== 'undefined') {
        // For browsers use XHR adapter
        adapter = require('./adapters/xhr');
      } else if (typeof process !== 'undefined') {
        // For node use HTTP adapter
        adapter = require('./adapters/http');
      }
      return adapter;
    }

    You can override the default behavior using the adapter config option as follows:

    axios.defaults.adapter = function() {
      return require('axios/adapters/http'); // always use Node.js adapter
    };

    Hope this helps!

  2. paul-piro commented on Dec 14, 2016

    @paul-piro
    Author

    i'm getting this error
    "adapter(...).then is not a function"
    i'm using
    var axios = require('axios');
    axios.defaults.adapter = function() {
    return require('axios/lib/adapters/http'); // always use Node.js adapter
    };

  3. paul-piro commented on Dec 14, 2016

    @paul-piro
    Author

    Solved:
    axios.defaults.adapter = require('axios/lib/adapters/http');

  4. ozalto commented on Feb 27, 2020

    @ozalto

    This line was a life-saver :)

  5. muscaiu commented on Mar 9, 2020

    @muscaiu

    The solution doesn't work for me.
    Still getting
    The provided value 'stream' is not a valid enum value of type XMLHttpRequestResponseType.
    Uncaught (in promise) TypeError: data.pipe is not a function

    import fs from 'fs';
    import path from 'path';
    import axios from 'axios';
    
    const downloadFile = async ()=> {
      let receivedBytes = 0;
    
      axios.defaults.adapter = require('axios/lib/adapters/http');
    
      const { data, headers } = await axios({
        url,
        method: 'GET',
        responseType: 'stream',
      });
    
      const totalLength = headers['content-length'];
      console.log('totalLength:', totalLength);
      console.log('data:', data);
    
      const writer = fs.createWriteStream(path.resolve(__dirname, 'downloads', `filename.zip`));
    
      data.pipe(writer);
    
      data.on('data', chunk => {
        console.log('chunk:', chunk.length);
        receivedBytes += chunk.length;
        const percentage = (receivedBytes * 100) / totalLength;
        console.log('percentage:', percentage);
      });
    
      data.on('error', error => {
        console.log('downloadError', error);
      });
    
      data.on('end', () => {
        console.log('download end');
      });
    
    };
    
    
  6. ANG95 commented on Mar 14, 2020

    @ANG95

    esto funcionó para mi Axios.defaults.adapter = require('axios/lib/adapters/http');

  7. ArnaudValensi commented on Mar 26, 2020

    @ArnaudValensi

    The problem is in the package.json of this repository:

      "browser": {
        "./lib/adapters/http.js": "./lib/adapters/xhr.js"
      },
    

    This makes webpack replace the http adapter:

    function getDefaultAdapter() {
      var adapter;
      if (typeof XMLHttpRequest !== 'undefined') {
        // For browsers use XHR adapter
        adapter = require('./adapters/xhr');
      } else if (typeof process !== 'undefined') {
        // For node use HTTP adapter
        adapter = require('./adapters/http'); // <- Webpack replace this by `adapter = require('./adapters/xhr');`
      }
      return adapter;
    }
    

    I ended up forking this repos, removing the browser property in the package.json.

    Then

    yarn add -D https://github.com/ArnaudValensi/axios.git#37c7730a2917b1d8e0a6396d1295a8c20a1d3c1a
    

    We still have to replace the adapter because the first condition of getDefaultAdapter() is still true :

    const axiosHttp = axios.create({
      adapter: require('axios/lib/adapters/http'),
    });
    
  8. davide-sergi commented on May 12, 2020

    @davide-sergi

    Is it possible to adapt Axios on top of electron chromium stack?

  9. locked and limited conversation to collaborators on May 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions