Fix: Linter warnings (#137)

* Automaticly fixable eslint issues, mostly 3 -> 2 space indentation

* fix: updated plex_userid to camelcase

* Linted and some consistency refactor on middleware

* eslint uses ecmaversion 2020 & allow empty catch rule

* Started linting source files

* Fixed eslint errors & improved a lot of error handling

* Set 2 eslint rules as warning temporarly
This commit was merged in pull request #137.
This commit is contained in:
2022-08-20 17:21:25 +02:00
committed by GitHub
parent cfbd4965db
commit 1815a429b0
83 changed files with 1625 additions and 1294 deletions

View File

@@ -3,27 +3,86 @@ const redisCache = require("../cache/redis");
const { Movie, Show, Person, Credits, ReleaseDates } = require("./types");
const tmdbErrorResponse = (error, typeString = undefined) => {
if (error.status === 404) {
let message = error.response.body.status_message;
class TMDBNotFoundError extends Error {
constructor(message) {
super(message);
throw {
status: 404,
message: message.slice(0, -1) + " in tmdb."
};
this.statusCode = 404;
}
}
class TMDBUnauthorizedError extends Error {
constructor(message = "TMDB returned access denied, requires api token.") {
super(message);
this.statusCode = 401;
}
}
class TMDBUnexpectedError extends Error {
constructor(type, errorMessage) {
const message = `An unexpected error occured while fetching ${type} from tmdb`;
super(message);
this.errorMessage = errorMessage;
this.statusCode = 500;
}
}
class TMDBNotReachableError extends Error {
constructor(
message = "TMDB api not reachable, check your internet connection"
) {
super(message);
}
}
const tmdbErrorResponse = (error, type = null) => {
if (error.status === 404) {
const message = error.response.body.status_message;
throw new TMDBNotFoundError(`${message.slice(0, -1)} in tmdb.`);
} else if (error.status === 401) {
throw {
status: 401,
message: error.response.body.status_message
};
throw new TMDBUnauthorizedError(error?.response?.body?.status_message);
} else if (error?.code === "ENOTFOUND") {
throw new TMDBNotReachableError();
}
throw {
status: 500,
message: `An unexpected error occured while fetching ${typeString} from tmdb`
};
throw new TMDBUnexpectedError(type, error);
};
/**
* Maps our response from tmdb api to a movie/show object.
* @param {String} response from tmdb.
* @param {String} The type declared in listSearch.
* @returns {Promise} dict with tmdb results, mapped as movie/show objects.
*/
function mapResults(response, type = null) {
const results = response?.results?.map(result => {
if (type === "movie" || result.media_type === "movie") {
const movie = Movie.convertFromTmdbResponse(result);
return movie.createJsonResponse();
}
if (type === "show" || result.media_type === "tv") {
const show = Show.convertFromTmdbResponse(result);
return show.createJsonResponse();
}
if (type === "person" || result.media_type === "person") {
const person = Person.convertFromTmdbResponse(result);
return person.createJsonResponse();
}
return {};
});
return {
results,
page: response?.page,
total_results: response?.total_results,
total_pages: response?.total_pages
};
}
class TMDB {
constructor(apiKey, cache, tmdbLibrary) {
this.tmdbLibrary = tmdbLibrary || moviedb(apiKey);
@@ -53,15 +112,17 @@ class TMDB {
this.defaultTTL = 86400;
}
getFromCacheOrFetchFromTmdb(cacheKey, tmdbMethod, query) {
return new Promise((resolve, reject) =>
this.cache
.get(cacheKey)
.then(resolve)
.catch(() => this.tmdb(tmdbMethod, query))
.then(resolve)
.catch(error => reject(tmdbErrorResponse(error, tmdbMethod)))
);
async getFromCacheOrFetchFromTmdb(cacheKey, tmdbMethod, query) {
try {
const result = await this.cache.get(cacheKey);
if (!result) throw new Error();
return result;
} catch {
return this.tmdb(tmdbMethod, query)
.then(result => this.cache.set(cacheKey, result, this.defaultTTL))
.catch(error => tmdbErrorResponse(error, tmdbMethod));
}
}
/**
@@ -75,9 +136,9 @@ class TMDB {
const query = { id: identifier };
const cacheKey = `tmdb/${this.cacheTags.movieInfo}:${identifier}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, "movieInfo", query)
.then(movie => this.cache.set(cacheKey, movie, this.defaultTTL))
.then(movie => Movie.convertFromTmdbResponse(movie));
return this.getFromCacheOrFetchFromTmdb(cacheKey, "movieInfo", query).then(
movie => Movie.convertFromTmdbResponse(movie)
);
}
/**
@@ -89,9 +150,11 @@ class TMDB {
const query = { id: identifier };
const cacheKey = `tmdb/${this.cacheTags.movieCredits}:${identifier}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, "movieCredits", query)
.then(credits => this.cache.set(cacheKey, credits, this.defaultTTL))
.then(credits => Credits.convertFromTmdbResponse(credits));
return this.getFromCacheOrFetchFromTmdb(
cacheKey,
"movieCredits",
query
).then(credits => Credits.convertFromTmdbResponse(credits));
}
/**
@@ -107,11 +170,7 @@ class TMDB {
cacheKey,
"movieReleaseDates",
query
)
.then(releaseDates =>
this.cache.set(cacheKey, releaseDates, this.defaultTTL)
)
.then(releaseDates => ReleaseDates.convertFromTmdbResponse(releaseDates));
).then(releaseDates => ReleaseDates.convertFromTmdbResponse(releaseDates));
}
/**
@@ -124,18 +183,18 @@ class TMDB {
const query = { id: identifier };
const cacheKey = `tmdb/${this.cacheTags.showInfo}:${identifier}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, "tvInfo", query)
.then(show => this.cache.set(cacheKey, show, this.defaultTTL))
.then(show => Show.convertFromTmdbResponse(show));
return this.getFromCacheOrFetchFromTmdb(cacheKey, "tvInfo", query).then(
show => Show.convertFromTmdbResponse(show)
);
}
showCredits(identifier) {
const query = { id: identifier };
const cacheKey = `tmdb/${this.cacheTags.showCredits}:${identifier}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, "tvCredits", query)
.then(credits => this.cache.set(cacheKey, credits, this.defaultTTL))
.then(credits => Credits.convertFromTmdbResponse(credits));
return this.getFromCacheOrFetchFromTmdb(cacheKey, "tvCredits", query).then(
credits => Credits.convertFromTmdbResponse(credits)
);
}
/**
@@ -148,9 +207,9 @@ class TMDB {
const query = { id: identifier };
const cacheKey = `tmdb/${this.cacheTags.personInfo}:${identifier}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, "personInfo", query)
.then(person => this.cache.set(cacheKey, person, this.defaultTTL))
.then(person => Person.convertFromTmdbResponse(person));
return this.getFromCacheOrFetchFromTmdb(cacheKey, "personInfo", query).then(
person => Person.convertFromTmdbResponse(person)
);
}
personCredits(identifier) {
@@ -161,18 +220,18 @@ class TMDB {
cacheKey,
"personCombinedCredits",
query
)
.then(credits => this.cache.set(cacheKey, credits, this.defaultTTL))
.then(credits => Credits.convertFromTmdbResponse(credits));
).then(credits => Credits.convertFromTmdbResponse(credits));
}
multiSearch(search_query, page = 1, include_adult = true) {
const query = { query: search_query, page, include_adult };
const cacheKey = `tmdb/${this.cacheTags.multiSearch}:${page}:${search_query}:${include_adult}`;
multiSearch(searchQuery, page = 1, includeAdult = true) {
const query = { query: searchQuery, page, include_adult: includeAdult };
const cacheKey = `tmdb/${this.cacheTags.multiSearch}:${page}:${searchQuery}:${includeAdult}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, "searchMulti", query)
.then(response => this.cache.set(cacheKey, response, this.defaultTTL))
.then(response => this.mapResults(response));
return this.getFromCacheOrFetchFromTmdb(
cacheKey,
"searchMulti",
query
).then(response => mapResults(response));
}
/**
@@ -181,13 +240,19 @@ class TMDB {
* @param {Number} page representing pagination of results
* @returns {Promise} dict with query results, current page and total_pages
*/
movieSearch(search_query, page = 1, include_adult = true) {
const tmdbquery = { query: search_query, page, include_adult };
const cacheKey = `tmdb/${this.cacheTags.movieSearch}:${page}:${search_query}:${include_adult}`;
movieSearch(searchQuery, page = 1, includeAdult = true) {
const tmdbquery = {
query: searchQuery,
page,
include_adult: includeAdult
};
const cacheKey = `tmdb/${this.cacheTags.movieSearch}:${page}:${searchQuery}:${includeAdult}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, "searchMovie", tmdbquery)
.then(response => this.cache.set(cacheKey, response, this.defaultTTL))
.then(response => this.mapResults(response, "movie"));
return this.getFromCacheOrFetchFromTmdb(
cacheKey,
"searchMovie",
tmdbquery
).then(response => mapResults(response, "movie"));
}
/**
@@ -196,13 +261,19 @@ class TMDB {
* @param {Number} page representing pagination of results
* @returns {Promise} dict with query results, current page and total_pages
*/
showSearch(search_query, page = 1, include_adult = true) {
const tmdbquery = { query: search_query, page, include_adult };
const cacheKey = `tmdb/${this.cacheTags.showSearch}:${page}:${search_query}:${include_adult}`;
showSearch(searchQuery, page = 1, includeAdult = true) {
const tmdbquery = {
query: searchQuery,
page,
include_adult: includeAdult
};
const cacheKey = `tmdb/${this.cacheTags.showSearch}:${page}:${searchQuery}:${includeAdult}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, "searchTv", tmdbquery)
.then(response => this.cache.set(cacheKey, response, this.defaultTTL))
.then(response => this.mapResults(response, "show"));
return this.getFromCacheOrFetchFromTmdb(
cacheKey,
"searchTv",
tmdbquery
).then(response => mapResults(response, "show"));
}
/**
@@ -211,59 +282,37 @@ class TMDB {
* @param {Number} page representing pagination of results
* @returns {Promise} dict with query results, current page and total_pages
*/
personSearch(search_query, page = 1, include_adult = true) {
const tmdbquery = { query: search_query, page, include_adult };
const cacheKey = `tmdb/${this.cacheTags.personSearch}:${page}:${search_query}:${include_adult}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, "searchPerson", tmdbquery)
.then(response => this.cache.set(cacheKey, response, this.defaultTTL))
.then(response => this.mapResults(response, "person"));
}
movieList(listname, page = 1) {
const query = { page: page };
const cacheKey = `tmdb/${this.cacheTags[listname]}:${page}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, listname, query)
.then(response => this.cache.set(cacheKey, response, this.defaultTTL))
.then(response => this.mapResults(response, "movie"));
}
showList(listname, page = 1) {
const query = { page: page };
const cacheKey = `tmdb/${this.cacheTags[listname]}:${page}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, listName, query)
.then(response => this.cache.set(cacheKey, response, this.defaultTTL))
.then(response => this.mapResults(response, "show"));
}
/**
* Maps our response from tmdb api to a movie/show object.
* @param {String} response from tmdb.
* @param {String} The type declared in listSearch.
* @returns {Promise} dict with tmdb results, mapped as movie/show objects.
*/
mapResults(response, type = undefined) {
let results = response.results.map(result => {
if (type === "movie" || result.media_type === "movie") {
const movie = Movie.convertFromTmdbResponse(result);
return movie.createJsonResponse();
} else if (type === "show" || result.media_type === "tv") {
const show = Show.convertFromTmdbResponse(result);
return show.createJsonResponse();
} else if (type === "person" || result.media_type === "person") {
const person = Person.convertFromTmdbResponse(result);
return person.createJsonResponse();
}
});
return {
results: results,
page: response.page,
total_results: response.total_results,
total_pages: response.total_pages
personSearch(searchQuery, page = 1, includeAdult = true) {
const tmdbquery = {
query: searchQuery,
page,
include_adult: includeAdult
};
const cacheKey = `tmdb/${this.cacheTags.personSearch}:${page}:${searchQuery}:${includeAdult}`;
return this.getFromCacheOrFetchFromTmdb(
cacheKey,
"searchPerson",
tmdbquery
).then(response => mapResults(response, "person"));
}
movieList(listName, page = 1) {
const query = { page };
const cacheKey = `tmdb/${this.cacheTags[listName]}:${page}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, listName, query).then(
response => mapResults(response, "movie")
);
}
showList(listName, page = 1) {
const query = { page };
const cacheKey = `tmdb/${this.cacheTags[listName]}:${page}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, listName, query).then(
response => mapResults(response, "show")
);
}
/**
@@ -278,7 +327,7 @@ class TMDB {
if (error) {
return reject(error);
}
resolve(reponse);
return resolve(reponse);
};
if (!argument) {