All files / app/service serviceFactory.ts

92.68% Statements 38/41
87.5% Branches 7/8
100% Functions 3/3
92.68% Lines 38/41

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 35 36 37 38 39 40 41 424x 4x 4x 4x 4x 4x 4x 4x 4x 1x 6x 6x 1x 1x 1x 1x 1x 1x 1x 6x 3x 3x 3x 6x       6x 1x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import { RedisClientInstance } from '../storage/redisClient.js';
import { RedisStorage } from '../storage/redisStorage.js';
import { RequestService } from './requestService.js';
import { TicketService } from './ticketService.js';
import { IdempotencyService } from './idempotencyService.js';
 
type ServiceClass = TicketService | RequestService | RedisStorage | IdempotencyService;
 
export class ServiceFactory {
  public static async getInstanceOfClass<T extends ServiceClass>(className: ClassName): Promise<T> {
    switch (className) {
      case ClassName.TicketService: {
        const redisStorage = await ServiceFactory.getInstanceOfClass<RedisStorage>(ClassName.RedisStorage);
        return new TicketService(redisStorage) as T;
      }
      case ClassName.RequestService: {
        const redisStorage = await ServiceFactory.getInstanceOfClass<RedisStorage>(ClassName.RedisStorage);
        return new RequestService(redisStorage) as T;
      }
      case ClassName.RedisStorage: {
        const redisClient = await RedisClientInstance.getRedisClient();
        return new RedisStorage(redisClient) as T;
      }
      case ClassName.IdempotencyService: {
        const redisStorage = await ServiceFactory.getInstanceOfClass<RedisStorage>(ClassName.RedisStorage);
        return new IdempotencyService(redisStorage) as T;
      }
      default:
        throw new Error('Unknown class name');
    }
  }
}
 
export enum ClassName {
  TicketService = 'TicketService',
  RequestService = 'RequestService',
  RedisStorage = 'RedisStorage',
  IdempotencyService = 'IdempotencyService',
}
 
export { RedisStorage, RequestService, TicketService, IdempotencyService };