1 |
|
2 | import get from "lodash/get";
|
3 | import { delay } from "./utils/promise";
|
4 | import { ApiResponse, ApiResponseError } from "./xhr";
|
5 |
|
6 | import { name as pkgName, version as pkgVersion } from "../package.json";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | export const thisPackage = { name: pkgName, version: pkgVersion };
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | export const getIn = (path: string) => (object: any) => get(object, path);
|
30 |
|
31 | export interface IPollingOptions {
|
32 | attempts?: number;
|
33 | maxAttempts?: number;
|
34 | pollStep?: number;
|
35 | }
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | export const handlePolling = (
|
47 | xhrRequest: any,
|
48 | uri: string,
|
49 | isPollingDone: (response: any) => boolean,
|
50 | options: IPollingOptions = {},
|
51 | ) => {
|
52 |
|
53 | const { attempts = 0, maxAttempts = 50, pollStep = 5000 } = options;
|
54 |
|
55 | return xhrRequest(uri)
|
56 | .then((r: any) => r.getData())
|
57 | .then((response: any) => {
|
58 | if (attempts > maxAttempts) {
|
59 | return Promise.reject(new Error(response));
|
60 | }
|
61 | return isPollingDone(response)
|
62 | ? Promise.resolve(response)
|
63 | : delay(pollStep).then(() => {
|
64 | return handlePolling(xhrRequest, uri, isPollingDone, {
|
65 | ...options,
|
66 | attempts: attempts + 1,
|
67 | });
|
68 | });
|
69 | });
|
70 | };
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | export const handleHeadPolling = (
|
82 | xhrRequest: any,
|
83 | uri: string,
|
84 | isPollingDone: (responseHeaders: Response, response: ApiResponse) => boolean,
|
85 | options: IPollingOptions = {},
|
86 | ) => {
|
87 | const { attempts = 0, maxAttempts = 50, pollStep = 5000 } = options;
|
88 |
|
89 | return xhrRequest(uri).then((response: any) => {
|
90 | if (attempts > maxAttempts) {
|
91 | return Promise.reject(new Error("Export timeout!!!"));
|
92 | }
|
93 | const responseHeaders = response.getHeaders();
|
94 | if (isPollingDone(responseHeaders, response)) {
|
95 | if (responseHeaders.status === 200) {
|
96 | return Promise.resolve({ uri });
|
97 | }
|
98 | return Promise.reject(new ApiResponseError(response.statusText, response, response.getData()));
|
99 | } else {
|
100 | return delay(pollStep).then(() =>
|
101 | handleHeadPolling(xhrRequest, uri, isPollingDone, {
|
102 | ...options,
|
103 | attempts: attempts + 1,
|
104 | }),
|
105 | );
|
106 | }
|
107 | });
|
108 | };
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | export function queryString(query: any) {
|
118 | function getSingleParam(key: string, value: string) {
|
119 | return Array.isArray(value)
|
120 | ? value.map(item => `${encodeURIComponent(key)}=${encodeURIComponent(item)}`).join("&")
|
121 | : `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
|
122 | }
|
123 |
|
124 | return query
|
125 | ? `?${Object.keys(query)
|
126 | .map(k => getSingleParam(k, query[k]))
|
127 | .join("&")}`
|
128 | : "";
|
129 | }
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | export function getAllPagesByOffsetLimit(
|
142 | xhr: any,
|
143 | uri: string,
|
144 | itemKey: string,
|
145 | offset: number = 0,
|
146 | pagesData: any[] = [],
|
147 | ) {
|
148 | const PAGE_LIMIT = 100;
|
149 | return new Promise((resolve: any, reject: any) => {
|
150 | xhr.get(`${uri}?offset=${offset}&limit=${PAGE_LIMIT}`)
|
151 | .then((r: any) => r.getData())
|
152 | .then((dataObjects: any[]) => {
|
153 | const projects = get(dataObjects, itemKey);
|
154 | const data = pagesData.concat(projects.items);
|
155 |
|
156 | const totalCount = get(projects, "paging.totalCount", 0);
|
157 | const nextPage = offset + PAGE_LIMIT;
|
158 | if (nextPage > totalCount) {
|
159 | resolve(data);
|
160 | } else {
|
161 | resolve(getAllPagesByOffsetLimit(xhr, uri, itemKey, nextPage, data));
|
162 | }
|
163 | }, reject);
|
164 | });
|
165 | }
|