Hooks

Hooks with arguments.

The arguments listed below are those your hook will receive when it's called.

onRequest

  • arguments: (config)

Example:

plugins/http.js
export default function ({ $http }) {
  $http.onRequest(config => {
    console.log('Making request to ' + config.url)
  })
}

See here for advanced usage.

onResponse

  • arguments: (request, options, response)

Example:

plugins/http.js
export default function ({ $http }) {
  $http.onResponse((req, options, res) => {
    console.log('Making request to ' + req.url)
    console.log('Options :', options)
    console.log('Response data :', res.body)
  })
}

See here for advanced usage.

onError

  • arguments: (error)

If the error originated from a request.

Example:

plugins/http.js
export default function ({ $http, redirect }) {
  $http.onError((err) => {
    // In case of unauthorized, redirect to a specific page
    if (error.statusCode === 401) {
      redirect('/401')
    }
  })
}

Available properties:

  • error.statusCode (may be undefined)
  • error.response?.data(may be undefined)

You can optionally return a value or promise that can resolve for fallback response. If hook returns any value, other hooks won't be called.

See here for advanced usage.

Edit this page on GitHub Updated at Sat, Feb 27, 2021