@cubejs-client/core 1.3.14 → 1.3.16

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.
Files changed (100) hide show
  1. package/dist/{cubejs-client-core.js → cubejs-client-core.cjs.js} +1016 -411
  2. package/dist/cubejs-client-core.cjs.js.map +1 -0
  3. package/dist/cubejs-client-core.umd.js +2901 -12088
  4. package/dist/cubejs-client-core.umd.js.map +1 -1
  5. package/dist/src/HttpTransport.d.ts +54 -0
  6. package/dist/src/HttpTransport.d.ts.map +1 -0
  7. package/dist/src/HttpTransport.js +55 -0
  8. package/dist/src/Meta.d.ts +62 -0
  9. package/dist/src/Meta.d.ts.map +1 -0
  10. package/dist/src/Meta.js +150 -0
  11. package/dist/src/ProgressResult.d.ts +8 -0
  12. package/dist/src/ProgressResult.d.ts.map +1 -0
  13. package/dist/src/ProgressResult.js +11 -0
  14. package/dist/src/RequestError.d.ts +6 -0
  15. package/dist/src/RequestError.d.ts.map +1 -0
  16. package/dist/src/RequestError.js +7 -0
  17. package/dist/src/ResultSet.d.ts +430 -0
  18. package/dist/src/ResultSet.d.ts.map +1 -0
  19. package/dist/src/ResultSet.js +952 -0
  20. package/dist/src/SqlQuery.d.ts +17 -0
  21. package/dist/src/SqlQuery.d.ts.map +1 -0
  22. package/dist/src/SqlQuery.js +11 -0
  23. package/dist/src/index.d.ts +194 -0
  24. package/dist/src/index.d.ts.map +1 -0
  25. package/dist/src/index.js +411 -0
  26. package/dist/src/index.umd.d.ts +3 -0
  27. package/dist/src/index.umd.d.ts.map +1 -0
  28. package/dist/src/index.umd.js +6 -0
  29. package/dist/src/time.d.ts +70 -0
  30. package/dist/src/time.d.ts.map +1 -0
  31. package/dist/src/time.js +249 -0
  32. package/dist/src/types.d.ts +424 -0
  33. package/dist/src/types.d.ts.map +1 -0
  34. package/dist/src/types.js +1 -0
  35. package/dist/src/utils.d.ts +19 -0
  36. package/dist/src/utils.d.ts.map +1 -0
  37. package/dist/src/utils.js +294 -0
  38. package/dist/test/CubeApi.test.d.ts +7 -0
  39. package/dist/test/CubeApi.test.d.ts.map +1 -0
  40. package/dist/test/CubeApi.test.js +279 -0
  41. package/dist/test/HttpTransport.test.d.ts +2 -0
  42. package/dist/test/HttpTransport.test.d.ts.map +1 -0
  43. package/dist/test/HttpTransport.test.js +244 -0
  44. package/dist/test/ResultSet.test.d.ts +7 -0
  45. package/dist/test/ResultSet.test.d.ts.map +1 -0
  46. package/dist/test/ResultSet.test.js +1725 -0
  47. package/dist/test/compare-date-range.test.d.ts +2 -0
  48. package/dist/test/compare-date-range.test.d.ts.map +1 -0
  49. package/dist/test/compare-date-range.test.js +742 -0
  50. package/dist/test/data-blending.test.d.ts +2 -0
  51. package/dist/test/data-blending.test.d.ts.map +1 -0
  52. package/dist/test/data-blending.test.js +423 -0
  53. package/dist/test/default-heuristics.test.d.ts +2 -0
  54. package/dist/test/default-heuristics.test.d.ts.map +1 -0
  55. package/dist/test/default-heuristics.test.js +108 -0
  56. package/dist/test/drill-down.test.d.ts +2 -0
  57. package/dist/test/drill-down.test.d.ts.map +1 -0
  58. package/dist/test/drill-down.test.js +373 -0
  59. package/dist/test/fixtures/datablending/load-responses.json +261 -0
  60. package/dist/test/granularity.test.d.ts +2 -0
  61. package/dist/test/granularity.test.d.ts.map +1 -0
  62. package/dist/test/granularity.test.js +218 -0
  63. package/dist/test/helpers.d.ts +283 -0
  64. package/dist/test/helpers.d.ts.map +1 -0
  65. package/dist/test/helpers.js +974 -0
  66. package/dist/test/index.test.d.ts +7 -0
  67. package/dist/test/index.test.d.ts.map +1 -0
  68. package/dist/test/index.test.js +370 -0
  69. package/dist/test/table.test.d.ts +2 -0
  70. package/dist/test/table.test.d.ts.map +1 -0
  71. package/dist/test/table.test.js +757 -0
  72. package/dist/test/utils.test.d.ts +2 -0
  73. package/dist/test/utils.test.d.ts.map +1 -0
  74. package/dist/test/utils.test.js +32 -0
  75. package/package.json +26 -21
  76. package/dist/cubejs-client-core.esm.js +0 -1639
  77. package/dist/cubejs-client-core.esm.js.map +0 -1
  78. package/dist/cubejs-client-core.js.map +0 -1
  79. package/index.d.ts +0 -1338
  80. package/src/HttpTransport.js +0 -60
  81. package/src/HttpTransport.test.js +0 -117
  82. package/src/Meta.js +0 -142
  83. package/src/ProgressResult.js +0 -13
  84. package/src/RequestError.js +0 -7
  85. package/src/ResultSet.js +0 -746
  86. package/src/SqlQuery.js +0 -13
  87. package/src/index.js +0 -398
  88. package/src/index.test.js +0 -454
  89. package/src/index.umd.js +0 -8
  90. package/src/tests/ResultSet.test.js +0 -1655
  91. package/src/tests/compare-date-range.test.js +0 -753
  92. package/src/tests/data-blending.test.js +0 -432
  93. package/src/tests/default-heuristics.test.js +0 -118
  94. package/src/tests/drill-down.test.js +0 -402
  95. package/src/tests/fixtures/datablending/load-responses.json +0 -261
  96. package/src/tests/granularity.test.js +0 -225
  97. package/src/tests/table.test.js +0 -791
  98. package/src/tests/utils.test.js +0 -35
  99. package/src/time.js +0 -296
  100. package/src/utils.js +0 -368
@@ -0,0 +1,17 @@
1
+ export type SqlQueryTuple = [string, any[], any];
2
+ export type SqlData = {
3
+ aliasNameToMember: Record<string, string>;
4
+ cacheKeyQueries: SqlQueryTuple[];
5
+ dataSource: boolean;
6
+ external: boolean;
7
+ sql: SqlQueryTuple;
8
+ preAggregations: any[];
9
+ rollupMatchResults: any[];
10
+ };
11
+ export default class SqlQuery {
12
+ private readonly sqlQuery;
13
+ constructor(sqlQuery: SqlData);
14
+ rawQuery(): SqlData;
15
+ sql(): string;
16
+ }
17
+ //# sourceMappingURL=SqlQuery.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SqlQuery.d.ts","sourceRoot":"","sources":["../../src/SqlQuery.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,aAAa,GAAG,CAAC,MAAM,EAAE,GAAG,EAAE,EAAE,GAAG,CAAC,CAAC;AAEjD,MAAM,MAAM,OAAO,GAAG;IACpB,iBAAiB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC1C,eAAe,EAAE,aAAa,EAAE,CAAC;IACjC,UAAU,EAAE,OAAO,CAAC;IACpB,QAAQ,EAAE,OAAO,CAAC;IAClB,GAAG,EAAE,aAAa,CAAC;IACnB,eAAe,EAAE,GAAG,EAAE,CAAC;IACvB,kBAAkB,EAAE,GAAG,EAAE,CAAC;CAC3B,CAAC;AAEF,MAAM,CAAC,OAAO,OAAO,QAAQ;IAC3B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAU;gBAEhB,QAAQ,EAAE,OAAO;IAI7B,QAAQ,IAAI,OAAO;IAInB,GAAG,IAAI,MAAM;CAGrB"}
@@ -0,0 +1,11 @@
1
+ export default class SqlQuery {
2
+ constructor(sqlQuery) {
3
+ this.sqlQuery = sqlQuery;
4
+ }
5
+ rawQuery() {
6
+ return this.sqlQuery;
7
+ }
8
+ sql() {
9
+ return this.rawQuery().sql[0];
10
+ }
11
+ }
@@ -0,0 +1,194 @@
1
+ import ResultSet from './ResultSet';
2
+ import SqlQuery from './SqlQuery';
3
+ import Meta from './Meta';
4
+ import ProgressResult from './ProgressResult';
5
+ import { ITransport, TransportOptions } from './HttpTransport';
6
+ import { ExtractTimeMembers, LoadResponse, PivotQuery, Query, QueryOrder, QueryType, TransformedQuery } from './types';
7
+ export type LoadMethodCallback<T> = (error: Error | null, resultSet: T) => void;
8
+ export type LoadMethodOptions = {
9
+ /**
10
+ * Key to store the current request's MUTEX inside the `mutexObj`. MUTEX object is used to reject orphaned queries results when new queries are sent. For example: if two queries are sent with the same `mutexKey` only the last one will return results.
11
+ */
12
+ mutexKey?: string;
13
+ /**
14
+ * Object to store MUTEX
15
+ */
16
+ mutexObj?: {
17
+ [key: string]: any;
18
+ };
19
+ /**
20
+ * Pass `true` to use continuous fetch behavior.
21
+ */
22
+ subscribe?: boolean;
23
+ /**
24
+ * A Cube API instance. If not provided will be taken from `CubeProvider`
25
+ */
26
+ cubeApi?: CubeApi;
27
+ /**
28
+ * If enabled, all members of the 'number' type will be automatically converted to numerical values on the client side
29
+ */
30
+ castNumerics?: boolean;
31
+ /**
32
+ * Function that receives `ProgressResult` on each `Continue wait` message.
33
+ */
34
+ progressCallback?(result: ProgressResult): void;
35
+ /**
36
+ * AbortSignal to cancel requests
37
+ */
38
+ signal?: AbortSignal;
39
+ };
40
+ export type DeeplyReadonly<T> = {
41
+ readonly [K in keyof T]: DeeplyReadonly<T[K]>;
42
+ };
43
+ export type ExtractMembers<T extends DeeplyReadonly<Query>> = (T extends {
44
+ dimensions: readonly (infer Names)[];
45
+ } ? Names : never) | (T extends {
46
+ measures: readonly (infer Names)[];
47
+ } ? Names : never) | (T extends {
48
+ timeDimensions: (infer U);
49
+ } ? ExtractTimeMembers<U> : never);
50
+ export type SingleQueryRecordType<T extends DeeplyReadonly<Query>> = ExtractMembers<T> extends never ? any : {
51
+ [K in string & ExtractMembers<T>]: string | number | boolean | null;
52
+ };
53
+ export type QueryArrayRecordType<T extends DeeplyReadonly<Query[]>> = T extends readonly [infer First, ...infer Rest] ? SingleQueryRecordType<DeeplyReadonly<First>> | QueryArrayRecordType<Rest & DeeplyReadonly<Query[]>> : never;
54
+ export type QueryRecordType<T extends DeeplyReadonly<Query | Query[]>> = T extends DeeplyReadonly<Query[]> ? QueryArrayRecordType<T> : T extends DeeplyReadonly<Query> ? SingleQueryRecordType<T> : never;
55
+ export interface UnsubscribeObj {
56
+ /**
57
+ * Allows to stop requests in-flight in long polling or web socket subscribe loops.
58
+ * It doesn't cancel any submitted requests to the underlying databases.
59
+ */
60
+ unsubscribe(): Promise<void>;
61
+ }
62
+ /**
63
+ * @deprecated use DryRunResponse
64
+ */
65
+ export type TDryRunResponse = {
66
+ queryType: QueryType;
67
+ normalizedQueries: Query[];
68
+ pivotQuery: PivotQuery;
69
+ queryOrder: Array<{
70
+ [k: string]: QueryOrder;
71
+ }>;
72
+ transformedQueries: TransformedQuery[];
73
+ };
74
+ export type DryRunResponse = {
75
+ queryType: QueryType;
76
+ normalizedQueries: Query[];
77
+ pivotQuery: PivotQuery;
78
+ queryOrder: Array<{
79
+ [k: string]: QueryOrder;
80
+ }>;
81
+ transformedQueries: TransformedQuery[];
82
+ };
83
+ export type ResponseFormat = 'compact' | 'default' | undefined;
84
+ export type CubeApiOptions = {
85
+ /**
86
+ * URL of your Cube.js Backend. By default, in the development environment it is `http://localhost:4000/cubejs-api/v1`
87
+ */
88
+ apiUrl: string;
89
+ /**
90
+ * Transport implementation to use. [HttpTransport](#http-transport) will be used by default.
91
+ */
92
+ transport?: ITransport<any>;
93
+ method?: TransportOptions['method'];
94
+ headers?: TransportOptions['headers'];
95
+ pollInterval?: number;
96
+ credentials?: TransportOptions['credentials'];
97
+ parseDateMeasures?: boolean;
98
+ resType?: 'default' | 'compact';
99
+ castNumerics?: boolean;
100
+ /**
101
+ * How many network errors would be retried before returning to users. Default to 0.
102
+ */
103
+ networkErrorRetries?: number;
104
+ /**
105
+ * AbortSignal to cancel requests
106
+ */
107
+ signal?: AbortSignal;
108
+ /**
109
+ * Fetch timeout in milliseconds. Would be passed as AbortSignal.timeout()
110
+ */
111
+ fetchTimeout?: number;
112
+ };
113
+ /**
114
+ * Main class for accessing Cube API
115
+ */
116
+ declare class CubeApi {
117
+ private readonly apiToken;
118
+ private readonly apiUrl;
119
+ private readonly method;
120
+ private readonly headers;
121
+ private readonly credentials;
122
+ protected readonly transport: ITransport<any>;
123
+ private readonly pollInterval;
124
+ private readonly parseDateMeasures;
125
+ private readonly castNumerics;
126
+ private readonly networkErrorRetries;
127
+ private updateAuthorizationPromise;
128
+ constructor(apiToken: string | (() => Promise<string>) | undefined, options: CubeApiOptions);
129
+ constructor(options: CubeApiOptions);
130
+ protected request(method: string, params?: any): import("./HttpTransport").ITransportResponse<any>;
131
+ private loadMethod;
132
+ private updateTransportAuthorization;
133
+ /**
134
+ * Add system properties to a query object.
135
+ */
136
+ private patchQueryInternal;
137
+ /**
138
+ * Process result fetched from the gateway#load method according
139
+ * to the network protocol.
140
+ */
141
+ protected loadResponseInternal(response: LoadResponse<any>, options?: LoadMethodOptions | null): ResultSet<any>;
142
+ load<QueryType extends DeeplyReadonly<Query | Query[]>>(query: QueryType, options?: LoadMethodOptions): Promise<ResultSet<QueryRecordType<QueryType>>>;
143
+ load<QueryType extends DeeplyReadonly<Query | Query[]>>(query: QueryType, options?: LoadMethodOptions, callback?: LoadMethodCallback<ResultSet<QueryRecordType<QueryType>>>): UnsubscribeObj;
144
+ load<QueryType extends DeeplyReadonly<Query | Query[]>>(query: QueryType, options?: LoadMethodOptions, callback?: LoadMethodCallback<ResultSet<any>>, responseFormat?: string): Promise<ResultSet<QueryRecordType<QueryType>>>;
145
+ private prepareQueryOptions;
146
+ /**
147
+ * Allows you to fetch data and receive updates over time. See [Real-Time Data Fetch](/product/apis-integrations/rest-api/real-time-data-fetch)
148
+ *
149
+ * ```js
150
+ * // Subscribe to a query's updates
151
+ * const subscription = await cubeApi.subscribe(
152
+ * {
153
+ * measures: ['Logs.count'],
154
+ * timeDimensions: [
155
+ * {
156
+ * dimension: 'Logs.time',
157
+ * granularity: 'hour',
158
+ * dateRange: 'last 1440 minutes',
159
+ * },
160
+ * ],
161
+ * },
162
+ * options,
163
+ * (error, resultSet) => {
164
+ * if (!error) {
165
+ * // handle the update
166
+ * }
167
+ * }
168
+ * );
169
+ *
170
+ * // Unsubscribe from a query's updates
171
+ * subscription.unsubscribe();
172
+ * ```
173
+ */
174
+ subscribe<QueryType extends DeeplyReadonly<Query | Query[]>>(query: QueryType, options: LoadMethodOptions | null, callback: LoadMethodCallback<ResultSet<QueryRecordType<QueryType>>>, responseFormat?: ResponseFormat): UnsubscribeObj;
175
+ sql(query: DeeplyReadonly<Query | Query[]>, options?: LoadMethodOptions): Promise<SqlQuery>;
176
+ sql(query: DeeplyReadonly<Query | Query[]>, options?: LoadMethodOptions, callback?: LoadMethodCallback<SqlQuery>): UnsubscribeObj;
177
+ meta(options?: LoadMethodOptions): Promise<Meta>;
178
+ meta(options?: LoadMethodOptions, callback?: LoadMethodCallback<Meta>): UnsubscribeObj;
179
+ dryRun(query: DeeplyReadonly<Query | Query[]>, options?: LoadMethodOptions): Promise<DryRunResponse>;
180
+ dryRun(query: DeeplyReadonly<Query | Query[]>, options: LoadMethodOptions, callback?: LoadMethodCallback<DryRunResponse>): UnsubscribeObj;
181
+ }
182
+ declare const _default: (apiToken: string | (() => Promise<string>), options: CubeApiOptions) => CubeApi;
183
+ export default _default;
184
+ export { CubeApi };
185
+ export { default as Meta } from './Meta';
186
+ export { default as SqlQuery } from './SqlQuery';
187
+ export { default as RequestError } from './RequestError';
188
+ export { default as ProgressResult } from './ProgressResult';
189
+ export { default as ResultSet } from './ResultSet';
190
+ export * from './HttpTransport';
191
+ export * from './utils';
192
+ export * from './time';
193
+ export * from './types';
194
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,SAAS,MAAM,aAAa,CAAC;AACpC,OAAO,QAAQ,MAAM,YAAY,CAAC;AAClC,OAAO,IAAI,MAAM,QAAQ,CAAC;AAC1B,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAC9C,OAAsB,EAAiB,UAAU,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAE7F,OAAO,EACL,kBAAkB,EAClB,YAAY,EAEZ,UAAU,EAEV,KAAK,EACL,UAAU,EACV,SAAS,EACT,gBAAgB,EACjB,MAAM,SAAS,CAAC;AAEjB,MAAM,MAAM,kBAAkB,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,EAAE,SAAS,EAAE,CAAC,KAAK,IAAI,CAAC;AAEhF,MAAM,MAAM,iBAAiB,GAAG;IAC9B;;OAEG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;OAEG;IACH,QAAQ,CAAC,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAA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AAC;AACjD,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,MAAM,gBAAgB,CAAC;AACzD,OAAO,EAAE,OAAO,IAAI,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAC7D,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,aAAa,CAAC;AACnD,cAAc,iBAAiB,CAAC;AAChC,cAAc,SAAS,CAAC;AACxB,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC"}
@@ -0,0 +1,411 @@
1
+ import { v4 as uuidv4 } from 'uuid';
2
+ import ResultSet from './ResultSet';
3
+ import SqlQuery from './SqlQuery';
4
+ import Meta from './Meta';
5
+ import ProgressResult from './ProgressResult';
6
+ import HttpTransport from './HttpTransport';
7
+ import RequestError from './RequestError';
8
+ let mutexCounter = 0;
9
+ const MUTEX_ERROR = 'Mutex has been changed';
10
+ function mutexPromise(promise) {
11
+ return promise
12
+ .then((result) => result)
13
+ .catch((error) => {
14
+ if (error !== MUTEX_ERROR) {
15
+ throw error;
16
+ }
17
+ });
18
+ }
19
+ /**
20
+ * Main class for accessing Cube API
21
+ */
22
+ class CubeApi {
23
+ /**
24
+ * Creates an instance of the `CubeApi`. The API entry point.
25
+ *
26
+ * ```js
27
+ * import cube from '@cubejs-client/core';
28
+ * const cubeApi = cube(
29
+ * 'CUBE-API-TOKEN',
30
+ * { apiUrl: 'http://localhost:4000/cubejs-api/v1' }
31
+ * );
32
+ * ```
33
+ *
34
+ * You can also pass an async function or a promise that will resolve to the API token
35
+ *
36
+ * ```js
37
+ * import cube from '@cubejs-client/core';
38
+ * const cubeApi = cube(
39
+ * async () => await Auth.getJwtToken(),
40
+ * { apiUrl: 'http://localhost:4000/cubejs-api/v1' }
41
+ * );
42
+ * ```
43
+ */
44
+ constructor(apiToken, options) {
45
+ if (apiToken && !Array.isArray(apiToken) && typeof apiToken === 'object') {
46
+ options = apiToken;
47
+ apiToken = undefined;
48
+ }
49
+ if (!options || (!options.transport && !options.apiUrl)) {
50
+ throw new Error('The `apiUrl` option is required');
51
+ }
52
+ this.apiToken = apiToken;
53
+ this.apiUrl = options.apiUrl;
54
+ this.method = options.method;
55
+ this.headers = options.headers || {};
56
+ this.credentials = options.credentials;
57
+ this.transport = options.transport || new HttpTransport({
58
+ authorization: typeof apiToken === 'string' ? apiToken : undefined,
59
+ apiUrl: this.apiUrl,
60
+ method: this.method,
61
+ headers: this.headers,
62
+ credentials: this.credentials,
63
+ fetchTimeout: options.fetchTimeout,
64
+ signal: options.signal
65
+ });
66
+ this.pollInterval = options.pollInterval || 5;
67
+ this.parseDateMeasures = options.parseDateMeasures;
68
+ this.castNumerics = typeof options.castNumerics === 'boolean' ? options.castNumerics : false;
69
+ this.networkErrorRetries = options.networkErrorRetries || 0;
70
+ this.updateAuthorizationPromise = null;
71
+ }
72
+ request(method, params) {
73
+ return this.transport.request(method, {
74
+ baseRequestId: uuidv4(),
75
+ ...params
76
+ });
77
+ }
78
+ loadMethod(request, toResult, options, callback) {
79
+ const mutexValue = ++mutexCounter;
80
+ if (typeof options === 'function' && !callback) {
81
+ callback = options;
82
+ options = undefined;
83
+ }
84
+ options = options || {};
85
+ const mutexKey = options.mutexKey || 'default';
86
+ if (options.mutexObj) {
87
+ options.mutexObj[mutexKey] = mutexValue;
88
+ }
89
+ const requestPromise = this
90
+ .updateTransportAuthorization()
91
+ .then(() => request());
92
+ let skipAuthorizationUpdate = true;
93
+ let unsubscribed = false;
94
+ const checkMutex = async () => {
95
+ const requestInstance = await requestPromise;
96
+ if (options &&
97
+ options.mutexObj &&
98
+ options.mutexObj[mutexKey] !== mutexValue) {
99
+ unsubscribed = true;
100
+ if (requestInstance.unsubscribe) {
101
+ await requestInstance.unsubscribe();
102
+ }
103
+ throw MUTEX_ERROR;
104
+ }
105
+ };
106
+ let networkRetries = this.networkErrorRetries;
107
+ const loadImpl = async (response, next) => {
108
+ const requestInstance = await requestPromise;
109
+ const subscribeNext = async () => {
110
+ if (options?.subscribe && !unsubscribed) {
111
+ if (requestInstance.unsubscribe) {
112
+ return next();
113
+ }
114
+ else {
115
+ await new Promise(resolve => setTimeout(() => resolve(), this.pollInterval * 1000));
116
+ return next();
117
+ }
118
+ }
119
+ return null;
120
+ };
121
+ const continueWait = async (wait = false) => {
122
+ if (!unsubscribed) {
123
+ if (wait) {
124
+ await new Promise(resolve => setTimeout(() => resolve(), this.pollInterval * 1000));
125
+ }
126
+ return next();
127
+ }
128
+ return null;
129
+ };
130
+ if (options?.subscribe && !skipAuthorizationUpdate) {
131
+ await this.updateTransportAuthorization();
132
+ }
133
+ skipAuthorizationUpdate = false;
134
+ if (('status' in response && response.status === 502) ||
135
+ ('error' in response && response.error?.toLowerCase() === 'network error') &&
136
+ --networkRetries >= 0) {
137
+ await checkMutex();
138
+ return continueWait(true);
139
+ }
140
+ // From here we're sure that response is only fetch Response
141
+ response = response;
142
+ let body = {};
143
+ let text = '';
144
+ try {
145
+ text = await response.text();
146
+ body = JSON.parse(text);
147
+ }
148
+ catch (_) {
149
+ body.error = text;
150
+ }
151
+ if (body.error === 'Continue wait') {
152
+ await checkMutex();
153
+ if (options?.progressCallback) {
154
+ options.progressCallback(new ProgressResult(body));
155
+ }
156
+ return continueWait();
157
+ }
158
+ if (response.status !== 200) {
159
+ await checkMutex();
160
+ if (!options?.subscribe && requestInstance.unsubscribe) {
161
+ await requestInstance.unsubscribe();
162
+ }
163
+ const error = new RequestError(body.error || '', body, response.status);
164
+ if (callback) {
165
+ callback(error);
166
+ }
167
+ else {
168
+ throw error;
169
+ }
170
+ return subscribeNext();
171
+ }
172
+ await checkMutex();
173
+ if (!options?.subscribe && requestInstance.unsubscribe) {
174
+ await requestInstance.unsubscribe();
175
+ }
176
+ const result = toResult(body);
177
+ if (callback) {
178
+ callback(null, result);
179
+ }
180
+ else {
181
+ return result;
182
+ }
183
+ return subscribeNext();
184
+ };
185
+ const promise = requestPromise.then(requestInstance => mutexPromise(requestInstance.subscribe(loadImpl)));
186
+ if (callback) {
187
+ return {
188
+ unsubscribe: async () => {
189
+ const requestInstance = await requestPromise;
190
+ unsubscribed = true;
191
+ if (requestInstance.unsubscribe) {
192
+ return requestInstance.unsubscribe();
193
+ }
194
+ return null;
195
+ }
196
+ };
197
+ }
198
+ else {
199
+ return promise;
200
+ }
201
+ }
202
+ async updateTransportAuthorization() {
203
+ if (this.updateAuthorizationPromise) {
204
+ await this.updateAuthorizationPromise;
205
+ return;
206
+ }
207
+ const tokenFetcher = this.apiToken;
208
+ if (typeof tokenFetcher === 'function') {
209
+ const promise = (async () => {
210
+ try {
211
+ const token = await tokenFetcher();
212
+ if (this.transport.authorization !== token) {
213
+ this.transport.authorization = token;
214
+ }
215
+ }
216
+ finally {
217
+ this.updateAuthorizationPromise = null;
218
+ }
219
+ })();
220
+ this.updateAuthorizationPromise = promise;
221
+ await promise;
222
+ }
223
+ }
224
+ /**
225
+ * Add system properties to a query object.
226
+ */
227
+ patchQueryInternal(query, responseFormat) {
228
+ if (responseFormat === 'compact' &&
229
+ query.responseFormat !== 'compact') {
230
+ return {
231
+ ...query,
232
+ responseFormat: 'compact',
233
+ };
234
+ }
235
+ else {
236
+ return query;
237
+ }
238
+ }
239
+ /**
240
+ * Process result fetched from the gateway#load method according
241
+ * to the network protocol.
242
+ */
243
+ loadResponseInternal(response, options = {}) {
244
+ if (response.results.length) {
245
+ if (options?.castNumerics) {
246
+ response.results.forEach((result) => {
247
+ const numericMembers = Object.entries({
248
+ ...result.annotation.measures,
249
+ ...result.annotation.dimensions,
250
+ }).reduce((acc, [k, v]) => {
251
+ if (v.type === 'number') {
252
+ acc.push(k);
253
+ }
254
+ return acc;
255
+ }, []);
256
+ result.data = result.data.map((row) => {
257
+ numericMembers.forEach((key) => {
258
+ if (row[key] != null) {
259
+ row[key] = Number(row[key]);
260
+ }
261
+ });
262
+ return row;
263
+ });
264
+ });
265
+ }
266
+ if (response.results[0].query.responseFormat &&
267
+ response.results[0].query.responseFormat === 'compact') {
268
+ response.results.forEach((result, j) => {
269
+ const data = [];
270
+ const { dataset, members } = result.data;
271
+ dataset.forEach((r) => {
272
+ const row = {};
273
+ members.forEach((m, i) => {
274
+ row[m] = r[i];
275
+ });
276
+ data.push(row);
277
+ });
278
+ response.results[j].data = data;
279
+ });
280
+ }
281
+ }
282
+ return new ResultSet(response, {
283
+ parseDateMeasures: this.parseDateMeasures
284
+ });
285
+ }
286
+ /**
287
+ * Fetch data for the passed `query`.
288
+ *
289
+ * ```js
290
+ * import cube from '@cubejs-client/core';
291
+ * import Chart from 'chart.js';
292
+ * import chartjsConfig from './toChartjsData';
293
+ *
294
+ * const cubeApi = cube('CUBEJS_TOKEN');
295
+ *
296
+ * const resultSet = await cubeApi.load({
297
+ * measures: ['Stories.count'],
298
+ * timeDimensions: [{
299
+ * dimension: 'Stories.time',
300
+ * dateRange: ['2015-01-01', '2015-12-31'],
301
+ * granularity: 'month'
302
+ * }]
303
+ * });
304
+ *
305
+ * const context = document.getElementById('myChart');
306
+ * new Chart(context, chartjsConfig(resultSet));
307
+ * ```
308
+ * @param query - [Query object](/product/apis-integrations/rest-api/query-format)
309
+ * @param options
310
+ * @param callback
311
+ * @param responseFormat
312
+ */
313
+ load(query, options, callback, responseFormat = 'default') {
314
+ [query, options] = this.prepareQueryOptions(query, options, responseFormat);
315
+ return this.loadMethod(() => this.request('load', {
316
+ query,
317
+ queryType: 'multi',
318
+ signal: options?.signal
319
+ }), (response) => this.loadResponseInternal(response, options), options, callback);
320
+ }
321
+ prepareQueryOptions(query, options, responseFormat = 'default') {
322
+ options = {
323
+ castNumerics: this.castNumerics,
324
+ ...options
325
+ };
326
+ if (responseFormat === 'compact') {
327
+ if (Array.isArray(query)) {
328
+ const patched = query.map((q) => this.patchQueryInternal(q, 'compact'));
329
+ return [patched, options];
330
+ }
331
+ else {
332
+ const patched = this.patchQueryInternal(query, 'compact');
333
+ return [patched, options];
334
+ }
335
+ }
336
+ return [query, options];
337
+ }
338
+ /**
339
+ * Allows you to fetch data and receive updates over time. See [Real-Time Data Fetch](/product/apis-integrations/rest-api/real-time-data-fetch)
340
+ *
341
+ * ```js
342
+ * // Subscribe to a query's updates
343
+ * const subscription = await cubeApi.subscribe(
344
+ * {
345
+ * measures: ['Logs.count'],
346
+ * timeDimensions: [
347
+ * {
348
+ * dimension: 'Logs.time',
349
+ * granularity: 'hour',
350
+ * dateRange: 'last 1440 minutes',
351
+ * },
352
+ * ],
353
+ * },
354
+ * options,
355
+ * (error, resultSet) => {
356
+ * if (!error) {
357
+ * // handle the update
358
+ * }
359
+ * }
360
+ * );
361
+ *
362
+ * // Unsubscribe from a query's updates
363
+ * subscription.unsubscribe();
364
+ * ```
365
+ */
366
+ subscribe(query, options, callback, responseFormat = 'default') {
367
+ [query, options] = this.prepareQueryOptions(query, options, responseFormat);
368
+ return this.loadMethod(() => this.request('subscribe', {
369
+ query,
370
+ queryType: 'multi',
371
+ signal: options?.signal
372
+ }), (response) => this.loadResponseInternal(response, options), { ...options, subscribe: true }, callback);
373
+ }
374
+ /**
375
+ * Get generated SQL string for the given `query`.
376
+ */
377
+ sql(query, options, callback) {
378
+ return this.loadMethod(() => this.request('sql', {
379
+ query,
380
+ signal: options?.signal
381
+ }), (response) => (Array.isArray(response) ? response.map((body) => new SqlQuery(body)) : new SqlQuery(response)), options, callback);
382
+ }
383
+ /**
384
+ * Get meta description of cubes available for querying.
385
+ */
386
+ meta(options, callback) {
387
+ return this.loadMethod(() => this.request('meta', {
388
+ signal: options?.signal
389
+ }), (body) => new Meta(body), options, callback);
390
+ }
391
+ /**
392
+ * Get query related meta without query execution
393
+ */
394
+ dryRun(query, options, callback) {
395
+ return this.loadMethod(() => this.request('dry-run', {
396
+ query,
397
+ signal: options?.signal
398
+ }), (response) => response, options, callback);
399
+ }
400
+ }
401
+ export default (apiToken, options) => new CubeApi(apiToken, options);
402
+ export { CubeApi };
403
+ export { default as Meta } from './Meta';
404
+ export { default as SqlQuery } from './SqlQuery';
405
+ export { default as RequestError } from './RequestError';
406
+ export { default as ProgressResult } from './ProgressResult';
407
+ export { default as ResultSet } from './ResultSet';
408
+ export * from './HttpTransport';
409
+ export * from './utils';
410
+ export * from './time';
411
+ export * from './types';
@@ -0,0 +1,3 @@
1
+ declare const cubeAll: any;
2
+ export default cubeAll;
3
+ //# sourceMappingURL=index.umd.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.umd.d.ts","sourceRoot":"","sources":["../../src/index.umd.ts"],"names":[],"mappings":"AAEA,QAAA,MAAM,OAAO,EAAE,GAAU,CAAC;AAM1B,eAAe,OAAO,CAAC"}
@@ -0,0 +1,6 @@
1
+ import cube, * as clientCoreExports from './index';
2
+ const cubeAll = cube;
3
+ Object.keys(clientCoreExports).forEach((key) => {
4
+ cubeAll[key] = clientCoreExports[key];
5
+ });
6
+ export default cubeAll;