Skip to content

Instantly share code, notes, and snippets.

@srph
Last active April 29, 2017 12:58
Show Gist options
  • Save srph/8b8b8d409a3397d86493 to your computer and use it in GitHub Desktop.
Save srph/8b8b8d409a3397d86493 to your computer and use it in GitHub Desktop.
axios: Configure the base path with interceptors
var axios = require('axios');
var join = require('url-join');
// https://github.com/sindresorhus/is-absolute-url/blob/master/index.js#L7
var isAbsoluteURLRegex = /^(?:\w+:)\/\//;
axios.interceptors.request.use(function(config) {
// Concatenate base path if not an absolute URL
if ( !isAbsoluteURLRegex.test(config.url) ) {
config.url = join('http://my-api.com', config.url);
}
return config;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment