axios: DefaultAdapter returns unknown/undefined by default

Describe the bug The Default Adapter returns with out being set if the if/elseif isn’t met. This means an axios call (through AWS Amplify API) in nextJS getInitialProps throws because “Adapter is not a function”

To Reproduce Make a next js project with amplify and init with api. GraphQL uses axios to make the calls. Tracing the stack shows that the axios defaults module does not set the adapter.

Expected behavior The adapter should be set.

Environment:

  • Axios Version 0.19.0
  • OS: MacOs
  • Browser Chrome
  • Additional Library Versions Next 9.1.1

Additional context/Screenshots

function getDefaultAdapter() {
  var adapter;
  // Only Node.JS has a process variable that is of [[Class]] process
  if (typeof process !== 'undefined' && Object.prototype.toString.call(process) === '[object process]') {
    // For node use HTTP adapter
    adapter = require('./adapters/http');
  } else if (typeof XMLHttpRequest !== 'undefined') {
    // For browsers use XHR adapter
    adapter = require('./adapters/xhr');
  }
  return adapter;
}

This is the method. The “Object.prototype.toString.call(process) === ‘[object process]’” is false because the process in nextJS is “‘[object Object]’”

This isn’t just an issue with nextJS. An if/elseif should never, imo, fall-through and return a variable that hasn’t been set. Throw from there or set a default in an else block.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 18

Commits related to this issue

Most upvoted comments

Any reason this issue is closed with the PR still open? I am experiencing the same issue with the adapter not being set while using AWS Amplify

Still having this issue with @aws-amplify/api@2.1.1 on server side.

Having the same problem within Cloudflare worker: (t.adapter || a.adapter) is not a function.

Problem is that not just Axios does not work, but also modules that use Axios like contentful/contentful-management.js for example.

I just ran examples/with-react-with-styles from the next.js repo and did:

    console.log("Process prototype toString()")
    console.log(Object.prototype.toString.call(process))

inside getInitialProps and it correctly logged [object process], can you help sharing what node version and nextjs version you’re using? I’m on the canary branch. We should probably fix still this but just wondering if this unblocks you and we can throw an error in case none of the conditions pass. Maybe it’s another dependency that’s loading and overriding process? @ryanmalesic

that’s true, I’ll push something for this whenever I get a chance