dashboard/plugins/axios.js

36 lines
1.2 KiB
JavaScript

import https from 'https';
import pkg from '../package.json';
export default function({
$axios, isDev, route, redirect, req
}) {
$axios.defaults.headers.common['Accept'] = 'application/json';
$axios.defaults.xsrfCookieName = 'CSRF';
$axios.defaults.xsrfHeaderName = 'X-Api-Csrf';
$axios.defaults.withCredentials = true;
if ( process.server ) {
$axios.defaults.headers.common['user-agent'] = `Dashboard v${ pkg.version }`;
}
if ( isDev ) {
// https://github.com/nuxt-community/axios-module/blob/dev/lib/module.js#L78
// forces localhost to http, for no obvious reason.
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;
} else if ( process.server ) {
// For requests from the server, set the base URL to the URL that the request came in on
$axios.onRequest((config) => {
if ( process.server && config.url.startsWith('/') ) {
config.baseURL = `${ req.protocol }://${ req.headers.host }`;
}
});
}
}