-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
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,108 @@ | ||
import { | ||
type CreateArg, | ||
type CreateResult, | ||
type DestroyArg, | ||
type DestroyResult, | ||
type ListArg, | ||
type ListResult, | ||
type Model, | ||
type RetrieveArg, | ||
type RetrieveResult, | ||
type UpdateArg, | ||
type UpdateResult, | ||
buildUrl, | ||
modelUrls, | ||
tagData, | ||
} from "codeforlife/utils/api" | ||
|
||
import api from "." | ||
|
||
export type Fruit = Model< | ||
number, | ||
{ | ||
name: string | ||
is_citrus: boolean | ||
expires_on: Date | ||
} | ||
> | ||
|
||
const fruitUrls = modelUrls("fruits/", "fruits/<id>/") | ||
|
||
export type RetrieveFruitResult = RetrieveResult< | ||
Fruit, | ||
"name" | "is_citrus" | "expires_on" | ||
> | ||
export type RetrieveFruitArg = RetrieveArg<Fruit> | ||
|
||
export type ListFruitsResult = ListResult< | ||
Fruit, | ||
"name" | "is_citrus" | "expires_on" | ||
> | ||
export type ListFruitsArg = ListArg | ||
|
||
export type CreateFruitResult = CreateResult< | ||
Fruit, | ||
"name" | "is_citrus" | "expires_on" | ||
> | ||
export type CreateFruitArg = CreateArg<Fruit, "name" | "is_citrus"> | ||
|
||
export type UpdateFruitResult = UpdateResult< | ||
Fruit, | ||
"name" | "is_citrus" | "expires_on" | ||
> | ||
export type UpdateFruitArg = UpdateArg<Fruit, never, "name" | "is_citrus"> | ||
|
||
export type DestroyFruitResult = DestroyResult | ||
export type DestroyFruitArg = DestroyArg<Fruit> | ||
|
||
const fruitApi = api.injectEndpoints({ | ||
endpoints: build => ({ | ||
retrieveFruit: build.query<RetrieveFruitResult, RetrieveFruitArg>({ | ||
query: id => ({ | ||
url: buildUrl(fruitUrls.detail, { url: { id } }), | ||
method: "GET", | ||
}), | ||
providesTags: tagData("Fruit"), | ||
}), | ||
listFruits: build.query<ListFruitsResult, ListFruitsArg>({ | ||
query: search => ({ | ||
url: buildUrl(fruitUrls.list, { search }), | ||
method: "GET", | ||
}), | ||
providesTags: tagData("Fruit"), | ||
}), | ||
createFruit: build.mutation<CreateFruitResult, CreateFruitArg>({ | ||
query: body => ({ | ||
url: fruitUrls.list, | ||
method: "POST", | ||
body, | ||
}), | ||
}), | ||
updateFruit: build.mutation<UpdateFruitResult, UpdateFruitArg>({ | ||
query: ({ id, ...body }) => ({ | ||
url: buildUrl(fruitUrls.detail, { url: { id } }), | ||
method: "PATCH", | ||
body, | ||
}), | ||
invalidatesTags: tagData("Fruit"), | ||
}), | ||
destroyFruit: build.mutation<DestroyFruitResult, DestroyFruitArg>({ | ||
query: id => ({ | ||
url: buildUrl(fruitUrls.detail, { url: { id } }), | ||
method: "DELETE", | ||
}), | ||
invalidatesTags: tagData("Fruit"), | ||
}), | ||
}), | ||
}) | ||
|
||
export default fruitApi | ||
export const { | ||
useRetrieveFruitQuery, | ||
useLazyRetrieveFruitQuery, | ||
useListFruitsQuery, | ||
useLazyListFruitsQuery, | ||
useCreateFruitMutation, | ||
useUpdateFruitMutation, | ||
useDestroyFruitMutation, | ||
} = fruitApi |
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,8 @@ | ||
import { createApi } from "codeforlife/api" | ||
|
||
const api = createApi({ | ||
tagTypes: ["Fruit"], | ||
}) | ||
|
||
export default api | ||
export const { useLogoutMutation } = api |