@nuxtjs/axios
Advanced tools
Comparing version 2.1.0 to 2.2.0
@@ -18,3 +18,4 @@ const chalk = require('chalk') | ||
credentials: true, | ||
proxyHeaders: true | ||
proxyHeaders: true, | ||
debug: false | ||
} | ||
@@ -33,2 +34,3 @@ | ||
const isSchemeLessBaseURL = options.baseURL.substr(0, 2) === '//' | ||
options.baseURL = new URL(options.baseURL, `http://${host}:${port}`) | ||
@@ -38,3 +40,3 @@ | ||
const sameHost = options.baseURL.host === `${host}:${port}` | ||
options.browserBaseURL = sameHost ? options.baseURL.pathname : options.baseURL | ||
options.browserBaseURL = sameHost ? options.baseURL.pathname : isSchemeLessBaseURL ? options.baseURL.toString().substr(5) : options.baseURL // 5 == 'http:'.length | ||
} | ||
@@ -41,0 +43,0 @@ |
{ | ||
"name": "@nuxtjs/axios", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"license": "MIT", | ||
@@ -13,4 +13,4 @@ "main": "index.js", | ||
"axios": "^0.16.2", | ||
"whatwg-url": "^5.0.0" | ||
"whatwg-url": "^6.1.0" | ||
} | ||
} |
@@ -53,4 +53,4 @@ import Axios from 'axios' | ||
// Set requests token | ||
function setToken (token, type, scopes = 'common') { | ||
// Sets a common header | ||
function setHeader (name, value, scopes = 'common') { | ||
if(!Array.isArray(scopes)) { | ||
@@ -60,9 +60,14 @@ scopes = [scopes] | ||
scopes.forEach(scope => { | ||
if (!token) { | ||
delete this.defaults.headers[scope].Authorization; | ||
if (!value) { | ||
delete this.defaults.headers[scope][name]; | ||
return | ||
} | ||
this.defaults.headers[scope].Authorization = (type ? type + ' ' : '') + token | ||
this.defaults.headers[scope][name] = value | ||
}) | ||
} | ||
// Set requests token | ||
function setToken (token, type, scopes = 'common') { | ||
value = !token ? null : (type ? type + ' ' : '') + token | ||
setHeader('Authorization', value, scopes) | ||
} | ||
@@ -99,2 +104,20 @@ // Nuxt friendly error handler | ||
function debug(level, messages) { | ||
if (!(console[level] instanceof Function)) { | ||
level = 'info' | ||
messages = arguments | ||
} else { | ||
level = arguments[0] | ||
messages = Array.prototype.slice.call(arguments, 1) | ||
} | ||
if (!messages.length) { | ||
console[level].call(null, '[@nuxtjs/axios] <empty debug message>') | ||
} else { | ||
for (var i = 0; i < messages.length; i++) { | ||
console[level].call(null, messages[i]) | ||
} | ||
} | ||
} | ||
export default (ctx) => { | ||
@@ -124,2 +147,19 @@ const { app, store, req } = ctx | ||
<% if(options.debug) { %> | ||
axios.interceptors.request.use(config => { | ||
debug('[@nuxtjs/axios] Request:', config) | ||
return config | ||
}, error => { | ||
debug('error', '[@nuxtjs/axios] Error:', error) | ||
return Promise.reject(error) | ||
}); | ||
axios.interceptors.response.use(config => { | ||
debug('[@nuxtjs/axios] Response:', config) | ||
return config | ||
}, error => { | ||
debug('error', '[@nuxtjs/axios] Error:', error) | ||
return Promise.reject(error) | ||
}); | ||
<% } %> | ||
// Error handler | ||
@@ -137,2 +177,3 @@ axios.interceptors.response.use(undefined, errorHandler.bind(ctx)); | ||
axios.setToken = setToken.bind(axios) | ||
axios.setHeader = setHeader.bind(axios) | ||
} |
@@ -127,2 +127,7 @@ # Axios | ||
### `debug` | ||
- Default: `false` | ||
Adds interceptors to log all responses and requests | ||
### `proxyHeaders` | ||
@@ -136,3 +141,26 @@ - Default: `true` | ||
## Helpers | ||
### `setHeader(name, value, scopes='common')` | ||
Axios instance has a helper to easily set any header. | ||
Parameters: | ||
- **name**: Name of the header | ||
- **value**: Value of the header | ||
- **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.setHeader('Authorization', '123') | ||
// Overrides `Authorization` header with new value | ||
this.$axios.setHeader('Authorization', '456') | ||
// Adds header: `Content-Type: application/x-www-form-urlencoded` to only post requests | ||
this.$axios.setHeader('Content-Type', 'application/x-www-form-urlencoded', ['post']) | ||
// Removes default Content-Type header from `post` scope | ||
this.$axios.setHeader('Content-Type', false, ['post']) | ||
``` | ||
### `setToken(token, type, scopes='common')` | ||
@@ -139,0 +167,0 @@ Axios instance has an additional helper to easily set global authentication header. |
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
18208
6
200
237
+ Addedlodash.sortby@4.7.0(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedtr46@1.0.1(transitive)
+ Addedwebidl-conversions@4.0.2(transitive)
+ Addedwhatwg-url@6.5.0(transitive)
- Removedtr46@0.0.3(transitive)
- Removedwebidl-conversions@3.0.1(transitive)
- Removedwhatwg-url@5.0.0(transitive)
Updatedwhatwg-url@^6.1.0