-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(apiSlice): api slice with common modules, get and post
- Loading branch information
1 parent
f389294
commit a8a8730
Showing
4 changed files
with
49 additions
and
2 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'; | ||
|
||
const tagTypes = ['A', 'B', 'C'] as const; | ||
type Tag = (typeof tagTypes)[number]; | ||
type Param = { | ||
data?: Record<string, any>; | ||
body?: Record<string, any>; | ||
url: string; | ||
tag: Tag; | ||
}; | ||
// Query Builder | ||
const buildQuery = (data?: Record<string, any>) => | ||
new URLSearchParams(data).toString(); | ||
|
||
export const apiSlice = createApi({ | ||
reducerPath: 'api', | ||
baseQuery: fetchBaseQuery({ baseUrl: '/' }), | ||
tagTypes, | ||
endpoints: (builder) => ({ | ||
// Common Endpoints: | ||
get: builder.query({ | ||
query: (param: Param) => | ||
param.data ? `${param.url}?${buildQuery(param.data)}` : param.url, | ||
providesTags: (result, error, param) => [{ type: param.tag }], // Cash Provider Tags | ||
}), | ||
post: builder.mutation({ | ||
query: (param: Param) => ({ | ||
url: param.url, | ||
method: 'POST', | ||
body: param.body, | ||
}), | ||
invalidatesTags: (result, error, param) => [{ type: param.tag }], // Cash Invalidator Tags | ||
}), | ||
}), | ||
}); | ||
|
||
// Export the auto-generated hook for the query endpoints | ||
export const { useGetQuery, usePostMutation } = apiSlice; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './apiSlice'; | ||
export * from './auth/authSlice'; | ||
export * from './catFacts/catFactsSlice'; | ||
export * from './counter/counterSlice'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters