<commit_before>import fetch from 'isomorphic-fetch';

/**
 * Helper to stich together any defined params into a string suitable for url arg submission
 */
function generateParamStr(params) {
  const cleanedParams = {};
  for (const key in params) {
    const value = params[key];
    if ((value !== null) && (value !== undefined)) {
      cleanedParams[key] = value;
    }
  }
  const paramStr = Object.keys(cleanedParams).map((key) => `${key}=${encodeURIComponent(cleanedParams[key])}`).join('&');
  return paramStr;
}

/**
 * Helper to create a promise that calls the API on the server. Pass in the endpoint url, with params
 * encoded on it already, and this will return a promise to call it with the appropriate headers and
 * such.  It also parses the json response for you.
 */
function createApiPromise(url, params) {
  let fullUrl = url;
  if (params !== undefined) {
    fullUrl = `${url}?${generateParamStr(params)}`;
  }
  return fetch(fullUrl, {
    method: 'get',
    credentials: 'include',
  }).then(
    response => response.json()
  );
}

/**
 * Helper to create a promise that calls the API on the server with some POST'd data. Pass in the endpoint url,
 * and a data object to encode and POST, and this will return a promise to call it with the appropriate headers
 * and such.  It also parses the json response for you.
 */

function createPostingApiPromise(url, params) {
  const formData = new FormData();
  for (const key in params) {
    formData.append(key, params[key]);
  }
  return fetch(url, {
    method: 'post',
    credentials: 'include',
    body: formData,
  }).then(
    response => response.json()
  );
}

export const topicsList = (linkId) => createApiPromise('/api/topics/list', { linkId });

export function topicSummary(topicId) {
  return createApiPromise(`/api/topics/${topicId}/summary`);
}

export const topicTopStories = (topicId, params) => createApiPromise(`/api/topics/${topicId}/stories`, params);

export const topicTopMedia = (topicId, params) => createApiPromise(`/api/topics/${topicId}/media`, params);

export const topicTopWords = (topicId, params) => createApiPromise(`/api/topics/${topicId}/words`, params);

export const topicSnapshotsList = (topicId) => createApiPromise(`/api/topics/${topicId}/snapshots/list`);

export const topicTimespansList = (topicId, snapshotId) => createApiPromise(`/api/topics/${topicId}/snapshots/${snapshotId}/timespans/list`);

export const topicSentenceCounts = (topicId, params) => createApiPromise(`/api/topics/${topicId}/sentences/count`, params);

export const story = (topicId, storiesId) => createApiPromise(`/api/topics/${topicId}/stories/${storiesId}`);<commit_msg>wrap for-in in if<commit_after>import fetch from 'isomorphic-fetch';

/**
 * Helper to stich together any defined params into a string suitable for url arg submission
 */
function generateParamStr(params) {
  const cleanedParams = {};
  for (const key in params) {
    if (params.hasOwnProperty(key)) {
      const value = params[key];
      if ((value !== null) && (value !== undefined)) {
        cleanedParams[key] = value;
      }
    }
  }
  const paramStr = Object.keys(cleanedParams).map((key) => `${key}=${encodeURIComponent(cleanedParams[key])}`).join('&');
  return paramStr;
}

/**
 * Helper to create a promise that calls the API on the server. Pass in the endpoint url, with params
 * encoded on it already, and this will return a promise to call it with the appropriate headers and
 * such.  It also parses the json response for you.
 */
function createApiPromise(url, params) {
  let fullUrl = url;
  if (params !== undefined) {
    fullUrl = `${url}?${generateParamStr(params)}`;
  }
  return fetch(fullUrl, {
    method: 'get',
    credentials: 'include',
  }).then(
    response => response.json()
  );
}

/**
 * Helper to create a promise that calls the API on the server with some POST'd data. Pass in the endpoint url,
 * and a data object to encode and POST, and this will return a promise to call it with the appropriate headers
 * and such.  It also parses the json response for you.
 */

function createPostingApiPromise(url, params) {
  const formData = new FormData();
  for (const key in params) {
    if (params.hasOwnProperty(key)) {
      formData.append(key, params[key]);
    }
  }
  return fetch(url, {
    method: 'post',
    credentials: 'include',
    body: formData,
  }).then(
    response => response.json()
  );
}

export const topicsList = (linkId) => createApiPromise('/api/topics/list', { linkId });

export function topicSummary(topicId) {
  return createApiPromise(`/api/topics/${topicId}/summary`);
}

export const topicTopStories = (topicId, params) => createApiPromise(`/api/topics/${topicId}/stories`, params);

export const topicTopMedia = (topicId, params) => createApiPromise(`/api/topics/${topicId}/media`, params);

export const topicTopWords = (topicId, params) => createApiPromise(`/api/topics/${topicId}/words`, params);

export const topicSnapshotsList = (topicId) => createApiPromise(`/api/topics/${topicId}/snapshots/list`);

export const topicTimespansList = (topicId, snapshotId) => createApiPromise(`/api/topics/${topicId}/snapshots/${snapshotId}/timespans/list`);

export const topicSentenceCounts = (topicId, params) => createApiPromise(`/api/topics/${topicId}/sentences/count`, params);

export const story = (topicId, storiesId) => createApiPromise(`/api/topics/${topicId}/stories/${storiesId}`);
{'fraction_non_alphanumeric': 0.14061924069295983, 'fraction_numerical': 0.0, 'mean_word_length': 4.367952522255193, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, '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': '1155030', 'n_tokens_mistral': 1608, 'n_tokens_neox': 1645, 'n_words': 492}