2023-03-27 23:18:34 +11:00
|
|
|
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
|
2023-03-26 22:26:15 +11:00
|
|
|
import {
|
2023-03-27 22:52:15 +11:00
|
|
|
pokeApiAllPagesCustomBaseQuery,
|
2023-03-26 22:26:15 +11:00
|
|
|
pokeApiFullListFetchArgs,
|
|
|
|
} from './paginationBaseQuery';
|
2023-03-27 23:18:34 +11:00
|
|
|
import {
|
|
|
|
PokemonListResponseData,
|
|
|
|
PokemonResponseData,
|
|
|
|
RegionListResponseData,
|
|
|
|
RegionResponseData,
|
|
|
|
TypeListResponseData,
|
|
|
|
TypeResponseData,
|
|
|
|
} from './types/api';
|
2023-03-19 22:42:09 +11:00
|
|
|
|
2023-03-26 22:26:15 +11:00
|
|
|
const pokeApiBaseQuery = async (
|
|
|
|
args: pokeApiFullListFetchArgs,
|
|
|
|
api: any,
|
|
|
|
extra: any,
|
|
|
|
) => {
|
|
|
|
const baseUrl = 'https://pokeapi.co/api/v2/';
|
|
|
|
|
|
|
|
if (args.fetchAllPages) {
|
2023-03-27 22:52:15 +11:00
|
|
|
return pokeApiAllPagesCustomBaseQuery(args, api, extra, baseUrl);
|
2023-03-26 22:26:15 +11:00
|
|
|
} else {
|
|
|
|
return fetchBaseQuery({ baseUrl })(args, api, extra);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-03-19 16:53:07 +11:00
|
|
|
export const pokedexApi = createApi({
|
|
|
|
reducerPath: 'pokedexApi',
|
2023-03-26 22:26:15 +11:00
|
|
|
baseQuery: pokeApiBaseQuery,
|
2023-03-19 16:53:07 +11:00
|
|
|
endpoints: builder => ({
|
2023-03-27 23:18:34 +11:00
|
|
|
getPokemonList: builder.query<PokemonListResponseData, void>({
|
2023-03-26 22:26:15 +11:00
|
|
|
query: () => ({ url: `pokemon`, fetchAllPages: true }),
|
2023-03-19 16:53:07 +11:00
|
|
|
}),
|
2023-03-27 23:18:34 +11:00
|
|
|
getRegionList: builder.query<RegionListResponseData, void>({
|
2023-03-26 22:26:15 +11:00
|
|
|
query: () => ({ url: 'region', fetchAllPages: true }),
|
2023-03-19 22:42:09 +11:00
|
|
|
}),
|
2023-03-27 23:18:34 +11:00
|
|
|
getTypeList: builder.query<TypeListResponseData, void>({
|
2023-03-26 22:26:15 +11:00
|
|
|
query: () => ({ url: 'type', fetchAllPages: true }),
|
2023-03-27 23:18:34 +11:00
|
|
|
transformResponse: (response: RegionListResponseData) => {
|
2023-03-19 22:42:09 +11:00
|
|
|
return {
|
|
|
|
...response,
|
|
|
|
results: [{ name: 'All Types', url: '' }, ...response.results],
|
|
|
|
};
|
|
|
|
},
|
|
|
|
}),
|
2023-03-27 23:18:34 +11:00
|
|
|
getPokemon: builder.query<PokemonResponseData, number | string>({
|
2023-03-26 22:26:15 +11:00
|
|
|
query: IdOrName => ({ url: `pokemon/${IdOrName}` }),
|
2023-03-26 18:39:04 +11:00
|
|
|
}),
|
2023-03-27 23:18:34 +11:00
|
|
|
getRegion: builder.query<RegionResponseData, number | string>({
|
2023-03-26 22:26:15 +11:00
|
|
|
query: IdOrName => ({ url: `region/${IdOrName}` }),
|
|
|
|
}),
|
2023-03-27 23:18:34 +11:00
|
|
|
getType: builder.query<TypeResponseData, number | string>({
|
2023-03-26 22:26:15 +11:00
|
|
|
query: IdOrName => ({ url: `type/${IdOrName}` }),
|
2023-03-26 18:39:04 +11:00
|
|
|
}),
|
2023-03-19 16:53:07 +11:00
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
2023-03-19 22:42:09 +11:00
|
|
|
export const {
|
|
|
|
useGetPokemonListQuery,
|
|
|
|
useGetRegionListQuery,
|
|
|
|
useGetTypeListQuery,
|
2023-03-26 22:26:15 +11:00
|
|
|
useGetPokemonQuery,
|
|
|
|
useGetRegionQuery,
|
|
|
|
useGetTypeQuery,
|
2023-03-19 22:42:09 +11:00
|
|
|
} = pokedexApi;
|