Source: synchronization/endpoint/fetch-collection-builder.js

/**
 * Copyright© 2016, Oracle and/or its affiliates. All rights reserved.
 */


/**
 * Class that exposes fluent APIs for fetching objects from an API endpoint.
 * @constructor
 * @global
 */
function FetchCollectionBuilder(endpoint, json){

  var _endpoint = endpoint;

  var _fetchFromService = false;
  // TODO: next version
  // var _listenForCacheChanges = false;
  // var _policy = null;
  var _offset = -1;
  var _limit = -1;
  var _fetchAll = false;
  var _withParams = {};
  var _withHeaders = {};
  // TODO: next version
  // var _sortBys = {};
  // var _queryClauses = [];

  // TODO: next version
  // if(json){
  //   _loadFrom(json);
  // }

  // TODO: next version
  // this.fromService = function(){
  //   if(_policy){
  //     throw new Error('Can\'t specify FromService and a SyncPolicy');
  //   }
  //   _fetchFromService = true;
  //   return _this;
  // };

  // TODO: next version
  // this.withPolicy = function(policy){
  //
  // };

  // TODO: next version
  // this.withParam = function(name, value){
  //
  //   if(!name){
  //     throw new Error('Wrong name argument');
  //   }
  //
  //   if(!value){
  //     throw new Error('Wrong value argument');
  //   }
  //
  //   if(_withParams[name]){
  //     throw new Error('Duplicate param');
  //   }
  //
  //   _withParams[name] = value;
  //
  //   return _this;
  // };

  // TODO: next version
  // this.withHeader = function(name, value){
  //
  //   if(!name){
  //     throw new Error('Wrong name argument');
  //   }
  //
  //   if(!value){
  //     throw new Error('Wrong value argument');
  //   }
  //
  //   if(_withHeaders[name]){
  //     throw new Error('Duplicate header');
  //   }
  //
  //   _withHeaders[name] = value;
  //
  //   return _this;
  // };

  // TODO: next version
  // this.sortByAscending = function(name){
  //
  // };
  //
  // this.sortByDescending = function(name){
  //
  // };

  // TODO: next version
  // this.offset = function(offset){
  //   if(_fetchAll){
  //     throw new Error('An offset can\'t be specified if an fetch all is specified');
  //   }
  //   if(offset < 0){
  //     throw new Error('Wrong offset argument');
  //   }
  //   _offset = offset;
  //   return _this;
  // };

  // TODO: next version
  // this.limit = function(limit){
  //   if(limit <= 0){
  //     throw new Error('Wrong limit argument');
  //   }
  //   _limit = limit;
  //   return _this;
  // };

  // TODO: next version
  // this.fetchAllPages = function (){
  //   if(_offset >= 0){
  //     throw new Error('FetchAllPages can\'t be specified if an offset is specified');
  //   }
  //   _fetchAll = true;
  //   return _this;
  // };

  // TODO: next version
  // this.queryFor = function(name, comparison, value){
  //
  // };

  // TODO: next version
  // this.listenForChanges = function(){
  //
  // };

  /**
   * Executes the fetch and returns the results.
   * @return {Promise.<MobileObjectCollection|NetworkResponse>}
   */
  this.execute = function(){
    return _endpoint._executeFetchObjects(_withHeaders, _withParams, _fetchAll, _offset, _limit, _fetchFromService);
  };

  // TODO: next version
  // function _loadFrom(json){
  //   _offset = json.offset;
  //   _limit = json.limit;
  //   _fetchAll = json.fetchAll;
  //   _fetchFromService = json.fetchFromService;
  //   _withParams = json.withParams;// TODO: {name:value}
  //   _withHeaders = json.withHeaders;// TODO: {name:value}
  //   _sortBys = json.sortBys;// TODO: {name:ascending}
  //   _queryClauses = json.queryClauses;// TODO: [{name:name,type:type,comparison:comparison,value:value}]
  // }
}