<commit_before>import { getErrorBadStatusCode } from './errors';
import { getJsonResponse } from './json';
import { AnyJson } from './typescript';

type ResponseOrError<T> =
  | { response: T; errors: undefined }
  | { response: undefined; errors: string[] };

export const handleFetchResponse = (
  response: Response,
): Promise<ResponseOrError<AnyJson>> =>
  getJsonResponse(response).then(
    (jsonResponse): ResponseOrError<AnyJson> =>
      response.ok
        ? {
            response: jsonResponse,
            errors: undefined,
          }
        : {
            response: undefined,
            errors: getErrorBadStatusCode(jsonResponse),
          },
  );<commit_msg>add type and status code to response/error<commit_after>import { getErrorBadStatusCode } from './errors';
import { getJsonResponse } from './json';
import { AnyJson } from './typescript';

type CommonProps = { status: number };

type ResponseOrError<T> = CommonProps &
  (
    | { type: 'response'; response: T; errors: undefined }
    | { type: 'error'; response: undefined; errors: string[] }
  );

export const handleFetchResponse = (
  response: Response,
): Promise<ResponseOrError<AnyJson>> =>
  getJsonResponse(response).then(
    (jsonResponse): ResponseOrError<AnyJson> =>
      response.ok
        ? {
            type: 'response',
            status: response.status,
            response: jsonResponse,
            errors: undefined,
          }
        : {
            type: 'error',
            status: response.status,
            errors: getErrorBadStatusCode(jsonResponse),
            response: undefined,
          },
  );
{'fraction_non_alphanumeric': 0.12384473197781885, 'fraction_numerical': 0.0, 'mean_word_length': 2.6412556053811658, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, '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': '4476951', 'n_tokens_mistral': 429, 'n_tokens_neox': 432, 'n_words': 103}