From 0394daf6ce8d73103aee5f5667a98fa089325569 Mon Sep 17 00:00:00 2001 From: Anton Lantukh Date: Fri, 12 Jan 2024 14:32:45 +0100 Subject: [PATCH] feat(epg): use getNamedModule --- src/modules/register.ts | 10 +++++----- src/services/epg/epg.service.ts | 10 ++-------- src/services/epg/jw.epg.service.ts | 5 ----- src/services/epg/viewNexa.epg.service.ts | 11 ----------- src/stores/EpgController.test.ts | 22 ++++++++++------------ src/stores/EpgController.ts | 11 +++-------- 6 files changed, 20 insertions(+), 49 deletions(-) diff --git a/src/modules/register.ts b/src/modules/register.ts index 581ddd8c7..6981ef9dd 100644 --- a/src/modules/register.ts +++ b/src/modules/register.ts @@ -1,7 +1,7 @@ // To organize imports in a better way /* eslint-disable import/order */ import 'reflect-metadata'; // include once in the app for inversify (see: https://github.com/inversify/InversifyJS/blob/master/README.md#-installation) -import { INTEGRATION } from '#src/config'; +import { EPG_TYPE, INTEGRATION } from '#src/config'; import { container } from '#src/modules/container'; import ApiService from '#src/services/api.service'; @@ -51,10 +51,6 @@ container.bind(GenericEntitlementService).toSelf(); container.bind(ApiService).toSelf(); container.bind(SettingsService).toSelf(); -// EPG services -container.bind(EpgService).to(JWEpgService); -container.bind(EpgService).to(ViewNexaEpgService); - // Common controllers container.bind(AppController).toSelf(); container.bind(WatchHistoryController).toSelf(); @@ -70,6 +66,10 @@ container.bind('INTEGRATION_TYPE').toDynamicValue((context) => { return context.container.get(AppController).getIntegrationType(); }); +// EPG services +container.bind(EpgService).to(JWEpgService).whenTargetNamed(EPG_TYPE.JWP); +container.bind(EpgService).to(ViewNexaEpgService).whenTargetNamed(EPG_TYPE.VIEW_NEXA); + // Cleeng integration container.bind(CleengService).toSelf(); container.bind(AccountService).to(CleengAccountService).whenTargetNamed(INTEGRATION.CLEENG); diff --git a/src/services/epg/epg.service.ts b/src/services/epg/epg.service.ts index 1a7ee0546..bb19d17de 100644 --- a/src/services/epg/epg.service.ts +++ b/src/services/epg/epg.service.ts @@ -1,13 +1,7 @@ -import type { EpgProgram, EpgScheduleType } from '#types/epg'; +import type { EpgProgram } from '#types/epg'; import type { PlaylistItem } from '#types/playlist'; -export default abstract class EpgProviderService { - readonly type: EpgScheduleType; - - protected constructor(type: EpgScheduleType) { - this.type = type; - } - +export default abstract class EpgService { /** * Fetch the schedule data for the given PlaylistItem */ diff --git a/src/services/epg/jw.epg.service.ts b/src/services/epg/jw.epg.service.ts index 488cff5ff..1066e424f 100644 --- a/src/services/epg/jw.epg.service.ts +++ b/src/services/epg/jw.epg.service.ts @@ -8,7 +8,6 @@ import type { PlaylistItem } from '#types/playlist'; import { getDataOrThrow } from '#src/utils/api'; import { logDev } from '#src/utils/common'; import type { EpgProgram } from '#types/epg'; -import { EPG_TYPE } from '#src/config'; const AUTHENTICATION_HEADER = 'API-KEY'; @@ -31,10 +30,6 @@ const jwEpgProgramSchema = object().shape({ @injectable() export default class JWEpgService extends EpgService { - constructor() { - super(EPG_TYPE.JWP); - } - transformProgram = async (data: unknown): Promise => { const program = await jwEpgProgramSchema.validate(data); const image = program.chapterPointCustomProperties?.find((item) => item.key === 'image')?.value || undefined; diff --git a/src/services/epg/viewNexa.epg.service.ts b/src/services/epg/viewNexa.epg.service.ts index 6f70528e6..cc8142de6 100644 --- a/src/services/epg/viewNexa.epg.service.ts +++ b/src/services/epg/viewNexa.epg.service.ts @@ -7,7 +7,6 @@ import EpgService from './epg.service'; import type { PlaylistItem } from '#types/playlist'; import { logDev } from '#src/utils/common'; import type { EpgProgram } from '#types/epg'; -import { EPG_TYPE } from '#src/config'; const viewNexaEpgProgramSchema = object().shape({ 'episode-num': object().shape({ @@ -30,13 +29,6 @@ const parseData = (date: string): string => parse(date, 'yyyyMdHms xxxx', new Da @injectable() export default class ViewNexaEpgService extends EpgService { - constructor() { - super(EPG_TYPE.VIEW_NEXA); - } - - /** - * Validate the given data with the viewNexaProgramSchema and transform it into an EpgProgram - */ transformProgram = async (data: unknown): Promise => { const program = await viewNexaEpgProgramSchema.validate(data); @@ -51,9 +43,6 @@ export default class ViewNexaEpgService extends EpgService { }; }; - /** - * Fetch the schedule data for the given PlaylistItem - */ fetchSchedule = async (item: PlaylistItem) => { const { XMLParser } = await import('fast-xml-parser'); diff --git a/src/stores/EpgController.test.ts b/src/stores/EpgController.test.ts index 2293d8d84..704add4f4 100644 --- a/src/stores/EpgController.test.ts +++ b/src/stores/EpgController.test.ts @@ -5,26 +5,24 @@ import EpgController from './EpgController'; import channel1 from '#test/epg/jwChannel.json'; import livePlaylistFixture from '#test/fixtures/livePlaylist.json'; +import EpgService from '#src/services/epg/epg.service'; import type { Playlist } from '#types/playlist'; -import { EPG_TYPE } from '#src/config'; const livePlaylist = livePlaylistFixture as Playlist; const transformProgram = vi.fn(); const fetchSchedule = vi.fn(); -const epgService = { transformProgram, fetchSchedule }; -const jwpEpgService = { - ...epgService, - type: EPG_TYPE.JWP, -}; - -const viewNexaEpgService = { - ...epgService, - type: EPG_TYPE.VIEW_NEXA, -}; +vi.mock('#src/modules/container', () => ({ + getNamedModule: (type: typeof EpgService) => { + switch (type) { + case EpgService: + return { transformProgram, fetchSchedule }; + } + }, +})); -const epgController = new EpgController([jwpEpgService, viewNexaEpgService]); +const epgController = new EpgController(); const mockProgram1 = { id: 'test', diff --git a/src/stores/EpgController.ts b/src/stores/EpgController.ts index 81ab05545..19a97d621 100644 --- a/src/stores/EpgController.ts +++ b/src/stores/EpgController.ts @@ -1,11 +1,12 @@ import { addDays, differenceInDays } from 'date-fns'; -import { injectable, multiInject } from 'inversify'; +import { injectable } from 'inversify'; import EpgService from '#src/services/epg/epg.service'; import { logDev } from '#src/utils/common'; import type { PlaylistItem } from '#types/playlist'; import type { EpgProgram, EpgChannel } from '#types/epg'; import { EPG_TYPE } from '#src/config'; +import { getNamedModule } from '#src/modules/container'; export const isFulfilled = (input: PromiseSettledResult): input is PromiseFulfilledResult => { if (input.status === 'fulfilled') { @@ -18,12 +19,6 @@ export const isFulfilled = (input: PromiseSettledResult): input is Promise @injectable() export default class EpgController { - private epgServices: EpgService[]; - - public constructor(@multiInject(EpgService) epgServices: EpgService[]) { - this.epgServices = epgServices || []; - } - /** * Update the start and end time properties of the given programs with the current date. * This can be used when having a static schedule or while developing @@ -101,7 +96,7 @@ export default class EpgController { getEpgService = (item: PlaylistItem) => { const scheduleType = item?.scheduleType || EPG_TYPE.JWP; - const service = this.epgServices.find((service) => service.type === scheduleType); + const service = getNamedModule(EpgService, scheduleType); if (!service) { throw Error(`No epg service was added for the ${scheduleType} schedule type`);