Set the baseURL for ajax requests (#373)

* Set the baseURL for ajax requests

* Use async/await on AJAX calls

* Add mix_public() cache generated asset cache busting

* Move storage container into separate class

* Fix some styling
This commit is contained in:
Nabeel S
2019-08-27 15:08:42 -04:00
committed by GitHub
parent fd2f4f2150
commit 09f3e3cfdf
16 changed files with 335 additions and 145 deletions

View File

@@ -2,12 +2,18 @@
* Bootstrap any Javascript libraries required
*/
window.axios = require('axios');
import Storage from "./storage";
/**
* Container for phpVMS specific functions
*/
window.phpvms = {};
window.phpvms = {
config: {},
Storage,
};
/**
* Configure Axios with both the csrf token and the API key
@@ -15,17 +21,17 @@ window.phpvms = {};
const base_url = document.head.querySelector('meta[name="base-url"]');
if(base_url) {
window.axios.default.baseURL = base_url;
console.log(`baseURL=${base_url.content}`);
window.phpvms.config.base_url = base_url.content;
window.axios.default.baseURL = base_url.content;
}
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
const token = document.head.querySelector('meta[name="csrf-token"]');
if (token) {
window.phpvms.config.csrf_token = token.content;
window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content
/*window.jquery.ajaxSetup({
'X-CSRF-TOKEN': token.content
})*/
} else {
console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token')
}
@@ -33,8 +39,10 @@ if (token) {
const api_key = document.head.querySelector('meta[name="api-key"]');
if (api_key) {
window.axios.defaults.headers.common['x-api-key'] = api_key.content;
window.phpvms.config.user_api_key = api_key.content;
window.PHPVMS_USER_API_KEY = api_key.content
} else {
window.phpvms.config.user_api_key = false;
window.PHPVMS_USER_API_KEY = false;
console.error('API Key not found!')
}