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
const result = await (immich as any).request('/test');
expect(result.data).toEqual({ foo: 'bar' });
expect(result.error).toBeNull();
expect(result).toEqual({ foo: 'bar' });
});
it('returns error on non-ok response', async () => {
it('returns null on non-ok response', async () => {
mockFetch.mockResolvedValueOnce({
ok: false,
status: 404,
@@ -51,8 +50,7 @@ describe('ImmichClient', () => {
});
const result = await (immich as any).request('/test');
expect(result.data).toBeNull();
expect(result.error).toContain('404 Not Found');
expect(result).toBeNull();
});
});

View File

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