Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@
"humanize-duration": "^3.33.2",
"js-yaml": "^4.1.1",
"lodash": "4.18.1",
"lru-cache": "11.2.1",
"mime": "^4.1.0",
"nanoid": "^5.1.7",
"next": "16.2.6",
"ip-address": "^10.2.0",
"node-cache": "5.1.2",
"node-gyp": "12.2.0",
"node-schedule": "2.1.1",
"nodemailer": "8.0.5",
Expand Down
20 changes: 3 additions & 17 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 14 additions & 8 deletions server/api/externalapi.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { CacheStore } from '@server/lib/cache';
import { proxyRequestInterceptor } from '@server/utils/customProxyAgent';
import type { AxiosInstance, AxiosRequestConfig } from 'axios';
import axios from 'axios';
import rateLimit from 'axios-rate-limit';
import type NodeCache from 'node-cache';

// 5 minute default TTL (in seconds)
const DEFAULT_TTL = 300;
Expand All @@ -11,7 +11,7 @@ const DEFAULT_TTL = 300;
const DEFAULT_ROLLING_BUFFER = 10000;

export interface ExternalAPIOptions {
nodeCache?: NodeCache;
nodeCache?: CacheStore;
headers?: Record<string, unknown>;
timeout?: number;
rateLimit?: {
Expand All @@ -23,7 +23,7 @@ export interface ExternalAPIOptions {
class ExternalAPI {
protected axios: AxiosInstance;
private baseUrl: string;
private cache?: NodeCache;
private cache?: CacheStore;

constructor(
baseUrl: string,
Expand Down Expand Up @@ -53,27 +53,33 @@ class ExternalAPI {
this.cache = options.nodeCache;
}

// transform runs before the cache write.
protected async get<T>(
endpoint: string,
config?: AxiosRequestConfig,
ttl?: number
ttl?: number,
options?: { cache?: CacheStore; transform?: (data: T) => T }
): Promise<T> {
const cache = options?.cache ?? this.cache;
const cacheKey = this.serializeCacheKey(endpoint, {
...config?.params,
headers: config?.headers,
});
const cachedItem = this.cache?.get<T>(cacheKey);
const cachedItem = cache?.get<T>(cacheKey);
if (cachedItem) {
return cachedItem;
}

const response = await this.axios.get<T>(endpoint, config);
const data = options?.transform
? options.transform(response.data)
: response.data;

if (this.cache && ttl !== 0) {
this.cache.set(cacheKey, response.data, ttl ?? DEFAULT_TTL);
if (cache && ttl !== 0) {
cache.set(cacheKey, data, ttl ?? DEFAULT_TTL);
}

return response.data;
return data;
}

protected async post<T>(
Expand Down
5 changes: 3 additions & 2 deletions server/api/plexapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,9 @@ class PlexAPI extends ExternalAPI {
mediaType: 'movie' | 'show'
): Promise<PlexLibraryItem[]> {
const response = await this.get<PlexLibraryResponse>(
`/library/sections/${id}/all?type=${
mediaType === 'show' ? '4' : '1'
`/library/sections/${id}/all?type=${mediaType === 'show' ? '4' : '1'}${
// Shows are queried as episodes, whose guids the scanner never reads.
mediaType === 'movie' ? '&includeGuids=1' : ''
}&sort=addedAt%3Adesc&addedAt>>=${Math.floor(options.addedAt / 1000)}`,
{
headers: {
Expand Down
Loading
Loading