refactor: remove singleton pattern to ensure consistency across all calls and API files

This commit is contained in:
Pierre
2025-03-20 13:51:45 +01:00
committed by HiItsStolas
parent b7f8e22db2
commit bf13cdce68
13 changed files with 19 additions and 42 deletions

View File

@@ -7,18 +7,10 @@ import { In } from 'typeorm';
import type { CoverArtResponse } from './interfaces';
class CoverArtArchive extends ExternalAPI {
private static instance: CoverArtArchive;
private readonly CACHE_TTL = 43200;
private readonly STALE_THRESHOLD = 30 * 24 * 60 * 60 * 1000;
public static getInstance(): CoverArtArchive {
if (!CoverArtArchive.instance) {
CoverArtArchive.instance = new CoverArtArchive();
}
return CoverArtArchive.instance;
}
private constructor() {
constructor() {
super(
'https://coverartarchive.org',
{},

View File

@@ -7,7 +7,6 @@ import { In } from 'typeorm';
import type { TadbArtistResponse } from './interfaces';
class TheAudioDb extends ExternalAPI {
private static instance: TheAudioDb;
private readonly apiKey = '195003';
private readonly CACHE_TTL = 43200;
private readonly STALE_THRESHOLD = 30 * 24 * 60 * 60 * 1000;
@@ -26,13 +25,6 @@ class TheAudioDb extends ExternalAPI {
);
}
public static getInstance(): TheAudioDb {
if (!TheAudioDb.instance) {
TheAudioDb.instance = new TheAudioDb();
}
return TheAudioDb.instance;
}
private isMetadataStale(metadata: MetadataArtist | null): boolean {
if (!metadata || !metadata.tadbUpdatedAt) return true;
return Date.now() - metadata.tadbUpdatedAt.getTime() > this.STALE_THRESHOLD;

View File

@@ -15,12 +15,11 @@ interface SearchPersonOptions {
}
class TmdbPersonMapper extends ExternalAPI {
private static instance: TmdbPersonMapper;
private readonly CACHE_TTL = 43200;
private readonly STALE_THRESHOLD = 30 * 24 * 60 * 60 * 1000;
private tmdb: TheMovieDb;
private constructor() {
constructor() {
super(
'https://api.themoviedb.org/3',
{
@@ -37,13 +36,6 @@ class TmdbPersonMapper extends ExternalAPI {
this.tmdb = new TheMovieDb();
}
public static getInstance(): TmdbPersonMapper {
if (!TmdbPersonMapper.instance) {
TmdbPersonMapper.instance = new TmdbPersonMapper();
}
return TmdbPersonMapper.instance;
}
private isMetadataStale(metadata: MetadataArtist | null): boolean {
if (!metadata || !metadata.tmdbUpdatedAt) return true;
return Date.now() - metadata.tmdbUpdatedAt.getTime() > this.STALE_THRESHOLD;

View File

@@ -56,6 +56,7 @@ export class MediaRequest {
options: MediaRequestOptions = {}
): Promise<MediaRequest> {
const tmdb = new TheMovieDb();
const listenBrainz = new ListenBrainzAPI();
const mediaRepository = getRepository(Media);
const requestRepository = getRepository(MediaRequest);
const userRepository = getRepository(User);
@@ -135,7 +136,7 @@ export class MediaRequest {
? await tmdb.getMovie({ movieId: requestBody.mediaId })
: requestBody.mediaType === MediaType.TV
? await tmdb.getTvShow({ tvId: requestBody.mediaId })
: await new ListenBrainzAPI().getAlbum(requestBody.mediaId.toString());
: await listenBrainz.getAlbum(requestBody.mediaId.toString());
let media = await mediaRepository.findOne({
where:
@@ -816,7 +817,7 @@ export class MediaRequest {
) {
const tmdb = new TheMovieDb();
const listenbrainz = new ListenBrainzAPI();
const coverArt = CoverArtArchive.getInstance();
const coverArt = new CoverArtArchive();
const musicbrainz = new MusicBrainz();
try {

View File

@@ -15,7 +15,7 @@ const artistRoutes = Router();
artistRoutes.get('/:id', async (req, res, next) => {
const listenbrainz = new ListenBrainzAPI();
const musicbrainz = new MusicBrainz();
const theAudioDb = TheAudioDb.getInstance();
const theAudioDb = new TheAudioDb();
const page = Number(req.query.page) || 1;
const pageSize = Number(req.query.pageSize) || 20;

View File

@@ -5,7 +5,7 @@ import { Router } from 'express';
const coverArtRoutes = Router();
coverArtRoutes.get('/batch/:ids', async (req, res) => {
const coverArtArchive = CoverArtArchive.getInstance();
const coverArtArchive = new CoverArtArchive();
const ids = (req.params.ids || '').split(',').filter(Boolean);
if (!ids.length) {

View File

@@ -1223,8 +1223,8 @@ discoverRoutes.get('/music/albums', async (req, res, next) => {
discoverRoutes.get('/music/artists', async (req, res, next) => {
const listenbrainz = new ListenBrainzAPI();
const personMapper = TmdbPersonMapper.getInstance();
const theAudioDb = TheAudioDb.getInstance();
const personMapper = new TmdbPersonMapper();
const theAudioDb = new TheAudioDb();
try {
const page = Number(req.query.page) || 1;

View File

@@ -18,8 +18,8 @@ const musicRoutes = Router();
musicRoutes.get('/:id', async (req, res, next) => {
const listenbrainz = new ListenBrainzAPI();
const musicbrainz = new MusicBrainz();
const personMapper = TmdbPersonMapper.getInstance();
const theAudioDb = TheAudioDb.getInstance();
const personMapper = new TmdbPersonMapper();
const theAudioDb = new TheAudioDb();
try {
const [albumDetails, media, onUserWatchlist] = await Promise.all([
@@ -203,8 +203,8 @@ musicRoutes.get('/:id', async (req, res, next) => {
musicRoutes.get('/:id/artist', async (req, res, next) => {
try {
const listenbrainzApi = new ListenBrainzAPI();
const personMapper = TmdbPersonMapper.getInstance();
const theAudioDb = TheAudioDb.getInstance();
const personMapper = new TmdbPersonMapper();
const theAudioDb = new TheAudioDb();
const metadataAlbumRepository = getRepository(MetadataAlbum);
const metadataArtistRepository = getRepository(MetadataArtist);

View File

@@ -19,7 +19,7 @@ const personRoutes = Router();
personRoutes.get('/:id', async (req, res, next) => {
const tmdb = new TheMovieDb();
const listenbrainz = new ListenBrainzAPI();
const theAudioDb = TheAudioDb.getInstance();
const theAudioDb = new TheAudioDb();
const page = Number(req.query.page) || 1;
const pageSize = Number(req.query.pageSize) || 20;

View File

@@ -38,8 +38,8 @@ searchRoutes.get('/', async (req, res, next) => {
} else {
const tmdb = new TheMovieDb();
const musicbrainz = new MusicBrainz();
const theAudioDb = TheAudioDb.getInstance();
const personMapper = TmdbPersonMapper.getInstance();
const theAudioDb = new TheAudioDb();
const personMapper = new TmdbPersonMapper();
const responses = await Promise.allSettled([
tmdb.searchMulti({

View File

@@ -27,7 +27,7 @@ export class IssueCommentSubscriber
let image = '';
const tmdb = new TheMovieDb();
const listenbrainz = new ListenBrainzAPI();
const coverArt = CoverArtArchive.getInstance();
const coverArt = new CoverArtArchive();
try {
const issue = (

View File

@@ -26,7 +26,7 @@ export class IssueSubscriber implements EntitySubscriberInterface<Issue> {
let image = '';
const tmdb = new TheMovieDb();
const listenbrainz = new ListenBrainzAPI();
const coverArt = CoverArtArchive.getInstance();
const coverArt = new CoverArtArchive();
try {
if (entity.media.mediaType === MediaType.MOVIE) {

View File

@@ -201,7 +201,7 @@ export class MediaRequestSubscriber
}
const listenbrainz = new ListenBrainzAPI();
const coverArt = CoverArtArchive.getInstance();
const coverArt = new CoverArtArchive();
const musicbrainz = new MusicBrainz();
try {