Skip to main content
The Pokemon Detail Adapter converts the complex, nested structure from the PokeAPI into a simplified PokemonDetail object that’s optimized for use throughout the application.

adaptPokemon

Main adapter function that transforms a raw API response into a structured Pokemon detail object.
adaptPokemon(apiResponse: ApiPokemonResponse): PokemonDetail

Parameters

apiResponse
ApiPokemonResponse
required
The raw Pokemon response from PokeAPI, containing:
  • id (number): Pokemon ID
  • name (string): Pokemon name
  • height (number): Height in decimeters
  • weight (number): Weight in hectograms
  • types (ApiPokemonType[]): Raw type data
  • sprites (ApiPokemonSprites): Sprite URLs object
  • flavor_text_entries (array): Description entries
  • genera (array): Genus entries in multiple languages
  • abilities (ApiPokemonAbility[]): Raw ability data
  • stats (ApiPokemonStat[]): Raw stat data
  • evolution_chain (object): Evolution chain reference
  • varieties (ApiVariety[]): Pokemon form varieties

Returns

result
PokemonDetail
Adapted Pokemon detail object with the following structure:

Usage Example

import { adaptPokemon } from '@/adapters/pokemon-detail.adapter'
import { fetchPokemonByID } from '@/lib/api/pokemon.api'

// Fetch raw data from API
const rawData = await fetchPokemonByID('pikachu', true)

// Adapt to clean format
const pokemon = adaptPokemon(rawData)

console.log(pokemon.name) // "pikachu"
console.log(pokemon.genus) // "Mouse"
console.log(pokemon.height) // 0.4 (meters)
console.log(pokemon.weight) // 6.0 (kg)
console.log(pokemon.types) // [{ name: "electric", url: "..." }]
console.log(pokemon.stats) // [{ name: "HP", value: 35 }, ...]
console.log(pokemon.assets.official.default) // "https://..."

Helper Functions

The adapter uses several internal helper functions to transform specific data:

distillGenus

Extracts the genus from multi-language entries.
distillGenus(
  genera: ApiPokemonResponse['genera'],
  lang?: ApiLanguage['name']
): string
Default language: "en"
Returns: Genus string with “Pokémon” suffix removed (e.g., “Mouse” from “Mouse Pokémon”)

distillDescription

Extracts and cleans the flavor text description.
distillDescription(
  flavorTextArray: ApiPokemonResponse['flavor_text_entries'],
  lang?: ApiLanguage['name']
): string
Default language: "en"
Returns: Cleaned description with whitespace normalized and special characters removed

mapTypes

Converts API type format to application format.
mapTypes(apiTypes: ApiPokemonResponse['types']): PokeType[]
Returns: Array of PokeType objects with name and url properties

mapAbilities

Converts API ability format to simplified format.
mapAbilities(abilities: ApiPokemonResponse['abilities']): object[]
Returns: Array of objects with name (string) and hidden (boolean)

mapStats

Converts API stats to application stat format with friendly names.
mapStats(apiStats: ApiPokemonResponse['stats']): PokeStat[]
Returns: Array of stats with mapped names (HP, ATK, DEF, SPA, SPD, SPE) and base values

mapVarieties

Converts Pokemon varieties (forms) to application format.
mapVarieties(
  apiVarieties: ApiVariety[],
  genus: string,
  description: string
): PokeVariety[]
Returns: Array of variety objects with full Pokemon data for each form

Data Transformations

The adapter performs several key transformations:
  • Height: Decimeters → Meters (divided by 10)
  • Weight: Hectograms → Kilograms (divided by 10)
  • Description: Removes newlines, form feeds, tabs, and extra whitespace
  • Genus: Removes “Pokémon” suffix
  • Variety names: Replaces hyphens with spaces
Maps API stat names to friendly abbreviations:
  • hp → “HP”
  • attack → “ATK”
  • defense → “DEF”
  • special-attack → “SPA”
  • special-defense → “SPD”
  • speed → “SPE”
  • Missing sprites default to Ditto sprite (ID 132)
  • Missing genus defaults to “Unknown”
  • Missing description defaults to “Not enough data has been collected yet to describe this Pokémon.”

Build docs developers (and LLMs) love