@nuxtjs/axios
Advanced tools
Comparing version 2.2.4 to 2.3.0
@@ -6,2 +6,14 @@ # Change Log | ||
<a name="2.3.0"></a> | ||
# [2.3.0](https://github.com/nuxt/modules/compare/@nuxtjs/axios@2.2.4...@nuxtjs/axios@2.3.0) (2017-07-24) | ||
### Features | ||
* **axios:** optionally disable error handling (#74) ([a195feb](https://github.com/nuxt/modules/commit/a195feb)) | ||
* **axios:** redirectError ([4ce1a1c](https://github.com/nuxt/modules/commit/4ce1a1c)) | ||
<a name="2.2.4"></a> | ||
@@ -8,0 +20,0 @@ ## [2.2.4](https://github.com/nuxt/modules/compare/@nuxtjs/axios@2.2.3...@nuxtjs/axios@2.2.4) (2017-07-20) |
@@ -19,3 +19,4 @@ const chalk = require('chalk') | ||
proxyHeaders: true, | ||
debug: false | ||
debug: false, | ||
redirectError: {} | ||
} | ||
@@ -22,0 +23,0 @@ |
{ | ||
"name": "@nuxtjs/axios", | ||
"version": "2.2.4", | ||
"version": "2.3.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -73,8 +73,11 @@ import Axios from 'axios' | ||
// Nuxt friendly error handler | ||
const redirectError = <%= serialize(options.redirectError) %> | ||
// Set appreciate `statusCode` and `message` to error instance | ||
function errorHandler(error) { | ||
if (error.response) { | ||
// Error from backend (non 2xx status code) | ||
if (error.response.status === 401) { | ||
return this.redirect('/login') | ||
// ...Auto redirect on special status codes | ||
if (redirectError[error.response.status]) { | ||
this.redirect(redirectError[error.response.status]) | ||
} | ||
@@ -92,14 +95,7 @@ error.statusCode = error.statusCode || parseInt(error.response.status) || 500 | ||
} | ||
// Display error page on unhandled promises | ||
if(process.browser) { | ||
return Promise.reject(error) | ||
} else { | ||
// Don't throw unhandled promises in SSR context | ||
return this.app._nuxt.error.call({$options: this.app}, { | ||
message: error.message, | ||
statusCode: error.statusCode | ||
}) | ||
} | ||
return Promise.reject(error) | ||
} | ||
<% if(options.debug) { %> | ||
function debug(level, messages) { | ||
@@ -122,15 +118,24 @@ if (!(console[level] instanceof Function)) { | ||
} | ||
<% } %> | ||
// Setup BaseURL | ||
const baseURL = process.browser | ||
? (process.env.API_URL_BROWSER || '<%= options.browserBaseURL %>') | ||
: (process.env.API_URL || '<%= options.baseURL %>') | ||
export default (ctx) => { | ||
const { app, store, req } = ctx | ||
<% if(options.proxyHeaders) { %> | ||
// Default headers | ||
const defaultHeaders = (req && req.headers) ? Object.assign({}, req.headers) : {} | ||
delete defaultHeaders.host | ||
<% } %> | ||
// Create new axios instance | ||
const baseURL = process.browser | ||
? (process.env.API_URL_BROWSER || '<%= options.browserBaseURL %>') | ||
: (process.env.API_URL || '<%= options.baseURL %>') | ||
const axios = Axios.create({ | ||
baseURL, | ||
<% if(options.proxyHeaders) { %>headers: (req && req.headers) ? Object.assign({}, req.headers, {host: ''}) : {} <% } %> | ||
<% if(options.proxyHeaders) { %>defaultHeaders,<% } %> | ||
}) | ||
<% if(options.credentials) { %> | ||
@@ -149,2 +154,3 @@ // Send credentials only to relative and API Backend requests | ||
<% if(options.debug) { %> | ||
// Debug | ||
axios.interceptors.request.use(config => { | ||
@@ -172,9 +178,9 @@ debug('[@nuxtjs/axios] Request:', config) | ||
ctx.$axios = axios | ||
if(store) { | ||
if (store) { | ||
store.$axios = axios | ||
} | ||
// token helper for authentication | ||
// Token helper for authentication | ||
axios.setToken = setToken.bind(axios) | ||
axios.setHeader = setHeader.bind(axios) | ||
} |
@@ -10,3 +10,3 @@ # Axios | ||
- Exposes `setToken` function to `$axios` so we can easily and globally set authentication tokens. | ||
- Throws *nuxt-friendly* exceptions and prevent SSR crashes. | ||
- Throws *nuxt-friendly* errors and optionally redirect on specific error codes. | ||
- Automatically enables `withCredentials` when requesting to base URL. | ||
@@ -140,2 +140,17 @@ - Proxy request headers in SSR. | ||
### `redirectError` | ||
- Default: `{}` | ||
This option is a map from specific error codes to page which they should be redirect. | ||
For example if you want redirecting all `401` errors to `/login` use: | ||
```js | ||
{ | ||
axios: { | ||
redirectError: { | ||
401: '/login' | ||
} | ||
} | ||
} | ||
``` | ||
## Helpers | ||
@@ -142,0 +157,0 @@ ### `setHeader(name, value, scopes='common')` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20382
212
252