Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve plex user mismatch due to caching issues #1242

Merged
merged 3 commits into from
Jan 12, 2025
Merged
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
46 changes: 21 additions & 25 deletions server/api/externalapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,13 @@ class ExternalAPI {
ttl?: number,
config?: RequestInit
): Promise<T> {
const headers = { ...this.defaultHeaders, ...config?.headers };
fallenbagel marked this conversation as resolved.
Show resolved Hide resolved
const cacheKey = this.serializeCacheKey(endpoint, {
...this.params,
...params,
headers,
});

const cachedItem = this.cache?.get<T>(cacheKey);
if (cachedItem) {
return cachedItem;
Expand All @@ -81,10 +84,7 @@ class ExternalAPI {
const url = this.formatUrl(endpoint, params);
const response = await this.fetch(url, {
...config,
headers: {
...this.defaultHeaders,
...config?.headers,
},
headers,
});
if (!response.ok) {
const text = await response.text();
Expand All @@ -111,10 +111,13 @@ class ExternalAPI {
ttl?: number,
config?: RequestInit
): Promise<T> {
const headers = { ...this.defaultHeaders, ...config?.headers };
const cacheKey = this.serializeCacheKey(endpoint, {
config: { ...this.params, ...params },
headers,
data,
});

const cachedItem = this.cache?.get<T>(cacheKey);
if (cachedItem) {
return cachedItem;
Expand All @@ -124,10 +127,7 @@ class ExternalAPI {
const response = await this.fetch(url, {
method: 'POST',
...config,
headers: {
...this.defaultHeaders,
...config?.headers,
},
headers,
body: data ? JSON.stringify(data) : undefined,
});
if (!response.ok) {
Expand Down Expand Up @@ -155,10 +155,13 @@ class ExternalAPI {
ttl?: number,
config?: RequestInit
): Promise<T> {
const headers = { ...this.defaultHeaders, ...config?.headers };
const cacheKey = this.serializeCacheKey(endpoint, {
config: { ...this.params, ...params },
data,
headers,
});

const cachedItem = this.cache?.get<T>(cacheKey);
if (cachedItem) {
return cachedItem;
Expand All @@ -168,10 +171,7 @@ class ExternalAPI {
const response = await this.fetch(url, {
method: 'PUT',
...config,
headers: {
...this.defaultHeaders,
...config?.headers,
},
headers,
body: JSON.stringify(data),
});
if (!response.ok) {
Expand Down Expand Up @@ -227,9 +227,11 @@ class ExternalAPI {
config?: RequestInit,
overwriteBaseUrl?: string
): Promise<T> {
const headers = { ...this.defaultHeaders, ...config?.headers };
const cacheKey = this.serializeCacheKey(endpoint, {
...this.params,
...params,
headers,
});
const cachedItem = this.cache?.get<T>(cacheKey);

Expand All @@ -244,10 +246,7 @@ class ExternalAPI {
const url = this.formatUrl(endpoint, params, overwriteBaseUrl);
this.fetch(url, {
...config,
headers: {
...this.defaultHeaders,
...config?.headers,
},
headers,
}).then(async (response) => {
if (!response.ok) {
const text = await response.text();
Expand All @@ -270,10 +269,7 @@ class ExternalAPI {
const url = this.formatUrl(endpoint, params, overwriteBaseUrl);
const response = await this.fetch(url, {
...config,
headers: {
...this.defaultHeaders,
...config?.headers,
},
headers,
});
if (!response.ok) {
const text = await response.text();
Expand All @@ -293,10 +289,10 @@ class ExternalAPI {
return data;
}

protected removeCache(endpoint: string, params?: Record<string, string>) {
protected removeCache(endpoint: string, options?: Record<string, string>) {
const cacheKey = this.serializeCacheKey(endpoint, {
...this.params,
...params,
...options,
});
this.cache?.del(cacheKey);
}
Expand Down Expand Up @@ -325,13 +321,13 @@ class ExternalAPI {

private serializeCacheKey(
endpoint: string,
params?: Record<string, unknown>
options?: Record<string, unknown>
) {
if (!params) {
if (!options) {
return `${this.baseUrl}${endpoint}`;
}

return `${this.baseUrl}${endpoint}${JSON.stringify(params)}`;
return `${this.baseUrl}${endpoint}${JSON.stringify(options)}`;
}

private async getDataFromResponse(response: Response) {
Expand Down
Loading