Skip to content

Latest commit

 

History

History
68 lines (48 loc) · 1.42 KB

4.hooks.md

File metadata and controls

68 lines (48 loc) · 1.42 KB
title description
Hooks
Hooks with arguments.

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

onRequest

  • arguments: (config)

Example:

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:

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:

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.