fix(metadata): refactor book metadata fetching to use fetch API and signals for state management (#495)

* fix(metadata): refactor book metadata fetching to use fetch API and signals for state management

* fix(metadata): update tests to mock fetch API for book metadata streaming

* fix(metadata): improve metadata fetching with error handling and retry logic

* fix(metadata): update author photo search to use SSE with auth headers and improve error handling

* fix(metadata): require bookId for cover fetching and improve error handling

* fix(metadata): simplify error handling in DuckDuckGo cover image fetching

* fix(metadata): refactor SSE data handling for cover image and book metadata fetching
This commit is contained in:
brios
2026-04-22 17:27:15 -04:00
committed by Zach Yale
parent 27db18ae80
commit fcee609b15
19 changed files with 702 additions and 397 deletions
@@ -3,7 +3,6 @@ import {of, throwError} from 'rxjs';
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
import {TranslocoService} from '@jsverse/transloco';
import type {AuthorPhotoResult} from '../../model/author.model';
import {AuthorService} from '../../service/author.service';
import {AuthorPhotoSearchComponent} from './author-photo-search.component';
import {
@@ -51,11 +50,11 @@ describe('AuthorPhotoSearchComponent', () => {
});
it('hydrates the initial author query, performs the bootstrap search, and sorts the returned photos', () => {
searchAuthorPhotos.mockReturnValue(of<AuthorPhotoResult[]>([
searchAuthorPhotos.mockReturnValue(of(
{index: 3, url: 'three', width: 300, height: 400},
{index: 1, url: 'one', width: 100, height: 200},
{index: 2, url: 'two', width: 200, height: 300},
]));
));
const component = TestBed.runInInjectionContext(() => new AuthorPhotoSearchComponent());
component.ngOnInit();
@@ -62,17 +62,20 @@ export class AuthorPhotoSearchComponent implements OnInit {
onSearch(): void {
if (!this.searchForm.valid) return;
this.searching = true;
this.photos = [];
this.authorService.searchAuthorPhotos(this.authorId, this.searchForm.value.query)
.pipe(finalize(() => this.searching = false))
.pipe(finalize(() => {
this.searching = false;
this.hasSearched = true;
}))
.subscribe({
next: (photos) => {
this.photos = photos.sort((a, b) => a.index - b.index);
this.hasSearched = true;
next: (photo) => {
this.photos.push(photo);
this.photos.sort((a, b) => a.index - b.index);
},
error: () => {
this.photos = [];
this.hasSearched = true;
console.error('Error searching photos');
}
});
}
@@ -116,10 +116,25 @@ export class AuthorService {
return this.http.delete<void>(this.baseUrl, {body: authorIds});
}
searchAuthorPhotos(authorId: number, query: string): Observable<AuthorPhotoResult[]> {
return this.http.get<AuthorPhotoResult[]>(`${this.baseUrl}/${authorId}/search-photos`, {
params: {q: query}
});
searchAuthorPhotos(authorId: number, query: string): Observable<AuthorPhotoResult> {
const token = this.authService.getInternalAccessToken();
const headers = new HttpHeaders()
.set('Content-Type', 'application/json')
.set('Authorization', `Bearer ${token}`);
return this.sseClient.stream(
`${this.baseUrl}/${authorId}/search-photos`,
{keepAlive: false, reconnectionDelay: 1000, responseType: 'event'},
{headers, params: {q: query}, withCredentials: true},
'GET'
).pipe(
map(event => {
if (event.type === 'error') {
throw new Error((event as ErrorEvent).message);
}
return JSON.parse((event as MessageEvent).data) as AuthorPhotoResult;
})
);
}
uploadAuthorPhotoFromUrl(authorId: number, imageUrl: string): Observable<void> {