From c429d555202620343bc79b70bad3aa61958fb368 Mon Sep 17 00:00:00 2001 From: Ricardo Gobbo de Souza Date: Mon, 21 Oct 2019 12:12:19 -0300 Subject: [PATCH] feat: add function helper `setBaseURL` (#296) --- docs/helpers.md | 25 +++++++++++++++++++++++++ lib/plugin.js | 3 +++ 2 files changed, 28 insertions(+) diff --git a/docs/helpers.md b/docs/helpers.md index 9094546..023b661 100644 --- a/docs/helpers.md +++ b/docs/helpers.md @@ -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. diff --git a/lib/plugin.js b/lib/plugin.js index bb511b0..cd065ae 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -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) {