{"size": 1293, "ext": "js", "max_stars_repo_path": "app/api/api-operations.js", "max_stars_repo_name": "dtodo1paco/node-react-boilerplate", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "app/api/api-operations.js", "max_issues_repo_name": "dtodo1paco/node-react-boilerplate", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "app/api/api-operations.js", "max_forks_repo_name": "dtodo1paco/node-react-boilerplate", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.0892857143, "max_line_length": 76, "alphanum_fraction": 0.6875483372}
import { buildError } from 'utils/errors';
import {
  AUTH_USER_START,
  AUTH_REAUTH_USER_START,
  AUTH_LOGOUT_START,
} from 'containers/App/constants';
import { USER_SETTINGS_SET_START } from 'containers/UserSettings/constants';
import { getUser } from './storage';
import { API_USER, API_AUTH } from './api-config';

//
// --- DEFINE ALL OPERATIONS DATA USING action.type CONSTANTS
//

const operationsMap = {};
operationsMap[USER_SETTINGS_SET_START] = {
  endpoint: `${API_USER}settings`,
  method: 'POST',
  requiresToken: true,
};
operationsMap[AUTH_REAUTH_USER_START] = {
  endpoint: API_AUTH,
  authId: 'me',
  method: 'GET',
  requiresToken: true,
};
operationsMap[AUTH_USER_START] = {
  endpoint: API_AUTH,
  method: 'POST',
  requiresToken: false,
};
operationsMap[AUTH_LOGOUT_START] = {
  endpoint: API_AUTH,
  authId: 'logout',
  method: 'POST',
  requiresToken: true,
};

//
// --- accessor to operation details
//
export const getOperationDetails = opId => {
  const opDet = operationsMap[opId];
  if (opDet) {
    // set token if required
    if (opDet.requiresToken) {
      opDet.token = getUser();
    }
  } else {
    // eslint-disable-next-line
    console.error(`Unknown operation: ${opId}`);
    throw buildError(500, `unknown.operation.${opId}`);
  }
  return opDet;
};
