Skip to content

Commit 60ad861

Browse files
author
matus.hudec
committed
comments
1 parent 2e403c3 commit 60ad861

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

src/httpClient/redisStorage.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,39 @@ import type { StorageValue } from 'axios-cache-interceptor';
33
// eslint-disable-next-line import/no-extraneous-dependencies
44
import { createClient } from 'redis';
55

6-
const keyPrefix = 'axios-cache-';
6+
const KEY_PREFIX = 'axios-cache-';
7+
8+
const MIN_TTL = 60000;
79

810
export default function createRedisStorage(client: ReturnType<typeof createClient>) {
911
// source https://axios-cache-interceptor.js.org/guide/storages#node-redis-storage
1012
return buildStorage({
1113
async find(key) {
12-
const result = await client.get(`${keyPrefix}${key}`);
14+
const result = await client.get(`${KEY_PREFIX}${key}`);
1315
return result ? (JSON.parse(result) as StorageValue) : undefined;
1416
},
1517

1618
// eslint-disable-next-line complexity
1719
async set(key, value, req) {
18-
await client.set(`${keyPrefix}${key}`, JSON.stringify(value), {
20+
await client.set(`${KEY_PREFIX}${key}`, JSON.stringify(value), {
1921
PXAT:
22+
// We don't want to keep indefinitely values in the storage if
23+
// their request don't finish somehow. Either set its value as
24+
// the TTL or 1 minute (MIN_TTL).
2025
value.state === 'loading'
21-
? Date.now() + (req?.cache && typeof req.cache.ttl === 'number' ? req.cache.ttl : 60000)
26+
? Date.now() +
27+
(req?.cache && typeof req.cache.ttl === 'number' ? req.cache.ttl : MIN_TTL)
2228
: (value.state === 'stale' && value.ttl) ||
2329
(value.state === 'cached' && !canStale(value))
2430
? value.createdAt + value.ttl!
25-
: undefined,
31+
: // otherwise, we can't determine when it should expire, so we keep
32+
// it indefinitely.
33+
undefined,
2634
});
2735
},
2836

2937
async remove(key) {
30-
await client.del(`${keyPrefix}${key}`);
38+
await client.del(`${KEY_PREFIX}${key}`);
3139
},
3240
});
3341
}

src/logger/logger.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import stringify from 'fast-safe-stringify';
55
import isError from 'is-error';
66
import { redactSecret } from '../util';
77

8-
const defaultPayloadLimit = 32768;
8+
const DEFAULT_PAYLOAD_LIMIT = 32768;
99

10-
const minPayloadLimit = 10000;
10+
const MIN_PAYLOAD_LIMIT = 10000;
1111

1212
export default class Logger {
1313
public invocationId: string;
@@ -24,9 +24,9 @@ export default class Logger {
2424
this.logFunction = configuration?.logFunction ?? console.log;
2525
this.jsonSpace = configuration?.jsonSpace ?? 2;
2626
this.payloadLimit = !configuration?.payloadLimit
27-
? defaultPayloadLimit
28-
: configuration?.payloadLimit < minPayloadLimit
29-
? minPayloadLimit
27+
? DEFAULT_PAYLOAD_LIMIT
28+
: configuration?.payloadLimit < MIN_PAYLOAD_LIMIT
29+
? MIN_PAYLOAD_LIMIT
3030
: configuration.payloadLimit;
3131

3232
this.invocationId = 'none';

0 commit comments

Comments
 (0)