Layer Structure
Poke-Nex implements Clean Architecture through three distinct layers, each with specific responsibilities:Layer 1: Fetchers
Purpose
Fetchers are responsible for raw data access from external APIs. They:- Make HTTP requests
- Handle network-level errors (404, 500, timeouts)
- Implement caching strategies
- Return raw API response types
- Don’t transform or interpret data
Implementation
File:src/lib/api/pokemon.api.ts:15-88
Key Characteristics:
- Returns
ApiPokemonResponse(raw API type) - Handles HTTP status codes
- Implements Next.js ISR caching with
revalidate: 604800(7 days) - Throws
ApiErrorwith context for debugging - Supports both REST and GraphQL endpoints
Layer 2: Adapters
Purpose
Adapters transform raw API data into clean, application-specific models. They:- Map API field names to app field names
- Normalize data (e.g., convert units, format strings)
- Extract nested data
- Filter and select relevant fields
- Convert types to match application models
Implementation: Pokemon Detail Adapter
File:src/adapters/pokemon-detail.adapter.ts:10-67
Helper Functions
Adapter Characteristics:
- Transforms
ApiPokemonResponse→PokemonDetail - Converts units (decimeters to meters, hectograms to kilograms)
- Extracts localized text (English descriptions, genus)
- Normalizes sprite URLs with fallbacks
- Maps API stat names to app abbreviations
Implementation: Pokemon Summary Adapter
File:src/adapters/pokemon-summary.adapter.ts:3-18
This adapter is specifically designed for GraphQL responses, transforming the nested
pokemontypes structure into a flat array of type names.Layer 3: Services
Purpose
Services orchestrate fetchers and adapters to provide a clean API for components. They:- Validate input parameters
- Call appropriate fetchers
- Transform data using adapters
- Handle and normalize errors
- Return standardized response format
- Implement business logic (e.g., fetching related data)
Implementation
File:src/services/pokemon.service.ts
Service Characteristics:
- Always returns
ServiceResponse<T>with bothdataanderrorfields - Never throws errors to components
- Validates inputs before calling fetchers
- Orchestrates multiple data sources (see
getPokemonDetailList) - Uses
handleServiceErrorto normalize all errors
Type Definitions
Service Response Type
File:src/types/service.types.ts:8-11
Error Types
File:src/types/service.types.ts:2-23
Benefits of This Pattern
API Independence
PokeAPI changes only affect fetchers and adapters. Services and components remain unchanged.
Easy Testing
Mock fetchers to test adapters. Mock services to test components. No network required.
Type Safety
Compile-time guarantees that components never receive raw API data.
Reusability
Adapters and services can be shared across different UI frameworks.
Next Steps
Trace Data Flow
Follow a complete request from component → service → adapter → fetcher → API