Skip to main content

Overview

EpiNeko uses a service layer pattern to abstract external API calls and data operations. This provides a clean separation between UI components and data fetching logic.

Service Architecture

jikan.ts

Fetches anime data from MyAnimeList via Jikan API

library.ts

Manages user library with Supabase database

Jikan Service

The Jikan service provides access to the Jikan API v4, an unofficial MyAnimeList API.

Base Configuration

The service uses Next.js’s next.revalidate for automatic cache management. Default cache time is 3600 seconds (1 hour).

Type Definitions

API Functions

getTopAnime

Fetch the top-rated anime from MyAnimeList.
Usage:

searchAnime

Search for anime by query string.
Usage:
The query is automatically URL-encoded to handle special characters safely.

getAnimeById

Fetch detailed information for a specific anime.
Usage:
Individual anime details are cached for 24 hours since they change infrequently.

getAnimeCharacters

Fetch character list for an anime.

getAnimeEpisodes

Fetch episode list for an anime with pagination.
Usage:

Error Handling

The Jikan service includes built-in error handling:
1

Rate Limit Detection

Catches 429 status and returns a user-friendly message
2

HTTP Error Handling

Throws descriptive errors for failed requests
3

Automatic Retries

Next.js cache system handles stale data revalidation

Library Service

The Library service manages user anime collections using Supabase.

Type Definitions

Database Operations

addToLibrary

Add a new anime to the user’s library.
Usage:
This function requires an authenticated user. It throws an error if called without authentication.

removeFromLibrary

Remove an anime from the user’s library.
Usage:

updateLibraryItem

Update an existing library entry.
Usage:

getLibrary

Retrieve all library items for the current user.
Usage:
Results are automatically sorted by most recently updated first.

getLibraryItem

Check if a specific anime is in the user’s library.
Usage:
This function returns null for unauthenticated users instead of throwing an error, making it safe to call in any context.

Supabase Utilities

Client-Side Client

For browser-based operations:
src/utils/supabase/client.ts

Server-Side Client

For server components and API routes:
src/utils/supabase/server.ts

Middleware Client

For session management in middleware:
src/utils/supabase/middleware.ts

Best Practices

Error Handling

Always wrap service calls in try-catch blocks

Loading States

Show feedback while waiting for API responses

Cache Strategy

Use appropriate revalidation times per endpoint

Authentication

Check user state before library operations

Common Patterns

Components

See how components use services

Environment Variables

Configure API credentials

Project Structure

Understand service organization