<commit_before>import Axios from 'axios';
import Vue from 'vue';

const inBrowser = typeof window !== 'undefined';

const API_URL = inBrowser ? (process.env.API_URL_BROWSER || '') : (process.env.API_URL || 'http://localhost:3000');

// Create new axios instance
const axios = Axios.create({
    baseURL: API_URL + (process.env.API_PREFIX || '/api')
});

// Throw nice http errors & keep SSR safe
function onError(e) {
    if (!inBrowser) {
        return {};
    }

    let response = {};
    if (e.response) {
        response = e.response.data;
    }

    throw Object.assign({
        statusCode: 500,
        message: 'Request error'
    }, response);
}

// Wap promise
function wrapPromise(p) {
    return p.then(res => {
        return res.data || {};
    }).catch(onError);
}

// Create wrappers
// https://github.com/mzabriskie/axios#request-method-aliases
export const $request = function (opts) {
    return wrapPromise(axios.request(opts));
};

export const $get = function (url, opts) {
    return wrapPromise(axios.get(url, opts));
};

export const $delete = function (url, opts) {
    return wrapPromise(axios.delete(url, opts));
};

export const $head = function (url, opts) {
    return wrapPromise(axios.head(url, opts));
};

export const $post = function (url, data, opts) {
    return wrapPromise(axios.post(url, data, opts));
};

export const $put = function (url, data, opts) {
    return wrapPromise(axios.put(url, data, opts));
};
export const $patch = function (url, data, opts) {
    return wrapPromise(axios.patch(url, data, opts));
};

// ----------------------------------------
// Vue Plugin
// ----------------------------------------
const VueAxios = {
    install(Vue) {
        // Globally register as $http
        Vue.prototype.$http = axios;

        // Mixins
        Vue.mixin({
            methods: {
                $request,
                $get,
                $delete,
                $head,
                $post,
                $put,
                $patch
            }
        });
    }

};

// ----------------------------------------
// Install vue plugin
// ----------------------------------------
Vue.use(VueAxios);

// ----------------------------------------
// setToken helper
// ----------------------------------------
export const setToken = function (token) {
    axios.defaults.headers.common.Authorization = token ? 'Bearer ' + token : null;
};<commit_msg>[axios] withCredentials with token is set<commit_after>import Axios from 'axios';
import Vue from 'vue';

const inBrowser = typeof window !== 'undefined';

const API_URL = inBrowser ? (process.env.API_URL_BROWSER || '') : (process.env.API_URL || 'http://localhost:3000');

// Global Auth Token
let token = null;

// Create new axios instance
const axios = Axios.create({
    baseURL: API_URL + (process.env.API_PREFIX || '/api'),
    withCredentials: !!token
});

// Throw nice http errors & keep SSR safe
function onError(e) {
    if (!inBrowser) {
        return {};
    }

    let response = {};
    if (e.response) {
        response = e.response.data;
    }

    throw Object.assign({
        statusCode: 500,
        message: 'Request error'
    }, response);
}

// Wap promise
function wrapPromise(p) {
    return p.then(res => {
        return res.data || {};
    }).catch(onError);
}

// Create wrappers
// https://github.com/mzabriskie/axios#request-method-aliases
export const $request = function (opts) {
    return wrapPromise(axios.request(opts));
};

export const $get = function (url, opts) {
    return wrapPromise(axios.get(url, opts));
};

export const $delete = function (url, opts) {
    return wrapPromise(axios.delete(url, opts));
};

export const $head = function (url, opts) {
    return wrapPromise(axios.head(url, opts));
};

export const $post = function (url, data, opts) {
    return wrapPromise(axios.post(url, data, opts));
};

export const $put = function (url, data, opts) {
    return wrapPromise(axios.put(url, data, opts));
};
export const $patch = function (url, data, opts) {
    return wrapPromise(axios.patch(url, data, opts));
};

// ----------------------------------------
// Vue Plugin
// ----------------------------------------
const VueAxios = {
    install(Vue) {
        // Globally register as $http
        Vue.prototype.$http = axios;

        // Mixins
        Vue.mixin({
            methods: {
                $request,
                $get,
                $delete,
                $head,
                $post,
                $put,
                $patch
            }
        });
    }

};

// ----------------------------------------
// Install vue plugin
// ----------------------------------------
Vue.use(VueAxios);

// ----------------------------------------
// setToken helper
// ----------------------------------------
export const setToken = function (_token) {
    token = _token;
    axios.defaults.headers.common.Authorization = _token ? `Bearer ${_token}` : null;
};
{'fraction_non_alphanumeric': 0.230288364589635, 'fraction_numerical': 0.0028231498285944748, 'mean_word_length': 1.7555555555555555, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1505275', 'n_tokens_mistral': 1464, 'n_tokens_neox': 1429, 'n_words': 359}