dashboard/shell/plugins/axios.js

34 lines
1.0 KiB
JavaScript

import https from 'https';
import { CSRF } from '@shell/config/cookies';
export default function({
$axios, store, isDev, req
}) {
$axios.defaults.headers.common['Accept'] = 'application/json';
$axios.defaults.withCredentials = true;
$axios.onRequest((config) => {
const options = { parseJSON: false };
const csrf = store.getters['cookies/get']({ key: CSRF, options });
// Request can ask not to send the CSRF header
if (csrf && !config.noApiCsrf) {
config.headers['x-api-csrf'] = csrf;
}
});
if ( isDev ) {
// https://github.com/nuxt-community/axios-module/blob/dev/lib/module.js#L78
// forces localhost to http, for no obvious reason.
// But we never have any reason to talk to anything plaintext.
if ( $axios.defaults.baseURL.startsWith('http://') ) {
$axios.defaults.baseURL = $axios.defaults.baseURL.replace(/^http:/, 'https:');
}
const insecureAgent = new https.Agent({ rejectUnauthorized: false });
$axios.defaults.httpsAgent = insecureAgent;
$axios.httpsAgent = insecureAgent;
}
}