pokeapi-typescript
pokeapi-typescript copied to clipboard
Incorrect Validation in listAll Method
Problem Description
The validation in listAll has two issues:
_isListTincorrectly expects a direct array instead of the API's object-with-resultsstructure_isTchecks foridandnameproperties, but the API's list endpoint only returnsnameandurl
Current Implementation
protected _isT(data: any): data is T {
return "id" in data && "name" in data;
}
protected _isListT(data: any): data is NamedApiResourceList<T> {
return Array.isArray(data) && this._isT(data[0]);
}
API Response Structure
List endpoint (e.g., /pokemon):
{
"count": 1302,
"next": "...",
"previous": null,
"results": [
{
"name": "bulbasaur",
"url": "https://pokeapi.co/api/v2/pokemon/1/"
}
]
}
Required Changes
- Update
_isListTto check the correct structure:
protected _isListT(data: any): data is NamedApiResourceList<T> {
return data &&
typeof data === 'object' &&
'results' in data &&
Array.isArray(data.results) &&
this._isNamedResource(data.results[0]);
}
- Create a new validator for list items:
protected _isNamedResource(data: any): data is { name: string, url: string } {
return data &&
typeof data.name === 'string' &&
typeof data.url === 'string';
}
- Update
_isT(for single item endpoints):
protected _isT(data: any): data is T {
return data &&
typeof data.id === 'number' &&
typeof data.name === 'string';
}