@nuxtjs/axios
Advanced tools
Comparing version 2.0.3 to 2.1.0
{ | ||
"name": "@nuxtjs/axios", | ||
"version": "2.0.3", | ||
"version": "2.1.0", | ||
"license": "MIT", | ||
@@ -12,5 +12,5 @@ "main": "index.js", | ||
"dependencies": { | ||
"axios": "^0.16.1", | ||
"axios": "^0.16.2", | ||
"whatwg-url": "^5.0.0" | ||
} | ||
} |
@@ -11,17 +11,6 @@ import Axios from 'axios' | ||
// Make `this.$axios` available | ||
if (!Vue.prototype.hasOwnProperty('$axios')) { | ||
// Add mixin to add this._axios | ||
Vue.mixin({ | ||
beforeCreate () { | ||
// Check if `axios` has been defined in App | ||
// Then fallback to $root.$axios | ||
// Finally use global instance of Axios | ||
this._axios = this.$options.axios || this.$root.$axios || Axios | ||
} | ||
}) | ||
// Add this.$axios instance | ||
Object.defineProperty(Vue.prototype, '$axios', { | ||
get () { | ||
return this._axios | ||
return this.$root.$options.$axios | ||
} | ||
@@ -66,8 +55,13 @@ }) | ||
// Set requests token | ||
function setToken (token, type = 'Bearer') { | ||
if (!token) { | ||
delete this.defaults.headers.common.Authorization; | ||
return | ||
function setToken (token, type, scopes = 'common') { | ||
if(!Array.isArray(scopes)) { | ||
scopes = [scopes] | ||
} | ||
this.defaults.headers.common.Authorization = (type ? type + ' ' : '') + token | ||
scopes.forEach(scope => { | ||
if (!token) { | ||
delete this.defaults.headers[scope].Authorization; | ||
return | ||
} | ||
this.defaults.headers[scope].Authorization = (type ? type + ' ' : '') + token | ||
}) | ||
} | ||
@@ -74,0 +68,0 @@ |
@@ -134,2 +134,32 @@ # Axios | ||
## Helpers | ||
### `setToken(token, type, scopes='common')` | ||
Axios instance has an additional helper to easily set global authentication header. | ||
Parameters: | ||
- **token**: Authorization token | ||
- **type**: Authorization token prefix(Usually `Bearer`). | ||
- **scopes**: Send only on specific type of requests. Defaults | ||
- Type: *Array* or *String* | ||
- Defaults to `common` meaning all types of requests | ||
- Can be `get`, `post`, `delete`, ... | ||
```js | ||
// Adds header: `Authorization: 123` to all requests | ||
this.$axios.setToken('123') | ||
// Overrides `Authorization` header with new value | ||
this.$axios.setToken('456') | ||
// Adds header: `Authorization: Bearer 123` to all requests | ||
this.$axios.setToken('123', 'Bearer') | ||
// Adds header: `Authorization: Bearer 123` to only post and delete requests | ||
this.$axios.setToken('123', 'Bearer', ['post', 'delete']) | ||
// Removes default Authorization header from `common` scope (all requests) | ||
this.$axios.setToken(false) | ||
``` | ||
## Dynamic API Backend | ||
@@ -136,0 +166,0 @@ Please notice that, `API_URL` is saved into bundle on build, CANNOT be changed |
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
14311
209
160
Updatedaxios@^0.16.2