All files / app/storage redisStorage.ts

94.11% Statements 32/34
84.61% Branches 11/13
100% Functions 8/8
94.11% Lines 32/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 355x 5x 5x 5x 1x 1x 1x 13x 13x 1x 1x 4x 4x 2x 2x 4x 4x 1x 1x 4x 4x     4x 4x 1x 1x 3x 3x 1x 1x 3x 3x 1x  
import { IStorage, IStorageSetOptions } from '../types/IStorage.js';
import { RedisClient, RedisSetOptions } from './redisClient.js';
 
export class RedisStorage implements IStorage {
  private readonly redisClient: RedisClient;
 
  constructor(redisClient: RedisClient) {
    this.redisClient = redisClient;
  }
 
  async set(key: string, value: string, options?: IStorageSetOptions): Promise<string> {
    const setResult = await this.redisClient.set(key, value, this.convertSetOptions(options));
    if (!setResult) {
      throw new Error(`RedisStorage has not set value: ${value} for key: ${key}`);
    }
    return setResult;
  }
 
  private convertSetOptions(options?: IStorageSetOptions): RedisSetOptions {
    const setOptions: RedisSetOptions = {};
    if (options?.expirationMilliseconds) {
      setOptions.expiration = { type: 'PX', value: options.expirationMilliseconds };
    }
    return setOptions;
  }
 
  get(key: string): Promise<string | null> {
    return this.redisClient.get(key);
  }
 
  increment(key: string): Promise<number> {
    return this.redisClient.incr(key);
  }
}