{"size": 2196, "ext": "js", "max_stars_repo_path": "web-interface/src/api/Auth.js", "max_stars_repo_name": "Egor4ik325/anyberry", "max_stars_repo_stars_event_min_datetime": "2021-09-12T16:28:52.000Z", "max_stars_repo_stars_event_max_datetime": "2021-09-12T16:28:52.000Z", "max_issues_repo_path": "web-interface/src/api/Auth.js", "max_issues_repo_name": "Egor4ik325/anyberry", "max_issues_repo_issues_event_min_datetime": "2021-09-06T08:31:56.000Z", "max_issues_repo_issues_event_max_datetime": "2021-09-06T08:35:25.000Z", "max_forks_repo_path": "web-interface/src/api/Auth.js", "max_forks_repo_name": "Egor4ik325/anyberry", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 29.28, "max_line_length": 101, "alphanum_fraction": 0.6006375228}
import axios from "axios";
import { USER_URL } from "./constants";
import { APIError, RegistrationError, DetailError, RegistrationBadRequestError } from "./exceptions";
import endpoints from "./endpoints";

// Determine whether user session 
export const checkSession = async () => {
    try {
        const response = await axios.get(USER_URL, { withCredentials: true });
        if (response.status === 200) {
            return true;
        }
        return false;
    } catch (errors) {
        console.error("Check session error", errors);
        return false;
    }
}


// Make a registration request to the API
// Return response or raised error object
export const register = async ({username, email, password}) => {
    try {
        const response = await axios.post(endpoints.register, {
            username: username,
            email: email,
            password1: password,
            password2: password,
        });

        return new RegistrationResponse(response);
    } catch (error) {
        // Notify registering user about API exceptions

        // If non-2xx response status code
        if (error.response) {
            // Dispatch error type based on status code
            if (error.response.status === 400) {
                throw new RegistrationBadRequestError(error.response);
            }

            // Extract information about error from response
            if ("detail" in error.response) {
                throw new DetailError({ detail: error.response.detail });
            }

            // Construct and raise error object (exception)
            throw new RegistrationError();
        }

        // Reraise other exceptions
        throw error;
    }
}

class DetailResponse {
    constructor(response) {
        const { detail = null } = response.data;
        this.detail = detail;
    }
}

class RegistrationResponse extends DetailResponse { }

export const resendEmail = async ({ email }) => {
    try {
        const response = await axios.post(endpoints.resend, { email: email });

        return new DetailResponse(response);
    } catch (error) {
        // Should not be any API errors (even throttling )
        throw error;
    }
}