<commit_before>import {flatten}          from 'lodash';
import {inspect}          from 'util';
import {writeFileSync}    from 'fs';
import gs                 from 'glob-stream';
import path               from 'path';
import eventStream        from 'event-stream';
import Hapi               from 'hapi';
import watch              from 'glob-watcher';
import appRootDir         from 'app-root-dir';
import format             from 'string-template';

import {forceRequireFile} from './utils'

let contract;
export function getRoutes (options) {
  contract = [];
  return new Promise(resolve => {
    try {
      return gs(options.src)
      .pipe(eventStream.through(
        (file, callback) => read(file, options),
        () => end(options, resolve)
      ));
    } catch (error) {
      console.error(error);
    }
  });
}

function formattedRoute (route, params = {}) {
  return format(route, params);
}

function read (file, options) {
  let fileContents = forceRequireFile(file.path);

  let transformedEndpoints = fileContents.map((endpoint) => {
    let request = endpoint.defaultRequest || {};

    let transformedEndpoint = {
      route: formattedRoute(endpoint.route, request.params),
      method: endpoint.method,
      authenticated: endpoint.authenticated || true,
      in_progress: endpoint.in_progress || false,
      payload: endpoint.response(request)
    };

    if (endpoint.defaultRequest) transformedEndpoint.defaultRequest = endpoint.defaultRequest;

    return transformedEndpoint;
  });

  contract.push(flatten(transformedEndpoints));
}

function end (options, resolve) {
  let contractPath = path.join(options.baseDir, options.dest, 'api_contract.json');
  let jsonContract = JSON.stringify(flatten(contract), null, 2);
  writeFileSync(contractPath, jsonContract);
  console.log(`Api Contract generated at: ${contractPath}`);
  resolve();
}<commit_msg>adds headers to the api contract<commit_after>import {flatten}          from 'lodash';
import {inspect}          from 'util';
import {writeFileSync}    from 'fs';
import gs                 from 'glob-stream';
import path               from 'path';
import eventStream        from 'event-stream';
import Hapi               from 'hapi';
import watch              from 'glob-watcher';
import appRootDir         from 'app-root-dir';
import format             from 'string-template';

import {forceRequireFile} from './utils'

let contract;
export function getRoutes (options) {
  contract = [];
  return new Promise(resolve => {
    try {
      return gs(options.src)
      .pipe(eventStream.through(
        (file, callback) => read(file, options),
        () => end(options, resolve)
      ));
    } catch (error) {
      console.error(error);
    }
  });
}

function formattedRoute (route, params = {}) {
  return format(route, params);
}

function read (file, options) {
  let fileContents = forceRequireFile(file.path);

  let transformedEndpoints = fileContents.map((endpoint) => {
    let request = endpoint.defaultRequest || {};

    let transformedEndpoint = {
      route: formattedRoute(endpoint.route, request.params),
      method: endpoint.method,
      headers: endpoint.headers || {},
      in_progress: endpoint.in_progress || false,
      payload: endpoint.response(request)
    };

    if (endpoint.defaultRequest) transformedEndpoint.defaultRequest = endpoint.defaultRequest;

    return transformedEndpoint;
  });

  contract.push(flatten(transformedEndpoints));
}

function end (options, resolve) {
  let contractPath = path.join(options.baseDir, options.dest, 'api_contract.json');
  let jsonContract = JSON.stringify(flatten(contract), null, 2);
  writeFileSync(contractPath, jsonContract);
  console.log(`Api Contract generated at: ${contractPath}`);
  resolve();
}
{'fraction_non_alphanumeric': 0.12164674634794156, 'fraction_numerical': 0.0005312084993359894, 'mean_word_length': 3.2219730941704037, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '454325', 'n_tokens_mistral': 1044, 'n_tokens_neox': 1024, 'n_words': 266}