Code Monkey home page Code Monkey logo

Comments (13)

uptownhr avatar uptownhr commented on May 22, 2024 1

@pi0 you're right. I restarted and still fixed. Sorry, should have rebuilt from scratch after upgrading.

from legacy-modules.

uptownhr avatar uptownhr commented on May 22, 2024

I was able to confirm that the proper response was returned when I bypass the proxy

from legacy-modules.

pi0 avatar pi0 commented on May 22, 2024

Hey. Are you testing with latest master version? What if you manually remove errorhandler's logic?

from legacy-modules.

pi0 avatar pi0 commented on May 22, 2024

Please upgrade to @nuxtjs/axios:2.3.0 and compare result :)

from legacy-modules.

pi0 avatar pi0 commented on May 22, 2024

Also i think this would be correct usage:

let user = (await app.$axios.get('/user/me')).data

from legacy-modules.

uptownhr avatar uptownhr commented on May 22, 2024

awesome. that did fix the issue!

However,

the caught error is now,

TypeError: Cannot read property '500' of undefined
at Object.errorHandler (server-bundle.js:1479:22)
at process._tickCallback (internal/process/next_tick.js:109:7)

from legacy-modules.

pi0 avatar pi0 commented on May 22, 2024

Would you please send a copy of generated .nuxt/axios.plugin.*.js?

from legacy-modules.

uptownhr avatar uptownhr commented on May 22, 2024
import Axios from 'axios'
import Vue from 'vue'

const axiosPlugin = {
  install() {
    if(Vue.__nuxt_axios_installed__) {
      return
    }
    Vue.__nuxt_axios_installed__ = true

    if (!Vue.prototype.hasOwnProperty('$axios')) {
      Object.defineProperty(Vue.prototype, '$axios', {
        get () {
          return this.$root.$options.$axios
        }
      })
    }

    // Vue Component Mixins
    Vue.mixin({
      methods: {
        // opts
        $request (opts) {
          return this.$axios.request(opts);
        },
        // url, opts
        $get (url, opts) {
          return this.$axios.get(url, opts);
        },
        $delete (url, opts) {
          return this.$axios.delete(url, opts);
        },
        $head (url, opts) {
          return this.$axios.head(url, opts);
        },
        // url, data, opts
        $post (url, data, opts) {
          return this.$axios.post(url, data, opts);
        },
        $put (url, data, opts) {
          return this.$axios.put(url, data, opts);
        },
        $patch (url, data, opts) {
          return this.$axios.patch(url, data, opts);
        }
      }
    })
  }
}

Vue.use(axiosPlugin)

// Sets a common header
function setHeader (name, value, scopes = 'common') {
  if(!Array.isArray(scopes)) {
    scopes = [scopes]
  }
  scopes.forEach(scope => {
    if (!value) {
      delete this.defaults.headers[scope][name];
      return
    }
    this.defaults.headers[scope][name] = value
  })
}

// Set requests token
function setToken (token, type, scopes = 'common') {
    const value = !token ? null : (type ? type + ' ' : '') + token
    this.setHeader('Authorization', value, scopes)
}

const redirectError = {}

// Set appreciate `statusCode` and `message` to error instance
function errorHandler(error) {
  if (error.response) {
    // Error from backend (non 2xx status code)
    // ...Auto redirect on special status codes
    if (redirectError[error.response.status]) {
      this.redirect(redirectError[error.response.status])
    }
    error.statusCode = error.statusCode || parseInt(error.response.status) || 500
    error.message = error.message || error.response.statusText || (error.statusCode + ' (Internal Server Error)')
  } else if (error.request) {
    // Error while making request
    error.statusCode = error.statusCode || 500
    error.message = error.message || 'request error'
  } else {
    // Something happened in setting up the request that triggered an Error
    error.statusCode = 500
    error.message = error.message || 'axios error'
  }

  return Promise.reject(error)
}



// Setup BaseURL
const baseURL = process.browser
  ? (process.env.API_URL_BROWSER || '/api')
  : (process.env.API_URL || 'http://667837fe8085:3000/api')

export default (ctx) => {
  const { app, store, req } = ctx

  
  // Default headers 
  const defaultHeaders = (req && req.headers) ? Object.assign({}, req.headers) : {}
  delete defaultHeaders.host
  

  // Create new axios instance
  const axios = Axios.create({
    baseURL,
    defaultHeaders,
  })

  
  // Send credentials only to relative and API Backend requests
  axios.interceptors.request.use(config => {
    if (config.withCredentials === undefined) {
      if (!/^https?:\/\//i.test(config.url) || config.url.indexOf(baseURL) === 0) {
        config.withCredentials = true
      }
    }
    return config
  });
  

  

  // Error handler
  axios.interceptors.response.use(undefined, errorHandler.bind(ctx));

  // Make accessible using *.$axios
  app.$axios = axios
  ctx.$axios = axios
  if (store) {
    store.$axios = axios
  }

  // Token helper for authentication
  axios.setToken = setToken.bind(axios)
  axios.setHeader = setHeader.bind(axios)
}

from legacy-modules.

pi0 avatar pi0 commented on May 22, 2024

I tought problem would be with new redirectError feature. But it is defined too 😕 Does disable this line fixes problem?

   if (redirectError[error.response.status]) {
      this.redirect(redirectError[error.response.status])
    }

from legacy-modules.

uptownhr avatar uptownhr commented on May 22, 2024

where should i put this?

from legacy-modules.

pi0 avatar pi0 commented on May 22, 2024

This line is in .nuxt/axios file you have submitted. Sorry for inconveniences. Trying to guess problem.

from legacy-modules.

uptownhr avatar uptownhr commented on May 22, 2024

i think that fixed it. now i see the response object.

from legacy-modules.

pi0 avatar pi0 commented on May 22, 2024

@uptownhr Sorry bothering, but would you please restart dev server if error exists? I can't reproduce it.

from legacy-modules.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.