Skip to content

Instantly share code, notes, and snippets.

@danielo515
Forked from srph/set-base-path.js
Last active April 29, 2017 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielo515/b4b3a95385786580b6333dc9dc4e8d40 to your computer and use it in GitHub Desktop.
Save danielo515/b4b3a95385786580b6333dc9dc4e8d40 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