<commit_before>// Request utils,
// feel free to replace with your code
// (get, post are used in ApiServices)

import {getLocalToken, resetLocalToken} from 'api'

export default function requestWrapper (method) {
	return async function (url, data = null, params = {}) {
		if (method === 'GET') {
			// Is it a GET?
			// GET doesn't have data
			params = data
			data = null
		} else if (data === Object(data)) {
			// (data === Object(data)) === _.isObject(data)
			data = JSON.stringify(data)
		} else {
			throw new Error(`XHR invalid, check ${method} on ${url}`)
		}

		// Default params for fetch = method + (Content-Type)
		const defaults = {
			method,
			headers: {
				'Content-Type': 'application/json; charset=UTF-8'
			}
		}

		// Check that req url is relative and request was sent to our domain
		if (url.match(/^https?:\/\//gi) > -1) {
			const token = getLocalToken()
			if (token) {
				defaults.headers.Authorization = `JWT ${token}`
			}
			url = process.env.BASE_API + url
		}

		if (data) {
			defaults.body = data
		}

		const paramsObj = {...defaults, headers: {...params, ...defaults.headers}}
		return fetch(url, paramsObj)
			.then(checkStatus)
			.then(parseJSON)
			.catch(err => {
				console.error(err)
			})
	}
}

// Checks response status in production env
function checkStatus (response) {
	const {status} = response
	if (status >= 200 && status < 300) {
		// Everything is ok
		return response
	} else if (status >= 300 && status < 400) {
		// 300 Multiple Choices
		// 301 - Moved Permanently,
		// 302 - Found, Moved Temporarily
		// 304 - not modified
		// 307 - Temporary Redirect
	} else if (status === 400) {
		// Probably is a validation error<commit_msg>refactor(src/common/api/utils/xhr_wrapper.js): some comments, fix export of localStorage stuff<commit_after>// Request utils,
// Feel free to replace with your code
// (get, post are used in ApiServices)

import {getLocalToken, resetLocalToken} from 'api/LocalStorageCookiesSvc'

function requestWrapper (method) {
	return async function (url, data = null, params = {}) {
		if (method === 'GET') {
			// It's a GET response
			// GET doesn't have data
			params = data
			data = null
		} else if (data === Object(data)) {
			// (data === Object(data)) === _.isObject(data)
			data = JSON.stringify(data)
		} else {
			throw new Error(`XHR invalid, check ${method} on ${url}`)
		}

		// Default params for fetch = method + (Content-Type)
		const defaults = {
			method,
			headers: {
				'Content-Type': 'application/json; charset=UTF-8'
			}
		}

		// Check that req url is relative and request was sent to our domain
		if (!/(http|https):\/\//.test(url)) {
			console.log(`Request ${url} was sent to our domain`)
			const token = getLocalToken()
			if (token) {
				defaults.headers.Authorization = `JWT ${token}`
			}
			url = process.env.BASE_API + url
		} else {
			console.log(`Request ${url} was sent to external domain`)
			// Request was set to an external domain
		}

		if (data) {
			defaults.body = data
		}

		const paramsObj = {...defaults, headers: {...params, ...defaults.headers}}
		return fetch(url, paramsObj)
			.then(checkStatus)
			.then(parseJSON)
			.catch(err => {
				console.error(err)
			})
	}
}

// Checks response status in production env
function checkStatus (response) {
	const {status} = response
	if (status >= 200 && status < 300) {
		// Everything is ok
		return response
	} else if (status >= 300 && status < 400) {
		// 300 Multiple Choices
		// 301 - Moved Permanently,
		// 302 - Found, Moved Temporarily
		// 304 - not modified
		// 307 - Temporary Redirect
	} else if (status === 400) {
		// Probably is a validation error
{'fraction_non_alphanumeric': 0.14371584699453552, 'fraction_numerical': 0.01721311475409836, 'mean_word_length': 2.890541976620616, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, 'https://': 0, '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': '6183582', 'n_tokens_mistral': 1369, 'n_tokens_neox': 1157, 'n_words': 377}