Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1028bbc

Browse files
ricardogobbosouzapi0
authored andcommittedOct 21, 2019
feat: add function helper setBaseURL (#296)
1 parent 3e38906 commit 1028bbc

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed
 

‎docs/helpers.md

+25
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,31 @@ let data = (await $axios.get('...')).data
3636
let data = await $axios.$get('...')
3737
```
3838

39+
### `setBaseURL(baseURL)`
40+
41+
Axios instance has an additional helper to easily change baseURL.
42+
43+
Use this when you need a dynamic runtime url. Otherwise use config and environment variables.
44+
45+
Parameters:
46+
47+
* **baseURL**: Base URL which is used and prepended to make requests in server side.
48+
49+
```js
50+
// Set baseURL (both client and server)
51+
this.$axios.setBaseURL('http://api.example.com')
52+
53+
// Change URL only for client
54+
if (process.client) {
55+
this.$axios.setBaseURL('http://api.example.com')
56+
}
57+
58+
// Change URL only for server
59+
if (process.server) {
60+
this.$axios.setBaseURL('http://api.example.com')
61+
}
62+
```
63+
3964
### `setHeader(name, value, scopes='common')`
4065

4166
Axios instance has a helper to easily set any header.

‎lib/plugin.js

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import Axios from 'axios'
33

44
// Axios.prototype cannot be modified
55
const axiosExtra = {
6+
setBaseURL (baseURL) {
7+
this.defaults.baseURL = baseURL
8+
},
69
setHeader (name, value, scopes = 'common') {
710
for (let scope of Array.isArray(scopes) ? scopes : [ scopes ]) {
811
if (!value) {

0 commit comments

Comments
 (0)
Please sign in to comment.