const axios = require('axios');
const through2 = require('through2');
const JSONStream = require('JSONStream');

function processRow(filing, enc, next) {
    if (filing.fec_file_id) {
        filing.fec_file_id = parseInt(
            filing.fec_file_id
                .replace('FEC-', '')
                .replace('SEN-','')
        );
    }

    this.push(filing);

    next();
}

module.exports = async options => {
    if (!options.key) {
        throw new Error('Error: key needed to use FEC API,' +
            'specify using options.key, ' +
            'or get one at https://api.data.gov/signup/');
    }

    const outStream = options.out || process.stdout;
    const baseURL = options.baseURL || 'https://api.open.fec.gov/v1/';
    const sort = options.sort || '-receipt_date';
    const perPage = options.perPage || 100;
    const page = options.page || 1;
    const endpoint = options.committee ?
        `committee/${options.committee}/filings/` :
        'efile/filings';
    const apiKey = options.key;

    const fecAPI = axios.create({
        baseURL
    });

    const response = await fecAPI.get(endpoint, {
        params: {
            sort,
            per_page: perPage,
            page,
            api_key: apiKey
        },
        responseType: 'stream'
    });

    return response.data
        .pipe(JSONStream.parse('results.*'))
        .pipe(
            through2.obj(
                processRow
            )
        );
};

const fetch = require('isomorphic-unfetch');
const streamify = require('into-stream');

module.exports = async options => {
    if (!options.key) {
        throw new Error('Error: key needed to use FEC API,' +
            'specify using options.key, ' +
            'or get one at https://api.data.gov/signup/');
    }

    const baseURL = options.baseURL || 'https://api.open.fec.gov/v1/';
    const endpoint = options.committee ?
        `committee/${options.committee}/filings/` :
        'efile/filings';

    const params = {
        sort: options.sort || '-receipt_date',
        per_page: options.perPage || 100,
        page: options.page || 1,
        api_key: options.key
    };

    // https://stackoverflow.com/a/34209399
    const query = Object.keys(params)
        .map(k => `${encodeURIComponent(k)}=${encodeURIComponent(params[k])}`) 
        .join('&');

    const response = await fetch(`${baseURL}${endpoint}?${query}`);
    const data = await response.json();

    return streamify.object(data.results.map(filing => {
        if (filing.fec_file_id) {
            return {
                ...filing,
                fec_file_id: parseInt(
                    filing.fec_file_id
                        .replace('FEC-', '')
                        .replace('SEN-','')
                )
            }
        }

        return filing;
    }));
};

Migrate API call to use fetch
Migrate API call to use fetch
