Skip to content

Commit bc52b41

Browse files
committed
refactor: move normalizeQueryCacheMode to normalize
1 parent 57c8066 commit bc52b41

File tree

2 files changed

+32
-26
lines changed

2 files changed

+32
-26
lines changed

packages/cubejs-api-gateway/src/gateway.ts

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -589,27 +589,6 @@ class ApiGateway {
589589
return requestStarted && (new Date().getTime() - requestStarted.getTime());
590590
}
591591

592-
private normalizeQueryCacheMode(query: Query, cacheMode: CacheMode | undefined): Query {
593-
if (cacheMode !== undefined) {
594-
query.cacheMode = cacheMode;
595-
} else if (!query.cache && query?.renewQuery !== undefined) {
596-
// TODO: Drop this when renewQuery will be removed
597-
query.cacheMode = query.renewQuery === true
598-
? 'must-revalidate'
599-
: 'stale-if-slow';
600-
} else if (!query.cache) {
601-
query.cacheMode = 'stale-if-slow';
602-
} else {
603-
query.cacheMode = query.cache;
604-
}
605-
606-
// TODO: Drop this when renewQuery will be removed
607-
query.renewQuery = undefined;
608-
query.cache = undefined;
609-
610-
return query;
611-
}
612-
613592
private filterVisibleItemsInMeta(context: RequestContext, cubes: any[]) {
614593
const isDevMode = getEnv('devMode');
615594
function visibilityFilter(item) {
@@ -1237,8 +1216,6 @@ class ApiGateway {
12371216
cacheMode?: CacheMode,
12381217
): Promise<[QueryType, NormalizedQuery[], NormalizedQuery[]]> {
12391218
let query = this.parseQueryParam(inputQuery);
1240-
query = Array.isArray(query) ? query.map(q => this.normalizeQueryCacheMode(q, cacheMode))
1241-
: this.normalizeQueryCacheMode(query, cacheMode);
12421219

12431220
let queryType: QueryType = QueryTypeEnum.REGULAR_QUERY;
12441221
if (!Array.isArray(query)) {
@@ -1277,7 +1254,7 @@ class ApiGateway {
12771254
}
12781255

12791256
return {
1280-
normalizedQuery: (normalizeQuery(currentQuery, persistent)),
1257+
normalizedQuery: (normalizeQuery(currentQuery, persistent, cacheMode)),
12811258
hasExpressionsInQuery
12821259
};
12831260
});

packages/cubejs-api-gateway/src/query.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,14 +291,43 @@ function parseInputMemberExpression(expression) {
291291
return expression;
292292
}
293293

294+
/**
295+
*
296+
* @param {Query} query
297+
* @param {CacheMode} cacheMode
298+
* @return {Query}
299+
*/
300+
function normalizeQueryCacheMode(query, cacheMode) {
301+
if (cacheMode !== undefined) {
302+
query.cacheMode = cacheMode;
303+
} else if (!query.cache && query?.renewQuery !== undefined) {
304+
// TODO: Drop this when renewQuery will be removed
305+
query.cacheMode = query.renewQuery === true
306+
? 'must-revalidate'
307+
: 'stale-if-slow';
308+
} else if (!query.cache) {
309+
query.cacheMode = 'stale-if-slow';
310+
} else {
311+
query.cacheMode = query.cache;
312+
}
313+
314+
// TODO: Drop this when renewQuery will be removed
315+
query.renewQuery = undefined;
316+
query.cache = undefined;
317+
318+
return query;
319+
}
320+
294321
/**
295322
* Normalize incoming network query.
296323
* @param {Query} query
297324
* @param {boolean} persistent
325+
* @param {CacheMode} [cacheMode]
298326
* @throws {UserError}
299-
* @returns {NormalizedQuery}
327+
* @returns {import('./types/query').NormalizedQuery}
300328
*/
301-
const normalizeQuery = (query, persistent) => {
329+
const normalizeQuery = (query, persistent, cacheMode) => {
330+
query = normalizeQueryCacheMode(query);
302331
const { error } = querySchema.validate(query);
303332
if (error) {
304333
throw new UserError(`Invalid query format: ${error.message || error.toString()}`);

0 commit comments

Comments
 (0)