Socket
Socket
Sign inDemoInstall

@nuxtjs/axios

Package Overview
Dependencies
Maintainers
3
Versions
86
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nuxtjs/axios - npm Package Compare versions

Comparing version 2.1.0 to 2.2.0

yarn.lock

6

index.js

@@ -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.

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc