{"size": 2285, "ext": "js", "max_stars_repo_path": "src/Constants.js", "max_stars_repo_name": "ViBiOh/dashboard", "max_stars_repo_stars_event_min_datetime": "2017-05-26T21:01:02.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-20T01:40:11.000Z", "max_issues_repo_path": "src/Constants.js", "max_issues_repo_name": "ViBiOh/dashboard", "max_issues_repo_issues_event_min_datetime": "2017-05-30T11:29:53.000Z", "max_issues_repo_issues_event_max_datetime": "2018-08-05T11:31:46.000Z", "max_forks_repo_path": "src/Constants.js", "max_forks_repo_name": "ViBiOh/dashboard", "max_forks_repo_forks_event_min_datetime": "2017-12-07T06:47:35.000Z", "max_forks_repo_forks_event_max_datetime": "2018-08-04T21:07:01.000Z"}
{"max_stars_count": 15, "max_issues_count": 7, "max_forks_count": 2, "avg_line_length": 21.7619047619, "max_line_length": 80, "alphanum_fraction": 0.6358862144}
import funtch from 'funtch';
import { computeRedirectSearch } from 'utils/SearchParams';

/**
 * App context that store variables.
 * @type {Object}
 */
let context = {};

/**
 * Name of Key for storage
 * @type {String}
 */
export const STORAGE_KEY_AUTH = 'auth';

/**
 * Maximum number of stats stored (one per second)
 * @type {Number}
 */
export const STATS_COUNT = 60;

/**
 * Timeout for input debounce.
 * @type {Number}
 */
export const DEBOUNCE_TIMEOUT = 300;

/**
 * Initialize context from remote endpoint
 * @return {Promise<Object>} Context
 */
export function init() {
  return new Promise(resolve => {
    if (process.env.NODE_ENV === 'development') {
      context = {
        API_URL: 'http://localhost:1082',
        AUTH_URL: 'http://localhost:1081',
        BASIC_AUTH_ENABLED: 'true',
        ENVIRONMENT: 'dev',
        GITHUB_AUTH_ENABLED: 'false',
        WS_URL: 'ws://localhost:1082/ws',
      };

      resolve(context);
    } else if (process.env.NODE_ENV === 'staging') {
      context = {
        API_URL: 'https://dashboard-api.vibioh.fr',
        AUTH_URL: 'https://dashboard-auth.vibioh.fr',
        BASIC_AUTH_ENABLED: 'false',
        ENVIRONMENT: 'dev',
        GITHUB_AUTH_ENABLED: 'true',
        WS_URL: 'wss://dashboard-api.vibioh.fr/ws',
      };

      resolve(context);
    } else {
      funtch.get('/env').then(env => {
        context = env;
        resolve(context);
      });
    }
  });
}

/**
 * Get given key from context.
 * @param {String} key Wanted key
 * @return {String} Value from context
 */
export function getFromContext(key) {
  return context[key];
}

/**
 * Return API endpoint URL
 * @return {String} API endpoint URL
 */
export function getApiUrl() {
  return getFromContext('API_URL');
}

/**
 * Return WebSocket endpoint URL
 * @return {String} WebSocket endpoint URL
 */
export function getWsUrl() {
  return getFromContext('WS_URL');
}

/**
 * Return OAuth API endpoint URL
 * @return {String} WebSocket endpoint URL
 */
export function getAuthApiUrl() {
  return getFromContext('AUTH_URL');
}

/**
 * Return Github Oauth API endpoint URL
 * @return {String} WebSocket endpoint URL
 */
export function getGithubOauthUrl(redirect) {
  return `${getAuthApiUrl()}/redirect/github${computeRedirectSearch(redirect)}`;
}
