<commit_before>// For working with folders, mostly for searching for envelopes

var dsUtils = require('./../dsUtils');

exports.init = function (accountId, baseUrl, accessToken) {
  return {
    /**
     * Get information about envelopes for the account with the given `apiToken`.
     *
     * @memberOf Folders
     * @public
     * @function
     * @param {string} envelopeType - The type of envelope to get.
     *   Valid values are 'all', 'awaiting_my_signature', 'out_for_signature',
     *   'completed', and 'drafts'.
     * @param {boolean} doFullRetrieval - If true, retrieve all envelopes of
     *  the given `envelopeType`. Otherwise, only get the first 50 most recent
     *  envelopes.
     * @param {function} [callback] - Returns in the form of function(error, response)
     *   such that envelopes will live at `response.folderItems.
     * @returns {Promise} - A thenable bluebird Promise; if callback is given it is called before the promise is resolved
     */
    getEnvelopes: function (envelopeType, doFullRetrieval, callback) {
      return getEnvelopes(accessToken, baseUrl, envelopeType, doFullRetrieval).asCallback(callback);
    },

    /**
     * Search DS envelopes with the given `searchTerm`.
     *
     * @memberOf Folders
     * @public
     * @function
     * @param {string} searchTerm - The term that the DS API should search for.
     * @param {function} [callback] - Returns in the form of function(error, matchingEnvelopes).
     * @returns {Promise} - A thenable bluebird Promise; if callback is given it is called before the promise is resolved
     */
    searchThroughEnvelopes: function (searchTerm, callback) {
      return searchThroughEnvelopes(accessToken, baseUrl, searchTerm).asCallback(callback);
    }
  };
};

/**
 * Get information about envelopes for the account with the given `apiToken`.
 *
 * @memberOf Folders
 * @private
 * @function
 * @param {string} apiToken - DocuSign API OAuth2 access token.
 * @param {string} baseUrl - DocuSign API base URL.
 * @param {string} envelopeType - The type of envelope to get.
 *   Valid values are 'all', 'awaiting_my_signature', 'out_for_signature',
 *   'completed', and 'drafts'.
 * @param {boolean} doFullRetrieval - If true, retrieve all envelopes of
 *  the given `envelopeType`. Otherwise, only get the first 50 most recent
 *  envelopes.
 * @returns {Promise} - A thenable bluebird Promise that it is fulfilled with envelopes at `result.folderItems`.
 */
function getEnvelopes (apiToken, baseUrl, envelopeType, doFullRetrieval) {
  var startUri = '/search_folders/' + envelopeType + '?start_position=0';
  function _getEnvelopes (uri, envelopes) {
    envelopes = envelopes || [];
    var options = {
      method: 'GET',
      url: baseUrl + uri + '&include_recipients=true',
      headers: dsUtils.getHeaders(apiToken)
    };
    return dsUtils.makeRequest('Get Envelopes', options).then(function (response) {
      envelopes = envelopes.concat(response.folderItems);
      // continue or return envelopes
      return doFullRetrieval ? _getEnvelopes(response.nextUri, envelopes) : envelopes;
    });
  }
  return _getEnvelopes(startUri);
}

/**
 * Search DS envelopes with the given `searchTerm`.
 *
 * @memberOf Folders
 * @private
 * @function
 * @param {string} apiToken - DocuSign API OAuth2 access token.
 * @param {string} baseUrl - DocuSign API base URL..
 * @param {string} searchTerm - The term that the DS API should search for.
 * @returns {Promise} - A thenable bluebird Promise
 */
function searchThroughEnvelopes (apiToken, baseUrl, searchTerm) {
  var options = {
    method: 'GET',<commit_msg>fix infinite loop in getEnvelopes<commit_after>// For working with folders, mostly for searching for envelopes

var dsUtils = require('./../dsUtils');

exports.init = function (accountId, baseUrl, accessToken) {
  return {
    /**
     * Get information about envelopes for the account with the given `apiToken`.
     *
     * @memberOf Folders
     * @public
     * @function
     * @param {string} envelopeType - The type of envelope to get.
     *   Valid values are 'all', 'awaiting_my_signature', 'out_for_signature',
     *   'completed', and 'drafts'.
     * @param {boolean} doFullRetrieval - If true, retrieve all envelopes of
     *  the given `envelopeType`. Otherwise, only get the first 50 most recent
     *  envelopes.
     * @param {function} [callback] - Returns in the form of function(error, response)
     *   such that envelopes will live at `response.folderItems.
     * @returns {Promise} - A thenable bluebird Promise; if callback is given it is called before the promise is resolved
     */
    getEnvelopes: function (envelopeType, doFullRetrieval, callback) {
      return getEnvelopes(accessToken, baseUrl, envelopeType, doFullRetrieval).asCallback(callback);
    },

    /**
     * Search DS envelopes with the given `searchTerm`.
     *
     * @memberOf Folders
     * @public
     * @function
     * @param {string} searchTerm - The term that the DS API should search for.
     * @param {function} [callback] - Returns in the form of function(error, matchingEnvelopes).
     * @returns {Promise} - A thenable bluebird Promise; if callback is given it is called before the promise is resolved
     */
    searchThroughEnvelopes: function (searchTerm, callback) {
      return searchThroughEnvelopes(accessToken, baseUrl, searchTerm).asCallback(callback);
    }
  };
};

/**
 * Get information about envelopes for the account with the given `apiToken`.
 *
 * @memberOf Folders
 * @private
 * @function
 * @param {string} apiToken - DocuSign API OAuth2 access token.
 * @param {string} baseUrl - DocuSign API base URL.
 * @param {string} envelopeType - The type of envelope to get.
 *   Valid values are 'all', 'awaiting_my_signature', 'out_for_signature',
 *   'completed', and 'drafts'.
 * @param {boolean} doFullRetrieval - If true, retrieve all envelopes of
 *  the given `envelopeType`. Otherwise, only get the first 50 most recent
 *  envelopes.
 * @returns {Promise} - A thenable bluebird Promise that it is fulfilled with envelopes at `result.folderItems`.
 */
function getEnvelopes (apiToken, baseUrl, envelopeType, doFullRetrieval) {
  var startUri = '/search_folders/' + envelopeType + '?start_position=0';
  function _getEnvelopes (uri, envelopes) {
    envelopes = envelopes || [];
    if (uri == null) {
      return envelopes;
    }
    var options = {
      method: 'GET',
      url: baseUrl + uri + '&include_recipients=true',
      headers: dsUtils.getHeaders(apiToken)
    };
    return dsUtils.makeRequest('Get Envelopes', options).then(function (response) {
      envelopes = envelopes.concat(response.folderItems);
      // continue or return envelopes
      return doFullRetrieval ? _getEnvelopes(response.nextUri, envelopes) : envelopes;
    });
  }
  return _getEnvelopes(startUri);
}

/**
 * Search DS envelopes with the given `searchTerm`.
 *
 * @memberOf Folders
 * @private
 * @function
 * @param {string} apiToken - DocuSign API OAuth2 access token.
 * @param {string} baseUrl - DocuSign API base URL..
 * @param {string} searchTerm - The term that the DS API should search for.
 * @returns {Promise} - A thenable bluebird Promise
 */
function searchThroughEnvelopes (apiToken, baseUrl, searchTerm) {
  var options = {
    method: 'GET',
{'fraction_non_alphanumeric': 0.10400437337706711, 'fraction_numerical': 0.001913352466858002, 'mean_word_length': 3.486817903126916, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2318814', 'n_tokens_mistral': 2173, 'n_tokens_neox': 2080, 'n_words': 724}