<commit_before>import { useStore } from "../store";
import { deleteCookie } from "./cookie";

export enum Method {
    GET = "GET",
    POST = "POST",
    DELETE = "DELETE",
    PATCH = "PATCH"
}

export enum RejectReason {
    NotFound = '{"message":"Not Found"}'
}

export async function doFetch(method: Method, path: string, params: URLSearchParams = new URLSearchParams(), body: any = undefined) {
    const { apiBaseUrl, scToken, managing } = useStore.getState();

    const headers: Record<string, string> = { 'content-type': 'application/json' }
    if (scToken) {
        headers.Authorization = `Bearer ${scToken}`
    }

    const config: RequestInit = {
        method: method,
        headers: {
            ...headers,
        },
    }

    if (body) {
        config.body = JSON.stringify(body)
    }

    const url = new URL(path, apiBaseUrl);
    if (managing) {
        url.searchParams.append('managing', managing);
    }

    params.forEach((value, key) => url.searchParams.append(key, value));

    return window.fetch(url.toString(), config)
        .then(async response => {
            if (response.ok) {
                const text = await response.text();
                return text !== "" ? JSON.parse(text) : null
            } else {
                if (response.status === 404) {
                    return Promise.reject(RejectReason.NotFound);
                }

                const errorMessage = await response.text()
                return Promise.reject(errorMessage)
            }<commit_msg>reset state when auth is invalid<commit_after>import { useStore } from "../store";
import { deleteCookie } from "./cookie";

export enum Method {
    GET = "GET",
    POST = "POST",
    DELETE = "DELETE",
    PATCH = "PATCH"
}

export enum RejectReason {
    NotFound = '{"message":"Not Found"}'
}

export async function doFetch(method: Method, path: string, params: URLSearchParams = new URLSearchParams(), body: any = undefined) {
    const { apiBaseUrl, scToken, managing } = useStore.getState();

    const headers: Record<string, string> = { 'content-type': 'application/json' }
    if (scToken) {
        headers.Authorization = `Bearer ${scToken}`
    }

    const config: RequestInit = {
        method: method,
        headers: {
            ...headers,
        },
    }

    if (body) {
        config.body = JSON.stringify(body)
    }

    const url = new URL(path, apiBaseUrl);
    if (managing) {
        url.searchParams.append('managing', managing);
    }

    params.forEach((value, key) => url.searchParams.append(key, value));

    return window.fetch(url.toString(), config)
        .then(async response => {
            if (response.status === 401) {
                useStore.setState({ scToken: undefined });
            }
            if (response.ok) {
                const text = await response.text();
                return text !== "" ? JSON.parse(text) : null
            } else {
                if (response.status === 404) {
                    return Promise.reject(RejectReason.NotFound);
                }

                const errorMessage = await response.text()
                return Promise.reject(errorMessage)
            }
{'fraction_non_alphanumeric': 0.13176838810641628, 'fraction_numerical': 0.0028169014084507044, 'mean_word_length': 2.0849420849420848, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 9, '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': '6474804', 'n_tokens_mistral': 908, 'n_tokens_neox': 865, 'n_words': 223}