UNPKG

5.77 kBJavaScriptView Raw
1"use strict";
2var __assign = (this && this.__assign) || function () {
3 __assign = Object.assign || function(t) {
4 for (var s, i = 1, n = arguments.length; i < n; i++) {
5 s = arguments[i];
6 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7 t[p] = s[p];
8 }
9 return t;
10 };
11 return __assign.apply(this, arguments);
12};
13var __importDefault = (this && this.__importDefault) || function (mod) {
14 return (mod && mod.__esModule) ? mod : { "default": mod };
15};
16Object.defineProperty(exports, "__esModule", { value: true });
17// (C) 2007-2019 GoodData Corporation
18var get_1 = __importDefault(require("lodash/get"));
19var promise_1 = require("./utils/promise");
20var xhr_1 = require("./xhr");
21var package_json_1 = require("../package.json");
22/**
23 * Utility methods. Mostly private
24 *
25 * @module util
26 * @class util
27 *
28 */
29/**
30 * Gooddata-js package signature
31 * @private
32 */
33exports.thisPackage = { name: package_json_1.name, version: package_json_1.version };
34/**
35 * Create getter function for accessing nested objects
36 *
37 * @param {String} path Target path to nested object
38 * @method getIn
39 * @private
40 */
41exports.getIn = function (path) { return function (object) { return get_1.default(object, path); }; };
42/**
43 * Helper for polling
44 *
45 * @param xhrRequest xhr module
46 * @param {String} uri
47 * @param {Function} isPollingDone
48 * @param {Object} options for polling (maxAttempts, pollStep)
49 * @private
50 */
51exports.handlePolling = function (xhrRequest, uri, isPollingDone, options) {
52 if (options === void 0) { options = {}; }
53 // TODO
54 var _a = options.attempts, attempts = _a === void 0 ? 0 : _a, _b = options.maxAttempts, maxAttempts = _b === void 0 ? 50 : _b, _c = options.pollStep, pollStep = _c === void 0 ? 5000 : _c;
55 return xhrRequest(uri)
56 .then(function (r) { return r.getData(); })
57 .then(function (response) {
58 if (attempts > maxAttempts) {
59 return Promise.reject(new Error(response));
60 }
61 return isPollingDone(response)
62 ? Promise.resolve(response)
63 : promise_1.delay(pollStep).then(function () {
64 return exports.handlePolling(xhrRequest, uri, isPollingDone, __assign({}, options, { attempts: attempts + 1 }));
65 });
66 });
67};
68/**
69 * Helper for polling with header status
70 *
71 * @param xhrRequest xhr module
72 * @param {String} uri
73 * @param {Function} isPollingDone
74 * @param {Object} options for polling (maxAttempts, pollStep)
75 * @private
76 */
77exports.handleHeadPolling = function (xhrRequest, uri, isPollingDone, options) {
78 if (options === void 0) { options = {}; }
79 var _a = options.attempts, attempts = _a === void 0 ? 0 : _a, _b = options.maxAttempts, maxAttempts = _b === void 0 ? 50 : _b, _c = options.pollStep, pollStep = _c === void 0 ? 5000 : _c;
80 return xhrRequest(uri).then(function (response) {
81 if (attempts > maxAttempts) {
82 return Promise.reject(new Error("Export timeout!!!"));
83 }
84 var responseHeaders = response.getHeaders();
85 if (isPollingDone(responseHeaders, response)) {
86 if (responseHeaders.status === 200) {
87 return Promise.resolve({ uri: uri });
88 }
89 return Promise.reject(new xhr_1.ApiResponseError(response.statusText, response, response.getData()));
90 }
91 else {
92 return promise_1.delay(pollStep).then(function () {
93 return exports.handleHeadPolling(xhrRequest, uri, isPollingDone, __assign({}, options, { attempts: attempts + 1 }));
94 });
95 }
96 });
97};
98/**
99 * Builds query string from plain object
100 * (Refactored from admin/routes.js)
101 *
102 * @param {Object} query parameters possibly including arrays inside
103 * @returns {string} querystring
104 */
105function queryString(query) {
106 function getSingleParam(key, value) {
107 return Array.isArray(value)
108 ? value.map(function (item) { return encodeURIComponent(key) + "=" + encodeURIComponent(item); }).join("&")
109 : encodeURIComponent(key) + "=" + encodeURIComponent(value);
110 }
111 return query
112 ? "?" + Object.keys(query)
113 .map(function (k) { return getSingleParam(k, query[k]); })
114 .join("&")
115 : "";
116}
117exports.queryString = queryString;
118/**
119 * Get all results from paged api by traversing all resulting pages
120 * This is usable for apis which support offset and limit (i.e. not those with next paging links)
121 *
122 * @param xhrGet xhr module
123 * @param {string} uri uri to be fetched, will append offset and limit for next pages
124 * @param {string} itemKey key under which to look for results (differs for different apis)
125 * @param {number} optional offset starting offset, default 0
126 * @param pagesData optional data to be pre-filled
127 */
128function getAllPagesByOffsetLimit(xhr, uri, itemKey, offset, pagesData) {
129 if (offset === void 0) { offset = 0; }
130 if (pagesData === void 0) { pagesData = []; }
131 var PAGE_LIMIT = 100;
132 return new Promise(function (resolve, reject) {
133 xhr.get(uri + "?offset=" + offset + "&limit=" + PAGE_LIMIT)
134 .then(function (r) { return r.getData(); })
135 .then(function (dataObjects) {
136 var projects = get_1.default(dataObjects, itemKey);
137 var data = pagesData.concat(projects.items);
138 var totalCount = get_1.default(projects, "paging.totalCount", 0);
139 var nextPage = offset + PAGE_LIMIT;
140 if (nextPage > totalCount) {
141 resolve(data);
142 }
143 else {
144 resolve(getAllPagesByOffsetLimit(xhr, uri, itemKey, nextPage, data));
145 }
146 }, reject);
147 });
148}
149exports.getAllPagesByOffsetLimit = getAllPagesByOffsetLimit;