<commit_before>// ensure env has promise support
// this should probably be moved elsewhere / should be part of the extra
// deps for older environemnts
import 'es6-promise';

import 'isomorphic-fetch';

import {
  isString,
  assign,
} from 'lodash';

import { GraphQLResult, GraphQLError } from 'graphql';

export interface Request {
  debugName?: string;
  query?: string;
  variables?: Object;
}

export interface NetworkInterface {
  _uri: string;
  _opts: RequestInit;
  query(requests: Array<Request>): Promise<Array<GraphQLResult>>;
}

export function createNetworkInterface(uri: string, opts: RequestInit = {}): NetworkInterface {
  if (!uri) {
    throw new Error('A remote enpdoint is required for a network layer');
  }

  if (!isString(uri)) {
    throw new Error('Remote endpoint must be a string');
  }

  const _uri: string = uri;
  const _opts: RequestInit = assign({}, opts);

  function fetchFromRemoteEndpoint(request: Request): Promise<IResponse> {
    return fetch(uri, assign({}, _opts, {
      body: JSON.stringify(request),
      headers: assign({}, _opts.headers, {
        Accept: '*/*',
        'Content-Type': 'application/json',
      }),
      method: 'POST',
    }));
  };

  function query(requests: Array<Request>): Promise<Array<GraphQLResult | GraphQLError>> {
    let clonedRequests = [...requests];

    return Promise.all(clonedRequests.map(request => (
      fetchFromRemoteEndpoint(request)
        .then(result => result.json())
        .then((payload: GraphQLResult) => {
          if (payload.hasOwnProperty('errors')) {
            throw payload;
          } else if (!payload.hasOwnProperty('data')) {
            throw new Error(
              `Server response was missing for query '${request.debugName}'.`<commit_msg>add typing to error<commit_after>// ensure env has promise support
// this should probably be moved elsewhere / should be part of the extra
// deps for older environemnts
import 'es6-promise';

import 'isomorphic-fetch';

import {
  isString,
  assign,
} from 'lodash';

import { GraphQLResult, GraphQLError } from 'graphql';

export interface Request {
  debugName?: string;
  query?: string;
  variables?: Object;
}

export interface NetworkInterface {
  _uri: string;
  _opts: RequestInit;
  query(requests: Array<Request>): Promise<Array<GraphQLResult>>;
}

export function createNetworkInterface(uri: string, opts: RequestInit = {}): NetworkInterface {
  if (!uri) {
    throw new Error('A remote enpdoint is required for a network layer');
  }

  if (!isString(uri)) {
    throw new Error('Remote endpoint must be a string');
  }

  const _uri: string = uri;
  const _opts: RequestInit = assign({}, opts);

  function fetchFromRemoteEndpoint(request: Request): Promise<IResponse> {
    return fetch(uri, assign({}, _opts, {
      body: JSON.stringify(request),
      headers: assign({}, _opts.headers, {
        Accept: '*/*',
        'Content-Type': 'application/json',
      }),
      method: 'POST',
    }));
  };

  function query(requests: Array<Request>): Promise<Array<GraphQLResult | GraphQLError>> {
    let clonedRequests = [...requests];

    return Promise.all(clonedRequests.map(request => (
      fetchFromRemoteEndpoint(request)
        .then(result => result.json())
        .then((payload: GraphQLResult) => {
          if (payload.hasOwnProperty('errors')) {
            throw payload as GraphQLError;
          } else if (!payload.hasOwnProperty('data')) {
            throw new Error(
              `Server response was missing for query '${request.debugName}'.`
{'fraction_non_alphanumeric': 0.12999437253798538, 'fraction_numerical': 0.0005627462014631402, 'mean_word_length': 3.454887218045113, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, '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': '1170139', 'n_tokens_mistral': 1020, 'n_tokens_neox': 1010, 'n_words': 292}