UNPKG

loop-modules

Version:

Shared modules for the Loop product suite.

211 lines (210 loc) 6.62 kB
import { Observable } from 'rxjs/Observable'; var APIDispatcher = (function () { function APIDispatcher(http, store) { this.http = http; this.store = store; this.loaded = false; this.fetchingNext = false; } /** * Wraps the http layer for GET requests * Injects Core API standards for filtering, sorting and pagination * * @param {string} url The relative API url * @param {number} [page=0] The page of the dataset to request * @param {number} [size=10] The maximum number of results to return * @param {APIRequestOptions} [options] The optional request options * @returns Observable of http get request */ APIDispatcher.prototype.get = function (url, page, size, options) { if (page === void 0) { page = 0; } if (size === void 0) { size = 10; } if (options === void 0) { options = {}; } return this.http.get(url + '?' + this.getPageFragment(page) + this.getSizeFragment(size) + this.getOptionalFragments(options)); }; /** * Wraps the http layer for POST requests * Injects Core API standards used for fetching items by identities * Allows for filtering, sorting and pagination * * @param {string} url The relative API url * @param {*} requestBody The request body to send to the server * @param {number} [page=0] The page of the dataset to request * @param {number} [size=10] The maximum number of results to return * @param {APIRequestOptions} [options] The optional request options * @returns */ APIDispatcher.prototype.post = function (url, requestBody, page, size, options) { if (page === void 0) { page = 0; } if (size === void 0) { size = 10; } if (options === void 0) { options = {}; } return this.http.post(url + '?' + this.getPageFragment(page) + this.getSizeFragment(size) + this.getOptionalFragments(options), requestBody); }; APIDispatcher.prototype.handleError = function (error) { var errMsg = (error.message) ? error.message : error.status ? error.status + " - " + error.statusText : 'Server error'; console.error(errMsg); return Observable.throw(errMsg); }; /** * This clears the previous requests * * @memberOf APIDispatcher */ APIDispatcher.prototype.clearRequest = function () { this.lastRequest = undefined; }; Object.defineProperty(APIDispatcher.prototype, "hasNext", { // TODO - This will need to be abstract once services are up-to-date // getNextPage() {}; /** * Determines if the paginated request has another page, based on the total number of pages vs. the currently requested content * * @readonly * @type {boolean} * @memberOf APIDispatcher */ get: function () { if (this.lastRequest) { return (this.lastRequest.totalPages - 1) > this.lastRequest.currentPage; } return false; }, enumerable: true, configurable: true }); /** * The get parameter fragment for page numbers * * @protected * @param {number} page * @returns {string} */ APIDispatcher.prototype.getPageFragment = function (page) { return 'page=' + page; }; /** * The get parameter fragment for collection size * * @protected * @param {number} size * @returns {string} */ APIDispatcher.prototype.getSizeFragment = function (size) { return '&size=' + size; }; /** * The get parameter fragments * * @protected * @param {APIRequestOptions} options * @returns {string} */ APIDispatcher.prototype.getOptionalFragments = function (options) { return this.getSearchFragment(options.search) + this.getSortByFragment(options.sortBy) + this.getUnreadFragment(options.unread) + this.getSortDirFragment(options.sortDir) + this.getArchivedFragment(options.archived) + this.getIsReviewedFragment(options.reviewed) + this.getIsSubmittedFragment(options.submitted); }; /** * The get parameter fragment for search term * * @protected * @param {string} search * @returns {string} */ APIDispatcher.prototype.getSearchFragment = function (search) { if (search) { return '&search=' + encodeURIComponent(search); } return ''; }; /** * The get parameter fragment for sort by * * @protected * @param {string} sortBy * @returns {string} */ APIDispatcher.prototype.getSortByFragment = function (sortBy) { if (sortBy) { return '&sortBy=' + sortBy; } return ''; }; /** * The get parameter fragment for sort direction * * @protected * @param {string} sortDir * @returns {string} */ APIDispatcher.prototype.getSortDirFragment = function (sortDir) { if (sortDir) { return '&sortDir=' + sortDir; } return ''; }; /** * The get parameter fragment for unread * * @protected * @param {string} unread * @returns {string} */ APIDispatcher.prototype.getUnreadFragment = function (unread) { if (unread) { return '&unread=' + unread; } return ''; }; /** * The get parameter fragment for unread * * @protected * @param {string} unread * @returns {string} */ APIDispatcher.prototype.getArchivedFragment = function (archived) { if (archived) { return '&isArchived=' + archived; } return ''; }; /** * The get parameter fragment for unread * * @protected * @param {string} unread * @returns {string} */ APIDispatcher.prototype.getIsReviewedFragment = function (reviewed) { if (reviewed !== undefined) { return '&reviewed=' + reviewed; } return ''; }; /** * The get parameter fragment for unread * * @protected * @param {string} unread * @returns {string} */ APIDispatcher.prototype.getIsSubmittedFragment = function (submitted) { if (submitted !== undefined) { return '&submitted=' + submitted; } return ''; }; return APIDispatcher; }()); export { APIDispatcher };