Skip to content

Instantly share code, notes, and snippets.

@AlaBenAicha
Created October 2, 2022 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlaBenAicha/22703f4292ead5ccfe86d7b0ec4754f5 to your computer and use it in GitHub Desktop.
Save AlaBenAicha/22703f4292ead5ccfe86d7b0ec4754f5 to your computer and use it in GitHub Desktop.
import { createApi, fetchBaseQuery } from '@rtk-incubator/rtk-query';
// Define a service using a base URL and expected endpoints
export const pokemonApi = createApi({
reducerPath: 'pokemonApi',
// baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }), // uncomment for brokenness repro ur welcome :)
baseQuery: async (baseUrl, prepareHeaders, ...rest) => {
const response = await fetch(`https://pokeapi.co/api/v2/${baseUrl}`, rest)
return {data: await response.json()}
},
endpoints: (builder) => ({
getPokemonByName: builder.query({
query: (name) => `pokemon/${name}`,
}),
getPokemonList: builder.query({
query: () => `pokemon`
})
}),
});
// Export hooks for usage in functional components, which are
// auto-generated based on the defined endpoints
export const { useGetPokemonByNameQuery } = pokemonApi;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment