feat: author pages with /authors/:id route (0.16.0)
- `GET /v1/authors/:id` returns `AuthorWithCount` (id, name, manga_count). - `GET /v1/authors/:id/mangas` paged works by that author. - `GET /v1/authors?search=` autocomplete (already used by Phase 1 forms; now formally exposed). - New `/authors/:id` page on the frontend; author chips on the manga detail page (added in Phase 1) now link to a real page. - Extracts `lib/components/MangaCard.svelte` — already used by the home page, ready for the collection page in Phase 3. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
91
frontend/src/lib/api/authors.test.ts
Normal file
91
frontend/src/lib/api/authors.test.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach, type MockInstance } from 'vitest';
|
||||
import { listAuthors, getAuthor, listAuthorMangas } from './authors';
|
||||
|
||||
function ok(body: unknown, status = 200): Response {
|
||||
return new Response(JSON.stringify(body), {
|
||||
status,
|
||||
headers: { 'content-type': 'application/json' }
|
||||
});
|
||||
}
|
||||
|
||||
function envelope(status: number, code: string, message: string): Response {
|
||||
return new Response(JSON.stringify({ error: { code, message } }), {
|
||||
status,
|
||||
headers: { 'content-type': 'application/json' }
|
||||
});
|
||||
}
|
||||
|
||||
describe('authors api client', () => {
|
||||
let fetchSpy: MockInstance<typeof globalThis.fetch>;
|
||||
|
||||
beforeEach(() => {
|
||||
fetchSpy = vi.spyOn(globalThis, 'fetch');
|
||||
});
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('listAuthors GETs /v1/authors with no params by default', async () => {
|
||||
fetchSpy.mockResolvedValueOnce(ok([]));
|
||||
await listAuthors();
|
||||
const url = fetchSpy.mock.calls[0][0] as string;
|
||||
expect(url).toMatch(/\/v1\/authors$/);
|
||||
});
|
||||
|
||||
it('listAuthors encodes search, limit, offset', async () => {
|
||||
fetchSpy.mockResolvedValueOnce(ok([]));
|
||||
await listAuthors({ search: 'miura', limit: 5, offset: 0 });
|
||||
const url = fetchSpy.mock.calls[0][0] as string;
|
||||
expect(url).toContain('search=miura');
|
||||
expect(url).toContain('limit=5');
|
||||
expect(url).toContain('offset=0');
|
||||
});
|
||||
|
||||
it('getAuthor returns the AuthorWithCount shape', async () => {
|
||||
fetchSpy.mockResolvedValueOnce(
|
||||
ok({
|
||||
id: 'a1',
|
||||
name: 'Kentaro Miura',
|
||||
created_at: '2026-01-01T00:00:00Z',
|
||||
manga_count: 3
|
||||
})
|
||||
);
|
||||
const a = await getAuthor('a1');
|
||||
expect(a.name).toBe('Kentaro Miura');
|
||||
expect(a.manga_count).toBe(3);
|
||||
});
|
||||
|
||||
it('getAuthor surfaces 404 as ApiError', async () => {
|
||||
fetchSpy.mockResolvedValueOnce(envelope(404, 'not_found', 'not found'));
|
||||
await expect(getAuthor('missing')).rejects.toMatchObject({
|
||||
status: 404,
|
||||
code: 'not_found'
|
||||
});
|
||||
});
|
||||
|
||||
it('listAuthorMangas hits the nested route and forwards pagination', async () => {
|
||||
fetchSpy.mockResolvedValueOnce(
|
||||
ok({
|
||||
items: [
|
||||
{
|
||||
id: 'm1',
|
||||
title: 'Berserk',
|
||||
status: 'ongoing',
|
||||
alt_titles: [],
|
||||
description: null,
|
||||
cover_image_path: null,
|
||||
created_at: '2026-01-01T00:00:00Z',
|
||||
updated_at: '2026-01-01T00:00:00Z'
|
||||
}
|
||||
],
|
||||
page: { limit: 50, offset: 0, total: 1 }
|
||||
})
|
||||
);
|
||||
const result = await listAuthorMangas('a1', { limit: 20, offset: 10 });
|
||||
expect(result.items[0].title).toBe('Berserk');
|
||||
const url = fetchSpy.mock.calls[0][0] as string;
|
||||
expect(url).toMatch(/\/v1\/authors\/a1\/mangas\?/);
|
||||
expect(url).toContain('limit=20');
|
||||
expect(url).toContain('offset=10');
|
||||
});
|
||||
});
|
||||
56
frontend/src/lib/api/authors.ts
Normal file
56
frontend/src/lib/api/authors.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { request, type Page } from './client';
|
||||
import type { Manga } from './client';
|
||||
|
||||
export type Author = {
|
||||
id: string;
|
||||
name: string;
|
||||
created_at: string;
|
||||
};
|
||||
|
||||
/** Returned by `GET /v1/authors/:id` — adds the count of attached mangas. */
|
||||
export type AuthorWithCount = Author & {
|
||||
manga_count: number;
|
||||
};
|
||||
|
||||
export type AuthorMangasPage = {
|
||||
items: Manga[];
|
||||
page: Page;
|
||||
};
|
||||
|
||||
export type ListAuthorsOptions = {
|
||||
search?: string;
|
||||
limit?: number;
|
||||
offset?: number;
|
||||
};
|
||||
|
||||
export type ListAuthorMangasOptions = {
|
||||
limit?: number;
|
||||
offset?: number;
|
||||
};
|
||||
|
||||
/** Autocomplete for author pickers. Server sorts by trigram similarity. */
|
||||
export async function listAuthors(opts: ListAuthorsOptions = {}): Promise<Author[]> {
|
||||
const params = new URLSearchParams();
|
||||
if (opts.search) params.set('search', opts.search);
|
||||
if (opts.limit != null) params.set('limit', String(opts.limit));
|
||||
if (opts.offset != null) params.set('offset', String(opts.offset));
|
||||
const qs = params.toString();
|
||||
return request<Author[]>(`/v1/authors${qs ? `?${qs}` : ''}`);
|
||||
}
|
||||
|
||||
export async function getAuthor(id: string): Promise<AuthorWithCount> {
|
||||
return request<AuthorWithCount>(`/v1/authors/${encodeURIComponent(id)}`);
|
||||
}
|
||||
|
||||
export async function listAuthorMangas(
|
||||
id: string,
|
||||
opts: ListAuthorMangasOptions = {}
|
||||
): Promise<AuthorMangasPage> {
|
||||
const params = new URLSearchParams();
|
||||
if (opts.limit != null) params.set('limit', String(opts.limit));
|
||||
if (opts.offset != null) params.set('offset', String(opts.offset));
|
||||
const qs = params.toString();
|
||||
return request<AuthorMangasPage>(
|
||||
`/v1/authors/${encodeURIComponent(id)}/mangas${qs ? `?${qs}` : ''}`
|
||||
);
|
||||
}
|
||||
107
frontend/src/lib/components/MangaCard.svelte
Normal file
107
frontend/src/lib/components/MangaCard.svelte
Normal file
@@ -0,0 +1,107 @@
|
||||
<script lang="ts">
|
||||
import { fileUrl } from '$lib/api/client';
|
||||
import type { Manga } from '$lib/api/client';
|
||||
import type { AuthorRef, GenreRef } from '$lib/api/mangas';
|
||||
import BookImage from '@lucide/svelte/icons/book-image';
|
||||
|
||||
let {
|
||||
manga,
|
||||
authors = [],
|
||||
genres = [],
|
||||
testid
|
||||
}: {
|
||||
manga: Manga;
|
||||
authors?: AuthorRef[];
|
||||
genres?: GenreRef[];
|
||||
testid?: string;
|
||||
} = $props();
|
||||
</script>
|
||||
|
||||
<li class="manga-card" data-testid={testid}>
|
||||
<a href="/manga/{manga.id}" class="cover-link" aria-hidden="true" tabindex="-1">
|
||||
{#if manga.cover_image_path}
|
||||
<img
|
||||
src={fileUrl(manga.cover_image_path)}
|
||||
alt=""
|
||||
class="cover"
|
||||
loading="lazy"
|
||||
/>
|
||||
{:else}
|
||||
<div class="cover cover-placeholder">
|
||||
<BookImage size={36} aria-hidden="true" />
|
||||
</div>
|
||||
{/if}
|
||||
</a>
|
||||
<div class="meta">
|
||||
<a href="/manga/{manga.id}" class="title">{manga.title}</a>
|
||||
{#if authors.length > 0}
|
||||
<span class="author">{authors.map((a) => a.name).join(', ')}</span>
|
||||
{/if}
|
||||
{#if genres.length > 0}
|
||||
<span class="genres">{genres.map((g) => g.name).join(' · ')}</span>
|
||||
{/if}
|
||||
</div>
|
||||
</li>
|
||||
|
||||
<style>
|
||||
.manga-card {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: var(--space-2);
|
||||
list-style: none;
|
||||
}
|
||||
|
||||
.cover-link {
|
||||
display: block;
|
||||
line-height: 0;
|
||||
}
|
||||
|
||||
.cover {
|
||||
width: 100%;
|
||||
aspect-ratio: 2 / 3;
|
||||
object-fit: cover;
|
||||
border-radius: var(--radius-md);
|
||||
background: var(--surface);
|
||||
}
|
||||
|
||||
.cover-placeholder {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
color: var(--text-muted);
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
.meta {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
min-width: 0;
|
||||
gap: var(--space-1);
|
||||
}
|
||||
|
||||
.title {
|
||||
font-weight: var(--weight-semibold);
|
||||
font-size: var(--font-sm);
|
||||
line-height: var(--leading-tight);
|
||||
color: var(--text);
|
||||
overflow: hidden;
|
||||
display: -webkit-box;
|
||||
-webkit-line-clamp: 2;
|
||||
line-clamp: 2;
|
||||
-webkit-box-orient: vertical;
|
||||
}
|
||||
|
||||
.title:hover {
|
||||
color: var(--primary);
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.author,
|
||||
.genres {
|
||||
color: var(--text-muted);
|
||||
font-size: var(--font-xs);
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user