@cubejs-client/core 0.29.8 → 0.29.33

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/index.d.ts CHANGED
@@ -1,14 +1,13 @@
1
1
  /**
2
2
  * @title @cubejs-client/core
3
3
  * @permalink /@cubejs-client-core
4
- * @menuCategory Cube.js Frontend
4
+ * @menuCategory Frontend Integrations
5
5
  * @subcategory Reference
6
6
  * @menuOrder 2
7
7
  * @description Vanilla JavaScript Cube.js client.
8
8
  */
9
9
 
10
10
  declare module '@cubejs-client/core' {
11
-
12
11
  export type TransportOptions = {
13
12
  /**
14
13
  * [jwt auth token](security)
@@ -80,6 +79,7 @@ declare module '@cubejs-client/core' {
80
79
  pollInterval?: number;
81
80
  credentials?: 'omit' | 'same-origin' | 'include';
82
81
  parseDateMeasures?: boolean;
82
+ resType?: 'default' | 'compact';
83
83
  };
84
84
 
85
85
  export type LoadMethodOptions = {
@@ -131,6 +131,11 @@ declare module '@cubejs-client/core' {
131
131
 
132
132
  type QueryType = 'regularQuery' | 'compareDateRangeQuery' | 'blendingQuery';
133
133
 
134
+ type LeafMeasure = {
135
+ measure: string;
136
+ additive: boolean;
137
+ };
138
+
134
139
  export type TransformedQuery = {
135
140
  allFiltersWithinSelectedDimensions: boolean;
136
141
  granularityHierarchies: Record<string, string[]>;
@@ -142,6 +147,7 @@ declare module '@cubejs-client/core' {
142
147
  measures: string[];
143
148
  sortedDimensions: string[];
144
149
  sortedTimeDimensions: [[string, string]];
150
+ measureToLeafMeasures?: Record<string, LeafMeasure[]>;
145
151
  };
146
152
 
147
153
  export type PreAggregationType = 'rollup' | 'rollupJoin' | 'originalSql';
@@ -159,7 +165,7 @@ declare module '@cubejs-client/core' {
159
165
  external: boolean | null;
160
166
  dbType: string;
161
167
  extDbType: string;
162
- requestId?: string,
168
+ requestId?: string;
163
169
  usedPreAggregations?: Record<string, UsedPreAggregation>;
164
170
  transformedQuery?: TransformedQuery;
165
171
  };
@@ -790,6 +796,7 @@ declare module '@cubejs-client/core' {
790
796
  timezone?: string;
791
797
  renewQuery?: boolean;
792
798
  ungrouped?: boolean;
799
+ responseFormat?: 'compact' | 'default';
793
800
  }
794
801
 
795
802
  export class ProgressResult {
@@ -825,7 +832,7 @@ declare module '@cubejs-client/core' {
825
832
  type TCubeMemberType = 'time' | 'number' | 'string' | 'boolean';
826
833
 
827
834
  // @see BaseCubeMember
828
- // @depreacated
835
+ // @deprecated
829
836
  export type TCubeMember = {
830
837
  type: TCubeMemberType;
831
838
  name: string;
@@ -841,6 +848,7 @@ declare module '@cubejs-client/core' {
841
848
  title: string;
842
849
  shortTitle: string;
843
850
  isVisible?: boolean;
851
+ meta?: any;
844
852
  };
845
853
 
846
854
  export type TCubeMeasure = BaseCubeMember & {
@@ -992,6 +1000,12 @@ declare module '@cubejs-client/core' {
992
1000
  * @param query - [Query object](query-format)
993
1001
  */
994
1002
  load(query: Query | Query[], options?: LoadMethodOptions, callback?: LoadMethodCallback<ResultSet>): void;
1003
+ load(
1004
+ query: Query | Query[],
1005
+ options?: LoadMethodOptions,
1006
+ callback?: LoadMethodCallback<ResultSet>,
1007
+ responseFormat?: string
1008
+ ): Promise<ResultSet>;
995
1009
 
996
1010
  /**
997
1011
  * Allows you to fetch data and receive updates over time. See [Real-Time Data Fetch](real-time-data-fetch)
@@ -1060,7 +1074,7 @@ declare module '@cubejs-client/core' {
1060
1074
  * );
1061
1075
  * ```
1062
1076
  *
1063
- * @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 on startup. In case of async function `authorization` is updated for `options.transport` on each request.
1077
+ * @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.
1064
1078
  * @order 1
1065
1079
  */
1066
1080
  export default function cubejs(apiToken: string | (() => Promise<string>), options: CubeJSApiOptions): CubejsApi;
@@ -1154,5 +1168,3 @@ declare module '@cubejs-client/core' {
1154
1168
  timeElapsed: number;
1155
1169
  };
1156
1170
  }
1157
-
1158
- import '@cubejs-client/dx';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cubejs-client/core",
3
- "version": "0.29.8",
3
+ "version": "0.29.33",
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": "2bd982f9f7aff8afc071c01015306421a2cd7739"
49
+ "gitHead": "b52e4400b0786c37544b285de1e4c66dcee96e43"
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, { baseRequestId: uuidv4(), ...params });
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.updateTransportAuthorization().then(() => request());
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 (options.mutexObj && options.mutexObj[mutexKey] !== mutexValue) {
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
- load(query, options, callback) {
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
- (response) => new ResultSet(response, { parseDateMeasures: this.parseDateMeasures }),
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);