@cubejs-client/core 0.29.5 → 0.29.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +34 -0
- package/dist/cubejs-client-core.esm.js +122 -32
- package/dist/cubejs-client-core.esm.js.map +1 -1
- package/dist/cubejs-client-core.js +122 -32
- package/dist/cubejs-client-core.js.map +1 -1
- package/dist/cubejs-client-core.umd.js +157 -62
- package/dist/cubejs-client-core.umd.js.map +1 -1
- package/index.d.ts +18 -7
- package/package.json +3 -3
- package/src/index.js +118 -19
- package/src/index.test.js +454 -0
- package/src/tests/ResultSet.test.js +8 -0
package/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @title @cubejs-client/core
|
|
3
3
|
* @permalink /@cubejs-client-core
|
|
4
|
-
* @menuCategory
|
|
4
|
+
* @menuCategory Frontend Integrations
|
|
5
5
|
* @subcategory Reference
|
|
6
6
|
* @menuOrder 2
|
|
7
7
|
* @description Vanilla JavaScript Cube.js client.
|
|
@@ -27,13 +27,21 @@ declare module '@cubejs-client/core' {
|
|
|
27
27
|
};
|
|
28
28
|
|
|
29
29
|
export interface ITransportResponse<R> {
|
|
30
|
-
subscribe: <CBResult>(
|
|
30
|
+
subscribe: <CBResult>(
|
|
31
|
+
cb: (
|
|
32
|
+
result: R,
|
|
33
|
+
resubscribe: () => Promise<CBResult>
|
|
34
|
+
) => CBResult
|
|
35
|
+
) => Promise<CBResult>;
|
|
31
36
|
// Optional, supported in WebSocketTransport
|
|
32
37
|
unsubscribe?: () => Promise<void>;
|
|
33
38
|
}
|
|
34
39
|
|
|
35
40
|
export interface ITransport<R> {
|
|
36
|
-
request(
|
|
41
|
+
request(
|
|
42
|
+
method: string,
|
|
43
|
+
params: Record<string, unknown>
|
|
44
|
+
): ITransportResponse<R>;
|
|
37
45
|
}
|
|
38
46
|
|
|
39
47
|
/**
|
|
@@ -80,6 +88,7 @@ declare module '@cubejs-client/core' {
|
|
|
80
88
|
pollInterval?: number;
|
|
81
89
|
credentials?: 'omit' | 'same-origin' | 'include';
|
|
82
90
|
parseDateMeasures?: boolean;
|
|
91
|
+
resType?: 'default' | 'compact';
|
|
83
92
|
};
|
|
84
93
|
|
|
85
94
|
export type LoadMethodOptions = {
|
|
@@ -790,6 +799,7 @@ declare module '@cubejs-client/core' {
|
|
|
790
799
|
timezone?: string;
|
|
791
800
|
renewQuery?: boolean;
|
|
792
801
|
ungrouped?: boolean;
|
|
802
|
+
responseFormat?: 'compact' | 'default'
|
|
793
803
|
}
|
|
794
804
|
|
|
795
805
|
export class ProgressResult {
|
|
@@ -825,13 +835,14 @@ declare module '@cubejs-client/core' {
|
|
|
825
835
|
type TCubeMemberType = 'time' | 'number' | 'string' | 'boolean';
|
|
826
836
|
|
|
827
837
|
// @see BaseCubeMember
|
|
828
|
-
// @
|
|
838
|
+
// @deprecated
|
|
829
839
|
export type TCubeMember = {
|
|
830
840
|
type: TCubeMemberType;
|
|
831
841
|
name: string;
|
|
832
842
|
title: string;
|
|
833
843
|
shortTitle: string;
|
|
834
844
|
isVisible?: boolean;
|
|
845
|
+
meta?: any;
|
|
835
846
|
};
|
|
836
847
|
|
|
837
848
|
export type BaseCubeMember = {
|
|
@@ -840,6 +851,7 @@ declare module '@cubejs-client/core' {
|
|
|
840
851
|
title: string;
|
|
841
852
|
shortTitle: string;
|
|
842
853
|
isVisible?: boolean;
|
|
854
|
+
meta?: any;
|
|
843
855
|
};
|
|
844
856
|
|
|
845
857
|
export type TCubeMeasure = BaseCubeMember & {
|
|
@@ -991,6 +1003,7 @@ declare module '@cubejs-client/core' {
|
|
|
991
1003
|
* @param query - [Query object](query-format)
|
|
992
1004
|
*/
|
|
993
1005
|
load(query: Query | Query[], options?: LoadMethodOptions, callback?: LoadMethodCallback<ResultSet>): void;
|
|
1006
|
+
load(query: Query | Query[], options?: LoadMethodOptions, callback?: LoadMethodCallback<ResultSet>, responseFormat?: string): Promise<ResultSet>;
|
|
994
1007
|
|
|
995
1008
|
/**
|
|
996
1009
|
* Allows you to fetch data and receive updates over time. See [Real-Time Data Fetch](real-time-data-fetch)
|
|
@@ -1059,7 +1072,7 @@ declare module '@cubejs-client/core' {
|
|
|
1059
1072
|
* );
|
|
1060
1073
|
* ```
|
|
1061
1074
|
*
|
|
1062
|
-
* @param apiToken - [API token](security) is used to authorize requests and determine SQL database you're accessing. In the development mode, Cube.js Backend will print the API token to the console on
|
|
1075
|
+
* @param apiToken - [API token](security) is used to authorize requests and determine SQL database you're accessing. In the development mode, Cube.js Backend will print the API token to the console on startup. In case of async function `authorization` is updated for `options.transport` on each request.
|
|
1063
1076
|
* @order 1
|
|
1064
1077
|
*/
|
|
1065
1078
|
export default function cubejs(apiToken: string | (() => Promise<string>), options: CubeJSApiOptions): CubejsApi;
|
|
@@ -1153,5 +1166,3 @@ declare module '@cubejs-client/core' {
|
|
|
1153
1166
|
timeElapsed: number;
|
|
1154
1167
|
};
|
|
1155
1168
|
}
|
|
1156
|
-
|
|
1157
|
-
import '@cubejs-client/dx';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cubejs-client/core",
|
|
3
|
-
"version": "0.29.
|
|
3
|
+
"version": "0.29.29",
|
|
4
4
|
"engines": {},
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -13,8 +13,8 @@
|
|
|
13
13
|
"types": "index.d.ts",
|
|
14
14
|
"author": "Cube Dev, Inc.",
|
|
15
15
|
"dependencies": {
|
|
16
|
+
"@babel/plugin-transform-runtime": "^7.17.0",
|
|
16
17
|
"@babel/runtime": "^7.1.2",
|
|
17
|
-
"@cubejs-client/dx": "^0.29.5",
|
|
18
18
|
"core-js": "^3.6.5",
|
|
19
19
|
"cross-fetch": "^3.0.2",
|
|
20
20
|
"dayjs": "^1.10.4",
|
|
@@ -46,5 +46,5 @@
|
|
|
46
46
|
"eslint-plugin-node": "^5.2.1",
|
|
47
47
|
"jest": "^26.0.1"
|
|
48
48
|
},
|
|
49
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "23638ee42cceb8fc80e821486ab30825e2b9a483"
|
|
50
50
|
}
|
package/src/index.js
CHANGED
|
@@ -10,6 +10,14 @@ let mutexCounter = 0;
|
|
|
10
10
|
|
|
11
11
|
const MUTEX_ERROR = 'Mutex has been changed';
|
|
12
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Query result dataset formats enum.
|
|
15
|
+
*/
|
|
16
|
+
const ResultType = {
|
|
17
|
+
DEFAULT: 'default',
|
|
18
|
+
COMPACT: 'compact'
|
|
19
|
+
};
|
|
20
|
+
|
|
13
21
|
function mutexPromise(promise) {
|
|
14
22
|
return new Promise(async (resolve, reject) => {
|
|
15
23
|
try {
|
|
@@ -24,7 +32,7 @@ function mutexPromise(promise) {
|
|
|
24
32
|
|
|
25
33
|
class CubejsApi {
|
|
26
34
|
constructor(apiToken, options) {
|
|
27
|
-
if (typeof apiToken === 'object') {
|
|
35
|
+
if (apiToken !== null && !Array.isArray(apiToken) && typeof apiToken === 'object') {
|
|
28
36
|
options = apiToken;
|
|
29
37
|
apiToken = undefined;
|
|
30
38
|
}
|
|
@@ -53,7 +61,10 @@ class CubejsApi {
|
|
|
53
61
|
}
|
|
54
62
|
|
|
55
63
|
request(method, params) {
|
|
56
|
-
return this.transport.request(method, {
|
|
64
|
+
return this.transport.request(method, {
|
|
65
|
+
baseRequestId: uuidv4(),
|
|
66
|
+
...params
|
|
67
|
+
});
|
|
57
68
|
}
|
|
58
69
|
|
|
59
70
|
loadMethod(request, toResult, options, callback) {
|
|
@@ -70,7 +81,9 @@ class CubejsApi {
|
|
|
70
81
|
options.mutexObj[mutexKey] = mutexValue;
|
|
71
82
|
}
|
|
72
83
|
|
|
73
|
-
const requestPromise = this
|
|
84
|
+
const requestPromise = this
|
|
85
|
+
.updateTransportAuthorization()
|
|
86
|
+
.then(() => request());
|
|
74
87
|
|
|
75
88
|
let skipAuthorizationUpdate = true;
|
|
76
89
|
let unsubscribed = false;
|
|
@@ -78,7 +91,10 @@ class CubejsApi {
|
|
|
78
91
|
const checkMutex = async () => {
|
|
79
92
|
const requestInstance = await requestPromise;
|
|
80
93
|
|
|
81
|
-
if (
|
|
94
|
+
if (
|
|
95
|
+
options.mutexObj &&
|
|
96
|
+
options.mutexObj[mutexKey] !== mutexValue
|
|
97
|
+
) {
|
|
82
98
|
unsubscribed = true;
|
|
83
99
|
if (requestInstance.unsubscribe) {
|
|
84
100
|
await requestInstance.unsubscribe();
|
|
@@ -213,18 +229,113 @@ class CubejsApi {
|
|
|
213
229
|
}
|
|
214
230
|
}
|
|
215
231
|
|
|
216
|
-
|
|
232
|
+
/**
|
|
233
|
+
* Add system properties to a query object.
|
|
234
|
+
* @param {Query} query
|
|
235
|
+
* @param {string} responseFormat
|
|
236
|
+
* @returns {void}
|
|
237
|
+
* @private
|
|
238
|
+
*/
|
|
239
|
+
patchQueryInternal(query, responseFormat) {
|
|
240
|
+
if (
|
|
241
|
+
responseFormat === ResultType.COMPACT &&
|
|
242
|
+
query.responseFormat !== ResultType.COMPACT
|
|
243
|
+
) {
|
|
244
|
+
query.responseFormat = ResultType.COMPACT;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Process result fetched from the gateway#load method according
|
|
250
|
+
* to the network protocol.
|
|
251
|
+
* @param {*} response
|
|
252
|
+
* @returns ResultSet
|
|
253
|
+
* @private
|
|
254
|
+
*/
|
|
255
|
+
loadResponseInternal(response) {
|
|
256
|
+
if (
|
|
257
|
+
response.results.length &&
|
|
258
|
+
response.results[0].query.responseFormat &&
|
|
259
|
+
response.results[0].query.responseFormat === ResultType.COMPACT
|
|
260
|
+
) {
|
|
261
|
+
response.results.forEach((result, j) => {
|
|
262
|
+
const data = [];
|
|
263
|
+
result.data.dataset.forEach((r) => {
|
|
264
|
+
const row = {};
|
|
265
|
+
result.data.members.forEach((m, i) => {
|
|
266
|
+
row[m] = r[i];
|
|
267
|
+
});
|
|
268
|
+
data.push(row);
|
|
269
|
+
});
|
|
270
|
+
response.results[j].data = data;
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
return new ResultSet(response, {
|
|
274
|
+
parseDateMeasures: this.parseDateMeasures
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Fetch data for the passed `query`. Operates with the
|
|
280
|
+
* `ApiGateway#load` method to fetch the data.
|
|
281
|
+
* @param {Query | Query[]} query
|
|
282
|
+
* @param {LoadMethodOptions | undefined} options
|
|
283
|
+
* @param {LoadMethodCallback<ResultSet> | undefined} callback
|
|
284
|
+
* @param {string} responseFormat
|
|
285
|
+
* @returns {undefined | Promise<ResultSet>}
|
|
286
|
+
*/
|
|
287
|
+
load(query, options, callback, responseFormat = ResultType.DEFAULT) {
|
|
288
|
+
if (responseFormat === ResultType.COMPACT) {
|
|
289
|
+
if (Array.isArray(query)) {
|
|
290
|
+
query.forEach((q) => {
|
|
291
|
+
this.patchQueryInternal(q, ResultType.COMPACT);
|
|
292
|
+
});
|
|
293
|
+
} else {
|
|
294
|
+
this.patchQueryInternal(query, ResultType.COMPACT);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
217
297
|
return this.loadMethod(
|
|
218
298
|
() => this.request('load', {
|
|
219
299
|
query,
|
|
220
|
-
queryType: 'multi'
|
|
300
|
+
queryType: 'multi',
|
|
221
301
|
}),
|
|
222
|
-
(
|
|
302
|
+
this.loadResponseInternal.bind(this),
|
|
223
303
|
options,
|
|
224
304
|
callback
|
|
225
305
|
);
|
|
226
306
|
}
|
|
227
307
|
|
|
308
|
+
/**
|
|
309
|
+
* Allows you to fetch data and receive updates over time. Operates
|
|
310
|
+
* with the `ApiGateway#load` method to fetch the data.
|
|
311
|
+
* @link real-time-data-fetch
|
|
312
|
+
* @param {Query | Query[]} query
|
|
313
|
+
* @param {LoadMethodOptions | null} options
|
|
314
|
+
* @param {LoadMethodCallback<ResultSet> | undefined} callback
|
|
315
|
+
* @param {string} responseFormat
|
|
316
|
+
* @returns {void}
|
|
317
|
+
*/
|
|
318
|
+
subscribe(query, options, callback, responseFormat = ResultType.DEFAULT) {
|
|
319
|
+
if (responseFormat === ResultType.COMPACT) {
|
|
320
|
+
if (Array.isArray(query)) {
|
|
321
|
+
query.forEach((q) => {
|
|
322
|
+
this.patchQueryInternal(q, ResultType.COMPACT);
|
|
323
|
+
});
|
|
324
|
+
} else {
|
|
325
|
+
this.patchQueryInternal(query, ResultType.COMPACT);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
return this.loadMethod(
|
|
329
|
+
() => this.request('subscribe', {
|
|
330
|
+
query,
|
|
331
|
+
queryType: 'multi',
|
|
332
|
+
}),
|
|
333
|
+
this.loadResponseInternal.bind(this),
|
|
334
|
+
{ ...options, subscribe: true },
|
|
335
|
+
callback
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
|
|
228
339
|
sql(query, options, callback) {
|
|
229
340
|
return this.loadMethod(
|
|
230
341
|
() => this.request('sql', { query }),
|
|
@@ -251,18 +362,6 @@ class CubejsApi {
|
|
|
251
362
|
callback
|
|
252
363
|
);
|
|
253
364
|
}
|
|
254
|
-
|
|
255
|
-
subscribe(query, options, callback) {
|
|
256
|
-
return this.loadMethod(
|
|
257
|
-
() => this.request('subscribe', {
|
|
258
|
-
query,
|
|
259
|
-
queryType: 'multi'
|
|
260
|
-
}),
|
|
261
|
-
(body) => new ResultSet(body, { parseDateMeasures: this.parseDateMeasures }),
|
|
262
|
-
{ ...options, subscribe: true },
|
|
263
|
-
callback
|
|
264
|
-
);
|
|
265
|
-
}
|
|
266
365
|
}
|
|
267
366
|
|
|
268
367
|
export default (apiToken, options) => new CubejsApi(apiToken, options);
|