Skip to content

Commit

Permalink
feat: add function helper setBaseURL (#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardogobbosouza authored and pi0 committed Oct 23, 2019
1 parent af1e86d commit c429d55
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
25 changes: 25 additions & 0 deletions docs/helpers.md
Expand Up @@ -36,6 +36,31 @@ let data = (await $axios.get('...')).data
let data = await $axios.$get('...')
```

### `setBaseURL(baseURL)`

Axios instance has an additional helper to easily change baseURL.

Use this when you need a dynamic runtime url. Otherwise use config and environment variables.

Parameters:

* **baseURL**: Base URL which is used and prepended to make requests in server side.

```js
// Set baseURL (both client and server)
this.$axios.setBaseURL('http://api.example.com')

// Change URL only for client
if (process.client) {
this.$axios.setBaseURL('http://api.example.com')
}

// Change URL only for server
if (process.server) {
this.$axios.setBaseURL('http://api.example.com')
}
```

### `setHeader(name, value, scopes='common')`

Axios instance has a helper to easily set any header.
Expand Down
3 changes: 3 additions & 0 deletions lib/plugin.js
Expand Up @@ -3,6 +3,9 @@ import Axios from 'axios'

// Axios.prototype cannot be modified
const axiosExtra = {
setBaseURL (baseURL) {
this.defaults.baseURL = baseURL
},
setHeader (name, value, scopes = 'common') {
for (let scope of Array.isArray(scopes) ? scopes : [ scopes ]) {
if (!value) {
Expand Down

0 comments on commit c429d55

Please sign in to comment.