cache.ts 654 Bytes
Newer Older
1
import * as apicache from 'apicache'
Chocobozzz's avatar
Chocobozzz committed
2
import { HttpStatusCode } from '../../shared/models/http/http-error-codes'
Chocobozzz's avatar
Chocobozzz committed
3
import { Redis } from '../lib/redis'
Chocobozzz's avatar
Chocobozzz committed
4

5
6
// Ensure Redis is initialized
Redis.Instance.init()
7

8
const defaultOptions = {
9
  redisClient: Redis.Instance.getClient(),
10
11
  appendKey: () => Redis.Instance.getPrefix(),
  statusCodes: {
12
13
14
15
    exclude: [
      HttpStatusCode.FORBIDDEN_403,
      HttpStatusCode.NOT_FOUND_404
    ]
16
  }
Chocobozzz's avatar
Chocobozzz committed
17
18
}

19
20
21
22
const cacheRoute = (extraOptions = {}) => apicache.options({
  ...defaultOptions,
  ...extraOptions
}).middleware
23

Chocobozzz's avatar
Chocobozzz committed
24
25
26
// ---------------------------------------------------------------------------

export {
Chocobozzz's avatar
Chocobozzz committed
27
  cacheRoute
Chocobozzz's avatar
Chocobozzz committed
28
}