Skip to content

Commit e2f1742

Browse files
committed
refactor api gw: move copy/paste into this.normalizeCacheMode()
1 parent f70d8ba commit e2f1742

File tree

1 file changed

+16
-36
lines changed

1 file changed

+16
-36
lines changed

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

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -314,66 +314,33 @@ class ApiGateway {
314314
*************************************************************** */
315315

316316
app.get(`${this.basePath}/v1/load`, userMiddlewares, userAsyncHandler(async (req: any, res) => {
317-
let cacheMode: CacheMode | undefined;
318-
319-
// TODO: Drop this fallback to renewQuery when it will be removed
320-
if (req.query.cache !== undefined) {
321-
cacheMode = req.query.cache;
322-
} else if (req.query.query?.renewQuery !== undefined) {
323-
cacheMode = req.query.query.renewQuery === true
324-
? 'must-revalidate'
325-
: 'stale-if-slow';
326-
}
327-
328317
await this.load({
329318
query: req.query.query,
330319
context: req.context,
331320
res: this.resToResultFn(res),
332321
queryType: req.query.queryType,
333-
cacheMode,
322+
cacheMode: this.normalizeCacheMode(req.query.query, req.query.cache),
334323
});
335324
}));
336325

337326
const jsonParser = bodyParser.json({ limit: '1mb' });
338327
app.post(`${this.basePath}/v1/load`, jsonParser, userMiddlewares, userAsyncHandler(async (req, res) => {
339-
let cacheMode: CacheMode | undefined;
340-
341-
// TODO: Drop this fallback to renewQuery when it will be removed
342-
if (req.query.cache !== undefined) {
343-
cacheMode = req.body.cache;
344-
} else if (req.body.query?.renewQuery !== undefined) {
345-
cacheMode = req.body.query.renewQuery === true
346-
? 'must-revalidate'
347-
: 'stale-if-slow';
348-
}
349-
350328
await this.load({
351329
query: req.body.query,
352330
context: req.context,
353331
res: this.resToResultFn(res),
354332
queryType: req.body.queryType,
355-
cacheMode,
333+
cacheMode: this.normalizeCacheMode(req.body.query, req.body.cache),
356334
});
357335
}));
358336

359337
app.get(`${this.basePath}/v1/subscribe`, userMiddlewares, userAsyncHandler(async (req: any, res) => {
360-
let cacheMode: CacheMode | undefined;
361-
362-
// TODO: Drop this fallback to renewQuery when it will be removed
363-
if (req.query.cache !== undefined) {
364-
cacheMode = req.query.cache;
365-
} else if (req.query.query?.renewQuery !== undefined) {
366-
cacheMode = req.query.query.renewQuery === true
367-
? 'must-revalidate'
368-
: 'stale-if-slow';
369-
}
370-
371338
await this.load({
372339
query: req.query.query,
373340
context: req.context,
374341
res: this.resToResultFn(res),
375342
queryType: req.query.queryType,
376-
cacheMode,
343+
cacheMode: this.normalizeCacheMode(req.query.query, req.query.cache),
377344
});
378345
}));
379346

@@ -620,6 +587,19 @@ class ApiGateway {
620587
return requestStarted && (new Date().getTime() - requestStarted.getTime());
621588
}
622589

590+
// TODO: Drop this when renewQuery will be removed
591+
private normalizeCacheMode(query, cache: string): CacheMode {
592+
if (cache !== undefined) {
593+
return cache as CacheMode;
594+
} else if (query?.renewQuery !== undefined) {
595+
return query.renewQuery === true
596+
? 'must-revalidate'
597+
: 'stale-if-slow';
598+
}
599+
600+
return 'stale-if-slow';
601+
}
602+
623603
private filterVisibleItemsInMeta(context: RequestContext, cubes: any[]) {
624604
const isDevMode = getEnv('devMode');
625605
function visibilityFilter(item) {

0 commit comments

Comments
 (0)