Main Client
Bundles all twelve clients behind one object, and gives them a single shared cache.
import { MainClient } from 'pokenode-ts';
const api = new MainClient();
const pikachu = await api.pokemon.getPokemonByName('pikachu');
const cheri = await api.berry.getBerryByName('cheri');
const surf = await api.move.getMoveByName('surf');Sub-clients
| Property | Client |
|---|---|
api.berry | BerryClient |
api.contest | ContestClient |
api.currency | CurrencyClient |
api.encounter | EncounterClient |
api.evolution | EvolutionClient |
api.game | GameClient |
api.item | ItemClient |
api.location | LocationClient |
api.machine | MachineClient |
api.move | MoveClient |
api.pokemon | PokemonClient |
api.utility | UtilityClient |
Each is the same class you would construct directly, with the same methods.
One cache for everything
This is the reason to use MainClient over constructing clients yourself. All twelve share one store, so a resource fetched through any of them is served from memory by the rest:
const api = new MainClient();
const species = await api.pokemon.getPokemonSpeciesByName('eevee');
// Already in the cache from the call above — no second request.
const same = await api.utility.getResourceByUrl(
'https://pokeapi.co/api/v2/pokemon-species/eevee',
);Construct the clients separately and you get a separate cache each, which is usually not what you want:
// Two caches. The same berry is fetched twice.
const berry = new BerryClient();
const main = new MainClient();Options
MainClient takes the same options as any client and passes them to all twelve:
import { MainClient, MemoryCache, consoleLogger } from 'pokenode-ts';
const api = new MainClient({
cache: new MemoryCache({ ttl: 60_000, maxEntries: 1000 }),
logger: consoleLogger,
});Pass a cache and every sub-client uses that one store. Pass cache: false and caching is off everywhere.
TIP
A shared store fills up faster than a per-client one, since twelve clients now compete for the same maxEntries. If you use MainClient heavily, raise it.
Clearing the cache
clearCache() empties the shared store, so it clears for every sub-client at once:
await api.clearCache();The store itself is exposed as api.cache — it is the same object as api.berry.cache, and it is undefined when caching is disabled. See the Cache guide.
Not a BaseClient
Changed in 2.0
MainClient no longer extends BaseClient, so mainClient instanceof BaseClient is now false.
It composes its sub-clients instead of inheriting from them. Under the old arrangement it built twelve independent caches, so a resource fetched through api.pokemon was fetched again by api.utility, and no request was ever deduplicated across them.