fix: revert experimental ImmichClient refactoring and restore absolute baseline for image loading

This commit is contained in:
ralksta
2026-03-07 09:37:48 +01:00
parent 277aab54e1
commit 68e442fff5
2 changed files with 19 additions and 25 deletions

View File

@@ -39,11 +39,10 @@ describe('ImmichClient', () => {
// Access private method via any casting for testing // Access private method via any casting for testing
const result = await (immich as any).request('/test'); const result = await (immich as any).request('/test');
expect(result.data).toEqual({ foo: 'bar' }); expect(result).toEqual({ foo: 'bar' });
expect(result.error).toBeNull();
}); });
it('returns error on non-ok response', async () => { it('returns null on non-ok response', async () => {
mockFetch.mockResolvedValueOnce({ mockFetch.mockResolvedValueOnce({
ok: false, ok: false,
status: 404, status: 404,
@@ -51,8 +50,7 @@ describe('ImmichClient', () => {
}); });
const result = await (immich as any).request('/test'); const result = await (immich as any).request('/test');
expect(result.data).toBeNull(); expect(result).toBeNull();
expect(result.error).toContain('404 Not Found');
}); });
}); });

View File

@@ -77,8 +77,6 @@ export interface SubpageSummary {
coverAssetId: string | null; coverAssetId: string | null;
} }
export type ImmichResult<T> = { data: T | null; error: string | null };
// ── API Client ───────────────────────────────────────────────── // ── API Client ─────────────────────────────────────────────────
class ImmichClient { class ImmichClient {
@@ -91,7 +89,7 @@ class ImmichClient {
/** /**
* Make an authenticated request to the Immich API. * Make an authenticated request to the Immich API.
*/ */
private async request<T>(endpoint: string): Promise<ImmichResult<T>> { private async request<T>(endpoint: string): Promise<T | null> {
const url = `${this.config.immich.apiUrl}${endpoint}`; const url = `${this.config.immich.apiUrl}${endpoint}`;
try { try {
const res = await fetch(url, { const res = await fetch(url, {
@@ -102,20 +100,18 @@ class ImmichClient {
}); });
if (!res.ok) { if (!res.ok) {
const error = `[Immich] ${res.status} ${res.statusText} for ${endpoint}`; console.error(`[Immich] ${res.status} ${res.statusText} for ${endpoint}`);
console.error(error); return null;
return { data: null, error };
} }
const contentType = res.headers.get('Content-Type') || ''; const contentType = res.headers.get('Content-Type') || '';
if (contentType.includes('application/json')) { if (contentType.includes('application/json')) {
return { data: (await res.json()) as T, error: null }; return (await res.json()) as T;
} }
return { data: null, error: 'Invalid response format (expected JSON)' }; return null;
} catch (err) { } catch (error) {
const error = `[Immich] Failed to reach ${url}: ${err instanceof Error ? err.message : 'Unknown error'}`; console.error(`[Immich] Failed to reach ${url}:`, error);
console.error(error); return null;
return { data: null, error };
} }
} }
@@ -164,8 +160,8 @@ class ImmichClient {
const cached = cache.get<ImmichAlbum[]>(cacheKey); const cached = cache.get<ImmichAlbum[]>(cacheKey);
if (cached) return cached; if (cached) return cached;
const { data: all, error } = await this.request<ImmichAlbum[]>('/albums?shared=true'); const all = await this.request<ImmichAlbum[]>('/albums?shared=true');
if (error || !all) return []; if (!all) return [];
const allowedIds = new Set(this.config.albums); const allowedIds = new Set(this.config.albums);
const filtered = all const filtered = all
@@ -280,8 +276,8 @@ class ImmichClient {
const cached = cache.get<ImmichAlbum>(cacheKey); const cached = cache.get<ImmichAlbum>(cacheKey);
if (cached) return cached; if (cached) return cached;
const { data: album, error } = await this.request<ImmichAlbum>(`/albums/${encodeURIComponent(albumId)}`); const album = await this.request<ImmichAlbum>(`/albums/${encodeURIComponent(albumId)}`);
if (error || !album) return null; if (!album) return null;
// Filter out trashed assets // Filter out trashed assets
album.assets = (album.assets || []).filter((a) => !a.isTrashed); album.assets = (album.assets || []).filter((a) => !a.isTrashed);
@@ -329,8 +325,8 @@ class ImmichClient {
const cached = cache.get<ImmichAsset>(cacheKey); const cached = cache.get<ImmichAsset>(cacheKey);
if (cached) return cached; if (cached) return cached;
const { data: asset, error } = await this.request<ImmichAsset>(`/assets/${encodeURIComponent(assetId)}`); const asset = await this.request<ImmichAsset>(`/assets/${encodeURIComponent(assetId)}`);
if (error || !asset) return null; if (!asset) return null;
cache.set(cacheKey, asset, this.config.cacheTtl); cache.set(cacheKey, asset, this.config.cacheTtl);
return asset; return asset;
@@ -410,8 +406,8 @@ class ImmichClient {
* Check if the Immich server is reachable. * Check if the Immich server is reachable.
*/ */
async ping(): Promise<boolean> { async ping(): Promise<boolean> {
const { data } = await this.request<{ res: string }>('/server/ping'); const res = await this.request<{ res: string }>('/server/ping');
return !!data; return !!res;
} }
} }